"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.ModelManager = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
class ModelManager {
    configPath;
    defaultModels = {
        primary: { provider: 'gemini-api', name: 'gemini-3.1-flash-lite-preview', raw: 'gemini-api/gemini-3.1-flash-lite-preview' },
        fallbacks: [
            { provider: 'openai-api', name: 'gpt-5.4-mini-2026-03-17', raw: 'openai-api/gpt-5.4-mini-2026-03-17' },
            { provider: 'ollama', name: 'qwen2.5-coder:7b', raw: 'ollama/qwen2.5-coder:7b' }
        ]
    };
    constructor() {
        this.configPath = path.join(process.cwd(), 'models.json');
        this.ensureConfigFile();
    }
    ensureConfigFile() {
        if (!fs.existsSync(this.configPath)) {
            fs.writeFileSync(this.configPath, JSON.stringify(this.defaultModels, null, 2));
        }
    }
    getModelConfig() {
        try {
            const content = fs.readFileSync(this.configPath, 'utf-8');
            return JSON.parse(content);
        }
        catch (e) {
            return this.defaultModels;
        }
    }
    saveConfig(config) {
        fs.writeFileSync(this.configPath, JSON.stringify(config, null, 2));
    }
    parseModelString(input) {
        if (!input.includes('/')) {
            throw new Error(`Formato inválido: '${input}'. O padrão exigido é <provider>/<model> (ex: gemini-cli/gemini-3.1-pro-preview).`);
        }
        const parts = input.split('/');
        const providerStr = parts[0];
        const name = parts.slice(1).join('/');
        const validProviders = [
            'openai-api', 'openai-cli', 'anthropic', 'gemini-api', 'gemini-cli', 'ollama', 'openrouter',
            'xai', 'deepseek', 'groq', 'together', 'mistral',
            'bedrock', 'qwen', 'kimi', 'minimax', 'glm', 'huggingface', 'nvidia-nim', 'perplexity',
            'vllm', 'sglang', 'llamacpp', 'lmstudio',
            'litellm', 'clawrouter', 'custom-openai',
        ];
        const provider = providerStr.toLowerCase();
        if (!validProviders.includes(provider)) {
            throw new Error(`Provedor não suportado: '${provider}'. Provedores válidos: ${validProviders.join(', ')}.`);
        }
        return { provider, name, raw: input };
    }
}
exports.ModelManager = ModelManager;
