import React, { useState, useCallback, useMemo } from "react";
import { Box, Text, useInput, useApp } from "ink";
import { CodeBuddyAgent } from "../../agent/codebuddy-agent.js";
import { getSettingsManager } from "../../utils/settings-manager.js";
import { logger } from "../../utils/logger.js";
function ApiKeyInputInner({ onApiKeySet }) {
    const [input, setInput] = useState("");
    const [error, setError] = useState("");
    const [isSubmitting, setIsSubmitting] = useState(false);
    const { exit } = useApp();
    // Memoized submit handler
    const handleSubmit = useCallback(async () => {
        if (!input.trim()) {
            setError("API key cannot be empty");
            return;
        }
        setIsSubmitting(true);
        try {
            const apiKey = input.trim();
            const agent = new CodeBuddyAgent(apiKey);
            // Set environment variable for current process
            process.env.GROK_API_KEY = apiKey;
            // Save to user settings
            try {
                const manager = getSettingsManager();
                manager.updateUserSetting('apiKey', apiKey);
                logger.info('API key saved to ~/.codebuddy/user-settings.json');
            }
            catch (_error) {
                logger.warn('Could not save API key to settings file. API key set for current session only.');
            }
            onApiKeySet(agent);
        }
        catch (_error) {
            setError("Invalid API key format");
            setIsSubmitting(false);
        }
    }, [input, onApiKeySet]);
    // Memoized input handler
    const handleInput = useCallback((inputChar, key) => {
        if (isSubmitting)
            return;
        if (key.ctrl && inputChar === "c") {
            exit();
            return;
        }
        if (key.return) {
            handleSubmit();
            return;
        }
        if (key.backspace || key.delete) {
            setInput((prev) => prev.slice(0, -1));
            setError("");
            return;
        }
        if (inputChar && !key.ctrl && !key.meta) {
            setInput((prev) => prev + inputChar);
            setError("");
        }
    }, [isSubmitting, exit, handleSubmit]);
    useInput(handleInput);
    // Memoized display text to prevent re-computation
    const displayText = useMemo(() => {
        if (input.length > 0) {
            return isSubmitting ? "*".repeat(input.length) : "*".repeat(input.length) + "█";
        }
        return isSubmitting ? " " : "█";
    }, [input.length, isSubmitting]);
    return (React.createElement(Box, { flexDirection: "column", paddingX: 2, paddingY: 1 },
        React.createElement(Text, { color: "yellow" }, "\uD83D\uDD11 CodeBuddy API Key Required"),
        React.createElement(Box, { marginBottom: 1 },
            React.createElement(Text, { color: "gray" }, "Please enter your CodeBuddy API key to continue:")),
        React.createElement(Box, { borderStyle: "round", borderColor: "blue", paddingX: 1, marginBottom: 1 },
            React.createElement(Text, { color: "gray" }, "\u276F "),
            React.createElement(Text, null, displayText)),
        error ? (React.createElement(Box, { marginBottom: 1 },
            React.createElement(Text, { color: "red" },
                "\u274C ",
                error))) : null,
        React.createElement(Box, { flexDirection: "column", marginTop: 1 },
            React.createElement(Text, { color: "gray", dimColor: true }, "\u2022 Press Enter to submit"),
            React.createElement(Text, { color: "gray", dimColor: true }, "\u2022 Press Ctrl+C to exit"),
            React.createElement(Text, { color: "gray", dimColor: true }, "Note: API key will be saved to ~/.codebuddy/user-settings.json")),
        isSubmitting ? (React.createElement(Box, { marginTop: 1 },
            React.createElement(Text, { color: "yellow" }, "\uD83D\uDD04 Validating API key..."))) : null));
}
// Export memoized component to prevent unnecessary re-renders
const ApiKeyInput = React.memo(ApiKeyInputInner);
export default ApiKeyInput;
//# sourceMappingURL=ApiKeyInput.js.map