/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
/* eslint-disable @typescript-eslint/no-non-null-assertion */
/* eslint-disable no-bitwise */
import { assert, Heap } from "@fluidframework/core-utils/internal";
import { DataProcessingError, UsageError } from "@fluidframework/telemetry-utils/internal";
import { DoublyLinkedList } from "./collections/index.js";
import { NonCollabClient, TreeMaintenanceSequenceNumber, UnassignedSequenceNumber, UniversalSequenceNumber, } from "./constants.js";
import { EndOfTreeSegment, StartOfTreeSegment } from "./endOfTreeSegment.js";
import { LocalReferenceCollection, SlidingPreference, anyLocalReferencePosition, createDetachedLocalReferencePosition, filterLocalReferencePositions, } from "./localReference.js";
import { MergeTreeMaintenanceType, } from "./mergeTreeDeltaCallback.js";
import { NodeAction, backwardExcursion, depthFirstNodeWalk, forwardExcursion, walkAllChildSegments, } from "./mergeTreeNodeWalk.js";
import { CollaborationWindow, Marker, MaxNodesInBlock, MergeBlock, assertSegmentLeaf, assignChild, isSegmentLeaf, reservedMarkerIdKey, seqLTE, } from "./mergeTreeNodes.js";
import { createAnnotateRangeOp, createInsertSegmentOp, createRemoveRangeOp, } from "./opBuilder.js";
import { MergeTreeDeltaType, ReferenceType, } from "./ops.js";
import { PartialSequenceLengths } from "./partialLengths.js";
import { PerspectiveImpl, isSegmentPresent } from "./perspective.js";
import { createMap, extend, extendIfUndefined } from "./properties.js";
import { DetachedReferencePosition, refGetTileLabels, refHasTileLabel, refTypeIncludesFlag, } from "./referencePositions.js";
import { SegmentGroupCollection } from "./segmentGroupCollection.js";
import { assertMoved, assertRemoved, isMergeNodeInfo, isMoved, isRemoved, overwriteInfo, removeRemovalInfo, toMoveInfo, toRemovalInfo, } from "./segmentInfos.js";
import { copyPropertiesAndManager, PropertiesManager, PropertiesRollback, } from "./segmentPropertiesManager.js";
import { Side } from "./sequencePlace.js";
import { SortedSegmentSet } from "./sortedSegmentSet.js";
import { zamboniSegments } from "./zamboni.js";
function isRemovedAndAcked(segment) {
    const removalInfo = toRemovalInfo(segment);
    return removalInfo !== undefined && removalInfo.removedSeq !== UnassignedSequenceNumber;
}
function isMovedAndAcked(segment) {
    const moveInfo = toMoveInfo(segment);
    return moveInfo !== undefined && moveInfo.movedSeq !== UnassignedSequenceNumber;
}
function isRemovedAndAckedOrMovedAndAcked(segment) {
    return isRemovedAndAcked(segment) || isMovedAndAcked(segment);
}
function isRemovedOrMoved(segment) {
    return isRemoved(segment) || isMoved(segment);
}
function nodeTotalLength(mergeTree, node) {
    if (!node.isLeaf()) {
        return node.cachedLength;
    }
    return mergeTree.localNetLength(node);
}
const LRUSegmentComparer = {
    min: { maxSeq: -2 },
    compare: (a, b) => a.maxSeq - b.maxSeq,
};
function ackSegment(segment, segmentGroup, opArgs) {
    const currentSegmentGroup = segment.segmentGroups?.dequeue();
    assert(currentSegmentGroup === segmentGroup, 0x043 /* "On ack, unexpected segmentGroup!" */);
    assert(opArgs.sequencedMessage !== undefined, 0xa6e /* must have sequencedMessage */);
    const { op, sequencedMessage: { sequenceNumber, minimumSequenceNumber }, } = opArgs;
    switch (op.type) {
        case MergeTreeDeltaType.ANNOTATE: {
            assert(!!segment.propertyManager, 0x044 /* "On annotate ack, missing segment property manager!" */);
            segment.propertyManager.ack(sequenceNumber, minimumSequenceNumber, op);
            return true;
        }
        case MergeTreeDeltaType.INSERT: {
            assert(segment.seq === UnassignedSequenceNumber, 0x045 /* "On insert, seq number already assigned!" */);
            segment.seq = sequenceNumber;
            segment.localSeq = undefined;
            return true;
        }
        case MergeTreeDeltaType.REMOVE: {
            assertRemoved(segment);
            segment.localRemovedSeq = undefined;
            if (segment.removedSeq === UnassignedSequenceNumber) {
                segment.removedSeq = sequenceNumber;
                return true;
            }
            return false;
        }
        case MergeTreeDeltaType.OBLITERATE:
        case MergeTreeDeltaType.OBLITERATE_SIDED: {
            assertMoved(segment);
            const obliterateInfo = segmentGroup.obliterateInfo;
            assert(obliterateInfo !== undefined, 0xa40 /* must have obliterate info */);
            segment.localMovedSeq = obliterateInfo.localSeq = undefined;
            const seqIdx = segment.movedSeqs.indexOf(UnassignedSequenceNumber);
            assert(seqIdx !== -1, 0x86f /* expected movedSeqs to contain unacked seq */);
            segment.movedSeqs[seqIdx] = sequenceNumber;
            if (segment.movedSeq === UnassignedSequenceNumber) {
                segment.movedSeq = sequenceNumber;
                return true;
            }
            return false;
        }
        default: {
            throw new Error(`${op.type} is in unrecognized operation type`);
        }
    }
}
export function errorIfOptionNotTrue(options, option) {
    if (options?.[option] !== true) {
        throw new Error(`${option} is not enabled.`);
    }
}
export function findRootMergeBlock(segmentOrNode) {
    if (segmentOrNode === undefined) {
        return undefined;
    }
    let maybeRoot = segmentOrNode.isLeaf()
        ? segmentOrNode.parent
        : segmentOrNode;
    while (maybeRoot?.parent !== undefined) {
        maybeRoot = maybeRoot.parent;
    }
    return maybeRoot?.mergeTree === undefined ? undefined : maybeRoot;
}
/**
 * Find the segment to which a reference will slide if it needs to slide, or undefined if there
 * is no valid segment (i.e. the tree is empty).
 *
 * @param segment - The segment to slide from.
 * @param cache - Optional cache mapping segments to their sliding destinations.
 * Excursions will be avoided for segments in the cache, and the cache will be populated with
 * entries for all segments visited during excursion.
 * This can reduce the number of times the tree needs to be scanned if a range containing many
 * SlideOnRemove references is removed.
 */
function getSlideToSegment(segment, slidingPreference = SlidingPreference.FORWARD, cache, useNewSlidingBehavior = false) {
    if (!segment ||
        !isRemovedAndAckedOrMovedAndAcked(segment) ||
        segment.endpointType !== undefined) {
        return [segment, undefined];
    }
    const cachedSegment = cache?.get(segment);
    if (cachedSegment !== undefined) {
        return [cachedSegment.seg, undefined];
    }
    const result = {};
    cache?.set(segment, result);
    const goFurtherToFindSlideToSegment = (seg) => {
        if (seg.seq !== UnassignedSequenceNumber && !isRemovedAndAckedOrMovedAndAcked(seg)) {
            result.seg = seg;
            return false;
        }
        if (cache !== undefined &&
            (toRemovalInfo(seg)?.removedSeq === toRemovalInfo(segment)?.removedSeq ||
                toMoveInfo(seg)?.movedSeq === toMoveInfo(segment)?.movedSeq)) {
            cache.set(seg, result);
        }
        return true;
    };
    if (slidingPreference === SlidingPreference.BACKWARD) {
        backwardExcursion(segment, goFurtherToFindSlideToSegment);
    }
    else {
        forwardExcursion(segment, goFurtherToFindSlideToSegment);
    }
    if (result.seg !== undefined) {
        return [result.seg, undefined];
    }
    // in the new sliding behavior, we don't look in the opposite direction
    // if we fail to find a segment to slide to in the right direction.
    //
    // in other words, rather than going `forward ?? backward ?? detached` (or
    // `backward ?? forward ?? detached`), we would slide `forward ?? detached`
    // or `backward ?? detached`
    //
    // in both of these cases detached may be substituted for one of the special
    // endpoint segments, if such behavior is enabled
    if (!useNewSlidingBehavior) {
        if (slidingPreference === SlidingPreference.BACKWARD) {
            forwardExcursion(segment, goFurtherToFindSlideToSegment);
        }
        else {
            backwardExcursion(segment, goFurtherToFindSlideToSegment);
        }
    }
    let maybeEndpoint;
    if (slidingPreference === SlidingPreference.BACKWARD) {
        maybeEndpoint = "start";
    }
    else if (slidingPreference === SlidingPreference.FORWARD) {
        maybeEndpoint = "end";
    }
    return [result.seg, maybeEndpoint];
}
/**
 * Returns the position to slide a reference to if a slide is required.
 * @param segoff - The segment and offset to slide from
 * @returns segment and offset to slide the reference to
 * @internal
 */
export function getSlideToSegoff(segoff, slidingPreference = SlidingPreference.FORWARD, useNewSlidingBehavior = false) {
    if (!isSegmentLeaf(segoff.segment)) {
        return segoff;
    }
    const [segment, _] = getSlideToSegment(segoff.segment, slidingPreference, undefined, useNewSlidingBehavior);
    if (segment === segoff.segment) {
        return segoff;
    }
    const offset = segment && segment.ordinal < segoff.segment.ordinal ? segment.cachedLength - 1 : 0;
    return {
        segment,
        offset,
    };
}
const forwardPred = (ref) => ref.slidingPreference !== SlidingPreference.BACKWARD;
const backwardPred = (ref) => ref.slidingPreference === SlidingPreference.BACKWARD;
class Obliterates {
    constructor(mergeTree) {
        this.mergeTree = mergeTree;
        /**
         * Array containing the all move operations within the
         * collab window.
         *
         * The moves are stored in sequence order which accelerates clean up in setMinSeq
         *
         * See https://github.com/microsoft/FluidFramework/blob/main/packages/dds/merge-tree/docs/Obliterate.md#remote-perspective
         * for additional context
         */
        this.seqOrdered = new DoublyLinkedList();
        /**
         * This contains a sorted lists of all obliterate starts
         * and is used to accelerate finding overlapping obliterates
         * as well as determining if there are any obliterates at all.
         */
        this.startOrdered = new SortedSegmentSet();
    }
    setMinSeq(minSeq) {
        // eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain
        while (!this.seqOrdered.empty && this.seqOrdered.first?.data.seq <= minSeq) {
            const ob = this.seqOrdered.shift();
            this.startOrdered.remove(ob.data.start);
            this.mergeTree.removeLocalReferencePosition(ob.data.start);
            this.mergeTree.removeLocalReferencePosition(ob.data.end);
        }
    }
    addOrUpdate(obliterateInfo) {
        const { seq, start } = obliterateInfo;
        if (seq !== UnassignedSequenceNumber) {
            this.seqOrdered.push(obliterateInfo);
        }
        this.startOrdered.addOrUpdate(start);
    }
    empty() {
        return this.startOrdered.size === 0;
    }
    findOverlapping(seg) {
        const overlapping = [];
        for (const start of this.startOrdered.items) {
            const startSeg = start.getSegment();
            if (isMergeNodeInfo(startSeg) && startSeg.ordinal <= seg.ordinal) {
                const ob = start.properties?.obliterate;
                const endSeg = ob.end.getSegment();
                if (isMergeNodeInfo(endSeg) && endSeg.ordinal >= seg.ordinal) {
                    overlapping.push(ob);
                }
            }
            else {
                // the start is past the seg, so exit
                break;
            }
        }
        return overlapping;
    }
}
/**
 * @internal
 */
export class MergeTree {
    constructor(options) {
        this.options = options;
        this.collabWindow = new CollaborationWindow();
        this.pendingSegments = new DoublyLinkedList();
        this.segmentsToScour = new Heap(LRUSegmentComparer);
        /**
         * Whether or not all blocks in the mergeTree currently have information about local partial lengths computed.
         * This information is only necessary on reconnect, and otherwise costly to bookkeep.
         * This field enables tracking whether partials need to be recomputed using localSeq information.
         */
        this.localPartialsComputed = false;
        // for now assume only markers have ids and so point directly at the Segment
        // if we need to have pointers to non-markers, we can change to point at local refs
        this.idToMarker = new Map();
        // TODO:AB#29553: This property doesn't seem to be adequately round-tripped through summarization.
        // Specifically, it seems like we drop information about obliterates within the collab window for at least V1 summaries.
        this.obliterates = new Obliterates(this);
        this.splitLeafSegment = (segment, pos) => {
            if (!(pos > 0 && segment)) {
                return {};
            }
            const next = segment.splitAt(pos);
            assertSegmentLeaf(next);
            if (segment?.segmentGroups) {
                next.segmentGroups ??= new SegmentGroupCollection(next);
                segment.segmentGroups.copyTo(next.segmentGroups);
            }
            if (segment.obliteratePrecedingInsertion) {
                next.obliteratePrecedingInsertion = segment.obliteratePrecedingInsertion;
            }
            copyPropertiesAndManager(segment, next);
            if (segment.localRefs) {
                segment.localRefs.split(pos, next);
            }
            this.mergeTreeMaintenanceCallback?.({
                operation: MergeTreeMaintenanceType.SPLIT,
                deltaSegments: [{ segment }, { segment: next }],
            }, undefined);
            return { next };
        };
        this.startOfTree = new StartOfTreeSegment(this);
        this.endOfTree = new EndOfTreeSegment(this);
        this._root = this.makeBlock(0);
        this._root.mergeTree = this;
        this.attributionPolicy = options?.attribution?.policyFactory?.();
    }
    get root() {
        return this._root;
    }
    set root(value) {
        this._root = value;
        value.mergeTree = this;
    }
    makeBlock(childCount) {
        const block = new MergeBlock(childCount);
        block.ordinal = "";
        return block;
    }
    /**
     * Compute the net length of this segment from a local perspective.
     * @param segment - Segment whose length to find
     * @param localSeq - localSeq at which to find the length of this segment. If not provided,
     * default is to consider the local client's current perspective. Only local sequence
     * numbers corresponding to un-acked operations give valid results.
     */
    localNetLength(segment, refSeq, localSeq) {
        const removalInfo = toRemovalInfo(segment);
        const moveInfo = toMoveInfo(segment);
        if (localSeq === undefined) {
            if (removalInfo !== undefined || moveInfo !== undefined) {
                if ((!!removalInfo && !seqLTE(removalInfo.removedSeq, this.collabWindow.minSeq)) ||
                    (!!moveInfo && !seqLTE(moveInfo.movedSeq, this.collabWindow.minSeq))) {
                    return 0;
                }
                // this segment removed and outside the collab window which means it is zamboni eligible
                // this also means the segment could not exist, so we should not consider it
                // when making decisions about conflict resolutions
                return undefined;
            }
            else {
                return segment.cachedLength;
            }
        }
        assert(refSeq !== undefined, 0x398 /* localSeq provided for local length without refSeq */);
        assert(segment.seq !== undefined, 0x399 /* segment with no seq in mergeTree */);
        const { seq } = segment;
        const { removedSeq, localRemovedSeq } = removalInfo ?? {};
        const { movedSeq, localMovedSeq } = moveInfo ?? {};
        if (seq === UnassignedSequenceNumber) {
            assert(segment.localSeq !== undefined, 0x39a /* unacked segment with undefined localSeq */);
            // inserted locally, still un-acked
            if (segment.localSeq > localSeq ||
                (localRemovedSeq !== undefined && localRemovedSeq <= localSeq) ||
                (localMovedSeq !== undefined && localMovedSeq <= localSeq)) {
                return 0;
            }
            const { cachedLength } = segment;
            return cachedLength;
        }
        else {
            // inserted remotely
            if (seq > refSeq ||
                (removedSeq !== undefined &&
                    removedSeq !== UnassignedSequenceNumber &&
                    removedSeq <= refSeq) ||
                (movedSeq !== undefined &&
                    movedSeq !== UnassignedSequenceNumber &&
                    movedSeq <= refSeq) ||
                (localRemovedSeq !== undefined && localRemovedSeq <= localSeq) ||
                (localMovedSeq !== undefined && localMovedSeq <= localSeq)) {
                return 0;
            }
            return segment.cachedLength;
        }
    }
    unlinkMarker(marker) {
        const id = marker.getId();
        if (id) {
            this.idToMarker.delete(id);
        }
    }
    addNode(block, node) {
        const index = block.childCount++;
        assignChild(block, node, index, false);
        return index;
    }
    reloadFromSegments(segments) {
        // This code assumes that a later call to `startCollaboration()` will initialize partial lengths.
        assert(!this.collabWindow.collaborating, 0x049 /* "Trying to reload from segments while collaborating!" */);
        const maxChildren = MaxNodesInBlock - 1;
        // Starting with the leaf segments, recursively builds the B-Tree layer by layer from the bottom up.
        const buildMergeBlock = (nodes) => {
            const blockCount = Math.ceil(nodes.length / maxChildren); // Compute # blocks require for this level of B-Tree
            const blocks = Array.from({ length: blockCount }); // Pre-alloc array to collect nodes
            // For each block in this level of the B-Tree...
            for (let nodeIndex = 0, blockIndex = 0; // Start with the first block and first node
             blockIndex < blockCount; // If we have more blocks, we also have more nodes to insert
             blockIndex++ // Advance to next block in this layer.
            ) {
                const block = (blocks[blockIndex] = this.makeBlock(0));
                // For each child of the current block, insert a node (while we have nodes left)
                // and update the block's info.
                for (let childIndex = 0; childIndex < maxChildren && nodeIndex < nodes.length; // While we still have children & nodes left
                 childIndex++, nodeIndex++ // Advance to next child & node
                ) {
                    // Insert the next node into the current block
                    this.addNode(block, nodes[nodeIndex]);
                }
                // Calculate this block's info.  Previously this was inlined into the above loop as a micro-optimization,
                // but it turns out to be negligible in practice since `reloadFromSegments()` is only invoked for the
                // snapshot header.  The bulk of the segments in long documents are inserted via `insertSegments()`.
                this.blockUpdate(block);
            }
            return blocks.length === 1 // If there is only one block at this layer...
                ? blocks[0] // ...then we're done.  Return the root.
                : buildMergeBlock(blocks); // ...otherwise recursively build the next layer above blocks.
        };
        if (segments.length > 0) {
            this.root = buildMergeBlock(segments);
            this.nodeUpdateOrdinals(this.root);
        }
        else {
            this.root = this.makeBlock(0);
        }
    }
    // For now assume min starts at zero
    startCollaboration(localClientId, minSeq, currentSeq) {
        this.collabWindow.clientId = localClientId;
        this.collabWindow.minSeq = minSeq;
        this.collabWindow.collaborating = true;
        this.collabWindow.currentSeq = currentSeq;
        this.nodeUpdateLengthNewStructure(this.root, true);
    }
    addToLRUSet(leaf, seq) {
        // If the parent node has not yet been marked for scour (i.e., needsScour is not false or undefined),
        // add the segment and mark the mark the node now.
        // TODO: 'seq' may be less than the current sequence number when inserting pre-ACKed
        //       segments from a snapshot.  We currently skip these for now.
        if (leaf.parent.needsScour !== true && seq > this.collabWindow.currentSeq) {
            leaf.parent.needsScour = true;
            this.segmentsToScour.add({ segment: leaf, maxSeq: seq });
        }
    }
    getLength(refSeq, clientId) {
        return this.blockLength(this.root, refSeq, clientId);
    }
    /**
     * Returns the current length of the MergeTree for the local client.
     */
    get length() {
        return this.root.cachedLength;
    }
    getPosition(node, refSeq, clientId, localSeq) {
        if (node.isLeaf() && node.endpointType === "start") {
            return 0;
        }
        let totalOffset = 0;
        let parent = node.parent;
        let prevParent;
        while (parent) {
            const children = parent.children;
            for (let childIndex = 0; childIndex < parent.childCount; childIndex++) {
                const child = children[childIndex];
                if ((!!prevParent && child === prevParent) || child === node) {
                    break;
                }
                totalOffset += this.nodeLength(child, refSeq, clientId, localSeq) ?? 0;
            }
            prevParent = parent;
            parent = parent.parent;
        }
        return totalOffset;
    }
    getContainingSegment(pos, refSeq, clientId, localSeq) {
        assert(localSeq === undefined || clientId === this.collabWindow.clientId, 0x39b /* localSeq provided for non-local client */);
        let segment;
        let offset;
        const leaf = (leafSeg, _, start) => {
            segment = leafSeg;
            offset = start;
            return false;
        };
        this.nodeMap(refSeq, clientId, leaf, undefined, pos, pos + 1, localSeq);
        return { segment, offset };
    }
    /**
     * Slides or removes references from the provided list of segments.
     *
     * The order of the references is preserved for references of the same sliding
     * preference. Relative order between references that slide backward and those
     * that slide forward is not preserved, even in the case when they slide to
     * the same segment.
     *
     * @remarks
     *
     * 1. Preserving the order of the references is a useful property for reference-based undo/redo
     * (see revertibles.ts).
     *
     * 2. For use cases which necessitate eventual consistency across clients,
     * this method should only be called with segments for which the current client sequence number is
     * max(remove segment sequence number, add reference sequence number).
     * See `packages\dds\merge-tree\REFERENCEPOSITIONS.md`
     *
     * @param segments - An array of (not necessarily contiguous) segments with increasing ordinals.
     */
    slideAckedRemovedSegmentReferences(segments) {
        // References are slid in groups to preserve their order.
        let currentForwardSlideGroup = [];
        let currentBackwardSlideGroup = [];
        let currentForwardMaybeEndpoint;
        let currentForwardSlideDestination;
        let currentForwardSlideIsForward;
        let currentBackwardMaybeEndpoint;
        let currentBackwardSlideDestination;
        let currentBackwardSlideIsForward;
        const slideGroup = (currentSlideDestination, currentSlideIsForward, currentSlideGroup, pred, maybeEndpoint) => {
            if (currentSlideIsForward === undefined) {
                return;
            }
            const nonEndpointRefsToAdd = currentSlideGroup.map((collection) => filterLocalReferencePositions(collection, (ref) => pred(ref) && (maybeEndpoint ? !ref.canSlideToEndpoint : true)));
            const endpointRefsToAdd = currentSlideGroup.map((collection) => filterLocalReferencePositions(collection, (ref) => pred(ref) && !!ref.canSlideToEndpoint));
            if (maybeEndpoint) {
                const endpoint = maybeEndpoint === "start" ? this.startOfTree : this.endOfTree;
                const localRefs = LocalReferenceCollection.setOrGet(endpoint);
                if (currentSlideIsForward) {
                    localRefs.addBeforeTombstones(...endpointRefsToAdd);
                }
                else {
                    localRefs.addAfterTombstones(...endpointRefsToAdd);
                }
            }
            if (currentSlideDestination === undefined) {
                for (const collection of currentSlideGroup) {
                    for (const ref of collection) {
                        if (pred(ref) && !refTypeIncludesFlag(ref, ReferenceType.StayOnRemove)) {
                            ref.callbacks?.beforeSlide?.(ref);
                            collection.removeLocalRef(ref);
                            ref.callbacks?.afterSlide?.(ref);
                        }
                    }
                }
            }
            else {
                const localRefs = LocalReferenceCollection.setOrGet(currentSlideDestination);
                if (currentSlideIsForward) {
                    localRefs.addBeforeTombstones(...nonEndpointRefsToAdd);
                }
                else {
                    localRefs.addAfterTombstones(...nonEndpointRefsToAdd);
                }
            }
        };
        const trySlideSegment = (segment, currentSlideDestination, currentSlideIsForward, currentSlideGroup, pred, slidingPreference, currentMaybeEndpoint, reassign) => {
            // avoid sliding logic if this segment doesn't have any references
            // with the given sliding preference
            if (!segment.localRefs || !anyLocalReferencePosition(segment.localRefs, pred)) {
                return;
            }
            const [slideToSegment, maybeEndpoint] = getSlideToSegment(segment, slidingPreference, slidingPreference === SlidingPreference.FORWARD
                ? forwardSegmentCache
                : backwardSegmentCache, this.options?.mergeTreeReferencesCanSlideToEndpoint);
            const slideIsForward = slideToSegment === undefined ? false : slideToSegment.ordinal > segment.ordinal;
            if (slideToSegment !== currentSlideDestination ||
                slideIsForward !== currentSlideIsForward ||
                maybeEndpoint !== currentMaybeEndpoint) {
                slideGroup(currentSlideDestination, currentSlideIsForward, currentSlideGroup, pred, this.options?.mergeTreeReferencesCanSlideToEndpoint ? maybeEndpoint : undefined);
                reassign(segment.localRefs, slideToSegment, slideIsForward, this.options?.mergeTreeReferencesCanSlideToEndpoint ? maybeEndpoint : undefined);
            }
            else {
                currentSlideGroup.push(segment.localRefs);
            }
        };
        const forwardSegmentCache = new Map();
        const backwardSegmentCache = new Map();
        for (const segment of segments) {
            assert(isRemovedAndAckedOrMovedAndAcked(segment), 0x2f1 /* slideReferences from a segment which has not been removed and acked */);
            if (segment.localRefs === undefined || segment.localRefs.empty) {
                continue;
            }
            trySlideSegment(segment, currentForwardSlideDestination, currentForwardSlideIsForward, currentForwardSlideGroup, forwardPred, SlidingPreference.FORWARD, currentForwardMaybeEndpoint, (localRefs, slideToSegment, slideIsForward, maybeEndpoint) => {
                currentForwardSlideGroup = [localRefs];
                currentForwardSlideDestination = slideToSegment;
                currentForwardSlideIsForward = slideIsForward;
                currentForwardMaybeEndpoint = maybeEndpoint;
            });
            trySlideSegment(segment, currentBackwardSlideDestination, currentBackwardSlideIsForward, currentBackwardSlideGroup, backwardPred, SlidingPreference.BACKWARD, currentBackwardMaybeEndpoint, (localRefs, slideToSegment, slideIsForward, maybeEndpoint) => {
                currentBackwardSlideGroup = [localRefs];
                currentBackwardSlideDestination = slideToSegment;
                currentBackwardSlideIsForward = slideIsForward;
                currentBackwardMaybeEndpoint = maybeEndpoint;
            });
        }
        slideGroup(currentForwardSlideDestination, currentForwardSlideIsForward, currentForwardSlideGroup, forwardPred, currentForwardMaybeEndpoint);
        slideGroup(currentBackwardSlideDestination, currentBackwardSlideIsForward, currentBackwardSlideGroup, backwardPred, currentBackwardMaybeEndpoint);
    }
    blockLength(node, refSeq, clientId) {
        return this.collabWindow.collaborating && clientId !== this.collabWindow.clientId
            ? node.partialLengths.getPartialLength(refSeq, clientId)
            : (node.cachedLength ?? 0);
    }
    /**
     * Compute local partial length information
     *
     * Public only for use by internal tests
     */
    computeLocalPartials(refSeq) {
        if (this.localPartialsComputed) {
            return;
        }
        const rebaseCollabWindow = new CollaborationWindow();
        rebaseCollabWindow.loadFrom(this.collabWindow);
        if (refSeq < this.collabWindow.minSeq) {
            rebaseCollabWindow.minSeq = refSeq;
        }
        this.root.partialLengths = PartialSequenceLengths.combine(this.root, rebaseCollabWindow, true, true);
        this.localPartialsComputed = true;
    }
    nodeLength(node, refSeq, clientId, localSeq) {
        if (!this.collabWindow.collaborating || this.collabWindow.clientId === clientId) {
            if (node.isLeaf()) {
                return this.localNetLength(node, refSeq, localSeq);
            }
            else if (localSeq === undefined) {
                // Local client sees all segments, even when collaborating
                return node.cachedLength;
            }
            else {
                this.computeLocalPartials(refSeq);
                // Local client should see all segments except those after localSeq.
                const partialLen = node.partialLengths.getPartialLength(refSeq, clientId, localSeq);
                PartialSequenceLengths.options.verifyExpected?.(this, node, refSeq, clientId, localSeq);
                return partialLen;
            }
        }
        else {
            // Sequence number within window
            if (node.isLeaf()) {
                const segment = node;
                const removalInfo = toRemovalInfo(segment);
                const moveInfo = toMoveInfo(segment);
                if (removalInfo !== undefined) {
                    if (seqLTE(removalInfo.removedSeq, this.collabWindow.minSeq)) {
                        return undefined;
                    }
                    if (seqLTE(removalInfo.removedSeq, refSeq) ||
                        removalInfo.removedClientIds.includes(clientId)) {
                        return 0;
                    }
                }
                if (moveInfo !== undefined) {
                    if (seqLTE(moveInfo.movedSeq, this.collabWindow.minSeq)) {
                        return undefined;
                    }
                    if (seqLTE(moveInfo.movedSeq, refSeq) ||
                        moveInfo.movedClientIds.includes(clientId)) {
                        return 0;
                    }
                }
                return seqLTE(node.seq ?? 0, refSeq) || segment.clientId === clientId
                    ? segment.cachedLength
                    : 0;
            }
            else {
                const partialLen = node.partialLengths.getPartialLength(refSeq, clientId);
                PartialSequenceLengths.options.verifyExpected?.(this, node, refSeq, clientId);
                return partialLen;
            }
        }
    }
    setMinSeq(minSeq) {
        assert(minSeq <= this.collabWindow.currentSeq, 0x04e /* "Trying to set minSeq above currentSeq of collab window!" */);
        // Only move forward
        assert(this.collabWindow.minSeq <= minSeq, 0x04f /* "minSeq of collab window > target minSeq!" */);
        if (minSeq > this.collabWindow.minSeq) {
            this.collabWindow.minSeq = minSeq;
            this.obliterates.setMinSeq(minSeq);
            if (MergeTree.options.zamboniSegments) {
                zamboniSegments(this);
            }
        }
    }
    /**
     * Returns the count of elements before the given reference position from the given perspective.
     *
     * @param refPos - The reference position to resolve.
     * @param refSeq - The number of the latest sequenced change to consider.
     * Defaults to including all edits which have been applied.
     * @param clientId - The ID of the client from whose perspective to resolve this reference. Defaults to the current client.
     * @param localSeq - The local sequence number to consider. Defaults to including all local edits.
     */
    referencePositionToLocalPosition(refPos, refSeq = Number.MAX_SAFE_INTEGER, clientId = this.collabWindow.clientId, localSeq = this.collabWindow.localSeq) {
        const seg = refPos.getSegment();
        if (!isSegmentLeaf(seg)) {
            // We have no idea where this reference is, because it refers to a segment which is not in the tree.
            return DetachedReferencePosition;
        }
        if (refPos.isLeaf()) {
            return this.getPosition(seg, refSeq, clientId, localSeq);
        }
        if (refTypeIncludesFlag(refPos, ReferenceType.Transient) || seg.localRefs?.has(refPos)) {
            if (seg !== this.startOfTree &&
                seg !== this.endOfTree &&
                !isSegmentPresent(seg, { refSeq, localSeq })) {
                const forward = refPos.slidingPreference === SlidingPreference.FORWARD;
                const moveInfo = toMoveInfo(seg);
                const removeInfo = toRemovalInfo(seg);
                const slideSeq = moveInfo !== undefined && moveInfo.movedSeq !== UnassignedSequenceNumber
                    ? moveInfo.movedSeq
                    : removeInfo !== undefined && removeInfo.removedSeq !== UnassignedSequenceNumber
                        ? removeInfo.removedSeq
                        : refSeq;
                const slideLocalSeq = moveInfo?.localMovedSeq ?? removeInfo?.localRemovedSeq;
                const perspective = new PerspectiveImpl(this, {
                    refSeq: slideSeq,
                    localSeq: slideLocalSeq,
                });
                const slidSegment = perspective.nextSegment(seg, forward);
                return (this.getPosition(slidSegment, refSeq, clientId, localSeq) +
                    (forward ? 0 : slidSegment.cachedLength === 0 ? 0 : slidSegment.cachedLength - 1));
            }
            return this.getPosition(seg, refSeq, clientId, localSeq) + refPos.getOffset();
        }
        return DetachedReferencePosition;
    }
    /**
     * Finds the nearest reference with ReferenceType.Tile to `startPos` in the direction dictated by `forwards`.
     * Uses depthFirstNodeWalk in addition to block-accelerated functionality. The search position will be included in
     * the nodes to walk, so searching on all positions, including the endpoints, can be considered inclusive.
     * Any out of bound search positions will return undefined, so in order to search the whole string, a forward
     * search can begin at 0, or a backward search can begin at length-1.
     *
     * @param startPos - Position at which to start the search
     * @param clientId - clientId dictating the perspective to search from
     * @param markerLabel - Label of the marker to search for
     * @param forwards - Whether the string should be searched in the forward or backward direction
     */
    searchForMarker(startPos, clientId, markerLabel, forwards = true) {
        let foundMarker;
        const { segment } = this.getContainingSegment(startPos, UniversalSequenceNumber, clientId);
        if (!isSegmentLeaf(segment)) {
            return undefined;
        }
        depthFirstNodeWalk(segment.parent, segment, (node) => {
            if (node.isLeaf()) {
                if (Marker.is(node) && refHasTileLabel(node, markerLabel)) {
                    foundMarker = node;
                }
            }
            else {
                const marker = forwards
                    ? node.leftmostTiles[markerLabel]
                    : node.rightmostTiles[markerLabel];
                if (marker !== undefined) {
                    assert(marker.isLeaf() && Marker.is(marker), 0x751 /* Object returned is not a valid marker */);
                    foundMarker = marker;
                }
            }
            return foundMarker === undefined ? NodeAction.Skip : NodeAction.Exit;
        }, undefined, undefined, forwards);
        return foundMarker;
    }
    updateRoot(splitNode) {
        if (splitNode !== undefined) {
            const newRoot = this.makeBlock(2);
            assignChild(newRoot, this.root, 0, false);
            assignChild(newRoot, splitNode, 1, false);
            this.root = newRoot;
            this.nodeUpdateOrdinals(this.root);
            this.nodeUpdateLengthNewStructure(this.root);
        }
    }
    /**
     * Assign sequence number to existing segment; update partial lengths to reflect the change
     * @param seq - sequence number given by server to pending segment
     */
    ackPendingSegment(opArgs) {
        const seq = opArgs.sequencedMessage.sequenceNumber;
        const pendingSegmentGroup = this.pendingSegments.shift()?.data;
        const nodesToUpdate = [];
        let overwrite = false;
        if (pendingSegmentGroup !== undefined) {
            const deltaSegments = [];
            const overlappingRemoves = [];
            pendingSegmentGroup.segments.map((pendingSegment) => {
                const overlappingRemove = !ackSegment(pendingSegment, pendingSegmentGroup, opArgs);
                overwrite ||= overlappingRemove || toMoveInfo(pendingSegment) !== undefined;
                overlappingRemoves.push(overlappingRemove);
                if (MergeTree.options.zamboniSegments) {
                    this.addToLRUSet(pendingSegment, seq);
                }
                if (!nodesToUpdate.includes(pendingSegment.parent)) {
                    nodesToUpdate.push(pendingSegment.parent);
                }
                deltaSegments.push({
                    segment: pendingSegment,
                });
            });
            if (pendingSegmentGroup.obliterateInfo !== undefined) {
                pendingSegmentGroup.obliterateInfo.seq = seq;
                this.obliterates.addOrUpdate(pendingSegmentGroup.obliterateInfo);
            }
            // Perform slides after all segments have been acked, so that
            // positions after slide are final
            if (opArgs.op.type === MergeTreeDeltaType.REMOVE ||
                opArgs.op.type === MergeTreeDeltaType.OBLITERATE ||
                opArgs.op.type === MergeTreeDeltaType.OBLITERATE_SIDED) {
                this.slideAckedRemovedSegmentReferences(pendingSegmentGroup.segments);
            }
            this.mergeTreeMaintenanceCallback?.({
                deltaSegments,
                operation: MergeTreeMaintenanceType.ACKNOWLEDGED,
            }, opArgs);
            const clientId = this.collabWindow.clientId;
            for (const node of nodesToUpdate) {
                this.blockUpdatePathLengths(node, seq, clientId, overwrite);
            }
        }
        if (MergeTree.options.zamboniSegments) {
            zamboniSegments(this);
        }
    }
    addToPendingList(segment, segmentGroup, localSeq, previousProps) {
        let _segmentGroup = segmentGroup;
        if (_segmentGroup === undefined) {
            _segmentGroup = {
                segments: [],
                localSeq,
                refSeq: this.collabWindow.currentSeq,
            };
            if (previousProps) {
                _segmentGroup.previousProps = [];
            }
            this.pendingSegments.push(_segmentGroup);
        }
        if ((!_segmentGroup.previousProps && !!previousProps) ||
            (!!_segmentGroup.previousProps && !previousProps)) {
            throw new Error("All segments in group should have previousProps or none");
        }
        if (previousProps) {
            _segmentGroup.previousProps.push(previousProps);
        }
        const segmentGroups = (segment.segmentGroups ??= new SegmentGroupCollection(segment));
        segmentGroups.enqueue(_segmentGroup);
        return _segmentGroup;
    }
    // TODO: error checking
    getMarkerFromId(id) {
        const marker = this.idToMarker.get(id);
        return marker === undefined ||
            isRemoved(marker) ||
            (isMoved(marker) && marker.moveDst === undefined)
            ? undefined
            : marker;
    }
    /**
     * Given a position specified relative to a marker id, lookup the marker
     * and convert the position to a character position.
     * @param relativePos - Id of marker (may be indirect) and whether position is before or after marker.
     * @param refseq - The reference sequence number at which to compute the position.
     * @param clientId - The client id with which to compute the position.
     */
    posFromRelativePos(relativePos, refseq = this.collabWindow.currentSeq, clientId = this.collabWindow.clientId) {
        let pos = -1;
        let marker;
        if (relativePos.id) {
            marker = this.getMarkerFromId(relativePos.id);
        }
        if (isSegmentLeaf(marker)) {
            pos = this.getPosition(marker, refseq, clientId);
            if (relativePos.before) {
                if (relativePos.offset !== undefined) {
                    pos -= relativePos.offset;
                }
            }
            else {
                pos += marker.cachedLength;
                if (relativePos.offset !== undefined) {
                    pos += relativePos.offset;
                }
            }
        }
        return pos;
    }
    insertSegments(pos, segments, refSeq, clientId, seq, opArgs) {
        this.ensureIntervalBoundary(pos, refSeq, clientId);
        const localSeq = seq === UnassignedSequenceNumber ? ++this.collabWindow.localSeq : undefined;
        this.blockInsert(pos, refSeq, clientId, seq, localSeq, segments);
        // opArgs == undefined => loading snapshot or test code
        if (opArgs !== undefined) {
            const deltaSegments = segments
                .filter((segment) => !toMoveInfo(segment))
                .map((segment) => ({ segment }));
            if (deltaSegments.length > 0) {
                this.mergeTreeDeltaCallback?.(opArgs, {
                    operation: MergeTreeDeltaType.INSERT,
                    deltaSegments,
                });
            }
        }
        if (this.collabWindow.collaborating &&
            MergeTree.options.zamboniSegments &&
            seq !== UnassignedSequenceNumber) {
            zamboniSegments(this);
        }
    }
    /**
     * Resolves a remote client's position against the local sequence
     * and returns the remote client's position relative to the local
     * sequence. The client ref seq must be above the minimum sequence number
     * or the return value will be undefined.
     * Generally this method is used in conjunction with signals which provide
     * point in time values for the below parameters, and is useful for things
     * like displaying user position. It should not be used with persisted values
     * as persisted values will quickly become invalid as the remoteClientRefSeq
     * moves below the minimum sequence number
     * @param remoteClientPosition - The remote client's position to resolve
     * @param remoteClientRefSeq - The reference sequence number of the remote client
     * @param remoteClientId - The client id of the remote client
     */
    resolveRemoteClientPosition(remoteClientPosition, remoteClientRefSeq, remoteClientId) {
        if (remoteClientRefSeq < this.collabWindow.minSeq) {
            return undefined;
        }
        const segmentInfo = this.getContainingSegment(remoteClientPosition, remoteClientRefSeq, remoteClientId);
        const { currentSeq, clientId } = this.collabWindow;
        if (isSegmentLeaf(segmentInfo?.segment)) {
            const segmentPosition = this.getPosition(segmentInfo.segment, currentSeq, clientId);
            return segmentPosition + segmentInfo.offset;
        }
        else {
            if (remoteClientPosition === this.getLength(remoteClientRefSeq, remoteClientId)) {
                return this.getLength(currentSeq, clientId);
            }
        }
    }
    blockInsert(pos, refSeq, clientId, seq, localSeq, newSegments) {
        // Keeping this function within the scope of blockInsert for readability.
        // eslint-disable-next-line unicorn/consistent-function-scoping
        const continueFrom = (node) => {
            let siblingExists = false;
            forwardExcursion(node, () => {
                siblingExists = true;
                return false;
            });
            return siblingExists;
        };
        let segmentGroup;
        const saveIfLocal = (locSegment) => {
            // Save segment so we can assign sequence number when acked by server
            if (this.collabWindow.collaborating) {
                if (locSegment.seq === UnassignedSequenceNumber &&
                    clientId === this.collabWindow.clientId) {
                    segmentGroup = this.addToPendingList(locSegment, segmentGroup, localSeq);
                }
                // LocSegment.seq === 0 when coming from SharedSegmentSequence.loadBody()
                // In all other cases this has to be true (checked by addToLRUSet):
                // locSegment.seq > this.collabWindow.currentSeq
                else if (locSegment.seq > this.collabWindow.minSeq &&
                    MergeTree.options.zamboniSegments) {
                    this.addToLRUSet(locSegment, locSegment.seq);
                }
            }
        };
        const onLeaf = (segment, _pos, context) => {
            const segmentChanges = {};
            if (segment) {
                // Insert before segment
                segmentChanges.replaceCurrent = context.candidateSegment;
                segmentChanges.next = segment;
            }
            else {
                segmentChanges.next = context.candidateSegment;
            }
            return segmentChanges;
        };
        const insertInfo = {
            clientId,
            seq,
            localSeq,
        };
        // TODO: build tree from segs and insert all at once
        let insertPos = pos;
        for (const newSegment of newSegments
            .filter((s) => s.cachedLength > 0)
            .map((s) => overwriteInfo(s, insertInfo))) {
            if (Marker.is(newSegment)) {
                const markerId = newSegment.getId();
                if (markerId) {
                    this.idToMarker.set(markerId, newSegment);
                }
            }
            const splitNode = this.insertingWalk(this.root, insertPos, refSeq, clientId, seq, {
                leaf: onLeaf,
                candidateSegment: newSegment,
                continuePredicate: continueFrom,
            });
            if (!isSegmentLeaf(newSegment)) {
                // Indicates an attempt to insert past the end of the merge-tree's content.
                const errorConstructor = localSeq === undefined ? DataProcessingError : UsageError;
                throw new errorConstructor("MergeTree insert failed", {
                    currentSeq: this.collabWindow.currentSeq,
                    minSeq: this.collabWindow.minSeq,
                    segSeq: insertInfo.seq,
                });
            }
            this.updateRoot(splitNode);
            insertPos += newSegment.cachedLength;
            if (!this.options?.mergeTreeEnableObliterate || this.obliterates.empty()) {
                saveIfLocal(newSegment);
                continue;
            }
            let oldest;
            let normalizedOldestSeq = 0;
            let newest;
            let normalizedNewestSeq = 0;
            const movedClientIds = [];
            const movedSeqs = [];
            let newestAcked;
            let oldestUnacked;
            for (const ob of this.obliterates.findOverlapping(newSegment)) {
                // compute a normalized seq that takes into account local seqs
                // but is still comparable to remote seqs to keep the checks below easy
                // REMOTE SEQUENCE NUMBERS                                     LOCAL SEQUENCE NUMBERS
                // [0, 1, 2, 3, ..., 100, ..., 1000, ..., (MAX - MaxLocalSeq), L1, L2, L3, L4, ..., L100, ..., L1000, ...(MAX)]
                const normalizedObSeq = ob.seq === UnassignedSequenceNumber
                    ? Number.MAX_SAFE_INTEGER - this.collabWindow.localSeq + ob.localSeq
                    : ob.seq;
                if (normalizedObSeq > refSeq) {
                    // Any obliterate from the same client that's inserting this segment cannot cause the segment to be marked as
                    // obliterated (since that client must have performed the obliterate before this insertion).
                    // We still need to consider such obliterates when determining the winning obliterate for the insertion point,
                    // see `obliteratePrecedingInsertion` docs.
                    if (clientId !== ob.clientId) {
                        if (oldest === undefined || normalizedOldestSeq > normalizedObSeq) {
                            normalizedOldestSeq = normalizedObSeq;
                            oldest = ob;
                            movedClientIds.unshift(ob.clientId);
                            movedSeqs.unshift(ob.seq);
                        }
                        else {
                            movedClientIds.push(ob.clientId);
                            movedSeqs.push(ob.seq);
                        }
                    }
                    if (newest === undefined || normalizedNewestSeq < normalizedObSeq) {
                        normalizedNewestSeq = normalizedObSeq;
                        newest = ob;
                    }
                    if (ob.seq !== UnassignedSequenceNumber &&
                        (newestAcked === undefined || newestAcked.seq < ob.seq)) {
                        newestAcked = ob;
                    }
                    if (ob.seq === UnassignedSequenceNumber &&
                        (oldestUnacked === undefined || oldestUnacked.localSeq > ob.localSeq)) {
                        // There can be one local obliterate surrounding a segment if a client repeatedly obliterates
                        // a region (ex: in the text ABCDEFG, obliterate D, then obliterate CE, then BF). In this case,
                        // the first one that's applied will be the one that actually removes the segment.
                        oldestUnacked = ob;
                    }
                }
            }
            newSegment.obliteratePrecedingInsertion = newest;
            // See doc comment on obliteratePrecedingInsertion for more details: if the newest obliterate was performed
            // by the same client that's inserting this segment, we let them insert into this range and therefore don't
            // mark it obliterated.
            if (oldest && newest?.clientId !== clientId) {
                let moveInfo;
                if (newestAcked === newest || newestAcked?.clientId !== clientId) {
                    moveInfo = {
                        movedClientIds,
                        movedSeq: oldest.seq,
                        movedSeqs,
                        localMovedSeq: oldestUnacked?.localSeq,
                        wasMovedOnInsert: oldest.seq !== UnassignedSequenceNumber,
                    };
                }
                else {
                    assert(oldestUnacked !== undefined, "Expected local obliterate to be defined if newestAcked is not equal to newest");
                    // There's a pending local obliterate for this range, so it will be marked as obliterated by us. However,
                    // all other clients are under the impression that the most recent acked obliterate won the right to insert
                    // in this range.
                    moveInfo = {
                        movedClientIds: [oldestUnacked.clientId],
                        movedSeq: oldestUnacked.seq,
                        movedSeqs: [oldestUnacked.seq],
                        localMovedSeq: oldestUnacked.localSeq,
                        wasMovedOnInsert: false,
                    };
                }
                overwriteInfo(newSegment, moveInfo);
                if (moveInfo.localMovedSeq !== undefined) {
                    assert(oldestUnacked?.segmentGroup !== undefined, 0x86c /* expected segment group to exist */);
                    this.addToPendingList(newSegment, oldestUnacked?.segmentGroup);
                }
                if (newSegment.parent) {
                    // The incremental update codepath in theory can handle most cases where segments are obliterated upon insertion,
                    // but it's not idempotent with respect to segment insertion in the first place. Since we already update partial
                    // lengths inside the inserting walk, we'd be at risk of double-counting the insertion in any case if we allow
                    // incremental updates here.
                    const newStructure = true;
                    this.blockUpdatePathLengths(newSegment.parent, moveInfo.movedSeq, clientId, newStructure);
                }
            }
            saveIfLocal(newSegment);
        }
    }
    ensureIntervalBoundary(pos, refSeq, clientId) {
        const splitNode = this.insertingWalk(this.root, pos, refSeq, clientId, TreeMaintenanceSequenceNumber, { leaf: this.splitLeafSegment });
        this.updateRoot(splitNode);
    }
    // Assume called only when pos == len
    breakTie(pos, node, seq) {
        if (node.isLeaf()) {
            if (pos !== 0) {
                return false;
            }
            // normalize the seq numbers
            // if the new seg is local (UnassignedSequenceNumber) give it the highest possible
            // seq for comparison, as it will get a seq higher than any other seq once sequences
            // if the current seg is local (UnassignedSequenceNumber) give it the second highest
            // possible seq, as the highest is reserved for the previous.
            const newSeq = seq === UnassignedSequenceNumber ? Number.MAX_SAFE_INTEGER : seq;
            const segSeq = node.seq === UnassignedSequenceNumber ? Number.MAX_SAFE_INTEGER - 1 : (node.seq ?? 0);
            return (newSeq > segSeq ||
                (isMoved(node) && node.movedSeq !== UnassignedSequenceNumber && node.movedSeq > seq) ||
                (isRemoved(node) &&
                    node.removedSeq !== UnassignedSequenceNumber &&
                    node.removedSeq > seq));
        }
        else {
            return true;
        }
    }
    insertingWalk(block, pos, refSeq, clientId, seq, context, isLastChildBlock = true) {
        let _pos = pos;
        const children = block.children;
        let childIndex;
        let child;
        let newNode;
        let fromSplit;
        for (childIndex = 0; childIndex < block.childCount; childIndex++) {
            child = children[childIndex];
            // ensure we walk down the far edge of the tree, even if all sub-tree is eligible for zamboni
            const isLastNonLeafBlock = isLastChildBlock && !child.isLeaf() && childIndex === block.childCount - 1;
            const len = this.nodeLength(child, refSeq, clientId) ?? (isLastChildBlock ? 0 : undefined);
            if (len === undefined) {
                // if the seg len is undefined, the segment
                // will be removed, so should just be skipped for now
                continue;
            }
            assert(len >= 0, 0x4bc /* Length should not be negative */);
            if (_pos < len || (_pos === len && this.breakTie(_pos, child, seq))) {
                // Found entry containing pos
                if (child.isLeaf()) {
                    const segment = child;
                    const segmentChanges = context.leaf(segment, _pos, context);
                    if (segmentChanges.replaceCurrent) {
                        assignChild(block, segmentChanges.replaceCurrent, childIndex, false);
                        segmentChanges.replaceCurrent.ordinal = child.ordinal;
                    }
                    if (segmentChanges.next) {
                        newNode = segmentChanges.next;
                        childIndex++; // Insert after
                    }
                    else {
                        // No change
                        return undefined;
                    }
                }
                else {
                    const childBlock = child;
                    // Internal node
                    const splitNode = this.insertingWalk(childBlock, _pos, refSeq, clientId, seq, context, isLastNonLeafBlock);
                    if (splitNode === undefined) {
                        this.blockUpdateLength(block, seq, clientId);
                        return undefined;
                    }
                    else if (splitNode === MergeTree.theUnfinishedNode) {
                        _pos -= len; // Act as if shifted segment
                        continue;
                    }
                    else {
                        newNode = splitNode;
                        fromSplit = splitNode;
                        childIndex++; // Insert after
                    }
                }
                break;
            }
            else {
                _pos -= len;
            }
        }
        if (!newNode && _pos === 0) {
            if (context.continuePredicate?.(block)) {
                return MergeTree.theUnfinishedNode;
            }
            else {
                const segmentChanges = context.leaf(undefined, _pos, context);
                newNode = segmentChanges.next;
                // Assert segmentChanges.replaceCurrent === undefined
            }
        }
        if (newNode) {
            for (let i = block.childCount; i > childIndex; i--) {
                block.children[i] = block.children[i - 1];
                block.children[i].index = i;
            }
            assignChild(block, newNode, childIndex, false);
            block.childCount++;
            block.setOrdinal(newNode, childIndex);
            if (block.childCount < MaxNodesInBlock) {
                if (fromSplit) {
                    this.nodeUpdateOrdinals(fromSplit);
                }
                this.blockUpdateLength(block, seq, clientId);
                return undefined;
            }
            else {
                // Don't update ordinals because higher block will do it
                const newNodeFromSplit = this.split(block);
                PartialSequenceLengths.options.verifyExpected?.(this, block, refSeq, clientId);
                PartialSequenceLengths.options.verifyExpected?.(this, newNodeFromSplit, refSeq, clientId);
                return newNodeFromSplit;
            }
        }
        else {
            return undefined;
        }
    }
    split(node) {
        const halfCount = MaxNodesInBlock / 2;
        const newNode = this.makeBlock(halfCount);
        node.childCount = halfCount;
        // Update ordinals to reflect lowered child count
        this.nodeUpdateOrdinals(node);
        for (let i = 0; i < halfCount; i++) {
            assignChild(newNode, node.children[halfCount + i], i, false);
            node.children[halfCount + i] = undefined;
        }
        this.nodeUpdateLengthNewStructure(node);
        this.nodeUpdateLengthNewStructure(newNode);
        return newNode;
    }
    nodeUpdateOrdinals(block) {
        for (let i = 0; i < block.childCount; i++) {
            const child = block.children[i];
            block.setOrdinal(child, i);
            if (!child.isLeaf()) {
                this.nodeUpdateOrdinals(child);
            }
        }
    }
    /**
     * Annotate a range with properties
     * @param start - The inclusive start position of the range to annotate
     * @param end - The exclusive end position of the range to annotate
     * @param propsOrAdjust - The properties or adjustments to annotate the range with
     * @param refSeq - The reference sequence number to use to apply the annotate
     * @param clientId - The id of the client making the annotate
     * @param seq - The sequence number of the annotate operation
     * @param opArgs - The op args for the annotate op. this is passed to the merge tree callback if there is one
     * @param rollback - Whether this is for a local rollback and what kind
     */
    annotateRange(start, end, propsOrAdjust, refSeq, clientId, seq, opArgs, rollback = PropertiesRollback.None) {
        if (propsOrAdjust.adjust !== undefined) {
            errorIfOptionNotTrue(this.options, "mergeTreeEnableAnnotateAdjust");
        }
        this.ensureIntervalBoundary(start, refSeq, clientId);
        this.ensureIntervalBoundary(end, refSeq, clientId);
        const deltaSegments = [];
        const localSeq = seq === UnassignedSequenceNumber ? ++this.collabWindow.localSeq : undefined;
        let segmentGroup;
        const opObj = propsOrAdjust.props ?? propsOrAdjust.adjust;
        const annotateSegment = (segment) => {
            assert(!Marker.is(segment) ||
                !(reservedMarkerIdKey in opObj) ||
                opObj.markerId === segment.properties?.markerId, 0x5ad /* Cannot change the markerId of an existing marker */);
            const propertyManager = (segment.propertyManager ??= new PropertiesManager());
            const propertyDeltas = propertyManager.handleProperties(propsOrAdjust, segment, seq, this.collabWindow.minSeq, this.collabWindow.collaborating, rollback);
            if (!isRemovedOrMoved(segment)) {
                deltaSegments.push({ segment, propertyDeltas });
            }
            if (this.collabWindow.collaborating) {
                if (seq === UnassignedSequenceNumber) {
                    segmentGroup = this.addToPendingList(segment, segmentGroup, localSeq, propertyDeltas);
                }
                else {
                    if (MergeTree.options.zamboniSegments) {
                        this.addToLRUSet(segment, seq);
                    }
                }
            }
            return true;
        };
        this.nodeMap(refSeq, clientId, annotateSegment, undefined, start, end);
        // OpArgs == undefined => test code
        if (deltaSegments.length > 0) {
            this.mergeTreeDeltaCallback?.(opArgs, {
                operation: MergeTreeDeltaType.ANNOTATE,
                deltaSegments,
            });
        }
        if (this.collabWindow.collaborating &&
            seq !== UnassignedSequenceNumber &&
            MergeTree.options.zamboniSegments) {
            zamboniSegments(this);
        }
    }
    obliterateRangeSided(start, end, refSeq, clientId, seq, opArgs) {
        const startPos = start.side === Side.Before ? start.pos : start.pos + 1;
        const endPos = end.side === Side.Before ? end.pos : end.pos + 1;
        this.ensureIntervalBoundary(startPos, refSeq, clientId);
        this.ensureIntervalBoundary(endPos, refSeq, clientId);
        let _overwrite = false;
        const localOverlapWithRefs = [];
        const movedSegments = [];
        const localSeq = seq === UnassignedSequenceNumber ? ++this.collabWindow.localSeq : undefined;
        const obliterate = {
            clientId,
            end: createDetachedLocalReferencePosition(undefined),
            refSeq,
            seq,
            start: createDetachedLocalReferencePosition(undefined),
            localSeq,
            segmentGroup: undefined,
        };
        const { segment: startSeg } = this.getContainingSegment(start.pos, refSeq, clientId);
        const { segment: endSeg } = this.getContainingSegment(end.pos, refSeq, clientId);
        assert(isSegmentLeaf(startSeg) && isSegmentLeaf(endSeg), 0xa3f /* segments cannot be undefined */);
        obliterate.start = this.createLocalReferencePosition(startSeg, start.side === Side.Before ? 0 : Math.max(startSeg.cachedLength - 1, 0), ReferenceType.StayOnRemove, {
            obliterate,
        });
        obliterate.end = this.createLocalReferencePosition(endSeg, end.side === Side.Before ? 0 : Math.max(endSeg.cachedLength - 1, 0), ReferenceType.StayOnRemove, {
            obliterate,
        });
        // Always create a segment group for obliterate,
        // even if there are no segments currently in the obliteration range.
        // Segments may be concurrently inserted into the obliteration range,
        // at which point they are added to the segment group.
        obliterate.segmentGroup = {
            segments: [],
            localSeq,
            refSeq: this.collabWindow.currentSeq,
            obliterateInfo: obliterate,
        };
        if (this.collabWindow.collaborating && clientId === this.collabWindow.clientId) {
            this.pendingSegments.push(obliterate.segmentGroup);
        }
        this.obliterates.addOrUpdate(obliterate);
        const markMoved = (segment, pos) => {
            if ((start.side === Side.After && startPos === pos + segment.cachedLength) || // exclusive start segment
                (end.side === Side.Before &&
                    endPos === pos &&
                    // TODO:AB#29765: The clientId check here should be handled by isSegmentPresent and/or PerspectiveImpl
                    (segment.clientId === clientId || isSegmentPresent(segment, { refSeq, localSeq }))) // exclusive end segment
            ) {
                // We walk these segments because we want to also walk any concurrently inserted segments between here and the obliterated segments.
                // These segments are outside of the obliteration range though, so return true to keep walking.
                return true;
            }
            const existingMoveInfo = toMoveInfo(segment);
            // The "last-to-obliterate-gets-to-insert" policy described by the doc comment on `obliteratePrecedingInsertion`
            // is mostly handled by logic at insertion time, but we need a small bit of handling here.
            // Specifically, we want to avoid marking a local-only segment as obliterated when we know one of our own local obliterates
            // will win against the obliterate we're processing, hence the early exit.
            if (segment.seq === UnassignedSequenceNumber &&
                segment.obliteratePrecedingInsertion?.seq === UnassignedSequenceNumber &&
                seq !== UnassignedSequenceNumber) {
                // We chose to not obliterate this segment because we are aware of an unacked local obliteration.
                // The local obliterate has not been sequenced yet, so it is still the newest obliterate we are aware of.
                // Other clients will also choose not to obliterate this segment because the most recent obliteration has the same clientId
                return true;
            }
            // Partial lengths incrementality is not supported for overlapping obliterate/removes.
            _overwrite ||= existingMoveInfo !== undefined || toRemovalInfo(segment) !== undefined;
            if (existingMoveInfo === undefined) {
                const movedSeg = overwriteInfo(segment, {
                    movedClientIds: [clientId],
                    movedSeq: seq,
                    localMovedSeq: localSeq,
                    movedSeqs: [seq],
                    wasMovedOnInsert: segment.seq === UnassignedSequenceNumber && seq !== UnassignedSequenceNumber,
                });
                const existingRemoval = toRemovalInfo(movedSeg);
                if (existingRemoval === undefined) {
                    movedSegments.push(movedSeg);
                }
                else if (existingRemoval.removedSeq === UnassignedSequenceNumber &&
                    segment.localRefs?.empty === false) {
                    // We removed this locally already so we don't need to event it again, but it might have references
                    // that need sliding now that a move may have been acked.
                    localOverlapWithRefs.push(segment);
                }
            }
            else {
                if (existingMoveInfo.movedSeq === UnassignedSequenceNumber) {
                    // Should not need explicit set here, but this should be implied:
                    assert(!existingMoveInfo.wasMovedOnInsert, 0xab4 /* Local obliterate cannot have removed a segment as soon as it was inserted */);
                    assert(seq !== UnassignedSequenceNumber, 0xab5 /* Cannot obliterate the same segment locally twice */);
                    existingMoveInfo.wasMovedOnInsert = segment.seq === UnassignedSequenceNumber;
                    // we moved this locally, but someone else moved it first
                    // so put them at the head of the list
                    // The list isn't ordered, but we keep the first move at the head
                    // for partialLengths bookkeeping purposes
                    existingMoveInfo.movedClientIds.unshift(clientId);
                    existingMoveInfo.movedSeq = seq;
                    existingMoveInfo.movedSeqs.unshift(seq);
                    if (segment.localRefs?.empty === false) {
                        localOverlapWithRefs.push(segment);
                    }
                }
                else {
                    // Do not replace earlier sequence number for move
                    existingMoveInfo.movedClientIds.push(clientId);
                    existingMoveInfo.movedSeqs.push(seq);
                }
            }
            assertMoved(segment);
            // Save segment so can assign moved sequence number when acked by server
            if (this.collabWindow.collaborating) {
                if (segment.movedSeq === UnassignedSequenceNumber &&
                    clientId === this.collabWindow.clientId) {
                    obliterate.segmentGroup = this.addToPendingList(segment, obliterate.segmentGroup, localSeq);
                }
                else {
                    if (MergeTree.options.zamboniSegments) {
                        this.addToLRUSet(segment, seq);
                    }
                }
            }
            return true;
        };
        const afterMarkMoved = (node) => {
            if (_overwrite) {
                this.nodeUpdateLengthNewStructure(node);
            }
            else {
                this.blockUpdateLength(node, seq, clientId);
            }
            return true;
        };
        this.nodeMap(refSeq, clientId, markMoved, afterMarkMoved, start.pos, end.pos + 1, // include the segment containing the end reference
        undefined, seq === UnassignedSequenceNumber ? undefined : seq);
        this.slideAckedRemovedSegmentReferences(localOverlapWithRefs);
        // opArgs == undefined => test code
        if (start.pos !== end.pos || start.side !== end.side) {
            this.mergeTreeDeltaCallback?.(opArgs, {
                operation: MergeTreeDeltaType.OBLITERATE,
                deltaSegments: movedSegments.map((segment) => ({ segment })),
            });
        }
        // these events are newly removed
        // so we slide after eventing in case the consumer wants to make reference
        // changes at remove time, like add a ref to track undo redo.
        if (!this.collabWindow.collaborating || clientId !== this.collabWindow.clientId) {
            this.slideAckedRemovedSegmentReferences(movedSegments);
        }
        if (this.collabWindow.collaborating &&
            seq !== UnassignedSequenceNumber &&
            MergeTree.options.zamboniSegments) {
            zamboniSegments(this);
        }
    }
    obliterateRange(start, end, refSeq, clientId, seq, opArgs) {
        errorIfOptionNotTrue(this.options, "mergeTreeEnableObliterate");
        if (this.options?.mergeTreeEnableSidedObliterate) {
            assert(typeof start === "object" && typeof end === "object", 0xa45 /* Start and end must be of type InteriorSequencePlace if mergeTreeEnableSidedObliterate is enabled. */);
            this.obliterateRangeSided(start, end, refSeq, clientId, seq, opArgs);
        }
        else {
            assert(typeof start === "number" && typeof end === "number", 0xa46 /* Start and end must be numbers if mergeTreeEnableSidedObliterate is not enabled. */);
            this.obliterateRangeSided({ pos: start, side: Side.Before }, { pos: end - 1, side: Side.After }, refSeq, clientId, seq, opArgs);
        }
    }
    markRangeRemoved(start, end, refSeq, clientId, seq, opArgs) {
        let _overwrite = false;
        this.ensureIntervalBoundary(start, refSeq, clientId);
        this.ensureIntervalBoundary(end, refSeq, clientId);
        let segmentGroup;
        const removedSegments = [];
        const localOverlapWithRefs = [];
        const localSeq = seq === UnassignedSequenceNumber ? ++this.collabWindow.localSeq : undefined;
        const markRemoved = (segment, pos, _start, _end) => {
            const existingRemovalInfo = toRemovalInfo(segment);
            // Partial lengths incrementality is not supported for overlapping obliterate/removes.
            _overwrite ||= existingRemovalInfo !== undefined || toMoveInfo(segment) !== undefined;
            if (existingRemovalInfo === undefined) {
                const removed = overwriteInfo(segment, {
                    removedClientIds: [clientId],
                    removedSeq: seq,
                    localRemovedSeq: localSeq,
                });
                const existingMoveInfo = toMoveInfo(removed);
                if (existingMoveInfo === undefined) {
                    removedSegments.push(removed);
                }
                else if (existingMoveInfo.movedSeq === UnassignedSequenceNumber &&
                    segment.localRefs?.empty === false) {
                    // We moved this locally already so we don't need to event it again, but it might have references
                    // that need sliding now that a remove may have been acked.
                    localOverlapWithRefs.push(segment);
                }
            }
            else {
                if (existingRemovalInfo.removedSeq === UnassignedSequenceNumber) {
                    // we removed this locally, but someone else removed it first
                    // so put them at the head of the list
                    // The list isn't ordered, but we keep the first removal at the head
                    // for partialLengths bookkeeping purposes
                    existingRemovalInfo.removedClientIds.unshift(clientId);
                    existingRemovalInfo.removedSeq = seq;
                    if (segment.localRefs?.empty === false) {
                        localOverlapWithRefs.push(segment);
                    }
                }
                else {
                    // Do not replace earlier sequence number for remove
                    existingRemovalInfo.removedClientIds.push(clientId);
                }
            }
            assertRemoved(segment);
            // Save segment so we can assign removed sequence number when acked by server
            if (this.collabWindow.collaborating) {
                if (segment.removedSeq === UnassignedSequenceNumber &&
                    clientId === this.collabWindow.clientId) {
                    segmentGroup = this.addToPendingList(segment, segmentGroup, localSeq);
                }
                else {
                    if (MergeTree.options.zamboniSegments) {
                        this.addToLRUSet(segment, seq);
                    }
                }
            }
            return true;
        };
        const afterMarkRemoved = (node) => {
            if (_overwrite) {
                this.nodeUpdateLengthNewStructure(node);
            }
            else {
                this.blockUpdateLength(node, seq, clientId);
            }
            return true;
        };
        this.nodeMap(refSeq, clientId, markRemoved, afterMarkRemoved, start, end);
        // these segments are already viewed as being removed locally and are not event-ed
        // so can slide non-StayOnRemove refs immediately
        this.slideAckedRemovedSegmentReferences(localOverlapWithRefs);
        // opArgs == undefined => test code
        if (removedSegments.length > 0) {
            this.mergeTreeDeltaCallback?.(opArgs, {
                operation: MergeTreeDeltaType.REMOVE,
                deltaSegments: removedSegments.map((segment) => ({ segment })),
            });
        }
        // these events are newly removed
        // so we slide after eventing in case the consumer wants to make reference
        // changes at remove time, like add a ref to track undo redo.
        if (!this.collabWindow.collaborating || clientId !== this.collabWindow.clientId) {
            this.slideAckedRemovedSegmentReferences(removedSegments);
        }
        if (this.collabWindow.collaborating &&
            seq !== UnassignedSequenceNumber &&
            MergeTree.options.zamboniSegments) {
            zamboniSegments(this);
        }
    }
    /**
     * Revert an unacked local op
     */
    rollback(op, localOpMetadata) {
        if (op.type === MergeTreeDeltaType.REMOVE) {
            const pendingSegmentGroup = this.pendingSegments.pop()?.data;
            if (pendingSegmentGroup === undefined || pendingSegmentGroup !== localOpMetadata) {
                throw new Error("Rollback op doesn't match last edit");
            }
            // Disabling because a for of loop causes the type of segment to be ISegmentLeaf, which does not have parent information stored
            // eslint-disable-next-line unicorn/no-array-for-each
            pendingSegmentGroup.segments.forEach((segment) => {
                const segmentSegmentGroup = segment?.segmentGroups?.pop();
                assert(segmentSegmentGroup === pendingSegmentGroup, 0x3ee /* Unexpected segmentGroup in segment */);
                assert(isRemoved(segment) && segment.removedClientIds[0] === this.collabWindow.clientId, 0x39d /* Rollback segment removedClientId does not match local client */);
                let updateNode = segment.parent;
                removeRemovalInfo(segment);
                for (updateNode; updateNode !== undefined; updateNode = updateNode.parent) {
                    this.blockUpdateLength(updateNode, UnassignedSequenceNumber, this.collabWindow.clientId);
                }
                // Note: optional chaining short-circuits:
                // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining#short-circuiting
                this.mergeTreeDeltaCallback?.({ op: createInsertSegmentOp(this.findRollbackPosition(segment), segment) }, {
                    operation: MergeTreeDeltaType.INSERT,
                    deltaSegments: [{ segment }],
                });
            });
        }
        else if (op.type === MergeTreeDeltaType.INSERT ||
            op.type === MergeTreeDeltaType.ANNOTATE) {
            const pendingSegmentGroup = this.pendingSegments.pop()?.data;
            if (pendingSegmentGroup === undefined ||
                pendingSegmentGroup !== localOpMetadata ||
                (op.type === MergeTreeDeltaType.ANNOTATE && !pendingSegmentGroup.previousProps)) {
                throw new Error("Rollback op doesn't match last edit");
            }
            let i = 0;
            for (const segment of pendingSegmentGroup.segments) {
                const segmentSegmentGroup = segment?.segmentGroups?.pop();
                assert(segmentSegmentGroup === pendingSegmentGroup, 0x3ef /* Unexpected segmentGroup in segment */);
                const start = this.findRollbackPosition(segment);
                if (op.type === MergeTreeDeltaType.INSERT) {
                    segment.seq = UniversalSequenceNumber;
                    segment.localSeq = undefined;
                    const removeOp = createRemoveRangeOp(start, start + segment.cachedLength);
                    this.markRangeRemoved(start, start + segment.cachedLength, UniversalSequenceNumber, this.collabWindow.clientId, UniversalSequenceNumber, { op: removeOp });
                } /* op.type === MergeTreeDeltaType.ANNOTATE */
                else {
                    const props = pendingSegmentGroup.previousProps[i];
                    const annotateOp = createAnnotateRangeOp(start, start + segment.cachedLength, props);
                    this.annotateRange(start, start + segment.cachedLength, { props }, UniversalSequenceNumber, this.collabWindow.clientId, UniversalSequenceNumber, { op: annotateOp }, PropertiesRollback.Rollback);
                    i++;
                }
            }
        }
        else {
            throw new Error("Unsupported op type for rollback");
        }
    }
    /**
     * Walk the segments up to the current segment and calculate its position
     */
    findRollbackPosition(segment) {
        let segmentPosition = 0;
        walkAllChildSegments(this.root, (seg) => {
            // If we've found the desired segment, terminate the walk and return 'segmentPosition'.
            if (seg === segment) {
                return false;
            }
            // If not removed, increase position
            if (!isRemoved(seg)) {
                segmentPosition += seg.cachedLength;
            }
            return true;
        });
        return segmentPosition;
    }
    nodeUpdateLengthNewStructure(node, recur = false) {
        this.blockUpdate(node);
        if (this.collabWindow.collaborating) {
            this.localPartialsComputed = false;
            node.partialLengths = PartialSequenceLengths.combine(node, this.collabWindow, recur);
        }
    }
    removeLocalReferencePosition(lref) {
        const segment = lref.getSegment();
        return segment?.localRefs?.removeLocalRef(lref);
    }
    createLocalReferencePosition(_segment, offset, refType, properties, slidingPreference, canSlideToEndpoint) {
        if (_segment !== "start" &&
            _segment !== "end" &&
            isRemovedAndAckedOrMovedAndAcked(_segment) &&
            !refTypeIncludesFlag(refType, ReferenceType.SlideOnRemove | ReferenceType.Transient | ReferenceType.StayOnRemove) &&
            _segment.endpointType === undefined) {
            throw new UsageError("Can only create SlideOnRemove or Transient local reference position on a removed or obliterated segment");
        }
        let segment;
        if (_segment === "start") {
            segment = this.startOfTree;
        }
        else if (_segment === "end") {
            segment = this.endOfTree;
        }
        else {
            assertSegmentLeaf(_segment);
            segment = _segment;
        }
        const localRefs = LocalReferenceCollection.setOrGet(segment);
        const segRef = localRefs.createLocalRef(offset, refType, properties, slidingPreference, canSlideToEndpoint);
        return segRef;
    }
    // Segments should either be removed remotely, removed locally, or inserted locally
    normalizeAdjacentSegments(affectedSegments) {
        // Eagerly demand this since we're about to shift elements in the list around
        const currentOrder = Array.from(affectedSegments, ({ data: seg }) => ({
            parent: seg.parent,
            index: seg.index,
            ordinal: seg.ordinal,
        }));
        // Last segment which was not affected locally.
        let lastLocalSegment = affectedSegments.last;
        while (lastLocalSegment !== undefined && isRemovedAndAcked(lastLocalSegment.data)) {
            lastLocalSegment = lastLocalSegment.prev;
        }
        if (!lastLocalSegment) {
            return;
        }
        for (let segmentToSlide = lastLocalSegment, nearerSegment = lastLocalSegment?.prev; segmentToSlide !== undefined; segmentToSlide = nearerSegment, nearerSegment = nearerSegment?.prev) {
            // Slide iterCur forward as far as possible
            if (isRemovedAndAcked(segmentToSlide.data)) {
                // Slide past all segments that are not also remotely removed
                affectedSegments.remove(segmentToSlide);
                affectedSegments.insertAfter(lastLocalSegment, segmentToSlide.data);
            }
            else if (isRemoved(segmentToSlide.data)) {
                assert(segmentToSlide.data.localRemovedSeq !== undefined, 0x54d /* Removed segment that hasnt had its removal acked should be locally removed */);
                // Slide each locally removed item past all segments that have localSeq > lremoveItem.localSeq
                // but not past remotely removed segments;
                let cur = segmentToSlide;
                let scan = cur.next;
                while (scan !== undefined &&
                    !isRemovedAndAcked(scan.data) &&
                    scan.data.localSeq !== undefined &&
                    scan.data.localSeq > segmentToSlide.data.localRemovedSeq) {
                    cur = scan;
                    scan = scan.next;
                }
                if (cur !== segmentToSlide) {
                    affectedSegments.remove(segmentToSlide);
                    affectedSegments.insertAfter(cur, segmentToSlide.data);
                }
            }
        }
        const newOrder = Array.from(affectedSegments, ({ data }) => data);
        for (const seg of newOrder)
            seg.localRefs?.walkReferences((lref) => lref.callbacks?.beforeSlide?.(lref));
        const perSegmentTrackingGroups = new Map();
        for (const segment of newOrder) {
            const { trackingCollection } = segment;
            const trackingGroups = [...trackingCollection.trackingGroups];
            perSegmentTrackingGroups.set(segment, trackingGroups);
            for (const group of trackingCollection.trackingGroups) {
                trackingCollection.unlink(group);
            }
        }
        for (let i = 0; i < newOrder.length; i++) {
            const seg = newOrder[i];
            const { parent, index, ordinal } = currentOrder[i];
            assignChild(parent, seg, index, false);
            seg.ordinal = ordinal;
        }
        for (const [segment, groups] of perSegmentTrackingGroups.entries()) {
            for (const group of groups) {
                segment.trackingCollection.link(group);
            }
        }
        // Finally, update internal node bookkeeping on ancestors of the swapped nodes.
        // Toposort would improve this by a log factor, but probably not worth the added code size
        const depths = new Map();
        const computeDepth = (block) => {
            if (!depths.has(block)) {
                depths.set(block, block.parent === undefined ? 0 : 1 + computeDepth(block.parent));
            }
            return depths.get(block);
        };
        for (const element of newOrder) {
            computeDepth(element);
        }
        for (const [node] of [...depths.entries()].sort((a, b) => b[1] - a[1])) {
            if (!node.isLeaf()) {
                this.nodeUpdateLengthNewStructure(node);
            }
        }
        for (const seg of newOrder)
            seg.localRefs?.walkReferences((lref) => lref.callbacks?.afterSlide?.(lref));
    }
    /**
     * Normalizes the segments nearby `segmentGroup` to be ordered as they would if the op submitting `segmentGroup`
     * is rebased to the current sequence number.
     * This primarily affects the ordering of adjacent segments that were removed between the original submission of
     * the local ops and now.
     * Consider the following sequence of events:
     * Initial state: "hi my friend" (seq: 0)
     * - Client 1 inserts "good " to make "hi my good friend" (op1, refSeq: 0)
     * - Client 2 deletes "my " to make "hi friend" (op2, refSeq: 0)
     * - op2 is sequenced giving seq 1
     * - Client 1 disconnects and reconnects at seq: 1.
     *
     * At this point in time, client 1 will have segments ["hi ", Removed"my ", Local"good ", "friend"].
     * However, the rebased op that it submits will cause client 2 to have segments
     * ["hi ", Local"good ", Removed"my ", "friend"].
     *
     * The difference in ordering can be problematic for tie-breaking concurrently inserted segments in some scenarios.
     * Rather than incur extra work tie-breaking these scenarios for all clients, when client 1 rebases its operation,
     * it can fix up its local state to align with what would be expected of the op it resubmits.
     */
    normalizeSegmentsOnRebase() {
        let currentRangeToNormalize = new DoublyLinkedList();
        let rangeContainsLocalSegs = false;
        let rangeContainsRemoteRemovedSegs = false;
        const normalize = () => {
            if (rangeContainsLocalSegs &&
                rangeContainsRemoteRemovedSegs &&
                currentRangeToNormalize.length > 1) {
                this.normalizeAdjacentSegments(currentRangeToNormalize);
            }
        };
        walkAllChildSegments(this.root, (seg) => {
            if (isRemoved(seg) || seg.seq === UnassignedSequenceNumber) {
                if (isRemovedAndAcked(seg)) {
                    rangeContainsRemoteRemovedSegs = true;
                }
                if (seg.seq === UnassignedSequenceNumber) {
                    rangeContainsLocalSegs = true;
                }
                currentRangeToNormalize.push(seg);
            }
            else {
                normalize();
                currentRangeToNormalize = new DoublyLinkedList();
                rangeContainsLocalSegs = false;
                rangeContainsRemoteRemovedSegs = false;
            }
            return true;
        });
        normalize();
    }
    blockUpdate(block) {
        let len;
        const rightmostTiles = createMap();
        const leftmostTiles = createMap();
        for (let i = 0; i < block.childCount; i++) {
            const node = block.children[i];
            const nodeLength = nodeTotalLength(this, node);
            if (nodeLength !== undefined) {
                len ??= 0;
                len += nodeLength;
            }
            if (node.isLeaf()) {
                const segment = node;
                if ((this.localNetLength(segment) ?? 0) > 0 && Marker.is(segment)) {
                    const markerId = segment.getId();
                    // Also in insertMarker but need for reload segs case
                    // can add option for this only from reload segs
                    if (markerId) {
                        this.idToMarker.set(markerId, segment);
                    }
                    if (refTypeIncludesFlag(segment, ReferenceType.Tile)) {
                        const tileLabels = refGetTileLabels(segment);
                        if (tileLabels) {
                            for (const tileLabel of tileLabels) {
                                // this depends on walking children in order
                                // The later, and right most children overwrite
                                // whereas early, and left most do not overwrite
                                rightmostTiles[tileLabel] = segment;
                                leftmostTiles[tileLabel] ??= segment;
                            }
                        }
                    }
                }
            }
            else {
                extend(rightmostTiles, node.rightmostTiles);
                extendIfUndefined(leftmostTiles, node.leftmostTiles);
            }
        }
        block.leftmostTiles = leftmostTiles;
        block.rightmostTiles = rightmostTiles;
        block.cachedLength = len;
    }
    blockUpdatePathLengths(startBlock, seq, clientId, newStructure = false) {
        let block = startBlock;
        while (block !== undefined) {
            if (newStructure) {
                this.nodeUpdateLengthNewStructure(block);
            }
            else {
                this.blockUpdateLength(block, seq, clientId);
            }
            block = block.parent;
        }
    }
    blockUpdateLength(node, seq, clientId) {
        this.blockUpdate(node);
        this.localPartialsComputed = false;
        if (this.collabWindow.collaborating &&
            seq !== UnassignedSequenceNumber &&
            seq !== TreeMaintenanceSequenceNumber) {
            if (node.partialLengths !== undefined &&
                MergeTree.options.incrementalUpdate &&
                clientId !== NonCollabClient) {
                node.partialLengths.update(node, seq, clientId, this.collabWindow);
            }
            else {
                node.partialLengths = PartialSequenceLengths.combine(node, this.collabWindow);
            }
            PartialSequenceLengths.options.verifyExpected?.(this, node, seq, clientId);
        }
    }
    /**
     * Map over all visible segments in a given range
     *
     * A segment is visible if its length is greater than 0
     *
     * See `this.nodeMap` for additional documentation
     */
    mapRange(handler, refSeq, clientId, accum, start, end, splitRange = false, visibilitySeq = refSeq) {
        if (splitRange) {
            if (start) {
                this.ensureIntervalBoundary(start, refSeq, clientId);
            }
            if (end) {
                this.ensureIntervalBoundary(end, refSeq, clientId);
            }
        }
        this.nodeMap(refSeq, clientId, (seg, pos, _start, _end) => handler(seg, pos, refSeq, clientId, _start, _end, accum), undefined, start, end, undefined, visibilitySeq);
    }
    /**
     * Map over all visible segments in a given range
     *
     * A segment is visible if its length is greater than 0
     *
     * @param refSeq - The sequence number used to determine the range (start
     * and end positions) of segments to iterate over.
     *
     * @param visibilitySeq - An additional sequence number to further configure
     * segment visibility during traversal. This is the same as refSeq, except
     * in the case of obliterate.
     *
     * In the case where `refSeq == visibilitySeq`, mapping is done on all
     * visible segments from `start` to `end`.
     *
     * If a segment is invisible at both `visibilitySeq` and `refSeq`, then it
     * will not be traversed and mapped. Otherwise, if the segment is visible at
     * either seq, it will be mapped.
     *
     * If a segment is only visible at `visibilitySeq`, it will still be mapped,
     * but it will not count as a segment within the range. That is, it will be
     * ignored for the purposes of tracking when traversal should end.
     */
    nodeMap(refSeq, clientId, leaf, post, start = 0, end, localSeq, visibilitySeq = refSeq) {
        const endPos = end ?? this.nodeLength(this.root, refSeq, clientId, localSeq) ?? 0;
        if (endPos === start) {
            return;
        }
        let pos = 0;
        depthFirstNodeWalk(this.root, this.root.children[0], (node) => {
            if (endPos <= pos) {
                return NodeAction.Exit;
            }
            const len = this.nodeLength(node, visibilitySeq, clientId, localSeq);
            const lenAtRefSeq = (visibilitySeq === refSeq
                ? len
                : this.nodeLength(node, refSeq, clientId, localSeq)) ?? 0;
            const isUnackedAndInObliterate = visibilitySeq !== refSeq &&
                (!node.isLeaf() || node.seq === UnassignedSequenceNumber);
            if ((len === undefined && lenAtRefSeq === 0) ||
                (len === 0 && !isUnackedAndInObliterate && lenAtRefSeq === 0)) {
                return NodeAction.Skip;
            }
            const nextPos = pos + lenAtRefSeq;
            // start is beyond the current node, so we can skip it
            if (start >= nextPos) {
                pos = nextPos;
                return NodeAction.Skip;
            }
            if (node.isLeaf()) {
                if (leaf(node, pos, start - pos, endPos - pos) === false) {
                    return NodeAction.Exit;
                }
                pos = nextPos;
            }
        }, undefined, post);
    }
}
MergeTree.options = {
    incrementalUpdate: true,
    insertAfterRemovedSegs: true,
    zamboniSegments: true,
};
MergeTree.theUnfinishedNode = { childCount: -1 };
//# sourceMappingURL=mergeTree.js.map