/**
 * Shared approval prompt for sandbox operations (write access, network access).
 *
 * A single Component class parameterized by title, content line, and buttons.
 * Eliminates duplication between write-access and network-approval prompts.
 */
import { Container, Spacer, Text, getEditorKeybindings } from '../../tui/index.js';
import { Border } from './approval.js';
const APPROVAL_BUTTONS = [
    { label: 'Allow Once', choice: 'once' },
    { label: 'Allow Always', choice: 'save' },
    { label: 'Deny', choice: 'deny' }
];
class SandboxPrompt extends Container {
    selectedButton = 0;
    buttonContainer = new Container();
    theme;
    buttons;
    onDone;
    constructor(config, theme, onDone) {
        super();
        this.theme = theme;
        this.buttons = config.buttons;
        this.onDone = onDone;
        const borderColor = (s) => theme.fg('border', s);
        this.addChild(new Border(borderColor));
        this.addChild(new Text(theme.bold(theme.fg('warning', ` ${config.title}`)), 0, 0));
        this.addChild(new Spacer(1));
        this.addChild(new Text('  ' + theme.fg('accent', config.content), 0, 0));
        this.addChild(new Spacer(1));
        this.renderButtons();
        this.addChild(this.buttonContainer);
        this.addChild(new Border(borderColor));
    }
    renderButtons() {
        this.buttonContainer.clear();
        const parts = [];
        for (let i = 0; i < this.buttons.length; i++) {
            const { label, choice } = this.buttons[i];
            if (i === this.selectedButton) {
                if (choice === 'deny') {
                    parts.push(this.theme.fg('error', this.theme.bold(`[ ${label} ]`)));
                }
                else if (choice === 'save') {
                    parts.push(this.theme.fg('success', this.theme.bold(`[ ${label} ]`)));
                }
                else {
                    parts.push(this.theme.bold(`[ ${label} ]`));
                }
            }
            else {
                parts.push(this.theme.fg('dim', `  ${label}  `));
            }
        }
        this.buttonContainer.addChild(new Text('  ' + parts.join('  '), 0, 0));
        this.buttonContainer.addChild(new Text(this.theme.fg('dim', '  \u2190\u2192 select  enter confirm  esc cancel'), 0, 0));
    }
    handleInput(keyData) {
        const kb = getEditorKeybindings();
        if (kb.matches(keyData, 'cursorLeft') || keyData === 'h') {
            this.selectedButton = Math.max(0, this.selectedButton - 1);
            this.renderButtons();
        }
        else if (kb.matches(keyData, 'cursorRight') || keyData === 'l') {
            this.selectedButton = Math.min(this.buttons.length - 1, this.selectedButton + 1);
            this.renderButtons();
        }
        else if (kb.matches(keyData, 'selectConfirm') || keyData === '\n' || keyData === '\r') {
            this.onDone(this.buttons[this.selectedButton].choice);
        }
        else if (kb.matches(keyData, 'selectCancel')) {
            this.onDone('deny');
        }
    }
}
/** Create a network domain approval prompt. */
export function createNetworkPrompt(domain, theme, onDone) {
    return new SandboxPrompt({ title: 'Network Access Requested', content: domain, buttons: APPROVAL_BUTTONS }, theme, onDone);
}
//# sourceMappingURL=sandbox-prompt.js.map