"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkMcpHealth = exports.checkLlmGatewayHealth = void 0;
const types_1 = require("./types");
// ---------------------------------------------------------------------------
// Direct endpoint health checks
//
// These ping Amplitude-owned services directly (no Statuspage intermediary).
// A non-expected HTTP status or any network error is treated as Down.
//
// LLM Gateway – FastAPI service
//   Source: amplitude/services/llm-gateway/src/llm_gateway/api/health.py
//   GET /_liveness → 200 {"status":"alive"}
//
// MCP – Cloudflare Worker
//   Source: amplitude/services/mcp/src/index.ts
//   GET / → 200 (HTML landing page)
// ---------------------------------------------------------------------------
function downResult(error) {
    return { status: types_1.ServiceHealthStatus.Down, error };
}
async function fetchEndpointHealth(url, timeoutMs = 5000, expectedStatus = 200) {
    try {
        const controller = new AbortController();
        const tid = setTimeout(() => controller.abort(), timeoutMs);
        const res = await fetch(url, { signal: controller.signal });
        clearTimeout(tid);
        if (res.status === expectedStatus) {
            return {
                status: types_1.ServiceHealthStatus.Healthy,
                rawIndicator: `HTTP ${res.status}`,
            };
        }
        return downResult(`HTTP ${res.status}`);
    }
    catch (e) {
        if (e instanceof Error && e.name === 'AbortError')
            return downResult('Request timed out');
        return downResult(e instanceof Error ? e.message : 'Unknown error');
    }
}
const checkLlmGatewayHealth = () => fetchEndpointHealth('https://gateway.us.amplitude.com/_liveness');
exports.checkLlmGatewayHealth = checkLlmGatewayHealth;
const checkMcpHealth = () => fetchEndpointHealth('https://mcp.amplitude.com/');
exports.checkMcpHealth = checkMcpHealth;
