/**
 * @license
 * Copyright 2026 HopCode Team
 * SPDX-License-Identifier: Apache-2.0
 */
import { AuthType, HOPCODE_OAUTH_MODELS, } from '@hoptrendy/hopcode-core';
import { t } from '../../i18n/index.js';
const CACHED_HOPCODE_OAUTH_MODELS = HOPCODE_OAUTH_MODELS.map((model) => ({
    id: model.id,
    label: model.name ?? model.id,
    description: model.description,
    isVision: model.capabilities?.vision ?? false,
}));
function getQwenOAuthModels() {
    return CACHED_HOPCODE_OAUTH_MODELS;
}
/**
 * Get available HopCode models
 * coder-model now has vision capabilities by default.
 */
export function getFilteredQwenModels() {
    return [...getQwenOAuthModels()];
}
/**
 * Currently we use the single model of `OPENAI_MODEL` in the env.
 * In the future, after settings.json is updated, we will allow users to configure this themselves.
 */
export function getOpenAIAvailableModelFromEnv() {
    const id = process.env['OPENAI_MODEL']?.trim();
    return id
        ? {
            id,
            label: id,
            get description() {
                return t('Configured via OPENAI_MODEL environment variable');
            },
        }
        : null;
}
export function getAnthropicAvailableModelFromEnv() {
    const id = process.env['ANTHROPIC_MODEL']?.trim();
    return id
        ? {
            id,
            label: id,
            get description() {
                return t('Configured via ANTHROPIC_MODEL environment variable');
            },
        }
        : null;
}
/**
 * Convert core AvailableModel to CLI AvailableModel format
 */
function convertCoreModelToCliModel(coreModel) {
    return {
        id: coreModel.id,
        label: coreModel.label,
        description: coreModel.description,
        isVision: coreModel.isVision ?? coreModel.capabilities?.vision ?? false,
    };
}
/**
 * Get available models for the given authType.
 *
 * If a Config object is provided, uses config.getAvailableModelsForAuthType().
 * Falls back to environment variables only when no config is provided.
 */
export function getAvailableModelsForAuthType(authType, config) {
    // Use config's model registry when available
    if (config) {
        try {
            const models = config.getAvailableModelsForAuthType(authType);
            if (models.length > 0) {
                return models.map(convertCoreModelToCliModel);
            }
        }
        catch {
            // If config throws (e.g., not initialized), return empty array
        }
        // When a Config object is provided, we intentionally do NOT fall back to env-based
        // "raw" models. These may reflect the currently effective config but should not be
        // presented as selectable options in /model.
        return [];
    }
    // Fall back to environment variables for specific auth types (no config provided)
    switch (authType) {
        case AuthType.HOPCODE_OAUTH: {
            return [...getQwenOAuthModels()];
        }
        case AuthType.USE_OPENAI: {
            const openAIModel = getOpenAIAvailableModelFromEnv();
            return openAIModel ? [openAIModel] : [];
        }
        case AuthType.USE_ANTHROPIC: {
            const anthropicModel = getAnthropicAvailableModelFromEnv();
            return anthropicModel ? [anthropicModel] : [];
        }
        default:
            return [];
    }
}
//# sourceMappingURL=availableModels.js.map