/**
 * Pure NERF health widget logic.
 *
 * Separates project-state detection and line rendering from the widget's
 * runtime integrations so the regressions can be tested directly.
 */
import { existsSync } from "node:fs";
import { detectProjectState } from "./detection.js";
import { nerfRoot } from "./paths.js";
export function detectHealthWidgetProjectState(basePath) {
    if (!existsSync(nerfRoot(basePath)))
        return "none";
    const { state } = detectProjectState(basePath);
    return state === "v2-nerf" ? "active" : "initialized";
}
function formatCost(n) {
    return n >= 1 ? `$${n.toFixed(2)}` : `${(n * 100).toFixed(1)}¢`;
}
/**
 * Build compact health lines for the widget.
 * Returns a string array suitable for setWidget().
 */
export function buildHealthLines(data) {
    if (data.projectState === "none") {
        return ["  NERF  No project loaded — run /nerf to start"];
    }
    if (data.projectState === "initialized") {
        return ["  NERF  Project initialized — run /nerf to continue setup"];
    }
    const parts = [];
    const totalIssues = data.environmentErrorCount + data.environmentWarningCount + (data.providerIssue ? 1 : 0);
    if (totalIssues === 0) {
        parts.push("● System OK");
    }
    else if (data.environmentErrorCount > 0 || data.providerIssue?.includes("✗")) {
        parts.push(`✗ ${totalIssues} issue${totalIssues > 1 ? "s" : ""}`);
    }
    else {
        parts.push(`⚠ ${totalIssues} warning${totalIssues > 1 ? "s" : ""}`);
    }
    if (data.budgetCeiling !== undefined && data.budgetCeiling > 0) {
        const pct = Math.min(100, (data.budgetSpent / data.budgetCeiling) * 100);
        parts.push(`Budget: ${formatCost(data.budgetSpent)}/${formatCost(data.budgetCeiling)} (${pct.toFixed(0)}%)`);
    }
    else if (data.budgetSpent > 0) {
        parts.push(`Spent: ${formatCost(data.budgetSpent)}`);
    }
    if (data.providerIssue) {
        parts.push(data.providerIssue);
    }
    if (data.environmentErrorCount > 0) {
        parts.push(`Env: ${data.environmentErrorCount} error${data.environmentErrorCount > 1 ? "s" : ""}`);
    }
    else if (data.environmentWarningCount > 0) {
        parts.push(`Env: ${data.environmentWarningCount} warning${data.environmentWarningCount > 1 ? "s" : ""}`);
    }
    return [`  ${parts.join("  │  ")}`];
}
