"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CLIError = void 0;
exports.apiFetch = apiFetch;
const auth_1 = require("./auth");
class CLIError extends Error {
    constructor(message, status) {
        super(message);
        this.status = status;
        this.name = 'CLIError';
    }
}
exports.CLIError = CLIError;
/** Authenticated fetch against the Long Tail API */
async function apiFetch(path, options) {
    const { server, token } = await (0, auth_1.resolveAuth)();
    const url = `${server}/api${path}`;
    const res = await fetch(url, {
        ...options,
        headers: {
            'Authorization': `Bearer ${token}`,
            'Content-Type': 'application/json',
            ...options?.headers,
        },
    });
    const body = await res.json().catch(() => ({ error: `HTTP ${res.status}` }));
    if (!res.ok) {
        throw new CLIError(body.error || `HTTP ${res.status}`, res.status);
    }
    return body;
}
