// Zentraler Wrapper fuer interne API-Aufrufe. Seit dem Umzug auf die eigene
// Subdomain app.yumder.de laeuft die App auf Root-Pfad ("/"), Apache proxied
// direkt ohne Unterpfad-Praefix - daher ist hier kein BASE_PATH mehr noetig.
export async function apiFetch(path: string, options?: RequestInit) {
  const res = await fetch(path, {
    ...options,
    headers: { 'Content-Type': 'application/json', ...(options?.headers || {}) },
  });
  if (!res.ok) {
    let body: any = null;
    try {
      body = await res.json();
    } catch {
      // Kein JSON-Body (z.B. leere 500er) - Fehler trotzdem mit Status melden.
    }
    const message = body?.error || `API error ${res.status}`;
    const err = new Error(message) as Error & { status?: number; body?: any; unfinished?: string[] };
    err.status = res.status;
    err.body = body;
    if (body && Array.isArray(body.unfinished)) err.unfinished = body.unfinished;
    throw err;
  }
  return res.json();
}

export function withBasePath(src: string | null | undefined): string | null | undefined {
  return src;
}
