import { buildOpenAIChatFromGeminiResponse } from '../../codecs/gemini-openai-codec.js';
import { buildChatResponseFromResponses } from '../../shared/responses-response-utils.js';
import { buildOpenAIChatFromAnthropicMessage } from './response-runtime.js';
import { resolveAliasMapFromRespSemanticsWithNative } from '../../../router/virtual-router/engine-selection/native-hub-pipeline-resp-semantics.js';
function injectResponsesReasoningExtension(payload) {
    if (payload.__responses_reasoning) {
        return;
    }
    const choices = Array.isArray(payload.choices) ? payload.choices : [];
    const primary = choices[0] && typeof choices[0] === 'object' ? choices[0] : undefined;
    const message = primary && typeof primary.message === 'object' ? primary.message : undefined;
    if (!message)
        return;
    const reasoningText = typeof message.reasoning_content === 'string' && message.reasoning_content.trim().length
        ? String(message.reasoning_content).trim()
        : undefined;
    if (!reasoningText)
        return;
    payload.__responses_reasoning = {
        content: [{ type: 'reasoning_text', text: reasoningText }]
    };
}
export class OpenAIChatResponseMapper {
    toChatCompletion(format, _ctx) {
        const payload = (format.payload ?? {});
        if (payload && typeof payload === 'object') {
            injectResponsesReasoningExtension(payload);
        }
        return payload;
    }
}
export class ResponsesResponseMapper {
    toChatCompletion(format, _ctx) {
        return buildChatResponseFromResponses(format.payload ?? {});
    }
}
export class AnthropicResponseMapper {
    toChatCompletion(format, _ctx, options) {
        const aliasMap = resolveAliasMapFromRespSemanticsWithNative(options?.requestSemantics);
        return buildOpenAIChatFromAnthropicMessage(format.payload ?? {}, { aliasMap });
    }
}
export class GeminiResponseMapper {
    toChatCompletion(format, _ctx) {
        return buildOpenAIChatFromGeminiResponse(format.payload ?? {});
    }
}
