/**
 * Typed API client for the Familienarchiv backend.
 *
 * Types are generated from the OpenAPI spec — run `npm run generate:api`
 * (with the backend running in dev mode) to regenerate src/lib/generated/api.ts.
 *
 * Usage in +page.server.ts:
 *
 *   export async function load({ fetch }) {
 *       const api = createApiClient(fetch);
 *       const { data, error } = await api.GET('/api/documents/{id}', {
 *           params: { path: { id } }
 *       });
 *   }
 */
import createClient from 'openapi-fetch';
import { env } from '$env/dynamic/private';
import type { paths } from '$lib/generated/api';

export function createApiClient(fetch: typeof globalThis.fetch) {
	return createClient<paths>({
		baseUrl: env.API_INTERNAL_URL || 'http://localhost:8080',
		fetch
	});
}

export interface ApiError {
	code?: string;
}

export function extractErrorCode(error: unknown): string | undefined {
	return (error as ApiError | undefined)?.code;
}
