"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 (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.loadOpenAICompatProxyRoutingConfig = void 0;
const fs = __importStar(require("fs"));
const yaml = __importStar(require("js-yaml"));
const config_manager_1 = require("../utils/config-manager");
function readRawConfigObject(configPath) {
    if (!fs.existsSync(configPath)) {
        return null;
    }
    const raw = fs.readFileSync(configPath, 'utf8');
    const parsed = configPath.endsWith('.yaml') || configPath.endsWith('.yml') ? yaml.load(raw) : JSON.parse(raw);
    return typeof parsed === 'object' && parsed !== null ? parsed : null;
}
function normalizeRoutingConfig(value) {
    if (typeof value !== 'object' || value === null) {
        return {};
    }
    const config = value;
    return {
        default: typeof config.default === 'string' && config.default.trim()
            ? config.default.trim()
            : undefined,
        background: typeof config.background === 'string' && config.background.trim()
            ? config.background.trim()
            : undefined,
        think: typeof config.think === 'string' && config.think.trim() ? config.think.trim() : undefined,
        longContext: typeof config.longContext === 'string' && config.longContext.trim()
            ? config.longContext.trim()
            : undefined,
        webSearch: typeof config.webSearch === 'string' && config.webSearch.trim()
            ? config.webSearch.trim()
            : undefined,
        longContextThreshold: typeof config.longContextThreshold === 'number' &&
            Number.isFinite(config.longContextThreshold)
            ? config.longContextThreshold
            : undefined,
    };
}
function loadOpenAICompatProxyRoutingConfig() {
    const activePath = (0, config_manager_1.getActiveConfigPath)();
    const rawConfig = readRawConfigObject(activePath);
    if (rawConfig?.proxy && typeof rawConfig.proxy === 'object') {
        return normalizeRoutingConfig(rawConfig.proxy.routing);
    }
    const legacyPath = (0, config_manager_1.getConfigPath)();
    if (legacyPath !== activePath) {
        const legacyConfig = readRawConfigObject(legacyPath);
        if (legacyConfig?.proxy && typeof legacyConfig.proxy === 'object') {
            return normalizeRoutingConfig(legacyConfig.proxy.routing);
        }
    }
    return {};
}
exports.loadOpenAICompatProxyRoutingConfig = loadOpenAICompatProxyRoutingConfig;
//# sourceMappingURL=routing-config.js.map