import { runReqOutboundStage3CompatWithNative } from '../../../router/virtual-router/engine-selection/native-hub-pipeline-req-outbound-semantics.js';
import { buildIflowRequestCompatInput } from './iflow-native-compat.js';
function isRecord(value) {
    return typeof value === 'object' && value !== null && !Array.isArray(value);
}
function isThinkingDisabled(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' ? String(value.type).trim().toLowerCase() : '';
    if (type === 'disabled' || type === 'off')
        return true;
    return false;
}
/**
 * iFlow/Kimi compat:
 * When thinking is active, iFlow requires reasoning_content to exist (and be non-empty)
 * on assistant tool-call messages. Some tool-call surfaces omit it, causing 400 validation errors.
 *
 * We only apply this for kimi-k2.5 to avoid affecting other models.
 *
 * Note: responses→chat bridging may omit top-level thinking while upstream Kimi still
 * enforces this contract. So for kimi-k2.5 we inject unless thinking is explicitly disabled.
 */
export function fillIflowKimiThinkingReasoningContent(payload) {
    try {
        if (!payload || typeof payload !== 'object' || Array.isArray(payload)) {
            return payload;
        }
        const model = typeof payload.model === 'string' ? payload.model.trim().toLowerCase() : '';
        if (!(model === 'kimi-k2.5' || model.startsWith('kimi-k2.5-'))) {
            return payload;
        }
        if (isThinkingDisabled(payload.thinking)) {
            return payload;
        }
        return runReqOutboundStage3CompatWithNative(buildIflowRequestCompatInput(payload)).payload;
    }
    catch {
        return payload;
    }
}
