import { jsxs as _jsxs, jsx as _jsx } from "react/jsx-runtime";
/**
 * @license
 * Copyright 2025 Vybestack LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import { useState, useCallback, useEffect } from 'react';
import { Box, Text } from 'ink';
import { Colors } from '../colors.js';
/**
 * Component for secure API key input with masking
 */
export const SecureKeyInput = ({ onSubmit, onCancel, providerName, }) => {
    const [keyValue, setKeyValue] = useState('');
    const [showKey, setShowKey] = useState(false);
    const handleKeyPress = useCallback((char, key) => {
        if (key.name === 'escape' || (key.ctrl && key.name === 'c')) {
            onCancel();
            return;
        }
        if (key.name === 'return') {
            onSubmit(keyValue);
            return;
        }
        if (key.name === 'backspace') {
            setKeyValue((prev) => prev.slice(0, -1));
            return;
        }
        // Toggle visibility with Ctrl+V
        if (key.ctrl && key.name === 'v') {
            setShowKey((prev) => !prev);
            return;
        }
        // Only accept printable characters
        if (char && char.length === 1 && !key.ctrl) {
            setKeyValue((prev) => prev + char);
        }
    }, [keyValue, onSubmit, onCancel]);
    // Set up key press handler
    useEffect(() => {
        const handleInput = (chunk, key) => {
            const keyInfo = key;
            const char = chunk.toString();
            handleKeyPress(char, keyInfo);
        };
        process.stdin.on('data', handleInput);
        process.stdin.setRawMode(true);
        process.stdin.resume();
        return () => {
            process.stdin.off('data', handleInput);
            process.stdin.setRawMode(false);
            process.stdin.pause();
        };
    }, [handleKeyPress]);
    const maskedValue = showKey ? keyValue : '•'.repeat(keyValue.length);
    return (_jsxs(Box, { flexDirection: "column", children: [_jsx(Box, { marginBottom: 1, children: _jsxs(Text, { color: Colors.AccentBlue, children: ["Enter API key for ", providerName, ":"] }) }), _jsxs(Box, { marginBottom: 1, children: [_jsx(Text, { color: Colors.Foreground, children: maskedValue }), keyValue.length > 0 && (_jsxs(Text, { color: Colors.Gray, children: [" (", keyValue.length, " characters)"] }))] }), _jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { color: Colors.Gray, children: "Press Enter to submit" }), _jsx(Text, { color: Colors.Gray, children: "Press Ctrl+V to toggle visibility" }), _jsx(Text, { color: Colors.Gray, children: "Press Escape to cancel" })] })] }));
};
//# sourceMappingURL=SecureKeyInput.js.map