import { readConfig } from "../config.js";
export async function apiGet(urlPath, port = 8080) {
    const config = readConfig();
    const token = config?.token ?? "";
    try {
        const res = await fetch(`http://localhost:${port}${urlPath}`, {
            headers: { Authorization: `Bearer ${token}` },
            signal: AbortSignal.timeout(5000),
        });
        if (!res.ok) {
            const error = { status: res.status, message: res.statusText };
            throw error;
        }
        return (await res.json());
    }
    catch (err) {
        if (err instanceof TypeError &&
            err.cause?.code === "ECONNREFUSED") {
            const error = { status: 0, message: 'Server not running. Run "agentgazer start" first.' };
            throw error;
        }
        throw err;
    }
}
export async function apiPost(urlPath, body, port = 8080) {
    const config = readConfig();
    const token = config?.token ?? "";
    try {
        const res = await fetch(`http://localhost:${port}${urlPath}`, {
            method: "POST",
            headers: {
                Authorization: `Bearer ${token}`,
                "Content-Type": "application/json",
            },
            body: JSON.stringify(body),
            signal: AbortSignal.timeout(5000),
        });
        if (!res.ok) {
            const error = { status: res.status, message: res.statusText };
            throw error;
        }
        return (await res.json());
    }
    catch (err) {
        if (err instanceof TypeError &&
            err.cause?.code === "ECONNREFUSED") {
            const error = { status: 0, message: 'Server not running. Run "agentgazer start" first.' };
            throw error;
        }
        throw err;
    }
}
export async function apiPut(urlPath, body, port = 8080) {
    const config = readConfig();
    const token = config?.token ?? "";
    try {
        const res = await fetch(`http://localhost:${port}${urlPath}`, {
            method: "PUT",
            headers: {
                Authorization: `Bearer ${token}`,
                "Content-Type": "application/json",
            },
            body: JSON.stringify(body),
            signal: AbortSignal.timeout(5000),
        });
        if (!res.ok) {
            const error = { status: res.status, message: res.statusText };
            throw error;
        }
        return (await res.json());
    }
    catch (err) {
        if (err instanceof TypeError &&
            err.cause?.code === "ECONNREFUSED") {
            const error = { status: 0, message: 'Server not running. Run "agentgazer start" first.' };
            throw error;
        }
        throw err;
    }
}
export async function apiDelete(urlPath, port = 8080) {
    const config = readConfig();
    const token = config?.token ?? "";
    try {
        const res = await fetch(`http://localhost:${port}${urlPath}`, {
            method: "DELETE",
            headers: { Authorization: `Bearer ${token}` },
            signal: AbortSignal.timeout(5000),
        });
        if (!res.ok) {
            const error = { status: res.status, message: res.statusText };
            throw error;
        }
        return (await res.json());
    }
    catch (err) {
        if (err instanceof TypeError &&
            err.cause?.code === "ECONNREFUSED") {
            const error = { status: 0, message: 'Server not running. Run "agentgazer start" first.' };
            throw error;
        }
        throw err;
    }
}
export function handleApiError(err) {
    if (err && typeof err === "object" && "message" in err) {
        console.error(err.message);
    }
    else {
        console.error("An unexpected error occurred.");
    }
    process.exit(1);
}
//# sourceMappingURL=api.js.map