function isRecord(value) {
    return typeof value === 'object' && value !== null && !Array.isArray(value);
}
function hasNonEmptyArray(value) {
    return Array.isArray(value) && value.length > 0;
}
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 root = structuredClone(payload);
        const model = typeof root.model === 'string' ? root.model.trim().toLowerCase() : '';
        if (!(model === 'kimi-k2.5' || model.startsWith('kimi-k2.5-'))) {
            return root;
        }
        if (isThinkingDisabled(root.thinking)) {
            return root;
        }
        const messages = Array.isArray(root.messages) ? root.messages : [];
        if (!messages.length) {
            return root;
        }
        for (const msg of messages) {
            if (!isRecord(msg))
                continue;
            const role = typeof msg.role === 'string' ? msg.role.trim().toLowerCase() : '';
            if (role !== 'assistant')
                continue;
            if (!hasNonEmptyArray(msg.tool_calls))
                continue;
            const rc = msg.reasoning_content;
            const rcText = typeof rc === 'string' ? rc : '';
            if (!rcText || !rcText.trim()) {
                msg.reasoning_content = '.';
            }
        }
        root.messages = messages;
        return root;
    }
    catch {
        return payload;
    }
}
