import { apiKey, pushToast } from '../state/store.js';
export class ApiError extends Error {
    status;
    code;
    constructor(msg, status, code) {
        super(msg);
        this.status = status;
        this.code = code;
    }
}
export async function api(path, init = {}) {
    const url = new URL(path, window.location.origin);
    for (const [k, v] of Object.entries(init.query ?? {})) {
        if (v !== undefined && v !== '')
            url.searchParams.set(k, String(v));
    }
    const headers = new Headers(init.headers ?? {});
    headers.set('accept', 'application/json');
    if (apiKey.value)
        headers.set('x-tuberosa-api-key', apiKey.value);
    if (init.body && !headers.has('content-type'))
        headers.set('content-type', 'application/json');
    const res = await fetch(url, { ...init, headers });
    const text = await res.text();
    const data = text ? JSON.parse(text) : undefined;
    if (!res.ok) {
        const body = data;
        const message = body?.error?.message ?? `Request failed: ${res.status}`;
        pushToast(message, 'bad');
        throw new ApiError(message, res.status, body?.error?.code);
    }
    return data;
}
