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 { FileEditPreviewBlock } from "./FileEditPreviewBlock.js";
import { McpApprovalPreviewBlock } from "./McpApprovalPreviewBlock.js";
import { PermissionRequestFrame } from "./PermissionRequestFrame.js";
import { RemoteFileApprovalPreviewBlock } from "./RemoteFileApprovalPreviewBlock.js";
import { ShellApprovalPreviewBlock } from "./ShellApprovalPreviewBlock.js";
import { useTerminalSize } from "../hooks/useTerminalSize.js";
export function ApprovalResultBlock(props) {
    const model = parseApprovalResultMessage(props.message);
    if (!model)
        return null;
    const { columns } = useTerminalSize();
    const width = Math.max(36, Math.min(100, columns - 6));
    const gate = approvalResultGateRecord(model);
    return (_jsxs(PermissionRequestFrame, { gate: gate, width: width, children: [_jsx(ApprovalResultRow, { label: "action", value: model.action }), _jsx(ApprovalResultRow, { label: "summary", value: model.summary }), _jsx(FileEditPreviewBlock, { summary: model.summary }), _jsx(ShellApprovalPreviewBlock, { summary: model.summary }), _jsx(RemoteFileApprovalPreviewBlock, { summary: model.summary }), _jsx(BrowserApprovalPreviewBlock, { summary: model.summary }), _jsx(ArtifactApprovalPreviewBlock, { summary: model.summary }), _jsx(McpApprovalPreviewBlock, { summary: model.summary }), _jsx(ActionSummaryBlock, { summary: model.summary }), _jsx(ApprovalDecisionOptions, { gateId: model.gateId, subjectType: gate.subjectType, status: gate.status, summary: model.summary })] }));
}
export function parseApprovalResultMessage(message) {
    const trimmed = message.trim();
    const required = trimmed.match(/^Approval required:\s+(.+)$/i);
    if (required) {
        const { gateId, rest } = splitOptionalGateId(required[1] ?? "");
        const [summary, hint] = splitApprovalHint(rest);
        return {
            status: "required",
            gateId,
            action: approvalActionFromSummary(summary),
            summary,
            ...(hint ? { hint } : {}),
        };
    }
    const decided = trimmed.match(/^Approval\s+(approved|rejected|cancelled|canceled):\s+(.+)$/i);
    if (!decided)
        return null;
    const rawStatus = (decided[1] ?? "").toLowerCase();
    const status = rawStatus === "canceled" ? "cancelled" : rawStatus;
    const { gateId, rest } = splitOptionalGateId(decided[2] ?? "");
    return {
        status: status,
        gateId,
        action: approvalActionFromSummary(rest),
        summary: rest,
    };
}
function splitOptionalGateId(value) {
    const trimmed = value.trim();
    const [first = "", ...tail] = trimmed.split(/\s+/);
    if (/^approval_[\w.-]+$/i.test(first) && tail.length > 0) {
        return { gateId: first, rest: tail.join(" ") };
    }
    return { gateId: "latest", rest: trimmed };
}
function splitApprovalHint(value) {
    for (const marker of [". Run ", ". Waiting for "]) {
        const index = value.indexOf(marker);
        if (index < 0)
            continue;
        const verb = marker.trimStart().replace(/^\.\s*/, "");
        return [
            value.slice(0, index).trim(),
            `${verb}${value.slice(index + marker.length).trim()}`,
        ];
    }
    return [value.trim(), undefined];
}
function approvalActionFromSummary(summary) {
    return summary.trim().split(/\s+/)[0] || "tool_action";
}
function approvalStatusColor(status) {
    if (status === "required")
        return "yellow";
    if (status === "approved")
        return "green";
    return "red";
}
function approvalResultGateRecord(model) {
    const status = model.status === "required" ? "pending" : model.status;
    return {
        id: model.gateId,
        runId: "",
        subjectType: subjectTypeForApprovalResult(model.summary),
        subjectId: model.action,
        status,
        summary: model.summary,
        rationale: "",
        createdAtMs: 0,
        updatedAtMs: 0,
    };
}
function subjectTypeForApprovalResult(summary) {
    const action = approvalActionFromSummary(summary);
    if (action === "plan")
        return "plan";
    if (action === "Question")
        return "question";
    return "tool_action";
}
function ApprovalResultRow(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 })] }));
}
