import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { useState } from 'react';
import { AuthType, CodingPlanRegion, isCodingPlanConfig, } from '@hoptrendy/hopcode-core';
import { Box, Text } from 'ink';
import Link from 'ink-link';
import { theme } from '../semantic-colors.js';
import { useKeypress } from '../hooks/useKeypress.js';
import { DescriptiveRadioButtonSelect } from '../components/shared/DescriptiveRadioButtonSelect.js';
import { ApiKeyInput } from '../components/ApiKeyInput.js';
import { TextInput } from '../components/shared/TextInput.js';
import { useUIState } from '../contexts/UIStateContext.js';
import { useUIActions } from '../contexts/UIActionsContext.js';
import { useConfig } from '../contexts/ConfigContext.js';
import { t } from '../../i18n/index.js';
import { ALIBABA_STANDARD_API_KEY_ENDPOINTS, } from '../../constants/alibabaStandardApiKey.js';
import { generateCustomApiKeyEnvKey, normalizeCustomModelIds, maskApiKey, } from './useAuth.js';
const MODEL_PROVIDERS_DOCUMENTATION_URL = 'https://qwenlm.github.io/qwen-code-docs/en/users/configuration/model-providers/';
function parseDefaultAuthType(defaultAuthType) {
    if (defaultAuthType &&
        Object.values(AuthType).includes(defaultAuthType)) {
        return defaultAuthType;
    }
    return null;
}
const ALIBABA_STANDARD_MODEL_IDS_PLACEHOLDER = 'qwen3.5-plus,glm-5,kimi-k2.5';
const ALIBABA_STANDARD_API_DOCUMENTATION_URLS = {
    'cn-beijing': 'https://bailian.console.aliyun.com/cn-beijing?tab=api#/api',
    'sg-singapore': 'https://modelstudio.console.alibabacloud.com/ap-southeast-1?tab=api#/api/?type=model&url=2712195',
    'us-virginia': 'https://modelstudio.console.alibabacloud.com/us-east-1?tab=api#/api/?type=model&url=2712195',
    'cn-hongkong': 'https://modelstudio.console.alibabacloud.com/cn-hongkong?tab=api#/api/?type=model&url=2712195',
};
export function AuthDialog() {
    const { pendingAuthType, authError } = useUIState();
    const { handleAuthSelect: onAuthSelect, handleCodingPlanSubmit, handleAlibabaStandardSubmit, handleOpenRouterSubmit, handleCustomApiKeySubmit, onAuthError, } = useUIActions();
    const config = useConfig();
    const [errorMessage, setErrorMessage] = useState(null);
    const [viewLevel, setViewLevel] = useState('main');
    const [regionIndex, setRegionIndex] = useState(0);
    const [region, setRegion] = useState(CodingPlanRegion.CHINA);
    const [alibabaStandardRegionIndex, setAlibabaStandardRegionIndex] = useState(0);
    const [apiKeyTypeIndex, setApiKeyTypeIndex] = useState(0);
    const [oauthProviderIndex, setOAuthProviderIndex] = useState(0);
    const [alibabaStandardRegion, setAlibabaStandardRegion] = useState('cn-beijing');
    const [alibabaStandardApiKey, setAlibabaStandardApiKey] = useState('');
    const [alibabaStandardApiKeyError, setAlibabaStandardApiKeyError] = useState(null);
    const [alibabaStandardModelId, setAlibabaStandardModelId] = useState('');
    const [alibabaStandardModelIdError, setAlibabaStandardModelIdError] = useState(null);
    // Custom API Key wizard state
    const [customProtocolIndex, setCustomProtocolIndex] = useState(0);
    const [customProtocol, setCustomProtocol] = useState(AuthType.USE_OPENAI);
    const [customBaseUrl, setCustomBaseUrl] = useState('');
    const [customBaseUrlError, setCustomBaseUrlError] = useState(null);
    const [customApiKey, setCustomApiKey] = useState('');
    const [customApiKeyError, setCustomApiKeyError] = useState(null);
    const [customModelIds, setCustomModelIds] = useState('');
    const [customModelIdsError, setCustomModelIdsError] = useState(null);
    // Advanced generation config state
    const [advancedThinkingEnabled, setAdvancedThinkingEnabled] = useState(false);
    const [advancedModalityEnabled, setAdvancedModalityEnabled] = useState(false);
    const [focusedConfigIndex, setFocusedConfigIndex] = useState(0);
    // 0 = thinking, 1 = modality
    // Main authentication entries (flat three-option layout)
    const mainItems = [
        {
            key: 'CODING_PLAN',
            title: t('Alibaba Cloud Coding Plan'),
            label: t('Alibaba Cloud Coding Plan'),
            description: t('Paid \u00B7 Up to 6,000 requests/5 hrs \u00B7 All Alibaba Cloud Coding Plan Models'),
            value: 'CODING_PLAN',
        },
        {
            key: 'API_KEY',
            title: t('API Key'),
            label: t('API Key'),
            description: t('Bring your own API key'),
            value: 'API_KEY',
        },
        {
            key: 'OAUTH',
            title: t('OAuth'),
            label: t('OAuth'),
            description: t('Browser-based authentication with third-party providers (e.g. OpenRouter, ModelScope)'),
            value: 'OAUTH',
        },
    ];
    // Region selection entries (shown after selecting Alibaba Cloud Coding Plan)
    const regionItems = [
        {
            key: 'china',
            title: '阿里云百炼 (aliyun.com)',
            label: '阿里云百炼 (aliyun.com)',
            description: (_jsx(Link, { url: "https://help.aliyun.com/zh/model-studio/coding-plan", fallback: false, children: _jsx(Text, { color: theme.text.secondary, children: "https://help.aliyun.com/zh/model-studio/coding-plan" }) })),
            value: CodingPlanRegion.CHINA,
        },
        {
            key: 'global',
            title: 'Alibaba Cloud (alibabacloud.com)',
            label: 'Alibaba Cloud (alibabacloud.com)',
            description: (_jsx(Link, { url: "https://www.alibabacloud.com/help/en/model-studio/coding-plan", fallback: false, children: _jsx(Text, { color: theme.text.secondary, children: "https://www.alibabacloud.com/help/en/model-studio/coding-plan" }) })),
            value: CodingPlanRegion.GLOBAL,
        },
    ];
    const alibabaStandardRegionItems = [
        {
            key: 'cn-beijing',
            title: t('China (Beijing)'),
            label: t('China (Beijing)'),
            description: (_jsxs(Text, { color: theme.text.secondary, children: ["Endpoint: ", ALIBABA_STANDARD_API_KEY_ENDPOINTS['cn-beijing']] })),
            value: 'cn-beijing',
        },
        {
            key: 'sg-singapore',
            title: t('Singapore'),
            label: t('Singapore'),
            description: (_jsxs(Text, { color: theme.text.secondary, children: ["Endpoint: ", ALIBABA_STANDARD_API_KEY_ENDPOINTS['sg-singapore']] })),
            value: 'sg-singapore',
        },
        {
            key: 'us-virginia',
            title: t('US (Virginia)'),
            label: t('US (Virginia)'),
            description: (_jsxs(Text, { color: theme.text.secondary, children: ["Endpoint: ", ALIBABA_STANDARD_API_KEY_ENDPOINTS['us-virginia']] })),
            value: 'us-virginia',
        },
        {
            key: 'cn-hongkong',
            title: t('China (Hong Kong)'),
            label: t('China (Hong Kong)'),
            description: (_jsxs(Text, { color: theme.text.secondary, children: ["Endpoint: ", ALIBABA_STANDARD_API_KEY_ENDPOINTS['cn-hongkong']] })),
            value: 'cn-hongkong',
        },
    ];
    const protocolItems = [
        {
            key: AuthType.USE_OPENAI,
            title: t('OpenAI-compatible'),
            label: t('OpenAI-compatible'),
            description: t('OpenAI Chat Completions API (OpenRouter, vLLM, Ollama, LM Studio, Fireworks, etc.)'),
            value: AuthType.USE_OPENAI,
        },
        {
            key: AuthType.USE_ANTHROPIC,
            title: t('Anthropic-compatible'),
            label: t('Anthropic-compatible'),
            description: t('Anthropic Messages API'),
            value: AuthType.USE_ANTHROPIC,
        },
        {
            key: AuthType.USE_GEMINI,
            title: t('Gemini-compatible'),
            label: t('Gemini-compatible'),
            description: t('Google Gemini API'),
            value: AuthType.USE_GEMINI,
        },
    ];
    const DEFAULT_CUSTOM_BASE_URLS = {
        [AuthType.USE_OPENAI]: 'https://api.openai.com/v1',
        [AuthType.USE_ANTHROPIC]: 'https://api.anthropic.com/v1',
        [AuthType.USE_GEMINI]: 'https://generativelanguage.googleapis.com',
    };
    const apiKeyTypeItems = [
        {
            key: 'ALIBABA_STANDARD_API_KEY',
            title: t('Alibaba Cloud ModelStudio Standard API Key'),
            label: t('Alibaba Cloud ModelStudio Standard API Key'),
            description: t('Quick setup for Model Studio (China/International)'),
            value: 'ALIBABA_STANDARD_API_KEY',
        },
        {
            key: 'CUSTOM_API_KEY',
            title: t('Custom API Key'),
            label: t('Custom API Key'),
            description: t('For other OpenAI / Anthropic / Gemini-compatible providers'),
            value: 'CUSTOM_API_KEY',
        },
    ];
    const oauthProviderItems = [
        {
            key: 'OPENROUTER_OAUTH',
            title: t('OpenRouter'),
            label: t('OpenRouter'),
            description: t('Browser OAuth · Auto-configure API key and OpenRouter models'),
            value: 'OPENROUTER_OAUTH',
        },
        {
            key: 'MODELSCOPE_OAUTH',
            title: t('ModelScope'),
            label: t('ModelScope'),
            description: t('Browser OAuth · Auto-configure API key and ModelScope models'),
            value: 'MODELSCOPE_OAUTH',
        },
        {
            key: 'QWEN_OAUTH_DISCONTINUED',
            title: t('Qwen'),
            label: t('Qwen'),
            description: t('Discontinued — switch to Coding Plan or API Key'),
            value: 'QWEN_OAUTH_DISCONTINUED',
        },
    ];
    // Map an AuthType to the corresponding main menu option.
    // QWEN_OAUTH maps to 'OAUTH'; USE_OPENAI maps to:
    // - CODING_PLAN when current config matches coding plan
    // - API_KEY for other OpenAI / Anthropic / Gemini-compatible configs
    const contentGenConfig = config.getContentGeneratorConfig();
    const isCurrentlyCodingPlan = isCodingPlanConfig(contentGenConfig?.baseUrl, contentGenConfig?.apiKeyEnvKey) !== false;
    const authTypeToMainOption = (authType) => {
        if (authType === AuthType.HOPCODE_OAUTH)
            return 'OAUTH';
        if (authType === AuthType.USE_OPENAI && isCurrentlyCodingPlan) {
            return 'CODING_PLAN';
        }
        return 'API_KEY';
    };
    const initialAuthIndex = Math.max(0, mainItems.findIndex((item) => {
        // Priority 1: pendingAuthType
        if (pendingAuthType) {
            return item.value === authTypeToMainOption(pendingAuthType);
        }
        // Priority 2: config.getAuthType() - the source of truth
        const currentAuthType = config.getAuthType();
        if (currentAuthType) {
            return item.value === authTypeToMainOption(currentAuthType);
        }
        // Priority 3: QWEN_DEFAULT_AUTH_TYPE env var
        const defaultAuthType = parseDefaultAuthType(process.env['QWEN_DEFAULT_AUTH_TYPE']);
        if (defaultAuthType) {
            return item.value === authTypeToMainOption(defaultAuthType);
        }
        // Priority 4: default to OAUTH
        return item.value === 'OAUTH';
    }));
    const handleMainSelect = async (value) => {
        setErrorMessage(null);
        onAuthError(null);
        if (value === 'CODING_PLAN') {
            // Navigate to region selection
            setViewLevel('region-select');
            return;
        }
        if (value === 'API_KEY') {
            setViewLevel('api-key-type-select');
            return;
        }
        if (value === 'OAUTH') {
            setViewLevel('oauth-provider-select');
            return;
        }
        await onAuthSelect(value);
    };
    const handleApiKeyTypeSelect = async (value) => {
        setErrorMessage(null);
        onAuthError(null);
        if (value === 'ALIBABA_STANDARD_API_KEY') {
            setAlibabaStandardModelIdError(null);
            setAlibabaStandardApiKeyError(null);
            setViewLevel('alibaba-standard-region-select');
            return;
        }
        // Reset custom wizard state and go to protocol selection
        setCustomProtocolIndex(0);
        setCustomProtocol(AuthType.USE_OPENAI);
        setCustomBaseUrl('');
        setCustomBaseUrlError(null);
        setCustomApiKey('');
        setCustomApiKeyError(null);
        setCustomModelIds('');
        setCustomModelIdsError(null);
        setAdvancedThinkingEnabled(false);
        setAdvancedModalityEnabled(false);
        setFocusedConfigIndex(0);
        setViewLevel('custom-protocol-select');
    };
    const handleOAuthProviderSelect = async (value) => {
        setErrorMessage(null);
        onAuthError(null);
        if (value === 'OPENROUTER_OAUTH') {
            await handleOpenRouterSubmit();
            return;
        }
        // Qwen OAuth free tier discontinued — show warning instead of proceeding
        if (value === 'QWEN_OAUTH_DISCONTINUED') {
            setErrorMessage(t('Qwen OAuth free tier was discontinued on 2026-04-15. Please select Coding Plan or API Key instead.'));
            return;
        }
        // Future: Add support for ModelScope OAuth when implemented
        if (value === 'MODELSCOPE_OAUTH') {
            // Currently not implemented, show message
            setErrorMessage(t('ModelScope OAuth is not yet implemented. Please select another option.'));
            return;
        }
        // For other OAuth providers, you can extend the functionality here
        await onAuthSelect(AuthType.USE_OPENAI);
    };
    const handleRegionSelect = async (selectedRegion) => {
        setErrorMessage(null);
        onAuthError(null);
        setRegion(selectedRegion);
        setViewLevel('api-key-input');
    };
    const handleAlibabaStandardRegionSelect = async (selectedRegion) => {
        setErrorMessage(null);
        onAuthError(null);
        setAlibabaStandardApiKeyError(null);
        setAlibabaStandardModelIdError(null);
        setAlibabaStandardRegion(selectedRegion);
        setViewLevel('alibaba-standard-api-key-input');
    };
    const handleApiKeyInputSubmit = async (apiKey) => {
        setErrorMessage(null);
        if (!apiKey.trim()) {
            setErrorMessage(t('API key cannot be empty.'));
            return;
        }
        // Submit to parent for processing with region info
        await handleCodingPlanSubmit(apiKey, region);
    };
    const handleAlibabaStandardApiKeySubmit = () => {
        const trimmedKey = alibabaStandardApiKey.trim();
        if (!trimmedKey) {
            setAlibabaStandardApiKeyError(t('API key cannot be empty.'));
            return;
        }
        setAlibabaStandardApiKeyError(null);
        if (!alibabaStandardModelId.trim()) {
            setAlibabaStandardModelId(ALIBABA_STANDARD_MODEL_IDS_PLACEHOLDER);
        }
        setViewLevel('alibaba-standard-model-id-input');
    };
    const handleAlibabaStandardModelSubmit = () => {
        const trimmedApiKey = alibabaStandardApiKey.trim();
        const trimmedModelIds = alibabaStandardModelId.trim();
        if (!trimmedApiKey) {
            setAlibabaStandardApiKeyError(t('API key cannot be empty.'));
            setViewLevel('alibaba-standard-api-key-input');
            return;
        }
        if (!trimmedModelIds) {
            setAlibabaStandardModelIdError(t('Model IDs cannot be empty.'));
            return;
        }
        setAlibabaStandardModelIdError(null);
        void handleAlibabaStandardSubmit(trimmedApiKey, alibabaStandardRegion, trimmedModelIds);
    };
    const handleCustomProtocolSelect = (protocol) => {
        setErrorMessage(null);
        onAuthError(null);
        setCustomProtocol(protocol);
        const defaultUrl = DEFAULT_CUSTOM_BASE_URLS[protocol] ?? '';
        setCustomBaseUrl(defaultUrl);
        setCustomBaseUrlError(null);
        setViewLevel('custom-base-url-input');
    };
    const handleCustomBaseUrlSubmit = () => {
        const trimmedUrl = customBaseUrl.trim();
        if (!trimmedUrl) {
            setCustomBaseUrlError(t('Base URL cannot be empty.'));
            return;
        }
        if (!/^https?:\/\//i.test(trimmedUrl)) {
            setCustomBaseUrlError(t('Base URL must start with http:// or https://.'));
            return;
        }
        setCustomBaseUrlError(null);
        setCustomApiKey('');
        setCustomApiKeyError(null);
        setViewLevel('custom-api-key-input');
    };
    const handleCustomApiKeySubmitLocal = () => {
        const trimmedKey = customApiKey.trim();
        if (!trimmedKey) {
            setCustomApiKeyError(t('API key cannot be empty.'));
            return;
        }
        setCustomApiKeyError(null);
        setCustomModelIds('');
        setCustomModelIdsError(null);
        setViewLevel('custom-model-id-input');
    };
    const handleCustomModelIdSubmit = () => {
        const normalized = normalizeCustomModelIds(customModelIds);
        if (normalized.length === 0) {
            setCustomModelIdsError(t('Model IDs cannot be empty.'));
            return;
        }
        setCustomModelIdsError(null);
        setViewLevel('custom-advanced-config');
    };
    const handleAdvancedConfigSubmit = () => {
        setViewLevel('custom-review-json');
    };
    const handleCustomReviewSubmit = () => {
        const trimmedBaseUrl = customBaseUrl.trim();
        const trimmedApiKey = customApiKey.trim();
        const trimmedModelIds = customModelIds;
        // Build generationConfig only if any advanced option is set
        const hasThinking = advancedThinkingEnabled;
        const hasModality = advancedModalityEnabled;
        const generationConfig = hasThinking || hasModality
            ? {
                enableThinking: hasThinking ? true : undefined,
                multimodal: hasModality
                    ? { image: true, video: true, audio: true }
                    : undefined,
            }
            : undefined;
        void handleCustomApiKeySubmit(customProtocol, trimmedBaseUrl, trimmedApiKey, trimmedModelIds, generationConfig);
    };
    const handleGoBack = () => {
        setErrorMessage(null);
        onAuthError(null);
        if (viewLevel === 'region-select') {
            setViewLevel('main');
        }
        else if (viewLevel === 'api-key-input') {
            setViewLevel('region-select');
        }
        else if (viewLevel === 'api-key-type-select') {
            setViewLevel('main');
        }
        else if (viewLevel === 'custom-protocol-select') {
            setViewLevel('api-key-type-select');
        }
        else if (viewLevel === 'custom-base-url-input') {
            setViewLevel('custom-protocol-select');
        }
        else if (viewLevel === 'custom-api-key-input') {
            setViewLevel('custom-base-url-input');
        }
        else if (viewLevel === 'custom-model-id-input') {
            setViewLevel('custom-api-key-input');
        }
        else if (viewLevel === 'custom-advanced-config') {
            setViewLevel('custom-model-id-input');
        }
        else if (viewLevel === 'custom-review-json') {
            setViewLevel('custom-advanced-config');
        }
        else if (viewLevel === 'alibaba-standard-region-select') {
            setViewLevel('api-key-type-select');
        }
        else if (viewLevel === 'alibaba-standard-api-key-input') {
            setViewLevel('alibaba-standard-region-select');
        }
        else if (viewLevel === 'alibaba-standard-model-id-input') {
            setViewLevel('alibaba-standard-api-key-input');
        }
        else if (viewLevel === 'oauth-provider-select') {
            setViewLevel('main');
        }
    };
    useKeypress((key) => {
        if (key.name === 'escape') {
            // Handle Escape based on current view level
            if (viewLevel === 'region-select') {
                handleGoBack();
                return;
            }
            if (viewLevel === 'api-key-input') {
                handleGoBack();
                return;
            }
            if (viewLevel === 'custom-protocol-select' ||
                viewLevel === 'custom-base-url-input' ||
                viewLevel === 'custom-api-key-input' ||
                viewLevel === 'custom-model-id-input' ||
                viewLevel === 'custom-advanced-config' ||
                viewLevel === 'custom-review-json') {
                handleGoBack();
                return;
            }
            if (viewLevel === 'api-key-type-select' ||
                viewLevel === 'alibaba-standard-region-select' ||
                viewLevel === 'alibaba-standard-api-key-input' ||
                viewLevel === 'alibaba-standard-model-id-input' ||
                viewLevel === 'oauth-provider-select') {
                handleGoBack();
                return;
            }
            // For main view, use existing logic
            if (errorMessage) {
                return;
            }
            if (config.getAuthType() === undefined) {
                setErrorMessage(t('You must select an auth method to proceed. Press Ctrl+C again to exit.'));
                return;
            }
            onAuthSelect(undefined);
        }
    }, { isActive: true });
    // Handle Enter key for review view to save
    useKeypress((key) => {
        if (key.name === 'return' && viewLevel === 'custom-review-json') {
            handleCustomReviewSubmit();
        }
    }, { isActive: true });
    // Advanced config keypress: ↑↓ to navigate, Space to toggle, Enter to submit
    useKeypress((key) => {
        if (viewLevel !== 'custom-advanced-config')
            return;
        const { name } = key;
        if (name === 'up') {
            setFocusedConfigIndex((v) => (v <= 0 ? 1 : v - 1));
            return;
        }
        if (name === 'down') {
            setFocusedConfigIndex((v) => (v >= 1 ? 0 : v + 1));
            return;
        }
        if (name === 'space') {
            if (focusedConfigIndex === 0) {
                setAdvancedThinkingEnabled((v) => !v);
            }
            else {
                setAdvancedModalityEnabled((v) => !v);
            }
            return;
        }
        if (name === 'return') {
            handleAdvancedConfigSubmit();
            return;
        }
    }, { isActive: true });
    // Render main auth selection
    const renderMainView = () => (_jsx(_Fragment, { children: _jsx(Box, { marginTop: 1, children: _jsx(DescriptiveRadioButtonSelect, { items: mainItems, initialIndex: initialAuthIndex, onSelect: handleMainSelect, itemGap: 1 }) }) }));
    // Render region selection for Alibaba Cloud Coding Plan
    const renderRegionSelectView = () => (_jsxs(_Fragment, { children: [_jsx(Box, { marginTop: 1, children: _jsx(Text, { color: theme.text.primary, children: t('Choose based on where your account is registered') }) }), _jsx(Box, { marginTop: 1, children: _jsx(DescriptiveRadioButtonSelect, { items: regionItems, initialIndex: regionIndex, onSelect: handleRegionSelect, onHighlight: (value) => {
                        const index = regionItems.findIndex((item) => item.value === value);
                        setRegionIndex(index);
                    }, itemGap: 1 }) }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { color: theme?.text?.secondary, children: t('Enter to select, ↑↓ to navigate, Esc to go back') }) })] }));
    // Render API key input for coding-plan mode
    const renderApiKeyInputView = () => (_jsx(Box, { marginTop: 1, children: _jsx(ApiKeyInput, { onSubmit: handleApiKeyInputSubmit, onCancel: handleGoBack, region: region }) }));
    const renderApiKeyTypeSelectView = () => (_jsxs(_Fragment, { children: [_jsx(Box, { marginTop: 1, children: _jsx(DescriptiveRadioButtonSelect, { items: apiKeyTypeItems, initialIndex: apiKeyTypeIndex, onSelect: handleApiKeyTypeSelect, onHighlight: (value) => {
                        const index = apiKeyTypeItems.findIndex((item) => item.value === value);
                        setApiKeyTypeIndex(index);
                    }, itemGap: 1 }) }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { color: theme?.text?.secondary, children: t('Enter to select, ↑↓ to navigate, Esc to go back') }) })] }));
    const renderAlibabaStandardRegionSelectView = () => (_jsxs(_Fragment, { children: [_jsx(Box, { marginTop: 1, children: _jsx(DescriptiveRadioButtonSelect, { items: alibabaStandardRegionItems, initialIndex: alibabaStandardRegionIndex, onSelect: handleAlibabaStandardRegionSelect, onHighlight: (value) => {
                        const index = alibabaStandardRegionItems.findIndex((item) => item.value === value);
                        setAlibabaStandardRegionIndex(index);
                    }, itemGap: 1 }) }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { color: theme?.text?.secondary, children: t('Enter to select, ↑↓ to navigate, Esc to go back') }) })] }));
    const renderAlibabaStandardApiKeyInputView = () => (_jsxs(Box, { marginTop: 1, flexDirection: "column", children: [_jsx(Box, { marginTop: 1, children: _jsxs(Text, { color: theme.text.secondary, children: ["Endpoint: ", ALIBABA_STANDARD_API_KEY_ENDPOINTS[alibabaStandardRegion]] }) }), _jsx(Box, { marginTop: 1, children: _jsxs(Text, { color: theme.text.secondary, children: [t('Documentation'), ":"] }) }), _jsx(Box, { marginTop: 0, children: _jsx(Link, { url: ALIBABA_STANDARD_API_DOCUMENTATION_URLS[alibabaStandardRegion], fallback: false, children: _jsx(Text, { color: theme.text.link, children: ALIBABA_STANDARD_API_DOCUMENTATION_URLS[alibabaStandardRegion] }) }) }), _jsx(Box, { marginTop: 1, children: _jsx(TextInput, { value: alibabaStandardApiKey, onChange: (value) => {
                        setAlibabaStandardApiKey(value);
                        if (alibabaStandardApiKeyError) {
                            setAlibabaStandardApiKeyError(null);
                        }
                    }, onSubmit: handleAlibabaStandardApiKeySubmit, placeholder: "sk-..." }) }), alibabaStandardApiKeyError && (_jsx(Box, { marginTop: 1, children: _jsx(Text, { color: theme.status.error, children: alibabaStandardApiKeyError }) })), _jsx(Box, { marginTop: 1, children: _jsx(Text, { color: theme.text.secondary, children: t('Enter to submit, Esc to go back') }) })] }));
    const renderAlibabaStandardModelIdInputView = () => (_jsxs(Box, { marginTop: 1, flexDirection: "column", children: [_jsx(Box, { marginTop: 1, children: _jsx(Text, { color: theme.text.secondary, children: t('You can enter multiple model IDs, separated by commas. Examples: qwen3.5-plus,glm-5,kimi-k2.5') }) }), _jsx(Box, { marginTop: 1, children: _jsx(TextInput, { value: alibabaStandardModelId, onChange: (value) => {
                        setAlibabaStandardModelId(value);
                        if (alibabaStandardModelIdError) {
                            setAlibabaStandardModelIdError(null);
                        }
                    }, onSubmit: handleAlibabaStandardModelSubmit, placeholder: ALIBABA_STANDARD_MODEL_IDS_PLACEHOLDER }) }), alibabaStandardModelIdError && (_jsx(Box, { marginTop: 1, children: _jsx(Text, { color: theme.status.error, children: alibabaStandardModelIdError }) })), _jsx(Box, { marginTop: 1, children: _jsx(Text, { color: theme.text.secondary, children: t('Enter to submit, Esc to go back') }) })] }));
    // Render custom protocol selection
    const renderCustomProtocolSelectView = () => (_jsxs(_Fragment, { children: [_jsx(Box, { marginTop: 1, children: _jsx(DescriptiveRadioButtonSelect, { items: protocolItems, initialIndex: customProtocolIndex, onSelect: handleCustomProtocolSelect, onHighlight: (value) => {
                        const index = protocolItems.findIndex((item) => item.value === value);
                        setCustomProtocolIndex(index);
                    }, itemGap: 1 }) }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { color: theme.text.secondary, children: t('Enter to select, ↑↓ to navigate, Esc to go back') }) })] }));
    // Render custom base URL input
    const renderCustomBaseUrlInputView = () => (_jsxs(Box, { marginTop: 1, flexDirection: "column", children: [_jsx(Box, { marginTop: 1, children: _jsx(Text, { color: theme.text.primary, children: t('Enter the API endpoint for this protocol.') }) }), _jsx(Box, { marginTop: 1, children: _jsx(TextInput, { value: customBaseUrl, onChange: (value) => {
                        setCustomBaseUrl(value);
                        if (customBaseUrlError) {
                            setCustomBaseUrlError(null);
                        }
                    }, onSubmit: handleCustomBaseUrlSubmit, placeholder: "https://api.openai.com/v1" }) }), customBaseUrlError && (_jsx(Box, { marginTop: 1, children: _jsx(Text, { color: theme.status.error, children: customBaseUrlError }) })), _jsx(Box, { marginTop: 1, children: _jsx(Link, { url: MODEL_PROVIDERS_DOCUMENTATION_URL, fallback: false, children: _jsx(Text, { color: theme.text.link, children: t('Need advanced generationConfig or capabilities? See documentation') }) }) }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { color: theme.text.secondary, children: t('Enter to submit, Esc to go back') }) })] }));
    // Render custom API key input
    const renderCustomApiKeyInputView = () => (_jsxs(Box, { marginTop: 1, flexDirection: "column", children: [_jsx(Box, { marginTop: 1, children: _jsx(Text, { color: theme.text.primary, children: t('Enter the API key for this endpoint.') }) }), _jsx(Box, { marginTop: 1, children: _jsx(TextInput, { value: customApiKey, onChange: (value) => {
                        setCustomApiKey(value);
                        if (customApiKeyError) {
                            setCustomApiKeyError(null);
                        }
                    }, onSubmit: handleCustomApiKeySubmitLocal, placeholder: "sk-..." }) }), customApiKeyError && (_jsx(Box, { marginTop: 1, children: _jsx(Text, { color: theme.status.error, children: customApiKeyError }) })), _jsx(Box, { marginTop: 1, children: _jsx(Text, { color: theme.text.secondary, children: t('Enter to submit, Esc to go back') }) })] }));
    // Render custom model ID input
    const renderCustomModelIdInputView = () => (_jsxs(Box, { marginTop: 1, flexDirection: "column", children: [_jsx(Box, { marginTop: 1, children: _jsx(Text, { color: theme.text.primary, children: t('Enter one or more model IDs, separated by commas.') }) }), _jsx(Box, { marginTop: 1, children: _jsx(TextInput, { value: customModelIds, onChange: (value) => {
                        setCustomModelIds(value);
                        if (customModelIdsError) {
                            setCustomModelIdsError(null);
                        }
                    }, onSubmit: handleCustomModelIdSubmit, placeholder: "qwen/qwen3-coder,openai/gpt-4.1" }) }), customModelIdsError && (_jsx(Box, { marginTop: 1, children: _jsx(Text, { color: theme.status.error, children: customModelIdsError }) })), _jsx(Box, { marginTop: 1, children: _jsx(Text, { color: theme.text.secondary, children: t('Enter to submit, Esc to go back') }) })] }));
    // Render custom advanced config
    const renderCustomAdvancedConfigView = () => {
        const checkmark = (v) => (v ? '◉' : '○');
        const cursor = (index) => focusedConfigIndex === index ? '›' : ' ';
        return (_jsxs(Box, { marginTop: 1, flexDirection: "column", children: [_jsx(Box, { marginTop: 1, children: _jsx(Text, { color: theme.text.primary, children: t('Optional: configure advanced generation settings.') }) }), _jsx(Box, { marginTop: 1, marginLeft: 2, children: _jsxs(Text, { color: focusedConfigIndex === 0 ? theme.status.success : undefined, children: [cursor(0), " ", checkmark(advancedThinkingEnabled), ' ', t('Enable thinking')] }) }), _jsx(Box, { marginTop: 0, marginLeft: 4, children: _jsx(Text, { color: theme.text.secondary, children: t('Allows the model to perform extended reasoning before responding.') }) }), _jsx(Box, { marginTop: 1, marginLeft: 2, children: _jsxs(Text, { color: focusedConfigIndex === 1 ? theme.status.success : undefined, children: [cursor(1), " ", checkmark(advancedModalityEnabled), ' ', t('Enable modality')] }) }), _jsx(Box, { marginTop: 0, marginLeft: 4, children: _jsx(Text, { color: theme.text.secondary, children: t('Enables image, video, and audio input/output capabilities.') }) }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { color: theme.text.secondary, children: t('\u2191\u2193 to navigate, Space to toggle, Enter to continue, Esc to go back') }) })] }));
    };
    // Render custom review JSON
    const renderCustomReviewJsonView = () => {
        const generatedEnvKey = generateCustomApiKeyEnvKey(customProtocol, customBaseUrl.trim());
        const normalizedIds = normalizeCustomModelIds(customModelIds);
        const maskedKey = maskApiKey(customApiKey);
        // Build generationConfig preview lines
        const hasThinking = advancedThinkingEnabled;
        const hasModality = advancedModalityEnabled;
        const hasGenConfig = hasThinking || hasModality;
        let genConfig;
        if (hasGenConfig) {
            genConfig = {};
            if (hasModality) {
                genConfig['modalities'] = {
                    image: true,
                    video: true,
                    audio: true,
                };
            }
            if (hasThinking) {
                genConfig['extra_body'] = {
                    enable_thinking: true,
                };
            }
        }
        const modelEntries = normalizedIds.map((id) => {
            const entry = {
                id,
                name: id,
                baseUrl: customBaseUrl.trim(),
                envKey: generatedEnvKey,
            };
            if (genConfig) {
                entry['generationConfig'] = genConfig;
            }
            return entry;
        });
        const preview = {
            env: { [generatedEnvKey]: maskedKey },
            modelProviders: {
                [customProtocol]: modelEntries,
            },
            security: {
                auth: {
                    selectedType: customProtocol,
                },
            },
            model: {
                name: normalizedIds[0],
            },
        };
        const jsonPreview = JSON.stringify(preview, null, 2);
        return (_jsxs(Box, { marginTop: 1, flexDirection: "column", children: [_jsx(Box, { marginTop: 1, children: _jsx(Text, { color: theme.text.primary, children: t('The following JSON will be saved to settings.json:') }) }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { children: jsonPreview }) }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { color: theme.text.secondary, children: t('Enter to save, Esc to go back') }) })] }));
    };
    const renderOAuthProviderSelectView = () => (_jsxs(_Fragment, { children: [_jsx(Box, { marginTop: 1, children: _jsx(DescriptiveRadioButtonSelect, { items: oauthProviderItems, initialIndex: oauthProviderIndex, onSelect: handleOAuthProviderSelect, onHighlight: (value) => {
                        const index = oauthProviderItems.findIndex((item) => item.value === value);
                        setOAuthProviderIndex(index);
                    }, itemGap: 1 }) }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { color: theme?.text?.secondary, children: t('Enter to select, ↑↓ to navigate, Esc to go back') }) })] }));
    const getViewTitle = () => {
        switch (viewLevel) {
            case 'main':
                return t('Select Authentication Method');
            case 'region-select':
                return t('Select Region for Coding Plan');
            case 'api-key-input':
                return t('Enter Coding Plan API Key');
            case 'api-key-type-select':
                return t('Select API Key Type');
            case 'custom-protocol-select':
                return t('Step 1/6 \u00B7 Protocol');
            case 'custom-base-url-input':
                return t('Step 2/6 \u00B7 Base URL');
            case 'custom-api-key-input':
                return t('Step 3/6 \u00B7 API Key');
            case 'custom-model-id-input':
                return t('Step 4/6 \u00B7 Model IDs');
            case 'custom-advanced-config':
                return t('Step 5/6 \u00B7 Advanced Config');
            case 'custom-review-json':
                return t('Step 6/6 \u00B7 Review');
            case 'alibaba-standard-region-select':
                return t('Select Region for Alibaba Cloud ModelStudio Standard API Key');
            case 'alibaba-standard-api-key-input':
                return t('Enter Alibaba Cloud ModelStudio Standard API Key');
            case 'alibaba-standard-model-id-input':
                return t('Enter Model IDs');
            case 'oauth-provider-select':
                return t('Select OAuth Provider');
            default:
                return t('Select Authentication Method');
        }
    };
    return (_jsxs(Box, { borderStyle: "single", borderColor: theme?.border?.default, flexDirection: "column", padding: 1, width: "100%", children: [_jsx(Text, { bold: true, children: getViewTitle() }), viewLevel === 'main' && renderMainView(), viewLevel === 'region-select' && renderRegionSelectView(), viewLevel === 'api-key-input' && renderApiKeyInputView(), viewLevel === 'api-key-type-select' && renderApiKeyTypeSelectView(), viewLevel === 'alibaba-standard-region-select' &&
                renderAlibabaStandardRegionSelectView(), viewLevel === 'alibaba-standard-api-key-input' &&
                renderAlibabaStandardApiKeyInputView(), viewLevel === 'alibaba-standard-model-id-input' &&
                renderAlibabaStandardModelIdInputView(), viewLevel === 'custom-protocol-select' &&
                renderCustomProtocolSelectView(), viewLevel === 'custom-base-url-input' && renderCustomBaseUrlInputView(), viewLevel === 'custom-api-key-input' && renderCustomApiKeyInputView(), viewLevel === 'custom-model-id-input' && renderCustomModelIdInputView(), viewLevel === 'custom-advanced-config' &&
                renderCustomAdvancedConfigView(), viewLevel === 'custom-review-json' && renderCustomReviewJsonView(), viewLevel === 'oauth-provider-select' && renderOAuthProviderSelectView(), (authError || errorMessage) && (_jsx(Box, { marginTop: 1, children: _jsx(Text, { color: theme.status.error, children: authError || errorMessage }) })), viewLevel === 'main' && (_jsxs(_Fragment, { children: [_jsx(Box, { marginY: 1, children: _jsx(Text, { color: theme.border.default, children: '\u2500'.repeat(80) }) }), _jsx(Box, { children: _jsxs(Text, { color: theme.text.primary, children: [t('Terms of Services and Privacy Notice'), ":"] }) }), _jsx(Box, { children: _jsx(Link, { url: "https://qwenlm.github.io/qwen-code-docs/en/users/support/tos-privacy/", fallback: false, children: _jsx(Text, { color: theme.text.secondary, underline: true, children: "https://qwenlm.github.io/qwen-code-docs/en/users/support/tos-privacy/" }) }) })] }))] }));
}
//# sourceMappingURL=AuthDialog.js.map