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 CacheGuardPanel(props) {
    const { columns } = useTerminalSize();
    const width = Math.max(62, Math.min(116, columns - 4));
    const nameWidth = Math.max(14, Math.min(28, Math.floor(width * 0.27)));
    return (_jsx(Box, { flexDirection: "column", marginBottom: 1, children: _jsxs(Pane, { width: width, title: "cache guard", 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.subtitle, Math.max(24, width - 20)) })] }), _jsx(StatusBadge, { label: props.model.badge, tone: props.model.badgeTone })] }), _jsxs(Box, { flexDirection: "row", marginTop: 1, children: [_jsx(StatusBadge, { label: "hit", tone: props.model.badgeTone }), _jsx(Text, { children: " " }), _jsx(ProgressBar, { ratio: props.model.ratio, width: Math.max(12, Math.min(28, width - 48)), showPercent: true }), _jsxs(Text, { color: "gray", children: [" ", truncateCells(props.model.summary, Math.max(16, width - 46))] })] }), _jsx(Box, { flexDirection: "column", marginTop: 1, children: props.model.rows.map((row) => (_jsxs(Box, { flexDirection: "row", children: [_jsx(StatusBadge, { label: row.label, tone: row.tone }), _jsxs(Text, { children: [" ", truncateCells(row.name.padEnd(nameWidth), nameWidth), " "] }), _jsx(Text, { color: "gray", children: truncateCells(row.detail, Math.max(12, width - nameWidth - 14)) })] }, row.key))) }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { color: "gray", children: truncateCells(props.model.footer, Math.max(24, width - 4)) }) })] }) }));
}
export function buildCacheGuardPanelModel(report) {
    const rows = [
        {
            key: "profile",
            label: "prof",
            tone: report.profile === "none" ? "muted" : toneForDecision(report.decision),
            name: report.profile,
            detail: `preflight=${report.preflightStatus} forecast=${report.forecastStatus} readiness=${report.readinessScore}`,
        },
        {
            key: "tokens",
            label: "tok",
            tone: toneForDecision(report.decision),
            name: `hit ${Math.round(report.estimatedHitRate * 100)}%`,
            detail: `stable~${report.stableTokens} dynamic~${report.dynamicTokens} reusable~${report.reusableTokens}`,
        },
        ...report.blockers.slice(0, 3).map((blocker, index) => ({
            key: `block:${index}`,
            label: "block",
            tone: "error",
            name: `block ${index + 1}`,
            detail: blocker,
        })),
        ...report.warnings.slice(0, 3).map((warning, index) => ({
            key: `warn:${index}`,
            label: "warn",
            tone: "warning",
            name: `warn ${index + 1}`,
            detail: warning,
        })),
        ...report.recommendations.slice(0, 3).map((recommendation, index) => ({
            key: `rec:${index}`,
            label: "tip",
            tone: "brand",
            name: `next ${index + 1}`,
            detail: recommendation,
        })),
    ];
    return {
        title: "DeepSeek cache guard",
        subtitle: report.goal,
        badge: report.decision,
        badgeTone: toneForDecision(report.decision),
        ratio: report.estimatedHitRate,
        summary: `profile=${report.profile} blockers=${report.blockers.length} warnings=${report.warnings.length}`,
        rows,
        footer: report.nextCommands.slice(0, 3).join(" | "),
    };
}
function toneForDecision(decision) {
    if (decision === "block")
        return "error";
    if (decision === "prepare")
        return "warning";
    return "success";
}
