import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Box, Text } from "ink";
import { ActionSummaryBlock } from "./ActionSummaryBlock.js";
import { ApprovalDecisionOptions } from "./ApprovalDecisionOptions.js";
import { ArtifactApprovalPreviewBlock } from "./ArtifactApprovalPreviewBlock.js";
import { BrowserApprovalPreviewBlock } from "./BrowserApprovalPreviewBlock.js";
import { FileEditReviewPanel } from "./FileEditReviewPanel.js";
import { McpApprovalPreviewBlock } from "./McpApprovalPreviewBlock.js";
import { PermissionRequestFrame } from "./PermissionRequestFrame.js";
import { PlanApprovalPreviewBlock } from "./PlanApprovalPreviewBlock.js";
import { QuestionApprovalPreviewBlock } from "./QuestionApprovalPreviewBlock.js";
import { RemoteFileApprovalPreviewBlock } from "./RemoteFileApprovalPreviewBlock.js";
import { ShellApprovalPreviewBlock } from "./ShellApprovalPreviewBlock.js";
import { useTerminalSize } from "../hooks/useTerminalSize.js";
export function ApprovalGateCard(props) {
    const model = approvalGateCardModel(props.gate);
    const { columns } = useTerminalSize();
    const width = Math.max(36, Math.min(100, columns - 4));
    return (_jsxs(PermissionRequestFrame, { gate: props.gate, width: width, children: [props.showRun ? _jsx(GateRow, { label: "run", value: displayRunLabel(props.gate.runId) }) : null, _jsx(GateRow, { label: "action", value: model.action }), _jsx(GateRow, { label: "summary", value: model.summary }), _jsx(FileEditReviewPanel, { summary: props.gate.summary, projectPath: props.projectPath, gateId: props.gate.id, width: Math.max(32, width - 6) }), _jsx(ShellApprovalPreviewBlock, { summary: props.gate.summary }), _jsx(RemoteFileApprovalPreviewBlock, { summary: props.gate.summary, width: Math.max(32, width - 6) }), _jsx(BrowserApprovalPreviewBlock, { summary: props.gate.summary, width: Math.max(32, width - 6) }), _jsx(ArtifactApprovalPreviewBlock, { summary: props.gate.summary, width: Math.max(32, width - 6) }), _jsx(McpApprovalPreviewBlock, { summary: props.gate.summary, width: Math.max(32, width - 6) }), _jsx(QuestionApprovalPreviewBlock, { projectPath: props.projectPath, gateId: props.gate.id, width: Math.max(32, width - 6) }), props.gate.subjectType === "plan" ? (_jsx(PlanApprovalPreviewBlock, { projectPath: props.projectPath, runId: props.gate.runId, width: Math.max(32, width - 6) })) : null, _jsx(ActionSummaryBlock, { summary: props.gate.summary }), _jsx(ApprovalDecisionOptions, { gateId: props.gate.id, subjectType: props.gate.subjectType, status: props.gate.status, summary: props.gate.summary, gate: props.gate, projectPath: props.projectPath, selectedIndex: props.selectedDecisionIndex }), props.children, _jsx(GateRow, { label: "hint", value: model.hint, color: "gray" })] }));
}
export function approvalGateCardModel(gate) {
    return {
        label: labelForSubject(gate.subjectType),
        labelColor: colorForSubject(gate.subjectType),
        statusColor: colorForStatus(gate.status),
        action: gate.summary.trim().split(/\s+/)[0] || gate.subjectType,
        hint: hintForGate(gate.subjectType, gate.id),
        summary: compactGateSummary(gate.summary),
    };
}
export function hintForGate(subjectType, id) {
    void id;
    if (subjectType === "question")
        return "Choose an answer above; DeepSeekCode will continue after your selection.";
    if (subjectType === "plan")
        return "Approve to continue, reject to revise, or Esc to cancel.";
    return "Allow once to continue this exact action, or reject it.";
}
export function labelForSubject(subjectType) {
    if (subjectType === "plan")
        return "plan";
    if (subjectType === "question")
        return "question";
    if (subjectType === "tool_action" || subjectType === "tool")
        return "approval";
    return "gate";
}
function colorForSubject(subjectType) {
    if (subjectType === "question")
        return "cyan";
    if (subjectType === "plan")
        return "yellow";
    return "magenta";
}
function colorForStatus(status) {
    if (status === "approved")
        return "green";
    if (status === "pending")
        return "yellow";
    return "red";
}
function compactGateSummary(value) {
    const singleLine = value.replace(/\s+/g, " ").trim();
    return singleLine.length > 180 ? `${singleLine.slice(0, 177)}...` : singleLine;
}
export function displayRunLabel(runId) {
    return /^run_[0-9a-f-]{8,}$/i.test(runId) ? "current run" : runId;
}
function GateRow(props) {
    return (_jsxs(Box, { flexDirection: "row", children: [_jsxs(Text, { color: "gray", children: [props.label.padEnd(7), " "] }), _jsx(Text, { color: props.color ?? "gray", children: props.value })] }));
}
