"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.normalizeMcpCallIds = normalizeMcpCallIds;
const tslib_1 = require("tslib");
function readEventType(e) {
    const top = e.type;
    if (top)
        return top;
    const data = e.data;
    const fromData = data === null || data === void 0 ? void 0 : data.type;
    if (fromData)
        return fromData;
    return e.event;
}
function readEventItem(e) {
    const top = e.item;
    if (top)
        return top;
    const data = e.data;
    return data === null || data === void 0 ? void 0 : data.item;
}
function readEventOutputIndex(e) {
    const top = e.output_index;
    if (typeof top === 'number')
        return top;
    const data = e.data;
    const fromData = data === null || data === void 0 ? void 0 : data.output_index;
    return typeof fromData === 'number' ? fromData : undefined;
}
function rewriteItemIdInEvent(event, normalizedId) {
    const e = event;
    const next = Object.assign({}, e);
    const topItem = e.item;
    if (topItem) {
        next.item = Object.assign(Object.assign({}, topItem), { id: normalizedId });
    }
    const data = e.data;
    if (data) {
        const dataItem = data.item;
        const nextData = Object.assign({}, data);
        if (dataItem) {
            nextData.item = Object.assign(Object.assign({}, dataItem), { id: normalizedId });
        }
        next.data = nextData;
    }
    return next;
}
/**
 * Workaround for a Yandex Responses-compatible backend that assigns an mcp_call item
 * a temporary id (prefixed `000_…`) during streaming, then swaps it for a different
 * permanent id in `response.output_item.done`. Without this rewrite, the downstream
 * tool-card lookup misses the existing part and renders a duplicate card. The
 * `output_index` field stays stable across the item's lifecycle, so it serves as the
 * canonical identity used to remap the done event's id back to the first-seen one.
 */
function normalizeMcpCallIds(source) {
    return tslib_1.__asyncGenerator(this, arguments, function* normalizeMcpCallIds_1() {
        var _a, e_1, _b, _c;
        const outputIndexToMcpCallId = new Map();
        try {
            for (var _d = true, source_1 = tslib_1.__asyncValues(source), source_1_1; source_1_1 = yield tslib_1.__await(source_1.next()), _a = source_1_1.done, !_a; _d = true) {
                _c = source_1_1.value;
                _d = false;
                const event = _c;
                const e = event;
                const type = readEventType(e);
                const item = readEventItem(e);
                const outputIndex = readEventOutputIndex(e);
                if (type === 'response.output_item.added' &&
                    (item === null || item === void 0 ? void 0 : item.type) === 'mcp_call' &&
                    typeof item.id === 'string' &&
                    typeof outputIndex === 'number') {
                    outputIndexToMcpCallId.set(outputIndex, item.id);
                    yield yield tslib_1.__await(event);
                    continue;
                }
                if (type === 'response.output_item.done' &&
                    (item === null || item === void 0 ? void 0 : item.type) === 'mcp_call' &&
                    typeof outputIndex === 'number' &&
                    outputIndexToMcpCallId.has(outputIndex)) {
                    const originalId = outputIndexToMcpCallId.get(outputIndex);
                    if (typeof item.id === 'string' && item.id !== originalId) {
                        yield yield tslib_1.__await(rewriteItemIdInEvent(event, originalId));
                        continue;
                    }
                }
                yield yield tslib_1.__await(event);
            }
        }
        catch (e_1_1) { e_1 = { error: e_1_1 }; }
        finally {
            try {
                if (!_d && !_a && (_b = source_1.return)) yield tslib_1.__await(_b.call(source_1));
            }
            finally { if (e_1) throw e_1.error; }
        }
    });
}
//# sourceMappingURL=normalizeMcpCallIds.js.map
