import { ProviderProtocolError } from '../provider-protocol-error.js';
import { runGeminiFromOpenAIChatCodecWithNative, runGeminiOpenAIRequestCodecWithNative, runGeminiOpenAIResponseCodecWithNative } from '../../router/virtual-router/engine-selection/native-compat-action-semantics.js';
function isJsonValue(value) {
    if (value === null ||
        typeof value === 'string' ||
        typeof value === 'number' ||
        typeof value === 'boolean') {
        return true;
    }
    if (Array.isArray(value)) {
        return value.every((entry) => isJsonValue(entry));
    }
    if (!value || typeof value !== 'object') {
        return false;
    }
    return Object.values(value).every((entry) => isJsonValue(entry));
}
function narrowJsonObject(value) {
    const out = {};
    for (const [key, entry] of Object.entries(value)) {
        if (isJsonValue(entry)) {
            out[key] = entry;
        }
    }
    return out;
}
function unwrapProviderProtocolError(result) {
    const raw = result.__providerProtocolError;
    if (!raw || typeof raw !== 'object' || Array.isArray(raw)) {
        return;
    }
    const error = raw;
    throw new ProviderProtocolError(String(error.message ?? 'Gemini provider protocol error'), {
        code: String(error.code ?? 'MALFORMED_RESPONSE'),
        protocol: typeof error.protocol === 'string' ? error.protocol : 'gemini-chat',
        providerType: typeof error.providerType === 'string' ? error.providerType : 'gemini',
        category: error.category === 'TOOL_ERROR' || error.category === 'INTERNAL_ERROR' || error.category === 'EXTERNAL_ERROR'
            ? error.category
            : undefined,
        details: error.details && typeof error.details === 'object' && !Array.isArray(error.details)
            ? error.details
            : undefined
    });
}
export function buildOpenAIChatFromGeminiRequest(payload) {
    const native = runGeminiOpenAIRequestCodecWithNative((payload ?? {}));
    const request = narrowJsonObject(native);
    return {
        ...request,
        messages: Array.isArray(native.messages) ? native.messages.filter((entry) => isJsonValue(entry)) : []
    };
}
export function buildOpenAIChatFromGeminiResponse(payload) {
    const result = runGeminiOpenAIResponseCodecWithNative((payload ?? {}));
    unwrapProviderProtocolError(result);
    return narrowJsonObject(result);
}
export function buildGeminiFromOpenAIChat(chatResp) {
    const result = runGeminiFromOpenAIChatCodecWithNative((chatResp ?? {}));
    return narrowJsonObject(result);
}
export class GeminiOpenAIConversionCodec {
    _dependencies;
    id = 'gemini-openai';
    initialized = false;
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    constructor(_dependencies) {
        this._dependencies = _dependencies;
    }
    async initialize() {
        this.initialized = true;
    }
    async ensureInit() {
        if (!this.initialized)
            await this.initialize();
    }
    async convertRequest(payload, _profile, _context) {
        await this.ensureInit();
        return buildOpenAIChatFromGeminiRequest(payload);
    }
    async convertResponse(payload, _profile, _context) {
        await this.ensureInit();
        return buildGeminiFromOpenAIChat(payload);
    }
}
