/**
 * Runs health + anomaly REST clients — @vauban-org/agent-sdk v0.5.3.
 *
 * Wraps:
 *   GET /api/agents/:id/health         → AgentHealth
 *   GET /api/agents/:id/anomalies      → Anomaly[]
 *   GET /api/router/circuit-breakers   → CircuitBreakerSnapshot[]
 *
 * Sprint: command-center:sprint-523:quick-6
 */
import { subscribeToRun } from "./subscribe.js";
async function apiFetch(url, getToken) {
    const headers = {
        Accept: "application/json",
    };
    if (getToken) {
        const token = await getToken();
        headers["Authorization"] = `Bearer ${token}`;
    }
    const response = await fetch(url, { headers });
    if (!response.ok) {
        throw new Error(`RunsClient: HTTP ${response.status} — ${url}`);
    }
    return response.json();
}
export function createRunsClient(opts) {
    const base = opts.baseUrl.replace(/\/$/, "");
    return {
        async getHealth(agentId, window = "24h") {
            const url = `${base}/api/agents/${encodeURIComponent(agentId)}/health?window=${window}`;
            return apiFetch(url, opts.getToken);
        },
        async getAnomalies(agentId) {
            const url = `${base}/api/agents/${encodeURIComponent(agentId)}/anomalies`;
            const res = await apiFetch(url, opts.getToken);
            return res.anomalies;
        },
        async getCircuitBreakers() {
            const url = `${base}/api/router/circuit-breakers`;
            const res = await apiFetch(url, opts.getToken);
            return res.circuit_breakers;
        },
        subscribeToRun(runId, subOpts) {
            return subscribeToRun(runId, {
                ...subOpts,
                baseUrl: base,
                getToken: opts.getToken,
            });
        },
    };
}
//# sourceMappingURL=health.js.map