import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Box, Text, useInput } from "ink";
const MAX_FIELD_CHARS = 600;
/**
 * Render the tool input compactly. Long string fields (file content, full
 * commands) are truncated with a marker so the prompt stays readable.
 */
function formatInput(input) {
    const truncated = {};
    for (const [key, value] of Object.entries(input)) {
        if (typeof value === "string" && value.length > MAX_FIELD_CHARS) {
            truncated[key] =
                value.slice(0, MAX_FIELD_CHARS) +
                    `… [${value.length - MAX_FIELD_CHARS} more chars]`;
        }
        else {
            truncated[key] = value;
        }
    }
    return JSON.stringify(truncated, null, 2);
}
/**
 * Mid-turn permission prompt for gated tool calls. Captures the agent's
 * input loop while visible — `useInput` here pre-empts the InputComposer
 * because Ink delivers raw stdin events to every mounted `useInput`. The
 * InputComposer is rendered with `disabled` while a prompt is active so
 * keystrokes here don't bleed into the chat input.
 *
 * Keys:
 *   y / Return  → allow once
 *   a           → allow every gated tool for the rest of the session
 *                 (equivalent to `/permissions auto all`). For narrower
 *                 modes (writes-only or commands-only), use the slash
 *                 command directly.
 *   n / Esc     → deny
 */
export function PermissionPrompt({ prompt }) {
    useInput((input, key) => {
        if (input === "y" || key.return) {
            prompt.resolve({ decision: "allow" });
        }
        else if (input === "a") {
            // Flip the auto-allow mode to "all" so the rest of the session
            // skips the prompt for every gated tool. Equivalent to running
            // `/permissions auto all`.
            prompt.resolve({ decision: "allow", setMode: "all" });
        }
        else if (input === "n" || key.escape) {
            prompt.resolve({ decision: "deny" });
        }
    });
    return (_jsxs(Box, { flexDirection: "column", borderStyle: "round", borderColor: "yellow", paddingX: 1, marginY: 1, children: [_jsx(Box, { children: _jsx(Text, { bold: true, color: "yellow", children: "\u26A0 Permission required" }) }), _jsxs(Box, { children: [_jsx(Text, { children: "The agent wants to run " }), _jsx(Text, { bold: true, color: "cyan", children: prompt.toolName }), _jsx(Text, { children: ":" })] }), _jsx(Box, { marginTop: 1, flexDirection: "column", children: _jsx(Text, { dimColor: true, children: formatInput(prompt.input) }) }), _jsx(Box, { marginTop: 1, children: _jsxs(Text, { children: [_jsx(Text, { bold: true, color: "green", children: "[y]" }), _jsx(Text, { children: " allow once  " }), _jsx(Text, { bold: true, color: "green", children: "[a]" }), _jsx(Text, { children: " allow all (session)  " }), _jsx(Text, { bold: true, color: "red", children: "[n]" }), _jsx(Text, { children: " deny" })] }) }), _jsx(Box, { children: _jsx(Text, { dimColor: true, children: "For finer control: /permissions auto writes|commands|all" }) })] }));
}
