"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.clearProviderOverride = exports.parseThinkingOverrideInput = exports.parseThinkingCommandArgs = void 0;
const thinking_validator_1 = require("../cliproxy/thinking-validator");
function parseThinkingCommandArgs(args) {
    const options = {};
    const requireValue = (index) => {
        const value = args[index];
        if (!value || value.startsWith('-')) {
            return undefined;
        }
        return value;
    };
    for (let i = 0; i < args.length; i++) {
        const arg = args[i];
        if (arg === '--mode') {
            const value = requireValue(i + 1);
            if (!value)
                return { options, error: `${arg} requires a value` };
            options.mode = value;
            i += 1;
        }
        else if (arg === '--override') {
            const value = requireValue(i + 1);
            if (!value)
                return { options, error: `${arg} requires a value` };
            options.override = value;
            i += 1;
        }
        else if (arg === '--clear-override') {
            options.clearOverride = true;
        }
        else if (arg === '--tier') {
            const tier = requireValue(i + 1);
            const level = requireValue(i + 2);
            if (!tier || !level)
                return { options, error: `${arg} requires 2 values: <tier> <level>` };
            options.tier = { tier, level };
            i += 2;
        }
        else if (arg === '--provider-override') {
            const provider = requireValue(i + 1);
            const tier = requireValue(i + 2);
            const level = requireValue(i + 3);
            if (!provider || !tier || !level) {
                return { options, error: `${arg} requires 3 values: <provider> <tier> <level>` };
            }
            options.providerOverride = {
                provider,
                tier,
                level,
            };
            i += 3;
        }
        else if (arg === '--clear-provider-override') {
            const provider = requireValue(i + 1);
            if (!provider)
                return { options, error: `${arg} requires at least 1 value: <provider> [tier]` };
            const tier = requireValue(i + 2);
            options.clearProviderOverride = { provider, tier };
            i += tier ? 2 : 1;
        }
        else if (arg === '--help' || arg === '-h') {
            options.help = true;
        }
        else if (arg.startsWith('-')) {
            return { options, error: `Unknown option: ${arg}` };
        }
        else {
            return { options, error: `Unexpected argument: ${arg}` };
        }
    }
    return { options };
}
exports.parseThinkingCommandArgs = parseThinkingCommandArgs;
function parseThinkingOverrideInput(rawOverride) {
    const normalized = rawOverride.toLowerCase().trim();
    if ((0, thinking_validator_1.isThinkingOffValue)(normalized)) {
        return { value: 'off' };
    }
    if (thinking_validator_1.VALID_THINKING_LEVELS.includes(normalized)) {
        return { value: normalized };
    }
    if (/^\d+$/.test(normalized)) {
        const budget = Number.parseInt(normalized, 10);
        if (budget < thinking_validator_1.THINKING_BUDGET_MIN || budget > thinking_validator_1.THINKING_BUDGET_MAX) {
            return {
                error: `Invalid override: numeric budget must be between ${thinking_validator_1.THINKING_BUDGET_MIN} and ${thinking_validator_1.THINKING_BUDGET_MAX}`,
            };
        }
        return { value: budget };
    }
    return {
        error: `Invalid override: ${rawOverride}`,
    };
}
exports.parseThinkingOverrideInput = parseThinkingOverrideInput;
function clearProviderOverride(currentOverrides, provider, tier) {
    const current = currentOverrides ?? {};
    const nextOverrides = { ...current };
    const providerEntry = nextOverrides[provider];
    if (!providerEntry) {
        return {
            nextOverrides: Object.keys(nextOverrides).length > 0 ? nextOverrides : undefined,
            changed: false,
        };
    }
    if (!tier) {
        delete nextOverrides[provider];
        return {
            nextOverrides: Object.keys(nextOverrides).length > 0 ? nextOverrides : undefined,
            changed: true,
        };
    }
    if (providerEntry[tier] === undefined) {
        return {
            nextOverrides: Object.keys(nextOverrides).length > 0 ? nextOverrides : undefined,
            changed: false,
        };
    }
    const nextProviderEntry = { ...providerEntry };
    delete nextProviderEntry[tier];
    if (Object.keys(nextProviderEntry).length === 0) {
        delete nextOverrides[provider];
    }
    else {
        nextOverrides[provider] = nextProviderEntry;
    }
    return {
        nextOverrides: Object.keys(nextOverrides).length > 0 ? nextOverrides : undefined,
        changed: true,
    };
}
exports.clearProviderOverride = clearProviderOverride;
//# sourceMappingURL=config-thinking-parser.js.map