import { jsxs as _jsxs, jsx as _jsx } from "react/jsx-runtime";
/**
 * PermissionDialog — inline permission prompt (Claude Code style).
 *
 * Instead of a centered modal overlay, this renders as a compact inline
 * prompt that appears in the chat flow, just above the input box.
 *
 * Visual structure:
 *
 *   ⚠ Bash(npm install lodash) — Allow? (Y)es / (N)o / (A)lways
 *
 * Keybindings (priority 10 — intercepts ALL keys except Ctrl+C):
 *   Y or Enter  — allow this time
 *   N or Esc    — deny
 *   A           — always allow this tool for the session
 *
 * CRITICAL: Does NOT block Ctrl+C. The global QUIT handler at priority 0
 * still fires because Ctrl+C is handled separately in the keypress
 * dispatcher before subscriber iteration.
 *
 * All colors from useTheme() semantic tokens.
 */
import { useCallback } from 'react';
import { Box, Text } from 'ink';
import { useTheme } from '../contexts/theme-context.js';
import { useUIState, useUIActions, } from '../contexts/ui-state-context.js';
import { useKeypress } from '../hooks/use-keypress.js';
import { Command } from '../key/commands.js';
import { useEngine } from '../engine/engine-context.js';
// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------
/** Maximum characters of tool input to show inline. */
const MAX_INPUT_PREVIEW = 60;
// ---------------------------------------------------------------------------
// Component
// ---------------------------------------------------------------------------
export function PermissionDialog() {
    const { colors } = useTheme();
    const ui = useUIState();
    const actions = useUIActions();
    const engine = useEngine();
    const dialog = ui.permissionDialog;
    // Truncate tool input for inline display
    const rawInput = dialog.toolInput || '';
    const truncatedInput = rawInput.length > MAX_INPUT_PREVIEW
        ? rawInput.slice(0, MAX_INPUT_PREVIEW) + '\u2026'
        : rawInput;
    // Resolve action — sends decision to engine and dismisses UI dialog
    const handleResolve = useCallback((decision) => {
        engine.resolvePermission(dialog.requestId, decision);
    }, [engine, dialog.requestId]);
    // "Always allow" — adds to session set, then allows
    const handleAlways = useCallback(() => {
        actions.addAlwaysAllowedTool(dialog.toolName);
        engine.resolvePermission(dialog.requestId, 'allow');
    }, [actions, engine, dialog.toolName, dialog.requestId]);
    // Keypress handler — priority 10 (highest modal priority)
    useKeypress(useCallback((input, key, command) => {
        // CRITICAL: Do NOT consume Ctrl+C — let it propagate to the global
        // QUIT handler. Ink's useInput provides ctrl=true for Ctrl+C.
        if (key.ctrl && input === 'c')
            return false;
        // Y or Enter = allow this time
        if (command === Command.DIALOG_ALLOW ||
            command === Command.DIALOG_CONFIRM ||
            input === 'y' || input === 'Y' ||
            key.return) {
            handleResolve('allow');
            return true;
        }
        // N or Esc = deny
        if (command === Command.DIALOG_DENY ||
            input === 'n' || input === 'N' ||
            key.escape) {
            handleResolve('deny');
            return true;
        }
        // A = always allow for session
        if (input === 'a' || input === 'A') {
            handleAlways();
            return true;
        }
        // Consume all other keys — prompt blocks everything except Ctrl+C
        return true;
    }, [handleResolve, handleAlways]), { isActive: ui.permissionDialog !== null, priority: 10 });
    // -------------------------------------------------------------------------
    // Render — compact inline prompt
    // -------------------------------------------------------------------------
    return (_jsx(Box, { flexDirection: "column", paddingX: 1, paddingY: 0, children: _jsxs(Box, { children: [_jsxs(Text, { color: colors.status.warning, children: ['\u26A0', " "] }), _jsx(Text, { color: colors.text.accent, bold: true, children: dialog.toolName }), truncatedInput ? (_jsxs(Text, { color: colors.text.secondary, children: ["(", truncatedInput, ")"] })) : null, _jsx(Text, { color: colors.text.primary, children: ' \u2014 Allow? ' }), _jsx(Text, { color: colors.status.done, bold: true, children: '(Y)es' }), _jsx(Text, { color: colors.text.secondary, children: ' / ' }), _jsx(Text, { color: colors.status.error, bold: true, children: '(N)o' }), _jsx(Text, { color: colors.text.secondary, children: ' / ' }), _jsx(Text, { color: colors.text.accent, bold: true, children: '(A)lways' })] }) }));
}
//# sourceMappingURL=permission-dialog.js.map