import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Box, Text } from "ink";
import { parseActionSummary } from "./ActionSummaryBlock.js";
import { Dialog } from "./design/Dialog.js";
import { truncateCells } from "./design/textLayout.js";
export function PermissionRequestFrame(props) {
    const model = permissionRequestFrameModel(props.gate);
    const subtitle = truncateCells(model.subtitle, Math.max(24, props.width - 22));
    const scope = truncateCells(model.scope, Math.max(24, props.width - 16));
    return (_jsx(Box, { flexDirection: "column", marginBottom: 1, children: _jsxs(Dialog, { width: props.width, title: _jsx(Text, { bold: true, color: model.titleColor, children: model.title }), subtitle: subtitle, tone: model.tone, paneTitle: "permission request", statusLabel: model.statusLabel, inputGuide: inputGuideForGate(props.gate.subjectType), children: [_jsxs(Box, { flexDirection: "row", children: [_jsx(Text, { color: "gray", children: "scope   " }), _jsx(Text, { color: "gray", children: scope })] }), _jsxs(Box, { flexDirection: "row", children: [_jsx(Text, { color: "gray", children: "risk    " }), _jsx(Text, { color: titleColorForRisk(model.risk), children: model.risk })] }), props.children] }) }));
}
export function permissionRequestFrameModel(gate) {
    const parsed = parseActionSummary(gate.summary);
    const fields = Object.fromEntries(parsed?.fields.map((field) => [field.key, field.value]) ?? []);
    const action = parsed?.action ?? gate.subjectType;
    const base = baseModelForGate(gate, action, fields);
    return {
        ...base,
        tone: toneForGate(gate.status, base.risk),
        titleColor: titleColorForRisk(base.risk),
        statusLabel: gate.status,
    };
}
function baseModelForGate(gate, action, fields) {
    if (gate.subjectType === "question") {
        return {
            title: "DeepSeekCode needs your answer",
            subtitle: compact(gate.summary.replace(/^Question for user:\s*/i, "")),
            scope: "answer one pending question",
            risk: "medium",
        };
    }
    if (gate.subjectType === "plan") {
        return {
            title: "DeepSeekCode wants to enter plan mode",
            subtitle: compact(gate.summary),
            scope: "approve or reject this plan draft",
            risk: "medium",
        };
    }
    if (action === "run_command") {
        const command = fields.command || "(unknown command)";
        return {
            title: "DeepSeekCode wants to run a command",
            subtitle: command,
            scope: fields.cwd ? `cwd ${fields.cwd}` : "local shell",
            risk: commandRisk(command),
        };
    }
    if (action === "ssh_run") {
        const command = fields.command || "(unknown command)";
        return {
            title: "DeepSeekCode wants to run SSH",
            subtitle: command,
            scope: fields.profile ? `profile ${fields.profile}` : "remote shell",
            risk: "high",
        };
    }
    if (action === "ssh_write_file") {
        return {
            title: fields.overwrite === "true"
                ? "DeepSeekCode wants to overwrite a remote file"
                : "DeepSeekCode wants to write a remote file",
            subtitle: fields.path || "(unknown path)",
            scope: fields.profile ? `profile ${fields.profile}` : "remote file",
            risk: fields.overwrite === "true" ? "high" : "medium",
        };
    }
    if (action === "write_file") {
        const overwrite = fields.overwrite === "true";
        return {
            title: overwrite ? "DeepSeekCode wants to overwrite a file" : "DeepSeekCode wants to create a file",
            subtitle: fields.path || "(unknown path)",
            scope: overwrite ? "replace file contents" : "create new file",
            risk: overwrite ? "high" : "medium",
        };
    }
    if (action === "apply_patch") {
        return {
            title: "DeepSeekCode wants to apply a patch",
            subtitle: fields.path || "(unknown path)",
            scope: fields.edits ? `${fields.edits} search/replace edit(s)` : "file patch",
            risk: "high",
        };
    }
    if (action.startsWith("browser_") || action === "browser_open") {
        return {
            title: "DeepSeekCode wants to use the browser",
            subtitle: fields.url || gate.summary,
            scope: "browser action",
            risk: "medium",
        };
    }
    if (action === "mcp_call") {
        return {
            title: "DeepSeekCode wants to call MCP",
            subtitle: fields.tool || gate.summary,
            scope: fields.server ? `server ${fields.server}` : "mcp tool",
            risk: "medium",
        };
    }
    if (action === "create_docx" || action === "create_pdf") {
        return {
            title: action === "create_docx"
                ? "DeepSeekCode wants to create a DOCX"
                : "DeepSeekCode wants to create a PDF",
            subtitle: fields.path || "(unknown path)",
            scope: `${fields.markdownChars || "0"} markdown chars`,
            risk: "medium",
        };
    }
    if (action === "computer_use") {
        return {
            title: "DeepSeekCode wants computer control",
            subtitle: `${fields.instructionChars || "0"} instruction chars`,
            scope: "local desktop automation",
            risk: "high",
        };
    }
    if (action === "invoke_agent") {
        return {
            title: "DeepSeekCode wants to invoke an agent",
            subtitle: fields.agent || gate.summary,
            scope: "local subagent",
            risk: "medium",
        };
    }
    return {
        title: "DeepSeekCode needs permission",
        subtitle: compact(gate.summary),
        scope: action || gate.subjectType,
        risk: "medium",
    };
}
export function inputGuideForGate(subjectType) {
    if (subjectType === "question")
        return "Use Up/Down, Enter, number keys, or type an answer; Esc rejects.";
    if (subjectType === "plan")
        return "Use Up/Down, Enter/Y to approve, N to reject, Esc to cancel.";
    return "Use Up/Down, Enter/Y to allow once, N to reject, Esc to cancel.";
}
function toneForGate(status, risk) {
    if (status === "approved")
        return "success";
    if (status === "rejected")
        return "error";
    if (status === "cancelled")
        return "muted";
    if (risk === "high")
        return "warning";
    return "brand";
}
function titleColorForRisk(risk) {
    if (risk === "high")
        return "yellow";
    if (risk === "low")
        return "green";
    return "cyan";
}
function commandRisk(command) {
    if (/(^|\s)(del|erase|rd|rmdir|rm|remove-item|git\s+clean|git\s+reset)(\s|$)/i.test(command))
        return "high";
    if (/(^|\s)(--force|-rf|-fr|\/s)(\s|$)/i.test(command))
        return "high";
    if (/>\s*\S+|>>\s*\S+/.test(command))
        return "high";
    if (/^(git\s+status|git\s+diff|pwd|dir|ls|node\s+--version|npm\.cmd\s+--version)\b/i.test(command.trim()))
        return "low";
    return "medium";
}
function compact(value) {
    const singleLine = value.replace(/\s+/g, " ").trim();
    return singleLine.length > 220 ? `${singleLine.slice(0, 217)}...` : singleLine;
}
