"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.revertMergeTreeDeltaRevertibles = exports.discardMergeTreeDeltaRevertible = exports.appendToMergeTreeDeltaRevertibles = void 0;
const common_utils_1 = require("@fluidframework/common-utils");
const container_utils_1 = require("@fluidframework/container-utils");
const collections_1 = require("./collections");
const endOfTreeSegment_1 = require("./endOfTreeSegment");
const localReference_1 = require("./localReference");
const mergeTreeNodes_1 = require("./mergeTreeNodes");
const mergeTreeNodeWalk_1 = require("./mergeTreeNodeWalk");
const mergeTreeTracking_1 = require("./mergeTreeTracking");
const ops_1 = require("./ops");
const properties_1 = require("./properties");
const referencePositions_1 = require("./referencePositions");
function appendLocalInsertToRevertibles(deltaArgs, revertibles) {
    var _a;
    if (((_a = revertibles[revertibles.length - 1]) === null || _a === void 0 ? void 0 : _a.operation) !== ops_1.MergeTreeDeltaType.INSERT) {
        revertibles.push({
            operation: ops_1.MergeTreeDeltaType.INSERT,
            trackingGroup: new mergeTreeTracking_1.TrackingGroup(),
        });
    }
    const last = revertibles[revertibles.length - 1];
    deltaArgs.deltaSegments.forEach((t) => last.trackingGroup.link(t.segment));
    return revertibles;
}
function appendLocalRemoveToRevertibles(driver, deltaArgs, revertibles) {
    var _a;
    if (((_a = revertibles[revertibles.length - 1]) === null || _a === void 0 ? void 0 : _a.operation) !== ops_1.MergeTreeDeltaType.REMOVE) {
        revertibles.push({
            operation: ops_1.MergeTreeDeltaType.REMOVE,
            trackingGroup: new mergeTreeTracking_1.TrackingGroup(),
        });
    }
    const last = revertibles[revertibles.length - 1];
    deltaArgs.deltaSegments.forEach((t) => {
        var _a, _b;
        const props = {
            segSpec: t.segment.toJSONObject(),
            referenceSpace: "mergeTreeDeltaRevertible",
        };
        const ref = driver.createLocalReferencePosition(t.segment, 0, ops_1.ReferenceType.SlideOnRemove, props);
        const internalDriver = driver;
        const driverRevertibleProps = (_a = internalDriver.__mergeTreeRevertible) !== null && _a !== void 0 ? _a : (internalDriver.__mergeTreeRevertible = {});
        ref.callbacks = (_b = driverRevertibleProps.refCallbacks) !== null && _b !== void 0 ? _b : (driverRevertibleProps.refCallbacks = {
            afterSlide: (r) => {
                var _a, _b;
                if (driver.localReferencePositionToPosition(r) === referencePositions_1.DetachedReferencePosition) {
                    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
                    const detached = (_a = driverRevertibleProps.detachedReferences) !== null && _a !== void 0 ? _a : (driverRevertibleProps.detachedReferences = new endOfTreeSegment_1.EndOfTreeSegment(r.getSegment()));
                    const refs = (_b = detached.localRefs) !== null && _b !== void 0 ? _b : (detached.localRefs = new localReference_1.LocalReferenceCollection(detached));
                    refs.addAfterTombstones([r]);
                }
            },
        });
        t.segment.trackingCollection.trackingGroups.forEach((tg) => {
            tg.link(ref);
            tg.unlink(t.segment);
        });
        last.trackingGroup.link(ref);
    });
    return revertibles;
}
function appendLocalAnnotateToRevertibles(deltaArgs, revertibles) {
    let last = revertibles[revertibles.length - 1];
    deltaArgs.deltaSegments.forEach((ds) => {
        const propertyDeltas = ds.propertyDeltas;
        if (propertyDeltas) {
            if ((last === null || last === void 0 ? void 0 : last.operation) === ops_1.MergeTreeDeltaType.ANNOTATE
                && (0, properties_1.matchProperties)(last === null || last === void 0 ? void 0 : last.propertyDeltas, propertyDeltas)) {
                last.trackingGroup.link(ds.segment);
            }
            else {
                last = {
                    operation: ops_1.MergeTreeDeltaType.ANNOTATE,
                    propertyDeltas,
                    trackingGroup: new mergeTreeTracking_1.TrackingGroup(),
                };
                last.trackingGroup.link(ds.segment);
                revertibles.push(last);
            }
        }
    });
    return revertibles;
}
/**
 * Revertibles are new and require the option
 * mergeTreeUseNewLengthCalculations to be set as true on the underlying merge tree
 * in order to function correctly.
 * @alpha
 */
function appendToMergeTreeDeltaRevertibles(driver, deltaArgs, revertibles) {
    switch (deltaArgs.operation) {
        case ops_1.MergeTreeDeltaType.INSERT:
            appendLocalInsertToRevertibles(deltaArgs, revertibles);
            break;
        case ops_1.MergeTreeDeltaType.REMOVE:
            appendLocalRemoveToRevertibles(driver, deltaArgs, revertibles);
            break;
        case ops_1.MergeTreeDeltaType.ANNOTATE:
            appendLocalAnnotateToRevertibles(deltaArgs, revertibles);
            break;
        default:
            throw new container_utils_1.UsageError(`Unsupported event delta type: ${deltaArgs.operation}`);
    }
}
exports.appendToMergeTreeDeltaRevertibles = appendToMergeTreeDeltaRevertibles;
/**
 * Revertibles are new and require the option
 * mergeTreeUseNewLengthCalculations to be set as true on the underlying merge tree
 * in order to function correctly.
 * @alpha
 */
function discardMergeTreeDeltaRevertible(revertibles) {
    revertibles.forEach((r) => {
        r.trackingGroup.tracked.forEach((t) => {
            var _a, _b;
            t.trackingCollection.unlink(r.trackingGroup);
            // remove untracked local references
            if (t.trackingCollection.empty && !t.isLeaf()) {
                (_b = (_a = t.getSegment()) === null || _a === void 0 ? void 0 : _a.localRefs) === null || _b === void 0 ? void 0 : _b.removeLocalRef(t);
            }
        });
    });
}
exports.discardMergeTreeDeltaRevertible = discardMergeTreeDeltaRevertible;
function revertLocalInsert(driver, revertible) {
    while (revertible.trackingGroup.size > 0) {
        const tracked = revertible.trackingGroup.tracked[0];
        (0, common_utils_1.assert)(tracked.trackingCollection.unlink(revertible.trackingGroup), 0x3f1 /* tracking group removed */);
        (0, common_utils_1.assert)(tracked.isLeaf(), 0x3f2 /* inserts must track segments */);
        if ((0, mergeTreeNodes_1.toRemovalInfo)(tracked) === undefined) {
            const start = driver.getPosition(tracked);
            driver.removeRange(start, start + tracked.cachedLength);
        }
    }
}
function revertLocalRemove(driver, revertible) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j;
    while (revertible.trackingGroup.size > 0) {
        const tracked = revertible.trackingGroup.tracked[0];
        (0, common_utils_1.assert)(tracked.trackingCollection.unlink(revertible.trackingGroup), 0x3f3 /* tracking group removed */);
        (0, common_utils_1.assert)(!tracked.isLeaf(), 0x3f4 /* removes must track local refs */);
        let realPos = driver.localReferencePositionToPosition(tracked);
        const refSeg = tracked.getSegment();
        // References which are on EndOfStringSegment don't return detached for pos,
        // they will return the length of the merge-tree. this case just catches
        // random references, likely not created in the revertible flow,
        // that are tying to be reverted for some reason.
        if (realPos === referencePositions_1.DetachedReferencePosition || refSeg === undefined) {
            throw new container_utils_1.UsageError("Cannot insert at detached references position");
        }
        if ((0, mergeTreeNodes_1.toRemovalInfo)(refSeg) === undefined
            && ((_a = refSeg.localRefs) === null || _a === void 0 ? void 0 : _a.isAfterTombstone(tracked))) {
            realPos++;
        }
        const props = tracked.properties;
        driver.insertFromSpec(realPos, props.segSpec);
        const insertSegment = driver.getContainingSegment(realPos).segment;
        (0, common_utils_1.assert)(insertSegment !== undefined, 0x3f5 /* insert segment must exist at position */);
        const localSlideFilter = (lref) => { var _a; return ((_a = lref.properties) === null || _a === void 0 ? void 0 : _a.referenceSpace) === "mergeTreeDeltaRevertible"; };
        const insertRef = {};
        const forward = insertSegment.ordinal < refSeg.ordinal;
        const refHandler = (lref) => {
            var _a, _b;
            // once we reach it keep the original reference where it is
            // we'll move tracking groups, and remove it as a last step.
            if (tracked === lref) {
                return false;
            }
            if (localSlideFilter(lref)) {
                if (forward) {
                    const before = (_a = insertRef.before) !== null && _a !== void 0 ? _a : (insertRef.before = new collections_1.List());
                    before.push(lref);
                }
                else {
                    const after = (_b = insertRef.after) !== null && _b !== void 0 ? _b : (insertRef.after = new collections_1.List());
                    after.unshift(lref);
                }
            }
        };
        (0, mergeTreeNodeWalk_1.depthFirstNodeWalk)(
        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
        insertSegment.parent, insertSegment, undefined, (seg) => {
            var _a;
            if (((_a = seg.localRefs) === null || _a === void 0 ? void 0 : _a.empty) === false) {
                return seg.localRefs.walkReferences(refHandler, undefined, forward);
            }
            return true;
        }, undefined, forward);
        const internalDriver = driver;
        if ((_d = (_c = (_b = internalDriver.__mergeTreeRevertible) === null || _b === void 0 ? void 0 : _b.detachedReferences) === null || _c === void 0 ? void 0 : _c.localRefs) === null || _d === void 0 ? void 0 : _d.has(tracked)) {
            (0, common_utils_1.assert)(forward, 0x3f6 /* forward should always be true when detached */);
            internalDriver.__mergeTreeRevertible.detachedReferences.localRefs.walkReferences(refHandler);
        }
        if (insertRef !== undefined) {
            const localRefs = (_e = insertSegment.localRefs) !== null && _e !== void 0 ? _e : (insertSegment.localRefs = new localReference_1.LocalReferenceCollection(insertSegment));
            if (((_f = insertRef.before) === null || _f === void 0 ? void 0 : _f.empty) === false) {
                localRefs.addBeforeTombstones(insertRef.before.map((n) => n.data));
            }
            if (((_g = insertRef.after) === null || _g === void 0 ? void 0 : _g.empty) === false) {
                localRefs.addAfterTombstones(insertRef.after.map((n) => n.data));
            }
        }
        tracked.trackingCollection.trackingGroups.forEach((tg) => {
            tg.link(insertSegment);
            tg.unlink(tracked);
        });
        (_j = (_h = tracked.getSegment()) === null || _h === void 0 ? void 0 : _h.localRefs) === null || _j === void 0 ? void 0 : _j.removeLocalRef(tracked);
    }
}
function revertLocalAnnotate(driver, revertible) {
    while (revertible.trackingGroup.size > 0) {
        const tracked = revertible.trackingGroup.tracked[0];
        const unlinked = tracked.trackingCollection.unlink(revertible.trackingGroup);
        (0, common_utils_1.assert)(unlinked && tracked.isLeaf(), 0x3f7 /* annotates must track segments */);
        if ((0, mergeTreeNodes_1.toRemovalInfo)(tracked) === undefined) {
            const start = driver.getPosition(tracked);
            driver.annotateRange(start, start + tracked.cachedLength, revertible.propertyDeltas);
        }
    }
}
/**
 * Revertibles are new and require the option
 * mergeTreeUseNewLengthCalculations to be set as true on the underlying merge tree
 * in order to function correctly.
 * @alpha
 */
function revertMergeTreeDeltaRevertibles(driver, revertibles) {
    while (revertibles.length > 0) {
        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
        const r = revertibles.pop();
        const operation = r.operation;
        switch (operation) {
            case ops_1.MergeTreeDeltaType.INSERT:
                revertLocalInsert(driver, r);
                break;
            case ops_1.MergeTreeDeltaType.REMOVE:
                revertLocalRemove(driver, r);
                break;
            case ops_1.MergeTreeDeltaType.ANNOTATE:
                revertLocalAnnotate(driver, r);
                break;
            default:
                (0, common_utils_1.unreachableCase)(operation);
        }
    }
}
exports.revertMergeTreeDeltaRevertibles = revertMergeTreeDeltaRevertibles;
//# sourceMappingURL=revertibles.js.map