import { runOpenAIRequestCodecWithNative, runOpenAIResponseCodecWithNative } from '../../router/virtual-router/engine-selection/native-compat-action-semantics.js';
export class OpenAIOpenAIConversionCodec {
    dependencies;
    id = 'openai-openai';
    initialized = false;
    ctxMap = new Map();
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    constructor(dependencies) {
        this.dependencies = dependencies;
    }
    async initialize() {
        this.initialized = true;
    }
    async convertRequest(payload, _profile, context) {
        await this.ensureInit();
        const requestId = context.requestId ?? `req_${Date.now()}`;
        try {
            const wantsStream = !!((payload && typeof payload === 'object' && payload.stream === true) ||
                context?.stream === true ||
                (context?.metadata && context.metadata.stream === true));
            const entryEndpoint = String(context.entryEndpoint || context.endpoint || '');
            this.ctxMap.set(requestId, { stream: wantsStream, entryEndpoint });
        }
        catch {
            // ignore context capture failures
        }
        return runOpenAIRequestCodecWithNative((payload ?? {}), {
            preserveStreamField: true,
            requestId,
            entryEndpoint: context.entryEndpoint ?? context.endpoint,
            endpoint: context.endpoint,
            stream: context.stream,
            metadata: context.metadata ?? {}
        });
    }
    async convertResponse(payload, _profile, context) {
        await this.ensureInit();
        const ctx = context.requestId ? this.ctxMap.get(context.requestId) : undefined;
        if (context.requestId) {
            this.ctxMap.delete(context.requestId);
        }
        return runOpenAIResponseCodecWithNative((payload ?? {}), {
            stream: ctx?.stream === true,
            endpoint: context.entryEndpoint || context.endpoint || ctx?.entryEndpoint,
            requestId: context.requestId,
            reasoningMode: context.metadata?.reasoningMode,
            idPrefixBase: 'reasoning_choice'
        });
    }
    async ensureInit() {
        if (!this.initialized)
            await this.initialize();
    }
}
