/**
 * @license
 * Copyright 2025 Vybestack LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import { CommandKind, } from './types.js';
import { AuthType } from '@vybestack/llxprt-code-core';
export const keyCommand = {
    name: 'key',
    description: 'set or remove API key for the current provider',
    kind: CommandKind.BUILT_IN,
    action: async (context, args) => {
        const config = context.services.config;
        if (!config) {
            return {
                type: 'message',
                messageType: 'error',
                content: 'No configuration available',
            };
        }
        // Settings service not needed for command-level auth
        const providerManager = config.getProviderManager();
        if (!providerManager) {
            return {
                type: 'message',
                messageType: 'error',
                content: 'No provider manager available',
            };
        }
        const activeProvider = providerManager.getActiveProvider();
        const providerName = activeProvider.name;
        const apiKey = args?.trim();
        // If no key provided or 'none', remove the key
        if (!apiKey || apiKey.toLowerCase() === 'none') {
            // Clear authentication using the provider's method (which now stores in SettingsService)
            if (activeProvider.setApiKey) {
                activeProvider.setApiKey('');
            }
            // If this is the Gemini provider, we might need to switch auth mode
            const requiresAuthRefresh = providerName === 'gemini';
            if (requiresAuthRefresh) {
                await config.refreshAuth(AuthType.LOGIN_WITH_GOOGLE);
            }
            const isPaidMode = activeProvider.isPaidMode?.() ?? true;
            const paymentMessage = !isPaidMode && providerName === 'gemini'
                ? '\n✅ You are now in FREE MODE - using OAuth authentication'
                : '';
            return {
                type: 'message',
                messageType: 'info',
                content: `API key removed for provider '${providerName}'${paymentMessage}`,
            };
        }
        // Set the API key using the provider's method (which now stores in SettingsService)
        if (activeProvider.setApiKey) {
            activeProvider.setApiKey(apiKey);
            // If this is the Gemini provider, we need to refresh auth to use API key mode
            const requiresAuthRefresh = providerName === 'gemini';
            if (requiresAuthRefresh) {
                await config.refreshAuth(AuthType.USE_GEMINI);
            }
            // Check if we're now in paid mode
            const isPaidMode = activeProvider.isPaidMode?.() ?? true;
            const paymentWarning = isPaidMode
                ? '\nWARNING: You are now in PAID MODE - API usage will be charged to your account'
                : '';
            // Trigger payment mode check if available
            const extendedContext = context;
            if (extendedContext.checkPaymentModeChange) {
                setTimeout(extendedContext.checkPaymentModeChange, 100);
            }
            return {
                type: 'message',
                messageType: 'info',
                content: `API key updated for provider '${providerName}'${paymentWarning}`,
            };
        }
        else {
            return {
                type: 'message',
                messageType: 'error',
                content: `Provider '${providerName}' does not support API key updates`,
            };
        }
    },
};
//# sourceMappingURL=keyCommand.js.map