"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkCloudflareComponentHealth = exports.checkCloudflareOverallHealth = exports.checkNpmComponentHealth = exports.checkNpmOverallHealth = exports.checkGithubHealth = exports.checkAmplitudeComponentHealth = exports.checkAmplitudeOverallHealth = exports.checkAnthropicHealth = void 0;
const types_1 = require("./types");
function mapIndicator(v) {
    switch (v) {
        case 'none':
            return types_1.ServiceHealthStatus.Healthy;
        case 'minor':
            return types_1.ServiceHealthStatus.Degraded;
        case 'major':
        case 'critical':
            return types_1.ServiceHealthStatus.Down;
        default:
            return types_1.ServiceHealthStatus.Degraded;
    }
}
function mapComponentRaw(v) {
    switch (v) {
        case 'operational':
            return types_1.ServiceHealthStatus.Healthy;
        case 'degraded_performance':
        case 'under_maintenance':
            return types_1.ServiceHealthStatus.Degraded;
        case 'partial_outage':
        case 'major_outage':
            return types_1.ServiceHealthStatus.Down;
        default:
            return types_1.ServiceHealthStatus.Degraded;
    }
}
function errResult(error) {
    return { status: types_1.ServiceHealthStatus.Degraded, error };
}
async function fetchStatuspageIndicator(url, timeoutMs = 5000) {
    try {
        const controller = new AbortController();
        const tid = setTimeout(() => controller.abort(), timeoutMs);
        const res = await fetch(url, { signal: controller.signal });
        clearTimeout(tid);
        if (!res.ok)
            return errResult(`HTTP ${res.status}`);
        const data = (await res.json());
        const indicator = data.status?.indicator ?? null;
        return {
            status: mapIndicator(indicator),
            rawIndicator: indicator ?? undefined,
        };
    }
    catch (e) {
        if (e instanceof Error && e.name === 'AbortError')
            return errResult('Request timed out');
        return errResult(e instanceof Error ? e.message : 'Unknown error');
    }
}
async function fetchStatuspageSummary(url, timeoutMs = 5000) {
    try {
        const controller = new AbortController();
        const tid = setTimeout(() => controller.abort(), timeoutMs);
        const res = await fetch(url, { signal: controller.signal });
        clearTimeout(tid);
        if (!res.ok)
            return errResult(`HTTP ${res.status}`);
        const data = (await res.json());
        const indicator = data.status?.indicator ?? null;
        const overall = mapIndicator(indicator);
        const affected = (data.components ?? [])
            .map((c) => ({
            name: c.name,
            status: mapComponentRaw(c.status),
            rawStatus: c.status,
        }))
            .filter((c) => c.status !== types_1.ServiceHealthStatus.Healthy);
        return {
            status: affected.length > 0 ? types_1.ServiceHealthStatus.Degraded : overall,
            rawIndicator: indicator ?? undefined,
            degradedOrDownComponents: affected.length > 0 ? affected : undefined,
        };
    }
    catch (e) {
        if (e instanceof Error && e.name === 'AbortError')
            return errResult('Request timed out');
        return errResult(e instanceof Error ? e.message : 'Unknown error');
    }
}
// ---------------------------------------------------------------------------
// Individual statuspage-backed checks
// ---------------------------------------------------------------------------
/** Model provider health — no-op, wizard uses Vertex AI via proxy. */
const checkAnthropicHealth = () => Promise.resolve({
    status: types_1.ServiceHealthStatus.Healthy,
    pageUrl: '',
});
exports.checkAnthropicHealth = checkAnthropicHealth;
const checkAmplitudeOverallHealth = () => fetchStatuspageIndicator('https://www.amplitudestatus.com/api/v2/status.json');
exports.checkAmplitudeOverallHealth = checkAmplitudeOverallHealth;
const checkAmplitudeComponentHealth = () => fetchStatuspageSummary('https://www.amplitudestatus.com/api/v2/summary.json');
exports.checkAmplitudeComponentHealth = checkAmplitudeComponentHealth;
const checkGithubHealth = () => fetchStatuspageIndicator('https://www.githubstatus.com/api/v2/status.json');
exports.checkGithubHealth = checkGithubHealth;
const checkNpmOverallHealth = () => fetchStatuspageIndicator('https://status.npmjs.org/api/v2/status.json');
exports.checkNpmOverallHealth = checkNpmOverallHealth;
const checkNpmComponentHealth = () => fetchStatuspageSummary('https://status.npmjs.org/api/v2/summary.json');
exports.checkNpmComponentHealth = checkNpmComponentHealth;
const checkCloudflareOverallHealth = () => fetchStatuspageIndicator('https://www.cloudflarestatus.com/api/v2/status.json');
exports.checkCloudflareOverallHealth = checkCloudflareOverallHealth;
const checkCloudflareComponentHealth = () => fetchStatuspageSummary('https://www.cloudflarestatus.com/api/v2/summary.json');
exports.checkCloudflareComponentHealth = checkCloudflareComponentHealth;
