"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.mockDeferStream = mockDeferStream;
exports.mockMultipartSubscriptionStream = mockMultipartSubscriptionStream;
const web_1 = require("node:stream/web");
const http_1 = require("@apollo/client/link/http");
const hasNextSymbol = Symbol("hasNext");
function mockIncrementalStream({ responseHeaders, }) {
    const CLOSE = Symbol();
    let streamController = null;
    let sentInitialChunk = false;
    const queue = [];
    function processQueue() {
        if (!streamController) {
            throw new Error("Cannot process queue without stream controller");
        }
        let chunk;
        while ((chunk = queue.shift())) {
            if (chunk === CLOSE) {
                streamController.close();
            }
            else {
                streamController.enqueue(chunk);
            }
        }
    }
    function createStream() {
        return new web_1.ReadableStream({
            start(c) {
                streamController = c;
                processQueue();
            },
        })
            .pipeThrough(new web_1.TransformStream({
            transform: (chunk, controller) => {
                controller.enqueue((!sentInitialChunk ? "\r\n---\r\n" : "") +
                    "content-type: application/json; charset=utf-8\r\n\r\n" +
                    JSON.stringify(chunk) +
                    (chunk[hasNextSymbol] ? "\r\n---\r\n" : "\r\n-----\r\n"));
                sentInitialChunk = true;
            },
        }))
            .pipeThrough(new web_1.TextEncoderStream());
    }
    const httpLink = new http_1.HttpLink({
        fetch(input, init) {
            return Promise.resolve(new Response(createStream(), {
                status: 200,
                headers: responseHeaders,
            }));
        },
    });
    function queueNext(event) {
        queue.push(event);
        if (streamController) {
            processQueue();
        }
    }
    function close() {
        queueNext(CLOSE);
        streamController = null;
        sentInitialChunk = false;
    }
    function enqueue(chunk, hasNext) {
        queueNext({ ...chunk, [hasNextSymbol]: hasNext });
        if (!hasNext) {
            close();
        }
    }
    return {
        httpLink,
        enqueue,
        close,
    };
}
function mockDeferStream() {
    const { httpLink, enqueue } = mockIncrementalStream({
        responseHeaders: new Headers({
            "Content-Type": 'multipart/mixed; boundary="-"; deferSpec=20220824',
        }),
    });
    return {
        httpLink,
        enqueueInitialChunk(chunk) {
            enqueue(chunk, chunk.hasNext);
        },
        enqueueSubsequentChunk(chunk) {
            enqueue(chunk, chunk.hasNext);
        },
        enqueueErrorChunk(errors) {
            enqueue({
                hasNext: true,
                incremental: [
                    {
                        // eslint-disable-next-line @typescript-eslint/no-restricted-types
                        errors: errors,
                    },
                ],
            }, true);
        },
    };
}
function mockMultipartSubscriptionStream() {
    const { httpLink, enqueue } = mockIncrementalStream({
        responseHeaders: new Headers({
            "Content-Type": "multipart/mixed",
        }),
    });
    enqueueHeartbeat();
    function enqueueHeartbeat() {
        enqueue({}, true);
    }
    return {
        httpLink,
        enqueueHeartbeat,
        enqueuePayloadResult(payload, hasNext = true) {
            enqueue({ payload }, hasNext);
        },
        enqueueProtocolErrors(errors) {
            enqueue({ payload: null, errors }, false);
        },
    };
}
//# sourceMappingURL=incremental.cjs.map
