import { chatConverters, responsesConverters, anthropicConverters, geminiConverters } from '../index.js';
export class SseCodecRegistry {
    codecs = new Map();
    register(codec) {
        this.codecs.set(codec.protocol, codec);
    }
    get(protocol) {
        const codec = this.codecs.get(protocol);
        if (!codec) {
            throw new Error(`SSE codec for protocol "${protocol}" is not registered`);
        }
        return codec;
    }
    list() {
        return Array.from(this.codecs.values());
    }
}
export const defaultSseCodecRegistry = new SseCodecRegistry();
function resolveModelId(payload, fallback) {
    if (typeof fallback === 'string' && fallback.trim()) {
        return fallback;
    }
    if (!payload || typeof payload !== 'object') {
        return 'unknown';
    }
    const record = payload;
    if (typeof record.model === 'string') {
        return record.model;
    }
    if (record.modelVersion && typeof record.modelVersion === 'string') {
        return record.modelVersion;
    }
    if (record.response && typeof record.response === 'object') {
        const inner = record.response;
        if (typeof inner.model === 'string') {
            return inner.model;
        }
        if (typeof inner.modelVersion === 'string') {
            return inner.modelVersion;
        }
    }
    return 'unknown';
}
function createChatCodec() {
    return {
        protocol: 'openai-chat',
        async convertJsonToSse(payload, context) {
            const model = resolveModelId(payload, context.model);
            return chatConverters.jsonToSse
                .convertResponseToJsonToSse(payload, {
                requestId: context.requestId,
                model
            });
        },
        async convertSseToJson(stream, context) {
            return chatConverters.sseToJson.convertSseToJson(stream, {
                requestId: context.requestId,
                model: context.model ?? 'unknown'
            });
        },
        async normalize(stream) {
            return stream;
        }
    };
}
function createResponsesCodec() {
    return {
        protocol: 'openai-responses',
        async convertJsonToSse(payload, context) {
            const model = resolveModelId(payload, context.model);
            return responsesConverters.jsonToSse
                .convertResponseToJsonToSse(payload, {
                requestId: context.requestId,
                model,
                resumeToolOutputs: context.resumeToolOutputs
            });
        },
        async convertSseToJson(stream, context) {
            return responsesConverters.sseToJson.convertSseToJson(stream, {
                requestId: context.requestId,
                model: context.model ?? 'unknown'
            });
        },
        async normalize(stream) {
            return stream;
        }
    };
}
function createAnthropicCodec() {
    return {
        protocol: 'anthropic-messages',
        async convertJsonToSse(payload, context) {
            const model = resolveModelId(payload, context.model);
            return anthropicConverters.jsonToSse
                .convertResponseToJsonToSse(payload, {
                requestId: context.requestId,
                model
            });
        },
        async convertSseToJson(stream, context) {
            return anthropicConverters.sseToJson.convertSseToJson(stream, {
                requestId: context.requestId
            });
        },
        async normalize(stream) {
            return stream;
        }
    };
}
function createGeminiCodec() {
    return {
        protocol: 'gemini-chat',
        async convertJsonToSse(payload, context) {
            const model = resolveModelId(payload, context.model);
            return geminiConverters.jsonToSse
                .convertResponseToJsonToSse(payload, {
                requestId: context.requestId,
                model
            });
        },
        async convertSseToJson(stream, context) {
            return geminiConverters.sseToJson.convertSseToJson(stream, {
                requestId: context.requestId,
                model: context.model
            });
        },
        async normalize(stream) {
            return stream;
        }
    };
}
defaultSseCodecRegistry.register(createChatCodec());
defaultSseCodecRegistry.register(createResponsesCodec());
defaultSseCodecRegistry.register(createAnthropicCodec());
defaultSseCodecRegistry.register(createGeminiCodec());
