/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { strict as assert } from "node:assert";
import { endpointPosAndSide, } from "../index.js";
import { TestClientLogger, createClientsAtInitialState } from "./testClientLogger.js";
const ClientIds = ["A", "B", "C", "D"];
export class ReconnectTestHelper {
    idxFromName(name) {
        return (name.codePointAt(0) ?? 0) - ("A".codePointAt(0) ?? 0);
    }
    constructor(options = {}) {
        this.ops = [];
        this.seq = 0;
        this.clients = createClientsAtInitialState({
            initialState: "",
            options: {
                mergeTreeEnableObliterate: true,
                mergeTreeEnableObliterateReconnect: true,
                ...options,
            },
        }, ...ClientIds);
        this.logger = new TestClientLogger(this.clients.all);
        this.perClientOps = this.clients.all.map(() => []);
    }
    insertText(clientName, pos, text) {
        const client = this.clients[clientName];
        this.ops.push(client.makeOpMessage(client.insertTextLocal(pos, text), ++this.seq));
    }
    removeRange(clientName, start, end) {
        const client = this.clients[clientName];
        this.ops.push(client.makeOpMessage(client.removeRangeLocal(start, end), ++this.seq));
    }
    obliterateRange(clientName, start, end) {
        const client = this.clients[clientName];
        this.ops.push(client.makeOpMessage(
        // TODO: remove type assertions when sidedness is enabled
        client.obliterateRangeLocal(start, end), ++this.seq));
    }
    insertTextLocal(clientName, pos, text) {
        const client = this.clients[clientName];
        const op = client.insertTextLocal(pos, text);
        assert(op);
        const seg = client.peekPendingSegmentGroups();
        assert(seg);
        return { op, seg, refSeq: client.getCollabWindow().currentSeq };
    }
    removeRangeLocal(clientName, start, end) {
        const client = this.clients[clientName];
        const op = client.removeRangeLocal(start, end);
        assert(op);
        const seg = client.peekPendingSegmentGroups();
        assert(seg);
        return { op, seg, refSeq: client.getCollabWindow().currentSeq };
    }
    obliterateRangeLocal(clientName, start, end) {
        const client = this.clients[clientName];
        let { startPos, endPos } = endpointPosAndSide(start, end);
        assert(startPos !== undefined && endPos !== undefined, "start and end positions must be defined");
        startPos = startPos === "start" ? 0 : startPos;
        endPos = endPos === "end" ? client.getLength() : endPos;
        assert(startPos !== "end" && endPos !== "start", "start cannot be end and end cannot be start");
        const op = client.obliterateRangeLocal(startPos, endPos);
        assert(op);
        const seg = client.peekPendingSegmentGroups();
        assert(seg);
        return { op, seg, refSeq: client.getCollabWindow().currentSeq };
    }
    disconnect(clientNames) {
        const clientIdxs = new Set(clientNames.map((element) => this.idxFromName(element)));
        for (const op of this.ops.splice(0))
            for (const [i, c] of this.clients.all.entries()) {
                if (clientIdxs.has(i)) {
                    this.perClientOps[i].push(op);
                }
                else {
                    c.applyMsg(op);
                }
            }
    }
    processAllOps() {
        for (const op of this.ops.splice(0))
            for (const c of this.clients.all) {
                c.applyMsg(op);
            }
    }
    reconnect(clientNames) {
        const clientIdxs = new Set(clientNames.map((element) => this.idxFromName(element)));
        for (const [i, clientOps] of this.perClientOps.entries()) {
            if (clientIdxs.has(i)) {
                for (const op of clientOps.splice(0))
                    this.clients.all[i].applyMsg(op);
            }
        }
    }
    submitDisconnectedOp(clientName, op) {
        const client = this.clients[clientName];
        this.ops.push(client.makeOpMessage(client.regeneratePendingOp(op.op, op.seg), ++this.seq));
    }
}
//# sourceMappingURL=reconnectHelper.js.map