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 { StatusBadge } from "./design/StatusBadge.js";
import { truncateCells } from "./design/textLayout.js";
export function CacheShapePanel(props) {
    const { columns } = useTerminalSize();
    const width = Math.max(62, Math.min(112, columns - 4));
    const fingerprintWidth = Math.max(12, Math.min(24, Math.floor(width * 0.24)));
    const lastWidth = 19;
    return (_jsx(Box, { flexDirection: "column", marginBottom: 1, children: _jsxs(Pane, { width: width, title: "cache shapes", 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 })] }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { color: "gray", children: truncateCells(props.model.summary, Math.max(24, width - 4)) }) }), _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.fingerprint.padEnd(fingerprintWidth), fingerprintWidth), " "] }), _jsx(Text, { color: "gray", children: truncateCells(row.lastSeenAt.padEnd(lastWidth), lastWidth) }), _jsxs(Text, { color: "gray", children: [" ", truncateCells(row.detail, Math.max(12, width - fingerprintWidth - lastWidth - 16))] })] }, row.key))) }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { color: "gray", children: truncateCells(props.model.footer, Math.max(24, width - 4)) }) })] }) }));
}
export function buildCacheShapePanelModel(records) {
    const rows = records.map((record) => ({
        key: record.fingerprint,
        label: `x${record.count}`,
        tone: toneForRisk(record.risk),
        fingerprint: record.fingerprint,
        lastSeenAt: compactTime(record.lastSeenAt),
        detail: [
            `risk=${record.risk}`,
            `dynamic=${Math.round(record.dynamicShare * 100)}%`,
            `stable=${record.stableChars}`,
            record.truncatedBlocks.length ? `truncated=${record.truncatedBlocks.join(",")}` : "truncated=none",
        ].join(" "),
    }));
    if (rows.length === 0) {
        rows.push({
            key: "empty",
            label: "none",
            tone: "muted",
            fingerprint: "no-shapes",
            lastSeenAt: "",
            detail: "run /cache plan <goal> to record a content-free prompt shape",
        });
    }
    const repeated = records.filter((record) => record.count > 1).length;
    const risky = records.filter((record) => record.risk !== "low").length;
    return {
        title: "DeepSeek prompt shape history",
        subtitle: "content-free fingerprints for cache-friendly prompt reuse",
        badge: records.length === 0 ? "empty" : risky > 0 ? "review" : "stable",
        badgeTone: records.length === 0 ? "muted" : risky > 0 ? "warning" : "success",
        summary: `shapes=${records.length} repeated=${repeated} review=${risky}`,
        rows,
        footer: "/cache plan <goal> | /cache shapes clear | /cache pin apply [goal]",
    };
}
function toneForRisk(risk) {
    if (risk === "high")
        return "error";
    if (risk === "medium")
        return "warning";
    return "success";
}
function compactTime(value) {
    const timestamp = Date.parse(value);
    if (!Number.isFinite(timestamp))
        return value;
    return new Date(timestamp).toISOString().replace("T", " ").slice(0, 19);
}
