import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
/**
 * @license
 * Copyright 2025 Vybestack LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import { useState, useCallback } from 'react';
import { Box, Text } from 'ink';
import { Colors } from '../../colors.js';
import { useKeypress } from '../../hooks/useKeypress.js';
export const CompletionStep = ({ provider, model, authMethod, onSaveProfile, onDismiss, isFocused = true, }) => {
    const [showProfilePrompt, setShowProfilePrompt] = useState(true);
    const [profileName, setProfileName] = useState('');
    const [saving, setSaving] = useState(false);
    const [error, setError] = useState();
    const providerDisplay = provider.charAt(0).toUpperCase() + provider.slice(1);
    const authDisplay = authMethod === 'oauth' ? 'OAuth' : 'API Key';
    const handleProfileSubmit = useCallback(async () => {
        const trimmedName = profileName.trim();
        if (!trimmedName) {
            setError('Profile name is required');
            return;
        }
        setSaving(true);
        setError(undefined);
        try {
            await onSaveProfile(trimmedName);
            setShowProfilePrompt(false);
        }
        catch (err) {
            setError(err instanceof Error ? err.message : 'Failed to save profile');
            setSaving(false);
        }
    }, [profileName, onSaveProfile]);
    // Handle keyboard input
    useKeypress((key) => {
        if (key.name === 'return') {
            if (showProfilePrompt && !saving) {
                // eslint-disable-next-line @typescript-eslint/no-floating-promises
                handleProfileSubmit();
            }
            else if (!showProfilePrompt) {
                onDismiss();
            }
            return;
        }
        // Only accept input in profile prompt mode
        if (!showProfilePrompt || saving) {
            return;
        }
        if (key.name === 'backspace' || key.name === 'delete') {
            setProfileName((prev) => prev.slice(0, -1));
            return;
        }
        // Accept printable characters (including paste - multi-char sequences)
        const char = key.sequence;
        if (char && !key.ctrl && !key.meta) {
            // Filter to only printable ASCII characters
            const printable = char.replace(/[^\x20-\x7E]/g, '');
            if (printable) {
                setProfileName((prev) => prev + printable);
            }
        }
    }, { isActive: isFocused });
    return (_jsxs(Box, { flexDirection: "column", children: [_jsxs(Box, { flexDirection: "column", marginBottom: 1, children: [_jsx(Text, { bold: true, color: Colors.AccentCyan, children: "Step 5 of 5: Save Your Profile" }), _jsx(Text, { color: Colors.Foreground, children: " " }), _jsx(Text, { bold: true, color: Colors.AccentGreen, children: '[OK] Authentication complete!' })] }), _jsxs(Box, { flexDirection: "column", marginBottom: 1, children: [_jsxs(Text, { color: Colors.Foreground, children: ["Provider: ", providerDisplay] }), model && _jsxs(Text, { color: Colors.Foreground, children: ["Model: ", model] }), _jsxs(Text, { color: Colors.Foreground, children: ["Authentication: ", authDisplay] })] }), showProfilePrompt ? (_jsxs(Box, { flexDirection: "column", children: [_jsx(Box, { marginBottom: 1, children: _jsx(Text, { bold: true, color: Colors.Foreground, children: "Save this setup as a profile" }) }), _jsx(Text, { color: Colors.Gray, children: "This profile will be loaded automatically on startup." }), _jsx(Text, { color: Colors.Gray, children: "Use /profile load <name> to switch profiles later." }), _jsxs(Box, { marginTop: 1, flexDirection: "column", children: [error && (_jsx(Box, { marginBottom: 1, children: _jsx(Text, { color: Colors.AccentRed, children: error }) })), saving ? (_jsx(Text, { color: Colors.Foreground, children: "Saving profile..." })) : (_jsxs(Box, { children: [_jsx(Text, { color: Colors.Foreground, children: "Profile name: " }), _jsx(Text, { color: Colors.Foreground, children: profileName }), _jsx(Text, { color: Colors.AccentCyan, children: "\u258C" })] }))] }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { color: Colors.Gray, children: "Enter a name and press Enter to save" }) })] })) : (_jsxs(Box, { flexDirection: "column", children: [_jsxs(Box, { marginBottom: 1, children: [_jsx(Text, { color: Colors.Foreground, children: "Try asking me something like:" }), _jsx(Text, { color: Colors.AccentCyan, children: '"Explain how async/await works in JavaScript"' })] }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { color: Colors.Gray, children: "Press Enter to continue..." }) })] }))] }));
};
//# sourceMappingURL=CompletionStep.js.map