import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Box, Text } from "ink";
import { useTerminalSize } from "../hooks/useTerminalSize.js";
import { Pane } from "./design/Pane.js";
import { ProgressBar } from "./design/ProgressBar.js";
import { StatusBadge } from "./design/StatusBadge.js";
import { truncateCells } from "./design/textLayout.js";
export function StatusPanel(props) {
    const { columns } = useTerminalSize();
    const width = Math.max(52, Math.min(112, columns - 4));
    const cacheTotal = props.model.cacheHitTokens + props.model.cacheMissTokens;
    const cacheRatio = cacheTotal > 0 ? props.model.cacheHitTokens / cacheTotal : 0;
    return (_jsx(Box, { flexDirection: "column", marginBottom: 1, children: _jsxs(Pane, { width: width, title: "status", tone: "brand", 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.subtitle, Math.max(24, width - 20)) })] }), _jsx(StatusBadge, { label: cacheTotal > 0 ? `${Math.round(cacheRatio * 100)}% cache` : "cache n/a", tone: cacheTone(cacheRatio, cacheTotal) })] }), _jsxs(Box, { marginTop: 1, marginBottom: 1, children: [_jsx(Text, { color: "gray", children: "cache " }), _jsx(ProgressBar, { ratio: cacheRatio, width: Math.max(12, Math.min(34, width - 22)), filledTone: cacheTone(cacheRatio, cacheTotal) }), _jsxs(Text, { color: "gray", children: [" hit ", props.model.cacheHitTokens, " / miss ", props.model.cacheMissTokens] })] }), _jsx(Box, { flexDirection: "column", children: props.model.rows.map((row) => (_jsx(StatusPanelRowView, { row: row, width: width }, row.key))) }), _jsx(Text, { color: "gray", children: truncateCells(props.model.footer, Math.max(24, width - 4)) })] }) }));
}
export function statusPanelModel(status) {
    return {
        title: status.product,
        subtitle: `${status.projectPath} / ${status.model}`,
        cacheHitTokens: status.cache.hitTokens,
        cacheMissTokens: status.cache.missTokens,
        rows: [
            row("provider", status.providerReady ? "ready" : "missing", status.providerReady ? "success" : "error", status.model, status.providerReady ? "DeepSeek provider configured" : "configure DEEPSEEK_API_KEY"),
            row("permissions", status.permissionProfile, permissionTone(status), `shell=${onOff(status.permissions.shell)} browser=${onOff(status.permissions.browser)}`, "use /permissions to switch profile"),
            row("runs", `${status.runs.unfinished} open`, status.runs.unfinished > 0 ? "warning" : "success", `recent=${status.runs.totalRecent}`, latestRunNote(status)),
            row("tasks", `${status.tasks.queued} queued`, status.tasks.failed > 0 ? "error" : status.tasks.running > 0 ? "warning" : "muted", `running=${status.tasks.running} ok=${status.tasks.succeeded}`, `failed=${status.tasks.failed} paused=${status.tasks.paused} cancelled=${status.tasks.cancelled}`),
            row("gates", `${status.gates.approvalsPending} approval`, gateTone(status), `validating=${status.gates.validationsPending}`, `failed validations=${status.gates.validationsFailed}`),
            row("git", status.git.available ? status.git.clean ? "clean" : "dirty" : "unavailable", gitTone(status), gitDetail(status), status.git.error ?? ""),
            row("data", "path", "muted", status.dataDir, "local state and transcripts"),
        ],
        footer: "/doctor | /cache doctor current | /runs | /permissions status",
    };
}
function StatusPanelRowView(props) {
    const detailWidth = Math.max(20, props.width - 38);
    const noteWidth = Math.max(20, props.width - 12);
    return (_jsxs(Box, { flexDirection: "column", marginBottom: 1, children: [_jsxs(Box, { flexDirection: "row", children: [_jsx(StatusBadge, { label: props.row.status, tone: props.row.tone }), _jsx(Text, { children: " " }), _jsx(Text, { color: "cyan", children: truncateCells(props.row.label.padEnd(20), 20) }), _jsx(Text, { color: "gray", children: truncateCells(props.row.detail, detailWidth) })] }), props.row.note ? (_jsxs(Box, { flexDirection: "row", children: [_jsx(Text, { color: "gray", children: "  " }), _jsx(Text, { color: "gray", children: truncateCells(props.row.note, noteWidth) })] })) : null] }));
}
function row(key, status, tone, detail, note) {
    return { key, label: key, status, tone, detail, note };
}
function onOff(value) {
    return value ? "on" : "off";
}
function cacheTone(ratio, total) {
    if (total <= 0)
        return "muted";
    if (ratio >= 0.75)
        return "success";
    if (ratio >= 0.35)
        return "warning";
    return "error";
}
function permissionTone(status) {
    if (status.permissions.shell && status.permissions.browser)
        return "warning";
    if (status.permissions.shell || status.permissions.browser)
        return "brand";
    return "success";
}
function gateTone(status) {
    if (status.gates.validationsFailed > 0)
        return "error";
    if (status.gates.approvalsPending > 0 || status.gates.validationsPending > 0)
        return "warning";
    return "success";
}
function gitTone(status) {
    if (!status.git.available)
        return "muted";
    if (status.git.conflicted > 0)
        return "error";
    return status.git.clean ? "success" : "warning";
}
function latestRunNote(status) {
    const latest = status.runs.latest;
    return latest ? `${latest.status} ${latest.id} actions=${latest.actionCount} artifacts=${latest.artifactCount} ${latest.message}` : "no runs";
}
function gitDetail(status) {
    const git = status.git;
    if (!git.available)
        return "git unavailable";
    if (git.clean)
        return "working tree clean";
    return `M=${git.modified} A=${git.added} D=${git.deleted} R=${git.renamed} ?=${git.untracked} U=${git.conflicted}`;
}
