import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Box, Text } from "ink";
import { Pane } from "./design/Pane.js";
import { StatusBadge } from "./design/StatusBadge.js";
import { flattenCellText, truncateCells } from "./design/textLayout.js";
import { useTerminalSize } from "../hooks/useTerminalSize.js";
import { formatPlanReference } from "../services/plans/planModeService.js";
export function PlanPanel(props) {
    const { columns } = useTerminalSize();
    const width = Math.max(42, Math.min(104, columns - 4));
    const model = planPanelModel(props.record, props.title);
    return (_jsxs(Pane, { width: width, title: model.title, tone: toneForStatus(model.status), paddingX: 1, children: [_jsxs(Text, { children: [_jsx(StatusBadge, { label: model.status, tone: toneForStatus(model.status) }), " ", _jsx(Text, { color: "gray", children: model.runLabel })] }), _jsx(PlanRow, { label: "path", value: model.path }), _jsx(PlanRow, { label: "size", value: `${model.chars} chars / ${model.lines} lines` }), _jsx(PlanRow, { label: "approval", value: model.approval, color: model.status === "pending" ? "yellow" : "gray" }), _jsx(PlanRow, { label: "summary", value: model.summary }), model.preview.length > 0 ? (_jsxs(Box, { flexDirection: "column", marginTop: 1, children: [_jsx(Text, { color: "gray", children: "preview" }), model.preview.map((line, index) => (_jsx(Text, { color: "gray", children: ` ${line}` }, `${index}-${line}`)))] })) : null, _jsx(CommandRows, { commands: model.commands })] }));
}
export function planPanelModel(record, title = "Plan mode") {
    const lines = record.content ? record.content.split(/\r?\n/) : [];
    const gate = record.gate;
    const status = gate?.status ?? (record.content.trim() ? "draft" : "empty");
    return {
        title,
        runId: record.runId,
        runLabel: displayRunLabel(record.runId),
        path: formatPlanReference(record.relativePath),
        chars: record.content.length,
        lines: record.content ? lines.length : 0,
        approval: gate ? gate.status : "none",
        status,
        summary: gate?.summary || firstMeaningfulLine(record.content) || "No plan content yet",
        preview: previewLines(record.content, 8, 96),
        commands: planCommands(record),
    };
}
function planCommands(record) {
    const commands = [
        {
            label: "show",
            command: "/plan show",
            description: "print the full draft",
            tone: "inspect",
        },
        {
            label: "path",
            command: "/plan path",
            description: "locate the markdown plan",
            tone: "inspect",
        },
    ];
    if (record.gate?.status === "pending") {
        commands.push({
            label: "approve",
            command: "/plan approve latest <reason>",
            description: "continue from this plan",
            tone: "allow",
        }, {
            label: "reject",
            command: "/plan reject latest <reason>",
            description: "ask for another plan",
            tone: "reject",
        }, {
            label: "cancel",
            command: "/plan cancel latest <reason>",
            description: "close the gate",
            tone: "neutral",
        });
        return commands;
    }
    if (record.content.trim()) {
        commands.push({
            label: "submit",
            command: "/plan exit",
            description: "request approval for this draft",
            tone: "allow",
        });
    }
    return commands;
}
function previewLines(content, maxLines, maxCells) {
    return content
        .split(/\r?\n/)
        .map((line) => flattenCellText(line))
        .filter(Boolean)
        .slice(0, maxLines)
        .map((line) => truncateCells(line, maxCells));
}
function firstMeaningfulLine(content) {
    return previewLines(content, 1, 96)[0] ?? "";
}
function displayRunLabel(runId) {
    return /^run_[0-9a-f-]{8,}$/i.test(runId) ? "current run" : runId;
}
function CommandRows(props) {
    return (_jsxs(Box, { flexDirection: "column", marginTop: 1, children: [_jsx(Text, { color: "gray", children: "commands" }), props.commands.map((command, index) => (_jsxs(Box, { flexDirection: "row", children: [_jsx(Text, { color: commandTone(command.tone), children: ` ${index + 1}. ${command.label.padEnd(8)} ` }), _jsx(Text, { color: "gray", children: command.command }), _jsx(Text, { color: "gray", children: `  ${command.description}` })] }, `${command.label}-${command.command}`)))] }));
}
function PlanRow(props) {
    return (_jsxs(Box, { flexDirection: "row", children: [_jsxs(Text, { color: "gray", children: [props.label.padEnd(9), " "] }), _jsx(Text, { color: props.color ?? "gray", children: props.value })] }));
}
function toneForStatus(status) {
    if (status === "approved")
        return "success";
    if (status === "pending" || status === "draft")
        return "warning";
    if (status === "empty")
        return "muted";
    return "error";
}
function commandTone(tone) {
    if (tone === "allow")
        return "green";
    if (tone === "reject")
        return "red";
    if (tone === "inspect")
        return "cyan";
    return "gray";
}
