"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.WizardReadiness = exports.DEFAULT_WIZARD_READINESS_CONFIG = void 0;
exports.checkAllExternalServices = checkAllExternalServices;
exports.evaluateWizardReadiness = evaluateWizardReadiness;
const types_1 = require("./types");
const statuspage_1 = require("./statuspage");
const endpoints_1 = require("./endpoints");
// ---------------------------------------------------------------------------
// Service labels (used in human-readable reason strings)
// ---------------------------------------------------------------------------
const SERVICE_LABELS = {
    anthropic: 'Anthropic',
    amplitudeOverall: 'Amplitude',
    amplitudeComponents: 'Amplitude (components)',
    github: 'GitHub',
    npmOverall: 'npm',
    npmComponents: 'npm (components)',
    cloudflareOverall: 'Cloudflare',
    cloudflareComponents: 'Cloudflare (components)',
    llmGateway: 'LLM Gateway',
    mcp: 'MCP',
};
/**
 * See README section "Health checks" for the full rationale.
 * Adjust these arrays to change what blocks a wizard run.
 */
exports.DEFAULT_WIZARD_READINESS_CONFIG = {
    downBlocksRun: [
        'anthropic',
        'amplitudeOverall',
        'npmOverall',
        'llmGateway',
        'mcp',
    ],
    degradedBlocksRun: ['anthropic'],
};
// ---------------------------------------------------------------------------
// Aggregate check
// ---------------------------------------------------------------------------
async function checkAllExternalServices() {
    const [anthropic, amplitudeOverall, amplitudeComponents, github, npmOverall, npmComponents, cloudflareOverall, cloudflareComponents, llmGateway, mcp,] = await Promise.all([
        (0, statuspage_1.checkAnthropicHealth)(),
        (0, statuspage_1.checkAmplitudeOverallHealth)(),
        (0, statuspage_1.checkAmplitudeComponentHealth)(),
        (0, statuspage_1.checkGithubHealth)(),
        (0, statuspage_1.checkNpmOverallHealth)(),
        (0, statuspage_1.checkNpmComponentHealth)(),
        (0, statuspage_1.checkCloudflareOverallHealth)(),
        (0, statuspage_1.checkCloudflareComponentHealth)(),
        (0, endpoints_1.checkLlmGatewayHealth)(),
        (0, endpoints_1.checkMcpHealth)(),
    ]);
    return {
        anthropic,
        amplitudeOverall,
        amplitudeComponents,
        github,
        npmOverall,
        npmComponents,
        cloudflareOverall,
        cloudflareComponents,
        llmGateway,
        mcp,
    };
}
// ---------------------------------------------------------------------------
// Wizard readiness evaluation
// ---------------------------------------------------------------------------
var WizardReadiness;
(function (WizardReadiness) {
    WizardReadiness["Yes"] = "yes";
    WizardReadiness["No"] = "no";
    WizardReadiness["YesWithWarnings"] = "yes_with_warnings";
})(WizardReadiness || (exports.WizardReadiness = WizardReadiness = {}));
function describeResult(label, h) {
    const parts = [`${label}: ${h.status}`];
    if (h.rawIndicator)
        parts.push(`indicator=${h.rawIndicator}`);
    if (h.error)
        parts.push(h.error);
    return parts.join(' — ');
}
const MAX_COMPONENT_NAMES = 8;
function describeComponents(label, h) {
    const affected = h.degradedOrDownComponents;
    if (!affected || affected.length === 0)
        return `${label} components: all operational`;
    const shown = affected
        .slice(0, MAX_COMPONENT_NAMES)
        .map((c) => `${c.name} (${c.status})`);
    const suffix = affected.length > MAX_COMPONENT_NAMES
        ? `, +${affected.length - MAX_COMPONENT_NAMES} more`
        : '';
    return `${label} components impacted: ${shown.join(', ')}${suffix}`;
}
async function evaluateWizardReadiness(config = exports.DEFAULT_WIZARD_READINESS_CONFIG) {
    const health = await checkAllExternalServices();
    const reasons = [];
    for (const key of Object.keys(health)) {
        const result = health[key];
        const label = SERVICE_LABELS[key];
        reasons.push(describeResult(label, result));
        if ('degradedOrDownComponents' in result) {
            reasons.push(describeComponents(label, result));
        }
    }
    const blocked = config.downBlocksRun.some((k) => health[k].status === types_1.ServiceHealthStatus.Down) ||
        (config.degradedBlocksRun ?? []).some((k) => health[k].status !== types_1.ServiceHealthStatus.Healthy);
    if (blocked) {
        return { decision: WizardReadiness.No, health, reasons };
    }
    const hasWarnings = Object.values(health).some((h) => h.status !== types_1.ServiceHealthStatus.Healthy);
    if (hasWarnings) {
        return { decision: WizardReadiness.YesWithWarnings, health, reasons };
    }
    return { decision: WizardReadiness.Yes, health, reasons };
}
