"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildThinkingStartupStatus = exports.shouldDisableCodexReasoning = exports.resolveRuntimeThinkingOverride = exports.parseEnvThinkingOverride = void 0;
const thinking_validator_1 = require("../thinking-validator");
/**
 * Parse CCS_THINKING env value using same rules as CLI parsing:
 * integer string => number, known level/off aliases => normalized string.
 * Unknown/invalid values are ignored to preserve config fallback behavior.
 */
function parseEnvThinkingOverride(raw) {
    if (raw === undefined)
        return undefined;
    const trimmed = raw.trim();
    if (!trimmed)
        return undefined;
    if (/^\d+$/.test(trimmed)) {
        const parsed = Number.parseInt(trimmed, 10);
        if (parsed < thinking_validator_1.THINKING_BUDGET_MIN || parsed > thinking_validator_1.THINKING_BUDGET_MAX) {
            return undefined;
        }
        return parsed;
    }
    const normalized = trimmed.toLowerCase();
    if ((0, thinking_validator_1.isThinkingOffValue)(normalized)) {
        return 'off';
    }
    if (thinking_validator_1.VALID_THINKING_LEVELS.includes(normalized)) {
        return normalized;
    }
    return undefined;
}
exports.parseEnvThinkingOverride = parseEnvThinkingOverride;
/**
 * Runtime precedence: CLI flag > CCS_THINKING env var.
 * Config is handled later during model/env resolution.
 */
function resolveRuntimeThinkingOverride(flagOverride, envValue) {
    if (flagOverride !== undefined) {
        return { thinkingOverride: flagOverride, thinkingSource: 'flag' };
    }
    const envOverride = parseEnvThinkingOverride(envValue);
    if (envOverride !== undefined) {
        return { thinkingOverride: envOverride, thinkingSource: 'env' };
    }
    return { thinkingOverride: undefined, thinkingSource: undefined };
}
exports.resolveRuntimeThinkingOverride = resolveRuntimeThinkingOverride;
/**
 * Effective off logic for codex reasoning proxy wiring.
 */
function shouldDisableCodexReasoning(thinkingConfig, thinkingOverride) {
    return ((thinkingConfig.mode === 'off' && thinkingOverride === undefined) ||
        (0, thinking_validator_1.isThinkingOffValue)(thinkingOverride) ||
        (thinkingOverride === undefined &&
            thinkingConfig.mode === 'manual' &&
            (0, thinking_validator_1.isThinkingOffValue)(thinkingConfig.override)));
}
exports.shouldDisableCodexReasoning = shouldDisableCodexReasoning;
/**
 * Build user-facing startup feedback label/source based on effective precedence.
 */
function buildThinkingStartupStatus(thinkingConfig, thinkingOverride, thinkingSource, sourceDisplay) {
    const overrideDisablesThinking = (0, thinking_validator_1.isThinkingOffValue)(thinkingOverride);
    const configDisablesThinking = thinkingOverride === undefined &&
        (thinkingConfig.mode === 'off' ||
            (thinkingConfig.mode === 'manual' && (0, thinking_validator_1.isThinkingOffValue)(thinkingConfig.override)));
    if (overrideDisablesThinking || configDisablesThinking) {
        if (thinkingSource === 'flag') {
            return {
                thinkingLabel: 'off',
                sourceLabel: `flag: ${sourceDisplay ?? '--thinking off'}`,
            };
        }
        if (thinkingSource === 'env') {
            return {
                thinkingLabel: 'off',
                sourceLabel: 'env: CCS_THINKING',
            };
        }
        return {
            thinkingLabel: 'off',
            sourceLabel: thinkingConfig.mode === 'manual' ? 'config: manual' : 'config: off',
        };
    }
    if (thinkingSource === 'flag') {
        return {
            thinkingLabel: String(thinkingOverride),
            sourceLabel: `flag: ${sourceDisplay ?? '--thinking'}`,
        };
    }
    if (thinkingSource === 'env') {
        return {
            thinkingLabel: String(thinkingOverride),
            sourceLabel: 'env: CCS_THINKING',
        };
    }
    if (thinkingConfig.mode === 'manual' && thinkingConfig.override !== undefined) {
        return {
            thinkingLabel: String(thinkingConfig.override),
            sourceLabel: 'config: manual',
        };
    }
    return {
        thinkingLabel: thinkingConfig.mode === 'auto' ? 'auto' : 'default',
        sourceLabel: 'config: auto',
    };
}
exports.buildThinkingStartupStatus = buildThinkingStartupStatus;
//# sourceMappingURL=thinking-override-resolver.js.map