import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Box, Text } from "ink";
import { parseActionSummary } from "./ActionSummaryBlock.js";
import { SelectList } from "./design/SelectList.js";
import { gateDecisionOptions } from "../services/approval/gateDecisionOptions.js";
export function ApprovalDecisionOptions(props) {
    const options = props.gate
        ? gateDecisionOptions({ gate: props.gate, projectPath: props.projectPath })
        : approvalDecisionOptionsModel(props);
    if (options.length === 0)
        return null;
    return (_jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { color: "gray", children: "choices" }), _jsx(SelectList, { selectedIndex: props.selectedIndex ?? 0, visibleCount: 5, width: 104, options: options.map((option) => ({
                    id: option.command,
                    label: option.label,
                    detail: option.shortcut ?? option.command,
                    description: option.description,
                    tone: decisionTone(option.tone),
                })) })] }));
}
export function approvalDecisionOptionsModel(input) {
    if (input.status !== "pending")
        return [];
    if (input.subjectType === "question") {
        return [
            {
                label: "inspect",
                command: "/question show latest",
                description: "read the prompt and choices",
                shortcut: "Enter",
                tone: "inspect",
            },
            {
                label: "answer",
                command: "/question answer latest <answer>",
                description: "resume with your answer",
                shortcut: "type answer",
                tone: "allow",
            },
            {
                label: "reject",
                command: "/question reject latest <reason>",
                description: "send it back with feedback",
                shortcut: "N",
                tone: "reject",
            },
        ];
    }
    if (input.subjectType === "plan") {
        return [
            {
                label: "approve",
                command: "/plan approve latest <reason>",
                description: "let the planned run continue",
                shortcut: "Enter / Y",
                tone: "allow",
            },
            {
                label: "reject",
                command: "/plan reject latest <reason>",
                description: "request a different plan",
                shortcut: "N",
                tone: "reject",
            },
            {
                label: "cancel",
                command: "/plan cancel latest <reason>",
                description: "close the plan gate",
                shortcut: "Esc",
                tone: "neutral",
            },
        ];
    }
    const options = [
        {
            label: "approve once",
            command: "/approval approve latest <reason>",
            description: "allow this exact action fingerprint",
            shortcut: "Enter / Y",
            tone: "allow",
        },
        {
            label: "reject",
            command: "/approval reject latest <reason>",
            description: "block and send feedback",
            shortcut: "N",
            tone: "reject",
        },
        {
            label: "cancel",
            command: "/approval cancel latest <reason>",
            description: "close without approving",
            shortcut: "Esc",
            tone: "neutral",
        },
    ];
    const diffCommand = diffCommandForSummary(input.summary);
    if (diffCommand) {
        options.push({
            label: "inspect diff",
            command: diffCommand,
            description: "run after retrying the approved edit",
            shortcut: "D",
            tone: "inspect",
        });
    }
    return options;
}
function diffCommandForSummary(summary) {
    const parsed = parseActionSummary(summary);
    if (!parsed || (parsed.action !== "write_file" && parsed.action !== "apply_patch"))
        return "";
    const path = parsed.fields.find((field) => field.key === "path")?.value.trim();
    if (!path || path === "(unknown)")
        return "/diff git";
    return `/diff git ${path}`;
}
function decisionTone(tone) {
    if (tone === "allow")
        return "success";
    if (tone === "reject")
        return "error";
    if (tone === "inspect")
        return "brand";
    return "muted";
}
