import { normalizeReasoningInAnthropicPayloadWithNative, normalizeReasoningInChatPayloadWithNative, normalizeReasoningInGeminiPayloadWithNative, normalizeReasoningInOpenAIPayloadWithNative, normalizeReasoningInResponsesPayloadWithNative } from '../../router/virtual-router/engine-selection/native-shared-conversion-semantics.js';
export const RESPONSES_INSTRUCTIONS_REASONING_FIELD = '__rcc_reasoning_instructions';
const REASONING_TEXT_MARKERS = [
    '<think',
    '</think',
    '<reflection',
    '</reflection',
    '```think',
    '```reflection'
];
function stringHasReasoningMarker(value) {
    const lower = value.toLowerCase();
    return REASONING_TEXT_MARKERS.some((marker) => lower.includes(marker));
}
function valueMayContainReasoningMarkup(value) {
    if (typeof value === 'string') {
        return stringHasReasoningMarker(value);
    }
    if (!value || typeof value !== 'object') {
        return false;
    }
    if (Array.isArray(value)) {
        for (const entry of value) {
            if (valueMayContainReasoningMarkup(entry)) {
                return true;
            }
        }
        return false;
    }
    const record = value;
    for (const entry of Object.values(record)) {
        if (valueMayContainReasoningMarkup(entry)) {
            return true;
        }
    }
    return false;
}
function assertReasoningNormalizerNativeAvailable() {
    if (typeof normalizeReasoningInChatPayloadWithNative !== 'function' ||
        typeof normalizeReasoningInResponsesPayloadWithNative !== 'function' ||
        typeof normalizeReasoningInGeminiPayloadWithNative !== 'function' ||
        typeof normalizeReasoningInAnthropicPayloadWithNative !== 'function' ||
        typeof normalizeReasoningInOpenAIPayloadWithNative !== 'function') {
        throw new Error('[reasoning-normalizer] native bindings unavailable');
    }
}
export function normalizeReasoningInChatPayload(payload) {
    assertReasoningNormalizerNativeAvailable();
    if (!payload)
        return;
    const normalized = normalizeReasoningInChatPayloadWithNative(payload);
    if (normalized && typeof normalized === 'object') {
        Object.assign(payload, normalized);
    }
}
export function normalizeReasoningInResponsesPayload(payload, options = { includeOutput: true }) {
    assertReasoningNormalizerNativeAvailable();
    if (!payload)
        return;
    const shouldNormalize = (options.includeInput === true && valueMayContainReasoningMarkup(payload.input)) ||
        (options.includeOutput === true && valueMayContainReasoningMarkup(payload.output)) ||
        (options.includeInstructions === true && valueMayContainReasoningMarkup(payload.instructions)) ||
        (options.includeRequiredAction === true && valueMayContainReasoningMarkup(payload.required_action));
    if (!shouldNormalize) {
        return;
    }
    const normalized = normalizeReasoningInResponsesPayloadWithNative(payload, options);
    if (normalized && typeof normalized === 'object') {
        Object.assign(payload, normalized);
    }
}
export function normalizeReasoningInGeminiPayload(payload) {
    assertReasoningNormalizerNativeAvailable();
    if (!payload)
        return;
    const normalized = normalizeReasoningInGeminiPayloadWithNative(payload);
    if (normalized && typeof normalized === 'object') {
        Object.assign(payload, normalized);
    }
}
export function normalizeReasoningInAnthropicPayload(payload) {
    assertReasoningNormalizerNativeAvailable();
    if (!payload)
        return;
    const normalized = normalizeReasoningInAnthropicPayloadWithNative(payload);
    if (normalized && typeof normalized === 'object') {
        Object.assign(payload, normalized);
    }
}
export function normalizeReasoningInOpenAIPayload(payload) {
    assertReasoningNormalizerNativeAvailable();
    if (!payload)
        return;
    const normalized = normalizeReasoningInOpenAIPayloadWithNative(payload);
    if (normalized && typeof normalized === 'object') {
        Object.assign(payload, normalized);
    }
}
