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 CachePreparePanel(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 prepare", 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: "prefix", 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 buildCachePreparePanelModel(input) {
    const createdChars = input.applied.created.reduce((sum, pin) => sum + pin.chars, 0);
    const rows = [
        ...(input.profileMatch ? [{
                key: `profile:${input.profileMatch.profile.name}`,
                label: "prof",
                tone: "success",
                name: input.profileMatch.profile.name,
                detail: `score=${input.profileMatch.score} ${input.profileMatch.reason}`,
            }] : []),
        ...input.applied.created.slice(0, 4).map((pin) => ({
            key: `created:${pin.name}`,
            label: "new",
            tone: "success",
            name: pin.name,
            detail: `source=${pin.sourcePath} chars=${pin.chars}`,
        })),
        ...input.applied.skipped.slice(0, 3).map((pin) => ({
            key: `skipped:${pin.name}`,
            label: "skip",
            tone: "muted",
            name: pin.name,
            detail: `source=${pin.sourcePath} already pinned`,
        })),
        ...input.applied.errors.slice(0, 3).map((error) => ({
            key: `error:${error.name}`,
            label: "err",
            tone: "error",
            name: error.name,
            detail: `${error.sourcePath} ${error.message}`,
        })),
        {
            key: "preflight:readiness",
            label: "ready",
            tone: toneForPreflightStatus(input.preflight.status),
            name: "preflight",
            detail: `status=${input.preflight.status} score=${input.preflight.readinessScore} effort=${input.preflight.effort}`,
        },
        {
            key: "preflight:shape",
            label: "shape",
            tone: toneForRisk(input.preflight.stabilityRisk),
            name: input.preflight.shapeFingerprint,
            detail: `risk=${input.preflight.stabilityRisk} dynamic=${Math.round(input.preflight.dynamicShare * 100)}% ${input.preflight.shapeRepeat}`,
        },
        ...input.preflight.nextCommands.slice(0, 3).map((command, index) => ({
            key: `cmd:${index}`,
            label: "cmd",
            tone: "brand",
            name: `next ${index + 1}`,
            detail: command,
        })),
    ];
    if (input.applied.created.length === 0 && input.applied.skipped.length === 0 && input.applied.errors.length === 0) {
        rows.unshift({
            key: "pins:empty",
            label: "pin",
            tone: "muted",
            name: "no-new-pins",
            detail: "inspect candidates with /cache pin suggest",
        });
    }
    return {
        title: "DeepSeek cache prepare",
        subtitle: input.preflight.goal,
        badge: badgeFor(input),
        badgeTone: toneFor(input),
        ratio: input.preflight.readinessScore / 100,
        summary: [
            input.profileMatch ? `profile=${input.profileMatch.profile.name} match=${input.profileMatch.score}` : undefined,
            `created=${input.applied.created.length}`,
            `skipped=${input.applied.skipped.length}`,
            `boostChars=${createdChars}`,
            `preflight=${input.preflight.status}`,
        ].filter(Boolean).join(" "),
        rows,
        footer: input.preflight.nextCommands.length
            ? input.preflight.nextCommands.join(" | ")
            : "/cache plan <goal> | /cache doctor",
    };
}
function badgeFor(input) {
    if (input.applied.errors.length > 0)
        return "pin-error";
    if (input.preflight.status === "blocked")
        return "blocked";
    if (input.profileMatch)
        return "matched";
    if (input.applied.created.length > 0)
        return "prepared";
    return input.preflight.status;
}
function toneFor(input) {
    if (input.applied.errors.length > 0 || input.preflight.status === "blocked")
        return "error";
    if (input.preflight.status === "ready")
        return "success";
    if (input.profileMatch)
        return "success";
    if (input.applied.created.length > 0)
        return "success";
    return "warning";
}
function toneForPreflightStatus(status) {
    if (status === "blocked")
        return "error";
    if (status === "ready")
        return "success";
    return "warning";
}
function toneForRisk(risk) {
    if (risk === "high")
        return "error";
    if (risk === "medium")
        return "warning";
    return "success";
}
