import { jsxs as _jsxs, jsx as _jsx } from "react/jsx-runtime";
/**
 * @license
 * Copyright 2025 Vybestack LLC
 * SPDX-License-Identifier: Apache-2.0
 */
/**
 * OAuth Code Input Dialog Component
 *
 * Allows users to paste authorization code from browser
 */
import { useState, useCallback } from 'react';
import { Box, Text } from 'ink';
import { Colors } from '../colors.js';
import { useKeypress } from '../hooks/useKeypress.js';
/**
 * @plan PLAN-20250822-GEMINIFALLBACK.P09
 * @requirement REQ-002.1
 * @pseudocode lines 38-45
 */
export const OAuthCodeDialog = ({ provider, onClose, onSubmit, }) => {
    const [code, setCode] = useState('');
    /**
     * @plan PLAN-20250822-GEMINIFALLBACK.P09
     * @requirement REQ-002.1, REQ-006.2
     * @pseudocode lines 38-45
     */
    const getInstructions = useCallback(() => {
        if (provider === 'gemini') {
            return [
                'The OAuth URL has been copied to your clipboard.',
                'Please paste it into your browser to authenticate with Google.',
                'After authenticating, paste the verification code you receive below:',
            ];
        }
        else {
            return [
                'Please check your browser and authorize the application.',
                'After authorizing, paste the authorization code below:',
            ];
        }
    }, [provider]);
    /**
     * @plan PLAN-20250822-GEMINIFALLBACK.P09
     * @requirement REQ-002.2, REQ-006.3
     * @pseudocode lines 46-65
     */
    const handleInput = useCallback((key) => {
        // Handle escape to close
        if (key.name === 'escape') {
            onClose();
            return;
        }
        // Handle enter to submit
        if (key.name === 'return') {
            if (code.trim()) {
                onSubmit(code.trim());
                onClose();
            }
            return;
        }
        // Handle clear (allow clearing the field with Cmd+K or Ctrl+L)
        if ((key.ctrl && key.name === 'l') || (key.meta && key.name === 'k')) {
            setCode('');
            return;
        }
        // ONLY accept pasted input - ignore ALL typed characters
        if (key.paste && key.sequence) {
            // The sequence already has the paste content without escape codes
            // Just filter to only allow valid OAuth code characters
            // Allow slash because Google auth codes often contain it (e.g., 4/XXXX...)
            const cleanInput = key.sequence.replace(/[^a-zA-Z0-9/_#-]/g, '');
            if (cleanInput) {
                // Replace the entire code with the pasted content (don't append)
                setCode(cleanInput);
            }
            return;
        }
        // Explicitly ignore ALL other input including:
        // - Regular typed characters
        // - Control codes
        // - Backspace (users must paste the entire correct code)
        // This prevents accidental control codes like I, IO, etc. from being added
    }, [code, onClose, onSubmit]);
    useKeypress(handleInput, { isActive: true });
    return (_jsxs(Box, { flexDirection: "column", borderStyle: "round", borderColor: Colors.AccentCyan, paddingX: 2, paddingY: 1, marginX: 2, marginY: 1, children: [_jsxs(Text, { bold: true, color: Colors.AccentCyan, children: [provider.charAt(0).toUpperCase() + provider.slice(1), " OAuth Authentication"] }), getInstructions().map((instruction, index) => (_jsx(Text, { color: Colors.Foreground, children: instruction }, index))), _jsxs(Box, { marginTop: 1, children: [_jsx(Text, { color: Colors.AccentCyan, children: "Code: " }), _jsx(Text, { color: Colors.Foreground, children: code || '(paste only - typing disabled)' })] }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { dimColor: true, children: "Paste code \u2022 Enter to submit \u2022 Escape to cancel \u2022 Ctrl+L to clear" }) })] }));
};
//# sourceMappingURL=OAuthCodeDialog.js.map