"use strict";
/**
 * Base Config Loader for CLIProxy Providers
 *
 * Loads provider configurations from config/base-{provider}.settings.json files.
 * This allows model mappings to be easily updated without digging into code.
 *
 * Config files are bundled with the npm package and read at runtime.
 */
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 (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.clearConfigCache = exports.getEnvVarsFromConfig = exports.getModelMappingFromConfig = exports.loadBaseConfig = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
/** Cached configs to avoid repeated file reads */
const configCache = new Map();
/**
 * Get path to base config file for provider
 * Config files are in the config/ directory relative to package root
 */
function getBaseConfigPath(provider) {
    // __dirname points to dist/cliproxy at runtime
    // Config files are at package root: ../config/
    const configDir = path.join(__dirname, '..', '..', '..', 'config');
    return path.join(configDir, `base-${provider}.settings.json`);
}
/**
 * Load base config for a provider
 * Returns parsed settings from config/base-{provider}.settings.json
 */
function loadBaseConfig(provider) {
    // Check cache first
    const cached = configCache.get(provider);
    if (cached) {
        return cached;
    }
    const configPath = getBaseConfigPath(provider);
    if (!fs.existsSync(configPath)) {
        throw new Error(`Base config not found for provider '${provider}': ${configPath}\n` +
            `Expected file: config/base-${provider}.settings.json`);
    }
    try {
        const content = fs.readFileSync(configPath, 'utf-8');
        const settings = JSON.parse(content);
        // Validate required fields
        if (!settings.env || typeof settings.env !== 'object') {
            throw new Error('Missing or invalid "env" object');
        }
        const required = [
            'ANTHROPIC_MODEL',
            'ANTHROPIC_DEFAULT_OPUS_MODEL',
            'ANTHROPIC_DEFAULT_SONNET_MODEL',
            'ANTHROPIC_DEFAULT_HAIKU_MODEL',
        ];
        for (const field of required) {
            if (!settings.env[field]) {
                throw new Error(`Missing required field: env.${field}`);
            }
        }
        // Cache and return
        configCache.set(provider, settings);
        return settings;
    }
    catch (error) {
        const err = error;
        throw new Error(`Failed to load base config for '${provider}': ${err.message}`);
    }
}
exports.loadBaseConfig = loadBaseConfig;
/**
 * Get model mapping from base config
 * Extracts model names from env vars
 */
function getModelMappingFromConfig(provider) {
    const config = loadBaseConfig(provider);
    return {
        defaultModel: config.env.ANTHROPIC_MODEL,
        claudeModel: config.env.ANTHROPIC_MODEL,
        opusModel: config.env.ANTHROPIC_DEFAULT_OPUS_MODEL,
        sonnetModel: config.env.ANTHROPIC_DEFAULT_SONNET_MODEL,
        haikuModel: config.env.ANTHROPIC_DEFAULT_HAIKU_MODEL,
    };
}
exports.getModelMappingFromConfig = getModelMappingFromConfig;
/**
 * Get full env vars from base config
 * Returns the complete env object for Claude CLI
 */
function getEnvVarsFromConfig(provider) {
    const config = loadBaseConfig(provider);
    return config.env;
}
exports.getEnvVarsFromConfig = getEnvVarsFromConfig;
/**
 * Clear config cache (useful for testing)
 */
function clearConfigCache() {
    configCache.clear();
}
exports.clearConfigCache = clearConfigCache;
//# sourceMappingURL=base-config-loader.js.map