import { parseLenientJsonishWithNative } from '../../router/virtual-router/engine-selection/native-shared-conversion-semantics.js';
const registry = new Map();
function ensureEntry(id) {
    let entry = registry.get(id);
    if (!entry) {
        entry = {};
        registry.set(id, entry);
    }
    return entry;
}
function pruneEntry(id) {
    const entry = registry.get(id);
    if (!entry)
        return;
    if (!entry.reasoning && !entry.outputText && !entry.payloadSnapshot && !entry.passthroughPayload) {
        registry.delete(id);
    }
}
function cloneSnapshot(snapshot) {
    try {
        const structuredCloneImpl = globalThis.structuredClone;
        if (typeof structuredCloneImpl === 'function') {
            return structuredCloneImpl(snapshot);
        }
    }
    catch {
        /* ignore structuredClone failures */
    }
    try {
        const serialized = JSON.stringify(snapshot);
        const parsed = parseLenientJsonishWithNative(serialized);
        if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {
            return parsed;
        }
        return undefined;
    }
    catch {
        return undefined;
    }
}
export function registerResponsesReasoning(id, reasoning) {
    if (typeof id !== 'string')
        return;
    if (!reasoning)
        return;
    const summary = Array.isArray(reasoning.summary)
        ? reasoning.summary
            .map((item) => ({ type: 'summary_text', text: String(item.text ?? '').trim() }))
            .filter((item) => item.text.length > 0)
        : undefined;
    const content = Array.isArray(reasoning.content)
        ? reasoning.content
            .map((item) => ({
            type: item.type === 'text' ? 'text' : 'reasoning_text',
            text: String(item.text ?? '').trim()
        }))
            .filter((item) => item.text.length > 0)
        : undefined;
    const hasSummary = Array.isArray(summary) && summary.length > 0;
    const hasContent = Array.isArray(content) && content.length > 0;
    const hasEncrypted = reasoning.encrypted_content !== undefined;
    if (!hasSummary && !hasContent && !hasEncrypted)
        return;
    const entry = ensureEntry(id);
    entry.reasoning = {
        summary: hasSummary ? summary : undefined,
        content: hasContent ? content : undefined,
        encrypted_content: reasoning.encrypted_content
    };
}
export function consumeResponsesReasoning(id) {
    if (typeof id !== 'string')
        return undefined;
    const entry = registry.get(id);
    if (!entry?.reasoning)
        return undefined;
    const value = {
        summary: entry.reasoning.summary ? [...entry.reasoning.summary] : undefined,
        content: entry.reasoning.content ? [...entry.reasoning.content] : undefined,
        encrypted_content: entry.reasoning.encrypted_content
    };
    entry.reasoning = undefined;
    pruneEntry(id);
    return value;
}
export function registerResponsesOutputTextMeta(id, meta) {
    if (typeof id !== 'string')
        return;
    if (!meta)
        return;
    const entry = ensureEntry(id);
    entry.outputText = {
        hasField: Boolean(meta.hasField),
        value: typeof meta.value === 'string' ? meta.value : undefined,
        raw: typeof meta.raw === 'string' ? meta.raw : undefined
    };
}
export function consumeResponsesOutputTextMeta(id) {
    if (typeof id !== 'string')
        return undefined;
    const entry = registry.get(id);
    if (!entry?.outputText)
        return undefined;
    const value = {
        hasField: Boolean(entry.outputText.hasField),
        value: entry.outputText.value,
        raw: entry.outputText.raw
    };
    entry.outputText = undefined;
    pruneEntry(id);
    return value;
}
export function registerResponsesPayloadSnapshot(id, snapshot) {
    if (typeof id !== 'string')
        return;
    if (!snapshot || typeof snapshot !== 'object')
        return;
    const clone = cloneSnapshot(snapshot);
    if (!clone)
        return;
    const entry = ensureEntry(id);
    entry.payloadSnapshot = clone;
}
export function consumeResponsesPayloadSnapshot(id) {
    if (typeof id !== 'string')
        return undefined;
    const entry = registry.get(id);
    if (!entry?.payloadSnapshot)
        return undefined;
    const clone = cloneSnapshot(entry.payloadSnapshot) ?? entry.payloadSnapshot;
    entry.payloadSnapshot = undefined;
    pruneEntry(id);
    return clone;
}
export function registerResponsesPassthrough(id, payload) {
    if (typeof id !== 'string')
        return;
    if (!payload || typeof payload !== 'object')
        return;
    const clone = cloneSnapshot(payload);
    if (!clone)
        return;
    const entry = ensureEntry(id);
    entry.passthroughPayload = clone;
}
export function consumeResponsesPassthrough(id) {
    if (typeof id !== 'string')
        return undefined;
    const entry = registry.get(id);
    if (!entry?.passthroughPayload)
        return undefined;
    const clone = cloneSnapshot(entry.passthroughPayload) ?? entry.passthroughPayload;
    entry.passthroughPayload = undefined;
    pruneEntry(id);
    return clone;
}
