function isRecord(value) {
    return typeof value === 'object' && value !== null && !Array.isArray(value);
}
function normalizeModel(value) {
    return typeof value === 'string' ? value.trim().toLowerCase() : '';
}
function isKimiK25Model(value) {
    const model = normalizeModel(value);
    return model === 'kimi-k2.5' || model.startsWith('kimi-k2.5-');
}
function mirrorMaxTokens(record) {
    const maxTokens = record.max_tokens;
    if (typeof maxTokens === 'number' && Number.isFinite(maxTokens) && maxTokens > 0) {
        record.max_new_tokens = Math.floor(maxTokens);
    }
}
function isThinkingExplicitlyDisabled(value) {
    if (value === false) {
        return true;
    }
    if (!isRecord(value)) {
        return false;
    }
    const enabled = value.enabled;
    if (enabled === false) {
        return true;
    }
    const type = typeof value.type === 'string' ? value.type.trim().toLowerCase() : '';
    return type === 'disabled' || type === 'off';
}
function normalizeThinkingForKimi(record) {
    const current = record.thinking;
    if (current === undefined || current === null) {
        record.thinking = { type: 'enabled' };
        return true;
    }
    if (current === true) {
        record.thinking = { type: 'enabled' };
        return true;
    }
    if (isThinkingExplicitlyDisabled(current)) {
        return false;
    }
    if (isRecord(current)) {
        if (typeof current.type !== 'string' || current.type.trim().length === 0) {
            current.type = 'enabled';
        }
    }
    return true;
}
/**
 * iFlow/Kimi request defaults aligned with iflow-cli:
 * - thinking enabled path: temperature=1
 * - thinking disabled path: temperature=0.6
 * - shared defaults: top_p=0.95, n=1, penalties=0, max_new_tokens=max_tokens
 *
 * RouteCodex keeps thinking enabled by default for kimi-k2.5 unless explicitly disabled.
 */
export function applyIflowKimiCliDefaults(payload) {
    try {
        if (!isRecord(payload)) {
            return payload;
        }
        const root = structuredClone(payload);
        if (!isKimiK25Model(root.model)) {
            return root;
        }
        const thinkingEnabled = normalizeThinkingForKimi(root);
        root.temperature = thinkingEnabled ? 1 : 0.6;
        root.top_p = 0.95;
        root.n = 1;
        root.presence_penalty = 0;
        root.frequency_penalty = 0;
        mirrorMaxTokens(root);
        return root;
    }
    catch {
        return payload;
    }
}
