/**
 * @license
 * Copyright 2026 HopCode Team Team
 * SPDX-License-Identifier: Apache-2.0
 */
import { AuthType, resolveModelConfig, } from '@hoptrendy/hopcode-core';
export function getAuthTypeFromEnv() {
    if (process.env['HOPCODE_OAUTH']) {
        return AuthType.HOPCODE_OAUTH;
    }
    if (process.env['OPENAI_API_KEY'] &&
        process.env['OPENAI_MODEL'] &&
        process.env['OPENAI_BASE_URL']) {
        return AuthType.USE_OPENAI;
    }
    if (process.env['GEMINI_API_KEY'] && process.env['GEMINI_MODEL']) {
        return AuthType.USE_GEMINI;
    }
    if (process.env['GOOGLE_API_KEY'] && process.env['GOOGLE_MODEL']) {
        return AuthType.USE_VERTEX_AI;
    }
    if (process.env['ANTHROPIC_API_KEY'] &&
        process.env['ANTHROPIC_MODEL'] &&
        process.env['ANTHROPIC_BASE_URL']) {
        return AuthType.USE_ANTHROPIC;
    }
    return undefined;
}
/**
 * Unified resolver for CLI generation config.
 *
 * Precedence (for OpenAI auth):
 * - model: argv.model > OPENAI_MODEL > HOPCODE_MODEL > settings.model.name
 * - apiKey: argv.openaiApiKey > OPENAI_API_KEY > settings.security.auth.apiKey
 * - baseUrl: argv.openaiBaseUrl > OPENAI_BASE_URL > settings.security.auth.baseUrl
 *
 * For non-OpenAI auth, only argv.model override is respected at CLI layer.
 */
export function resolveCliGenerationConfig(inputs) {
    const { argv, settings, selectedAuthType } = inputs;
    const env = inputs.env ?? process.env;
    const authType = selectedAuthType;
    // Find modelProvider from settings.modelProviders based on authType and model
    let modelProvider;
    if (authType && settings.modelProviders) {
        const providers = settings.modelProviders[authType];
        if (providers && Array.isArray(providers)) {
            // Try to find by requested model (from CLI or settings)
            const requestedModel = argv.model || settings.model?.name;
            if (requestedModel) {
                modelProvider = providers.find((p) => p.id === requestedModel);
            }
        }
    }
    const configSources = {
        authType,
        cli: {
            model: argv.model,
            apiKey: argv.openaiApiKey,
            baseUrl: argv.openaiBaseUrl,
        },
        settings: {
            model: settings.model?.name,
            apiKey: settings.security?.auth?.apiKey,
            baseUrl: settings.security?.auth?.baseUrl,
            generationConfig: settings.model?.generationConfig,
        },
        modelProvider,
        env,
    };
    const resolved = resolveModelConfig(configSources);
    // Resolve OpenAI logging config (CLI-specific, not part of core resolver)
    const enableOpenAILogging = (typeof argv.openaiLogging === 'undefined'
        ? settings.model?.enableOpenAILogging
        : argv.openaiLogging) ?? false;
    const openAILoggingDir = argv.openaiLoggingDir || settings.model?.openAILoggingDir;
    // Build the full generation config
    // Note: we merge the resolved config with logging settings
    const generationConfig = {
        ...resolved.config,
        enableOpenAILogging,
        openAILoggingDir,
    };
    return {
        model: resolved.config.model || '',
        apiKey: resolved.config.apiKey || '',
        baseUrl: resolved.config.baseUrl || '',
        generationConfig,
        sources: resolved.sources,
        warnings: resolved.warnings,
    };
}
//# sourceMappingURL=modelConfigUtils.js.map