import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Box, Text } from "ink";
import { Pane } from "./design/Pane.js";
import { SelectList } from "./design/SelectList.js";
import { StatusBadge } from "./design/StatusBadge.js";
import { truncateCells } from "./design/textLayout.js";
import { ctrlOToExpand } from "./CtrlOToExpand.js";
export function DiagnosticsDisplay(props) {
    const width = Math.max(56, Math.min(112, props.width ?? 88));
    return (_jsx(Box, { flexDirection: "column", marginBottom: 1, children: _jsxs(Pane, { width: width, title: "doctor", tone: props.model.badgeTone, paddingX: 1, children: [_jsxs(Box, { flexDirection: "row", justifyContent: "space-between", children: [_jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { bold: true, color: "cyan", children: props.model.title }), _jsx(Text, { color: "gray", children: truncateCells(props.model.summary, Math.max(24, width - 20)) })] }), _jsx(StatusBadge, { label: props.model.badge, tone: props.model.badgeTone })] }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { color: "gray", children: "checks" }) }), _jsx(SelectList, { options: diagnosticsDisplayOptions(props.model), selectedIndex: firstActionableCheckIndex(props.model.checks), visibleCount: 8, width: width }), _jsx(Text, { color: "gray", children: truncateCells(props.model.footer, Math.max(24, width - 4)) })] }) }));
}
export function diagnosticsDisplayOptions(model) {
    return model.checks.map((check) => ({
        id: check.id,
        label: check.label,
        detail: `${check.status} | ${check.detail}`,
        description: check.action,
        selected: check.tone === "error" || check.tone === "warning",
        tone: check.tone,
    }));
}
export function diagnosticsDisplayModel(input) {
    const checks = [
        check("project", "project", "ok", "brand", input.config.projectPath, "/project"),
        check("data", "data", "ok", "muted", input.config.dataDir, "/config"),
        check("state", "state", "ok", "muted", input.config.stateDbPath, "/status"),
        input.providerReady
            ? check("provider", "provider", "ready", "success", input.providerName ?? input.config.model, "/model")
            : check("provider", "provider", "missing", "error", "DEEPSEEK_API_KEY not configured", "set DEEPSEEK_API_KEY or configure .deepseekcode/providers.json"),
        permissionCheck(input.permissions, input.config.permissionProfile),
        check("runs", "recent runs", `${input.runs.length}`, input.runs.length > 0 ? "brand" : "muted", latestRunDetail(input.runs), "/runs"),
    ];
    const problems = checks.filter((row) => row.tone === "error" || row.tone === "warning").length;
    return {
        title: "DeepSeekCode doctor",
        summary: `${checks.length} checks | ${problems} attention item${problems === 1 ? "" : "s"}`,
        badge: problems > 0 ? `${problems} issue${problems === 1 ? "" : "s"}` : "ok",
        badgeTone: problems > 0 ? "warning" : "success",
        checks,
        footer: `/status | /config | /cache doctor current | /permissions status | ${ctrlOToExpand()}`,
    };
}
function check(id, label, status, tone, detail, action) {
    return { id, label, status, tone, detail, action };
}
function permissionCheck(permissions, fallbackProfile) {
    const profile = permissions.profile ?? fallbackProfile;
    const elevated = permissions.allowShell || permissions.allowBrowser;
    const detail = [
        profile,
        `shell ${permissions.allowShell ? "on" : "off"}`,
        `browser ${permissions.allowBrowser ? "on" : "off"}`,
    ].join(" | ");
    return check("permissions", "permissions", elevated ? "elevated" : "safe", elevated ? "warning" : "success", detail, "/permissions status");
}
function latestRunDetail(runs) {
    const latest = runs[0];
    if (!latest)
        return "no persisted runs";
    return `${latest.id} ${latest.status} actions=${latest.actionCount} events=${latest.eventCount}`;
}
function firstActionableCheckIndex(checks) {
    const index = checks.findIndex((check) => check.tone === "error" || check.tone === "warning");
    return Math.max(0, index);
}
