/*!
 * Copyright (c) 2026 Vertex Software LLC. All rights reserved.
 */
import { encode, Fixtures as StreamFixtures, WebSocketClientMock, } from "@vertexvis/stream-api";
import { Async } from "@vertexvis/utils";
import { ViewerStream } from "../lib/stream/stream";
import * as Fixtures from "./fixtures";
import { random } from "./random";
export const key1 = 'urn:vertex:stream-key:123';
export const key2 = 'urn:vertex:stream-key:234';
export function makeViewerStream() {
    const ws = new WebSocketClientMock();
    const stream = new ViewerStream(ws);
    return { stream, ws };
}
export async function loadViewerStreamKey(urn, { viewer, stream, ws }, { token = random.string({ alpha: true }), beforeConnected, } = {}) {
    jest.spyOn(stream, 'startStream').mockResolvedValue(StreamFixtures.Responses.startStream({
        result: {
            token: {
                token: token,
                expiresIn: new Date().getTime() + 10000,
            },
        },
    }).response);
    jest
        .spyOn(stream, 'syncTime')
        .mockResolvedValue(StreamFixtures.Responses.syncTime().response);
    const connecting = stream.stateChanged.onceWhen((s) => s.type === 'connecting');
    const loaded = viewer.load(urn);
    // Emit frame drawn on next event loop
    await connecting;
    await Async.delay(10);
    await (beforeConnected === null || beforeConnected === void 0 ? void 0 : beforeConnected());
    ws.receiveMessage(encode(StreamFixtures.Requests.drawFrame({
        payload: Fixtures.drawFramePayloadPerspective,
    })));
    await loaded;
}
export async function gracefulReconnect({ stream, ws }, { beforeReconnect } = {}) {
    jest
        .spyOn(stream, 'reconnect')
        .mockResolvedValue(StreamFixtures.Responses.reconnect().response);
    jest
        .spyOn(stream, 'syncTime')
        .mockResolvedValue(StreamFixtures.Responses.syncTime().response);
    const connecting = stream.stateChanged.onceWhen((s) => s.type === 'reconnecting');
    ws.receiveMessage(encode(StreamFixtures.Requests.gracefulReconnect()));
    const result = await (beforeReconnect === null || beforeReconnect === void 0 ? void 0 : beforeReconnect());
    await connecting;
    await Async.delay(10);
    ws.receiveMessage(encode(StreamFixtures.Requests.drawFrame({
        payload: Fixtures.drawFramePayloadPerspective,
    })));
    return result;
}
export function receiveFrame(ws, transformPayload) {
    ws.receiveMessage(encode(StreamFixtures.Requests.drawFrame({
        payload: transformPayload === null || transformPayload === void 0 ? void 0 : transformPayload(Fixtures.drawFramePayloadPerspective),
    })));
}
//# sourceMappingURL=viewer.js.map
