"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
const node_assert_1 = require("node:assert");
const ops_js_1 = require("../ops.js");
const clientTestHelper_js_1 = require("./clientTestHelper.js");
const testUtils_js_1 = require("./testUtils.js");
for (const mergeTreeEnableSidedObliterate of [true, false]) {
    describe(`obliterate delta callback enableSidedObliterate = ${mergeTreeEnableSidedObliterate}`, () => {
        (0, testUtils_js_1.useStrictPartialLengthChecks)();
        let length;
        let cb;
        beforeEach(() => {
            length = 0;
            cb = (opArgs, deltaArgs) => {
                switch (opArgs.op.type) {
                    case ops_js_1.MergeTreeDeltaType.INSERT: {
                        for (const { segment } of deltaArgs.deltaSegments) {
                            length += segment.cachedLength;
                        }
                        break;
                    }
                    case ops_js_1.MergeTreeDeltaType.REMOVE:
                    case ops_js_1.MergeTreeDeltaType.OBLITERATE_SIDED:
                    case ops_js_1.MergeTreeDeltaType.OBLITERATE: {
                        for (const { segment } of deltaArgs.deltaSegments) {
                            length -= segment.cachedLength;
                        }
                        break;
                    }
                    default:
                }
            };
        });
        describe("is invoked", () => {
            it("on local obliterate", () => {
                const helper = new clientTestHelper_js_1.ClientTestHelper({ mergeTreeEnableSidedObliterate });
                let count = 0;
                helper.clients.A.on("delta", (opArgs, deltaArgs) => {
                    if (opArgs.op.type === ops_js_1.MergeTreeDeltaType.OBLITERATE ||
                        opArgs.op.type === ops_js_1.MergeTreeDeltaType.OBLITERATE_SIDED) {
                        count += 1;
                    }
                });
                helper.insertText("A", 0, "a");
                node_assert_1.strict.equal(count, 0);
                helper.obliterateRange("A", 0, 1);
                node_assert_1.strict.equal(count, 1);
                helper.processAllOps();
                node_assert_1.strict.equal(count, 1);
                node_assert_1.strict.equal(helper.clients.A.getText(), "");
                helper.logger.validate();
            });
            it("on remote obliterate", () => {
                const helper = new clientTestHelper_js_1.ClientTestHelper({ mergeTreeEnableSidedObliterate });
                let count = 0;
                helper.clients.A.on("delta", (opArgs, deltaArgs) => {
                    if (opArgs.op.type === ops_js_1.MergeTreeDeltaType.OBLITERATE ||
                        opArgs.op.type === ops_js_1.MergeTreeDeltaType.OBLITERATE_SIDED) {
                        count += 1;
                    }
                });
                helper.insertText("B", 0, "a");
                node_assert_1.strict.equal(count, 0);
                helper.obliterateRange("B", 0, 1);
                node_assert_1.strict.equal(count, 0);
                helper.processAllOps();
                node_assert_1.strict.equal(count, 1);
                node_assert_1.strict.equal(helper.clients.A.getText(), "");
                helper.logger.validate();
            });
        });
        describe("overlapping obliterate and remove", () => {
            const text = "abcdef";
            it("remove first", () => {
                const helper = new clientTestHelper_js_1.ClientTestHelper({ mergeTreeEnableSidedObliterate });
                helper.clients.A.on("delta", cb);
                helper.insertText("A", 0, text);
                helper.processAllOps();
                node_assert_1.strict.equal(length, text.length);
                helper.removeRange("B", 0, text.length);
                helper.obliterateRange("C", 0, text.length);
                helper.processAllOps();
                node_assert_1.strict.equal(length, 0);
                helper.logger.validate();
                helper.clients.A.off("delta", cb);
            });
            it("obliterate first", () => {
                const helper = new clientTestHelper_js_1.ClientTestHelper({ mergeTreeEnableSidedObliterate });
                helper.clients.A.on("delta", cb);
                helper.insertText("B", 0, text);
                helper.processAllOps();
                node_assert_1.strict.equal(length, text.length);
                helper.obliterateRange("C", 0, text.length);
                helper.removeRange("B", 0, text.length);
                helper.processAllOps();
                node_assert_1.strict.equal(length, 0);
                helper.logger.validate();
                helper.clients.A.off("delta", cb);
            });
        });
        it("overlapping obliterate and obliterate", () => {
            const helper = new clientTestHelper_js_1.ClientTestHelper({ mergeTreeEnableSidedObliterate });
            helper.clients.A.on("delta", cb);
            const text = "abcdef";
            helper.insertText("B", 0, text);
            helper.processAllOps();
            node_assert_1.strict.equal(length, text.length);
            helper.obliterateRange("B", 0, text.length);
            helper.obliterateRange("C", 0, text.length);
            helper.processAllOps();
            node_assert_1.strict.equal(length, 0);
            helper.logger.validate();
            helper.clients.A.off("delta", cb);
        });
        describe("insert into obliterated range", () => {
            const text = "abcdef";
            it("insert first", () => {
                const helper = new clientTestHelper_js_1.ClientTestHelper({ mergeTreeEnableSidedObliterate });
                helper.clients.A.on("delta", cb);
                helper.insertText("B", 0, text);
                helper.processAllOps();
                node_assert_1.strict.equal(length, text.length);
                helper.insertText("B", 3, text);
                helper.obliterateRange("C", 0, text.length);
                helper.processAllOps();
                node_assert_1.strict.equal(length, 0);
                helper.logger.validate();
                helper.clients.A.off("delta", cb);
            });
            it("obliterate first", () => {
                const helper = new clientTestHelper_js_1.ClientTestHelper({ mergeTreeEnableSidedObliterate });
                helper.clients.A.on("delta", cb);
                helper.insertText("B", 0, text);
                helper.processAllOps();
                node_assert_1.strict.equal(length, text.length);
                helper.obliterateRange("C", 0, text.length);
                helper.insertText("B", 3, text);
                helper.processAllOps();
                node_assert_1.strict.equal(length, 0);
                helper.logger.validate();
                helper.clients.A.off("delta", cb);
            });
        });
    });
}
//# sourceMappingURL=obliterate.deltaCallback.spec.js.map