/**
 * NERF Health Widget — always-on ambient health signal rendered belowEditor.
 *
 * Shows a compact 1-2 line summary: progress score, budget, provider key
 * status, and doctor/environment issue count. Refreshes every 60 seconds.
 * Quiet when everything is healthy; turns amber/red when issues arise.
 *
 * Widget key: "nerf-health", placement: "belowEditor"
 */
import { runProviderChecks, summariseProviderIssues } from "./doctor-providers.js";
import { runEnvironmentChecks } from "./doctor-environment.js";
import { loadEffectiveNerfPreferences } from "./preferences.js";
import { loadLedgerFromDisk, getProjectTotals } from "./metrics.js";
import { projectRoot } from "./commands/context.js";
import { buildHealthLines, detectHealthWidgetProjectState, } from "./health-widget-core.js";
// ── Data loader ────────────────────────────────────────────────────────────────
function loadHealthWidgetData(basePath) {
    let budgetCeiling;
    let budgetSpent = 0;
    let providerIssue = null;
    let environmentErrorCount = 0;
    let environmentWarningCount = 0;
    const projectState = detectHealthWidgetProjectState(basePath);
    try {
        const prefs = loadEffectiveNerfPreferences();
        budgetCeiling = prefs?.preferences?.budget_ceiling;
        const ledger = loadLedgerFromDisk(basePath);
        if (ledger) {
            const totals = getProjectTotals(ledger.units ?? []);
            budgetSpent = totals.cost;
        }
    }
    catch { /* non-fatal */ }
    try {
        const providerResults = runProviderChecks();
        providerIssue = summariseProviderIssues(providerResults);
    }
    catch { /* non-fatal */ }
    try {
        const envResults = runEnvironmentChecks(basePath);
        for (const r of envResults) {
            if (r.status === "error")
                environmentErrorCount++;
            else if (r.status === "warning")
                environmentWarningCount++;
        }
    }
    catch { /* non-fatal */ }
    return {
        projectState,
        budgetCeiling,
        budgetSpent,
        providerIssue,
        environmentErrorCount,
        environmentWarningCount,
        lastRefreshed: Date.now(),
    };
}
// ── Widget init ────────────────────────────────────────────────────────────────
const REFRESH_INTERVAL_MS = 60_000;
/**
 * Initialize the always-on nerf-health widget (belowEditor).
 * Call once from the extension entry point after context is available.
 */
export function initHealthWidget(ctx) {
    if (!ctx.hasUI)
        return;
    const basePath = projectRoot();
    // String-array fallback — used in RPC mode (factory is a no-op there)
    const initialData = loadHealthWidgetData(basePath);
    ctx.ui.setWidget("nerf-health", buildHealthLines(initialData), { placement: "belowEditor" });
    // Factory-based widget for TUI mode — replaces the string-array above
    ctx.ui.setWidget("nerf-health", (_tui, _theme) => {
        let data = initialData;
        let cachedLines;
        let refreshInFlight = false;
        const refresh = async () => {
            if (refreshInFlight)
                return;
            refreshInFlight = true;
            try {
                data = loadHealthWidgetData(basePath);
                cachedLines = undefined;
                _tui.requestRender();
            }
            catch { /* non-fatal */ }
            finally {
                refreshInFlight = false;
            }
        };
        // Fire first enrichment immediately. requestRender() inside is a no-op
        // if the widget has not yet rendered, so this is safe before factory return.
        void refresh();
        const refreshTimer = setInterval(() => {
            void refresh();
        }, REFRESH_INTERVAL_MS);
        return {
            render(_width) {
                if (!cachedLines)
                    cachedLines = buildHealthLines(data);
                return cachedLines;
            },
            invalidate() { cachedLines = undefined; },
            dispose() {
                clearInterval(refreshTimer);
            },
        };
    }, { placement: "belowEditor" });
}
