// eslint-disable-next-line @typescript-eslint/no-explicit-any
const cfg  = (window as any).accessmateConfig ?? {};
const BASE  = cfg.restBase  ?? '/wp-json/accessmate/v1';
const NONCE = cfg.nonce     ?? '';

export async function apiFetch<T>(path: string, options: RequestInit = {}): Promise<T> {
  const res = await fetch(BASE + path, {
    ...options,
    headers: {
      'Content-Type': 'application/json',
      'X-WP-Nonce': NONCE,
      ...options.headers,
    },
  });
  if (!res.ok) throw new Error(await res.text());
  return res.json() as Promise<T>;
}
