"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createReadableStream = void 0;
const tslib_1 = require("tslib");
const uuid_1 = require("uuid");
const openai_response_chunks_json_1 = tslib_1.__importDefault(require("./__mocks__/openai-response-chunks.json"));
const createStreamData = () => {
    const idMatchMap = {};
    return openai_response_chunks_json_1.default.map((chunk) => {
        if (chunk.response && chunk.response.id) {
            if (!idMatchMap[chunk.response.id]) {
                const newRespId = `resp-${(0, uuid_1.v4)()}`;
                idMatchMap[chunk.response.id] = newRespId;
            }
            return Object.assign(Object.assign({}, chunk), { response: Object.assign(Object.assign({}, chunk.response), { id: idMatchMap[chunk.response.id] }) });
        }
        if (chunk.item) {
            if (!idMatchMap[chunk.item.id]) {
                const prefix = chunk.item.id.startsWith('tool') ? 'tool-cc' : 'msg-cc';
                const newItemId = `${prefix}-${(0, uuid_1.v4)()}`;
                idMatchMap[chunk.item.id] = newItemId;
            }
            return Object.assign(Object.assign({}, chunk), { item: Object.assign(Object.assign({}, chunk.item), { id: idMatchMap[chunk.item.id] }) });
        }
        if (chunk.item_id) {
            if (!idMatchMap[chunk.item_id]) {
                const prefix = chunk.item_id.startsWith('tool') ? 'tool-cc' : 'msg-cc';
                const newItemId = `${prefix}-${(0, uuid_1.v4)()}`;
                idMatchMap[chunk.item_id] = newItemId;
            }
            return Object.assign(Object.assign({}, chunk), { item_id: idMatchMap[chunk.item_id] });
        }
        throw new Error('unknown chunk type');
    });
};
const createReadableStream = () => {
    const readableStream = new ReadableStream({
        async start(controller) {
            const streamData = createStreamData();
            for (const chunk of streamData) {
                const dataString = JSON.stringify(chunk).trim();
                controller.enqueue(new TextEncoder().encode(`data: ${dataString}\n`));
                await new Promise((r) => setTimeout(r, 10));
            }
            controller.close();
        },
    });
    return readableStream;
};
exports.createReadableStream = createReadableStream;
//# sourceMappingURL=createMockOpenAIStream.js.map
