import { jsxs as _jsxs, jsx as _jsx } from "react/jsx-runtime";
/**
 * ConfirmationInput — Continue/cancel prompt.
 * Enter confirms the focused option, Escape cancels.
 * Tab / Shift-Tab cycle between options (via Ink's focus manager).
 * Up / Down arrows toggle focus, preserved for muscle memory.
 * Options stack vertically to match PickerMenu.
 */
import { Box, Text, useFocus, useFocusManager } from 'ink';
import { useScreenInput } from '../hooks/useScreenInput.js';
import { Icons, Colors } from '../styles.js';
import { PromptLabel } from './PromptLabel.js';
const Option = ({ id, label, icon, autoFocus, onSelect }) => {
    const { isFocused } = useFocus({ id, autoFocus });
    // Each focused option owns its own Enter handler. When unfocused the
    // hook is inert (isActive=false) so we never double-fire onConfirm.
    useScreenInput((_input, key) => {
        if (key.return) {
            onSelect();
        }
    }, { isActive: isFocused });
    return (_jsxs(Text, { bold: isFocused, color: isFocused ? Colors.accent : Colors.muted, children: [isFocused ? icon : ' ', " ", label] }));
};
export const ConfirmationInput = ({ message, onConfirm, onCancel, onEscape, confirmLabel = 'Continue [Enter]', cancelLabel = 'Cancel [Esc]', idPrefix = 'confirmation-input', }) => {
    const { focusNext, focusPrevious, focus } = useFocusManager();
    const confirmId = `${idPrefix}-confirm`;
    const cancelId = `${idPrefix}-cancel`;
    // Parent owns Escape + arrow-key cycling. Tab / Shift-Tab is handled
    // automatically by Ink's focus manager.
    useScreenInput((_input, key) => {
        if (key.upArrow) {
            focusPrevious();
            return;
        }
        if (key.downArrow) {
            focusNext();
            return;
        }
        if (key.escape) {
            // Ink's focus manager clears activeFocusId on Escape before this
            // handler runs, leaving both Options unfocused (Enter inert).
            // Re-acquire focus so the component stays interactive if onCancel
            // doesn't unmount it.
            focus(confirmId);
            (onEscape ?? onCancel)();
        }
    });
    return (_jsxs(Box, { flexDirection: "column", children: [_jsx(PromptLabel, { message: message }), _jsxs(Box, { flexDirection: "column", marginTop: 1, marginLeft: 2, children: [_jsx(Option, { id: confirmId, label: confirmLabel, icon: Icons.triangleSmallRight, autoFocus: true, onSelect: onConfirm }), _jsx(Option, { id: cancelId, label: cancelLabel, icon: Icons.triangleSmallRight, onSelect: onCancel })] })] }));
};
