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 CachePreflightPanel(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 preflight", 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: "ready", 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 buildCachePreflightPanelModel(report) {
    const rows = [
        {
            key: "plan",
            label: "plan",
            tone: report.droppedChars > 0 || report.truncatedBlocks.length > 0 ? "warning" : "success",
            name: "prompt budget",
            detail: `tokens~${report.planTokens} dropped=${report.droppedChars} truncated=${report.truncatedBlocks.length ? report.truncatedBlocks.join(",") : "none"}`,
        },
        {
            key: "stability",
            label: "shape",
            tone: toneForRisk(report.stabilityRisk),
            name: "prompt shape",
            detail: `${report.stabilityRisk} dynamic=${Math.round(report.dynamicShare * 100)}% ${report.shapeRepeat} ${report.shapeFingerprint}`,
        },
        {
            key: "pins",
            label: "pin",
            tone: toneForPinSeverity(report.pinSeverity),
            name: "stable pins",
            detail: `count=${report.pinCount} severity=${report.pinSeverity} issues=${report.pinIssues}`,
        },
        {
            key: "suggestions",
            label: "hint",
            tone: report.suggestionCount > 0 ? "brand" : "muted",
            name: "pin suggestions",
            detail: report.topSuggestions.length
                ? report.topSuggestions.map((suggestion) => `${suggestion.name}:${suggestion.score}`).join(" ")
                : "none",
        },
        ...report.recommendations.slice(0, 5).map((recommendation, index) => ({
            key: `rec:${index}`,
            label: "next",
            tone: recommendationTone(recommendation),
            name: `step ${index + 1}`,
            detail: recommendation,
        })),
        ...report.nextCommands.slice(0, 4).map((command, index) => ({
            key: `cmd:${index}`,
            label: "cmd",
            tone: "success",
            name: `command ${index + 1}`,
            detail: command,
        })),
    ];
    return {
        title: "DeepSeek cache preflight",
        subtitle: report.goal,
        badge: report.status,
        badgeTone: toneForStatus(report.status),
        ratio: report.readinessScore / 100,
        summary: `score=${report.readinessScore} effort=${report.effort} readiness=${report.readinessStatus}`,
        rows,
        footer: report.nextCommands.length ? report.nextCommands.join(" | ") : "/cache pin apply <goal> | /cache plan <goal> | /cache doctor",
    };
}
function toneForStatus(status) {
    if (status === "ready")
        return "success";
    if (status === "blocked")
        return "error";
    return "warning";
}
function toneForRisk(risk) {
    if (risk === "high")
        return "error";
    if (risk === "medium")
        return "warning";
    return "success";
}
function toneForPinSeverity(severity) {
    if (severity === "error")
        return "error";
    if (severity === "warning")
        return "warning";
    return "success";
}
function recommendationTone(recommendation) {
    return /ready|proceed/i.test(recommendation) ? "success" : "brand";
}
