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";
import { EffortCallout, effortCalloutModel } from "./EffortCallout.js";
const MAX_ACTION_CONTEXT = 28_000;
const MAX_ACTION_DYNAMIC = 36_000;
const MAX_SIDE_CONTEXT = 18_000;
const MAX_SIDE_DYNAMIC = 24_000;
const MAX_OUTPUT = 1_800;
export function BudgetPanel(props) {
    const { columns } = useTerminalSize();
    const width = Math.max(58, Math.min(112, columns - 4));
    return (_jsx(Box, { flexDirection: "column", marginBottom: 1, children: _jsxs(Pane, { width: width, title: "budget", 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, { flexDirection: "column", marginTop: 1, children: props.model.rows.map((row) => (_jsx(BudgetPanelRowView, { row: row, width: width }, row.key))) }), props.model.callout ? _jsx(EffortCallout, { model: props.model.callout, width: width }) : null, _jsx(Text, { color: "gray", children: truncateCells(props.model.footer, Math.max(24, width - 4)) })] }) }));
}
export function budgetPanelModel(input) {
    return {
        title: input.action === "set"
            ? "Effort updated"
            : input.action === "auto"
                ? "Effort reset"
                : input.action === "path"
                    ? "Effort config path"
                    : "Effort budget",
        subtitle: input.path ?? "DeepSeek prompt and output budget",
        badge: input.budget.effort,
        badgeTone: toneForEffort(input.budget.effort),
        rows: [
            {
                key: "action-context",
                label: "context",
                value: `${input.budget.actionContextChars}`,
                ratio: input.budget.actionContextChars / MAX_ACTION_CONTEXT,
                tone: "brand",
                note: "stable project/action context budget",
            },
            {
                key: "dynamic",
                label: "dynamic",
                value: `${input.budget.actionDynamicChars}`,
                ratio: input.budget.actionDynamicChars / MAX_ACTION_DYNAMIC,
                tone: "warning",
                note: "dynamic request and selected-file budget",
            },
            {
                key: "side",
                label: "side",
                value: `${input.budget.sideQuestionContextChars}/${input.budget.sideQuestionDynamicChars}`,
                ratio: ((input.budget.sideQuestionContextChars / MAX_SIDE_CONTEXT) + (input.budget.sideQuestionDynamicChars / MAX_SIDE_DYNAMIC)) / 2,
                tone: "muted",
                note: "side-question context/dynamic budget",
            },
            {
                key: "output",
                label: "output",
                value: `${input.runtimeMaxOutputTokens ?? input.budget.maxOutputTokens}`,
                ratio: (input.runtimeMaxOutputTokens ?? input.budget.maxOutputTokens) / MAX_OUTPUT,
                tone: "success",
                note: "provider max output token cap",
            },
        ],
        callout: effortCalloutModel({
            effort: input.budget.effort,
        }),
        footer: "/effort low|medium|high|max|auto | /cache plan <goal>",
    };
}
function BudgetPanelRowView(props) {
    const valueWidth = Math.max(8, Math.min(14, Math.floor(props.width * 0.14)));
    const noteWidth = Math.max(20, props.width - 46);
    const barWidth = Math.max(8, Math.min(22, props.width - valueWidth - noteWidth - 20));
    return (_jsx(Box, { flexDirection: "column", marginBottom: 1, children: _jsxs(Box, { flexDirection: "row", children: [_jsx(StatusBadge, { label: props.row.label, tone: props.row.tone }), _jsx(Text, { children: " " }), _jsx(Text, { color: "gray", children: truncateCells(props.row.value.padEnd(valueWidth), valueWidth) }), _jsx(ProgressBar, { ratio: props.row.ratio, width: barWidth, filledTone: props.row.tone }), _jsx(Text, { color: "gray", children: " " }), _jsx(Text, { color: "gray", children: truncateCells(props.row.note, noteWidth) })] }) }));
}
function toneForEffort(effort) {
    if (effort === "low")
        return "success";
    if (effort === "max")
        return "warning";
    if (effort === "auto")
        return "brand";
    return "muted";
}
