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 CacheReadinessPanel(props) {
    const { columns } = useTerminalSize();
    const width = Math.max(62, Math.min(112, columns - 4));
    const nameWidth = Math.max(14, Math.min(28, Math.floor(width * 0.28)));
    return (_jsx(Box, { flexDirection: "column", marginBottom: 1, children: _jsxs(Pane, { width: width, title: "cache readiness", 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: props.model.badge, tone: props.model.badgeTone })] }), _jsxs(Box, { flexDirection: "row", marginTop: 1, children: [_jsx(StatusBadge, { label: "score", 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 buildCacheReadinessPanelModel(report) {
    const rows = [
        {
            key: "telemetry",
            label: "hit",
            tone: report.telemetry.hitTokens + report.telemetry.missTokens > 0 ? toneForRate(report.telemetry.rate) : "muted",
            name: "telemetry",
            detail: `rate=${report.telemetry.rate} hit=${report.telemetry.hitTokens} miss=${report.telemetry.missTokens} runs=${report.telemetry.observedRuns}`,
        },
        {
            key: "pins",
            label: "pin",
            tone: toneForSeverity(report.pinSeverity),
            name: "stable pins",
            detail: `count=${report.pinCount} chars=${report.totalPinChars} issues=${report.pinIssues}`,
        },
        {
            key: "shapes",
            label: "shape",
            tone: report.riskyShapes > 0 ? "warning" : report.totalShapes > 0 ? "success" : "muted",
            name: "prompt shapes",
            detail: `count=${report.totalShapes} repeated=${report.repeatedShapes} risky=${report.riskyShapes}${report.latestShape ? ` latest=${report.latestShape}` : ""}`,
        },
        ...report.recommendations.slice(0, 5).map((recommendation, index) => ({
            key: `rec:${index}`,
            label: "next",
            tone: recommendationTone(recommendation),
            name: `step ${index + 1}`,
            detail: recommendation,
        })),
    ];
    return {
        title: "DeepSeek cache readiness",
        subtitle: "single-screen preflight for token-saving DeepSeek runs",
        badge: report.status,
        badgeTone: toneForStatus(report.status),
        ratio: report.score / 100,
        summary: `score=${report.score} telemetry=${report.telemetry.rate} pins=${report.pinCount} shapes=${report.totalShapes}`,
        rows,
        footer: "/cache pin apply <goal> | /cache plan <goal> | /cache doctor",
    };
}
function toneForStatus(status) {
    if (status === "ready")
        return "success";
    if (status === "review")
        return "warning";
    return "muted";
}
function toneForSeverity(severity) {
    if (severity === "error")
        return "error";
    if (severity === "warning")
        return "warning";
    return "success";
}
function toneForRate(rate) {
    const value = Number(rate.replace("%", ""));
    if (!Number.isFinite(value))
        return "muted";
    if (value >= 60)
        return "success";
    return "warning";
}
function recommendationTone(recommendation) {
    return /strong|looks strong/i.test(recommendation) ? "success" : "brand";
}
