import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
/**
 * @license
 * Copyright 2025 Vybestack LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import React, { useState, useCallback } from 'react';
import { Box, Text } from 'ink';
import { Colors } from '../colors.js';
import { RadioButtonSelect } from './shared/RadioButtonSelect.js';
import { SettingScope } from '../../config/settings.js';
import { useKeypress } from '../hooks/useKeypress.js';
import { useRuntimeApi } from '../contexts/RuntimeContext.js';
// TODO: Re-add _parseDefaultAuthType if needed for future auth type parsing
// function was removed as it was unused
export function AuthDialog({ onSelect, settings, initialErrorMessage, }) {
    const runtime = useRuntimeApi();
    const [errorMessage, setErrorMessage] = useState(initialErrorMessage || null);
    // Track enabled providers from settings (oauthEnabledProviders is an object)
    const [enabledProviders, setEnabledProviders] = useState(() => {
        const oauthProviders = settings.merged.oauthEnabledProviders || {};
        const enabled = new Set();
        for (const [provider, isEnabled] of Object.entries(oauthProviders)) {
            if (isEnabled) {
                enabled.add(`oauth_${provider}`);
            }
        }
        return enabled;
    });
    // Update enabledProviders state when settings change
    React.useEffect(() => {
        const oauthProviders = settings.merged.oauthEnabledProviders || {};
        const enabled = new Set();
        for (const [provider, isEnabled] of Object.entries(oauthProviders)) {
            if (isEnabled) {
                enabled.add(`oauth_${provider}`);
            }
        }
        setEnabledProviders(enabled);
    }, [settings.merged.oauthEnabledProviders]);
    const items = [
        {
            key: 'oauth_gemini',
            label: `Gemini (Google OAuth) ${enabledProviders.has('oauth_gemini') ? '[ON]' : '[OFF]'}`,
            value: 'oauth_gemini',
        },
        {
            key: 'oauth_qwen',
            label: `Qwen (OAuth) ${enabledProviders.has('oauth_qwen') ? '[ON]' : '[OFF]'}`,
            value: 'oauth_qwen',
        },
        {
            key: 'oauth_anthropic',
            label: `Anthropic Claude (OAuth) ${enabledProviders.has('oauth_anthropic') ? '[ON]' : '[OFF]'}`,
            value: 'oauth_anthropic',
        },
        {
            key: 'oauth_codex',
            label: `Codex (ChatGPT OAuth) ${enabledProviders.has('oauth_codex') ? '[ON]' : '[OFF]'}`,
            value: 'oauth_codex',
        },
        {
            key: 'close',
            label: 'Close',
            value: 'close',
        },
    ];
    // Default to first item (Gemini OAuth)
    const initialAuthIndex = 0;
    // Ensure we have a valid initial index (default to 0 if not found)
    const safeInitialIndex = initialAuthIndex >= 0 ? initialAuthIndex : 0;
    const handleAuthSelect = useCallback(async (authMethod) => {
        setErrorMessage(null);
        // Handle Close option
        if (authMethod === 'close') {
            onSelect(undefined, SettingScope.User);
            return;
        }
        // Map oauth_gemini -> gemini, etc.
        const providerName = authMethod.replace('oauth_', '');
        // Use the actual oauthManager to toggle the provider
        // This will call the same code as /auth gemini enable/disable
        const oauthManager = runtime.getCliOAuthManager();
        if (oauthManager) {
            try {
                const newState = await oauthManager.toggleOAuthEnabled(providerName);
                // Update local state to reflect the change
                const newEnabledProviders = new Set(enabledProviders);
                if (newState) {
                    newEnabledProviders.add(authMethod);
                }
                else {
                    newEnabledProviders.delete(authMethod);
                }
                setEnabledProviders(newEnabledProviders);
            }
            catch (error) {
                setErrorMessage(`Failed to toggle ${providerName}: ${error}`);
            }
        }
        // Don't close the dialog - let user continue toggling
    }, [onSelect, enabledProviders, runtime]);
    useKeypress((key) => {
        if (key.name === 'escape') {
            // Allow ESC to close the dialog
            onSelect(undefined, SettingScope.User);
        }
    }, { isActive: true });
    return (_jsxs(Box, { borderStyle: "round", borderColor: Colors.Gray, flexDirection: "column", padding: 1, width: "100%", children: [_jsx(Text, { bold: true, children: "OAuth Authentication" }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { children: "Select an OAuth provider to authenticate:" }) }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { dimColor: true, children: "Note: You can also use API keys via /key, /keyfile, --key, --keyfile, or environment variables" }) }), _jsx(Box, { marginTop: 1, children: _jsx(RadioButtonSelect, { items: items, initialIndex: safeInitialIndex, onSelect: handleAuthSelect }) }), errorMessage && (_jsx(Box, { marginTop: 1, children: _jsx(Text, { color: Colors.AccentRed, children: errorMessage }) })), _jsx(Box, { marginTop: 1, children: _jsx(Text, { color: Colors.Gray, children: "(Use Enter to select, ESC to close)" }) }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { children: "Terms of Services and Privacy Notice for Gemini CLI" }) }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { color: Colors.AccentBlue, children: 'https://github.com/acoliver/llxprt-code/blob/main/docs/tos-privacy.md' }) })] }));
}
//# sourceMappingURL=AuthDialog.js.map