/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { assert, unreachableCase } from "@fluidframework/common-utils";
import { addToNestedSet, fail, getOrAddEmptyToMap, getOrAddInNestedMap, nestedSetContains, StackyIterator, tryGetFromNestedMap, } from "../../util";
import { CrossFieldTarget, } from "../modular-schema";
import { NoopMarkType, } from "./format";
import { MarkListFactory } from "./markListFactory";
import { applyMoveEffectsToMark, getMoveEffect, isMoveMark, makeMergeable, splitMove, } from "./moveEffectTable";
export function isModify(mark) {
    return mark.type === "Modify";
}
export function isNewAttach(mark) {
    return mark.type === "Insert" || mark.type === "MoveIn";
}
export function isAttach(mark) {
    return isNewAttach(mark) || isReattach(mark);
}
export function isReattach(mark) {
    return mark.type === "Revive" || mark.type === "ReturnTo";
}
export function isActiveReattach(mark) {
    // No need to check Reattach.lastDeletedBy because it can only be set if the mark is conflicted
    return isReattach(mark) && !isConflictedReattach(mark);
}
// TODO: Name is misleading
export function isConflictedReattach(mark) {
    return isReattach(mark) && isReattachConflicted(mark);
}
// TODO: Name is misleading
export function isReattachConflicted(mark) {
    return (mark.detachEvent === undefined ||
        (mark.inverseOf !== undefined && mark.inverseOf !== mark.detachEvent.revision));
}
export function isReturnMuted(mark) {
    var _a;
    return (_a = mark.isSrcConflicted) !== null && _a !== void 0 ? _a : isReattachConflicted(mark);
}
export function areEqualDetachEvents(a, b) {
    return a.index === b.index && a.revision === b.revision;
}
export function getCellId(mark, revision) {
    var _a;
    if (isNewAttach(mark)) {
        const rev = (_a = mark.revision) !== null && _a !== void 0 ? _a : revision;
        if (rev !== undefined) {
            // TODO: Should use an ID for this attach instead of incorrectly specifying the index as 0.
            return { revision: rev, index: 0 };
        }
        return undefined;
    }
    return mark.detachEvent;
}
export function cloneMark(mark) {
    const clone = Object.assign({}, mark);
    if (clone.type === "Insert" || clone.type === "Revive") {
        clone.content = [...clone.content];
    }
    if (isAttach(clone) && clone.lineage !== undefined) {
        clone.lineage = [...clone.lineage];
    }
    return clone;
}
/**
 * @returns `true` iff `lhs` and `rhs`'s `HasTiebreakPolicy` fields are structurally equal.
 */
export function isEqualPlace(lhs, rhs) {
    var _a, _b;
    return (lhs.heed === rhs.heed &&
        lhs.tiebreak === rhs.tiebreak &&
        areSameLineage((_a = lhs.lineage) !== null && _a !== void 0 ? _a : [], (_b = rhs.lineage) !== null && _b !== void 0 ? _b : []));
}
function areSameLineage(lineage1, lineage2) {
    if (lineage1.length !== lineage2.length) {
        return false;
    }
    for (let i = 0; i < lineage1.length; i++) {
        const event1 = lineage1[i];
        const event2 = lineage2[i];
        if (event1.revision !== event2.revision || event1.offset !== event2.offset) {
            return false;
        }
    }
    return true;
}
/**
 * @param mark - The mark to get the length of.
 * @param ignorePairing - When true, the length of a paired mark (e.g. MoveIn/MoveOut) whose matching mark is not active
 * will be treated the same as if the matching mark were active.
 * @returns The number of nodes within the output context of the mark.
 */
export function getOutputLength(mark, ignorePairing = false) {
    return areOutputCellsEmpty(mark) ? 0 : getMarkLength(mark);
}
/**
 * @param mark - The mark to get the length of.
 * @returns The number of nodes within the input context of the mark.
 */
export function getInputLength(mark) {
    return areInputCellsEmpty(mark) ? 0 : getMarkLength(mark);
}
export function markEmptiesCells(mark) {
    return !areInputCellsEmpty(mark) && areOutputCellsEmpty(mark);
}
export function markFillsCells(mark) {
    return areInputCellsEmpty(mark) && !areOutputCellsEmpty(mark);
}
export function markHasCellEffect(mark) {
    return areInputCellsEmpty(mark) !== areOutputCellsEmpty(mark);
}
export function isExistingCellMark(mark) {
    const type = mark.type;
    switch (type) {
        case NoopMarkType:
        case "Delete":
        case "Modify":
        case "MoveOut":
        case "ReturnFrom":
        case "ReturnTo":
        case "Revive":
            return true;
        case "Insert":
        case "MoveIn":
            return false;
        default:
            unreachableCase(type);
    }
}
export function areInputCellsEmpty(mark) {
    if (isNewAttach(mark)) {
        return true;
    }
    return mark.detachEvent !== undefined;
}
export function areOutputCellsEmpty(mark) {
    var _a, _b;
    const type = mark.type;
    switch (type) {
        case NoopMarkType:
        case "Insert":
            return false;
        case "MoveIn":
            return (_a = mark.isSrcConflicted) !== null && _a !== void 0 ? _a : false;
        case "Delete":
        case "MoveOut":
            return true;
        case "Modify":
            return mark.detachEvent !== undefined;
        case "ReturnFrom":
            return mark.detachEvent !== undefined || !mark.isDstConflicted;
        case "ReturnTo":
            return (mark.detachEvent !== undefined &&
                (((_b = mark.isSrcConflicted) !== null && _b !== void 0 ? _b : false) || isReattachConflicted(mark)));
        case "Revive":
            return mark.detachEvent !== undefined && isReattachConflicted(mark);
        default:
            unreachableCase(type);
    }
}
export function getMarkLength(mark) {
    const type = mark.type;
    switch (type) {
        case "Insert":
            return mark.content.length;
        case "Modify":
            return 1;
        case NoopMarkType:
        case "Delete":
        case "MoveIn":
        case "MoveOut":
        case "ReturnFrom":
        case "ReturnTo":
        case "Revive":
            return mark.count;
        default:
            unreachableCase(type);
    }
}
export function isNoopMark(mark) {
    return mark.type === NoopMarkType;
}
export function getOffsetAtRevision(lineage, reattachRevision) {
    if (lineage === undefined || reattachRevision === undefined) {
        return undefined;
    }
    for (const event of lineage) {
        if (event.revision === reattachRevision) {
            return event.offset;
        }
    }
    return undefined;
}
export function isDetachMark(mark) {
    const type = mark === null || mark === void 0 ? void 0 : mark.type;
    return type === "Delete" || type === "MoveOut" || type === "ReturnFrom";
}
export function isDeleteMark(mark) {
    return (mark === null || mark === void 0 ? void 0 : mark.type) === "Delete";
}
/**
 * Attempts to extend `lhs` to include the effects of `rhs`.
 * @param lhs - The mark to extend.
 * @param rhs - The effect so extend `rhs` with.
 * @returns `true` iff the function was able to mutate `lhs` to include the effects of `rhs`.
 * When `false` is returned, `lhs` is left untouched.
 */
export function tryExtendMark(lhs, rhs, revision, moveEffects, recordMerges) {
    var _a, _b, _c;
    if (rhs.type !== lhs.type) {
        return false;
    }
    const type = rhs.type;
    if (type === NoopMarkType) {
        lhs.count += rhs.count;
        return true;
    }
    if (type !== "Modify" && rhs.revision !== lhs.revision) {
        return false;
    }
    if ((type !== "MoveIn" && type !== "ReturnTo" && rhs.changes !== undefined) ||
        lhs.changes !== undefined) {
        return false;
    }
    if (isExistingCellMark(lhs)) {
        assert(isExistingCellMark(rhs), 0x6a6 /* Should be existing cell mark */);
        if (((_a = lhs.detachEvent) === null || _a === void 0 ? void 0 : _a.revision) !== ((_b = rhs.detachEvent) === null || _b === void 0 ? void 0 : _b.revision)) {
            return false;
        }
        if (lhs.detachEvent !== undefined &&
            lhs.detachEvent.index + getMarkLength(lhs) !== ((_c = rhs.detachEvent) === null || _c === void 0 ? void 0 : _c.index)) {
            return false;
        }
    }
    switch (type) {
        case "Insert": {
            const lhsInsert = lhs;
            if (isEqualPlace(lhsInsert, rhs) &&
                lhsInsert.id + lhsInsert.content.length === rhs.id) {
                lhsInsert.content.push(...rhs.content);
                return true;
            }
            break;
        }
        case "MoveIn":
        case "ReturnTo": {
            const lhsMoveIn = lhs;
            if (isEqualPlace(lhsMoveIn, rhs) &&
                moveEffects !== undefined &&
                lhsMoveIn.isSrcConflicted === rhs.isSrcConflicted &&
                tryMergeMoves(CrossFieldTarget.Destination, lhsMoveIn, rhs, revision, moveEffects, recordMerges)) {
                return true;
            }
            break;
        }
        case "Delete": {
            const lhsDetach = lhs;
            lhsDetach.count += rhs.count;
            return true;
        }
        case "MoveOut": {
            const lhsMoveOut = lhs;
            if (moveEffects !== undefined &&
                tryMergeMoves(CrossFieldTarget.Source, lhsMoveOut, rhs, revision, moveEffects, recordMerges)) {
                return true;
            }
            break;
        }
        case "ReturnFrom": {
            const lhsReturn = lhs;
            if (moveEffects !== undefined &&
                tryMergeMoves(CrossFieldTarget.Source, lhsReturn, rhs, revision, moveEffects, recordMerges)) {
                return true;
            }
            break;
        }
        case "Revive": {
            const lhsRevive = lhs;
            if (lhsRevive.inverseOf === rhs.inverseOf) {
                lhsRevive.content.push(...rhs.content);
                lhsRevive.count += rhs.count;
                return true;
            }
            break;
        }
        default:
            break;
    }
    return false;
}
function tryMergeMoves(target, left, right, revision, moveEffects, recordMerges) {
    var _a;
    const rev = (_a = left.revision) !== null && _a !== void 0 ? _a : revision;
    const oppEnd = target === CrossFieldTarget.Source ? CrossFieldTarget.Destination : CrossFieldTarget.Source;
    const prevMergeId = getMoveEffect(moveEffects, oppEnd, rev, left.id, false).mergeRight;
    if (prevMergeId !== undefined && prevMergeId !== right.id) {
        makeMergeable(moveEffects, oppEnd, rev, prevMergeId, right.id);
    }
    else {
        makeMergeable(moveEffects, oppEnd, rev, left.id, right.id);
    }
    const leftEffect = getMoveEffect(moveEffects, target, rev, left.id);
    if (leftEffect.mergeRight === right.id) {
        const rightEffect = getMoveEffect(moveEffects, target, rev, right.id);
        assert(rightEffect.mergeLeft === left.id, 0x56d /* Inconsistent merge info */);
        const nextId = rightEffect.mergeRight;
        if (nextId !== undefined) {
            makeMergeable(moveEffects, target, rev, left.id, nextId);
        }
        else {
            leftEffect.mergeRight = undefined;
        }
        if (recordMerges) {
            splitMove(moveEffects, target, revision, left.id, right.id, left.count, right.count);
            // TODO: This breaks the nextId mergeability, and would also be overwritten if we merged again
            makeMergeable(moveEffects, target, revision, left.id, right.id);
        }
        left.count += right.count;
        return true;
    }
    return false;
}
/**
 * Keeps track of the different ways detached nodes may be referred to.
 * Allows updating changesets so they refer to a detached node by the details
 * of the last detach that affected them.
 *
 * WARNING: this code consumes O(N) space and time for marks that affect N nodes.
 * This is code is currently meant for usage in tests.
 * It should be tested and made more efficient before production use.
 */
export class DetachedNodeTracker {
    constructor() {
        // Maps the index for a node to its last characterization as a reattached node.
        this.nodes = new Map();
        this.equivalences = [];
    }
    /**
     * Updates the internals of this instance to account for `change` having been applied.
     * @param change - The change that is being applied. Not mutated.
     * Must be applicable (i.e., `isApplicable(change)` must be true).
     */
    apply(change) {
        var _a, _b, _c, _d;
        let index = 0;
        for (const mark of change.change) {
            const inputLength = getInputLength(mark);
            if (isDetachMark(mark)) {
                const newNodes = new Map();
                const after = index + inputLength;
                for (const [k, v] of this.nodes) {
                    if (k >= index) {
                        if (k >= after) {
                            newNodes.set(k - inputLength, v);
                        }
                        else {
                            // The node is removed
                            this.equivalences.push({
                                old: v,
                                new: {
                                    rev: (_c = (_b = (_a = change.rollbackOf) !== null && _a !== void 0 ? _a : mark.revision) !== null && _b !== void 0 ? _b : change.revision) !== null && _c !== void 0 ? _c : fail("Unable to track detached nodes"),
                                    index: k,
                                },
                            });
                        }
                    }
                    else {
                        newNodes.set(k, v);
                    }
                }
                this.nodes = newNodes;
            }
            index += inputLength;
        }
        index = 0;
        for (const mark of change.change) {
            const inputLength = getInputLength(mark);
            if (isActiveReattach(mark)) {
                const newNodes = new Map();
                for (const [k, v] of this.nodes) {
                    if (k >= index) {
                        newNodes.set(k + inputLength, v);
                    }
                    else {
                        newNodes.set(k, v);
                    }
                }
                const detachEvent = (_d = mark.detachEvent) !== null && _d !== void 0 ? _d : fail("Unable to track detached nodes");
                for (let i = 0; i < mark.count; ++i) {
                    newNodes.set(index + i, {
                        rev: detachEvent.revision,
                        index: detachEvent.index + i,
                    });
                }
                this.nodes = newNodes;
            }
            if (!isDetachMark(mark)) {
                index += inputLength;
            }
        }
    }
    /**
     * Checks whether the given `change` is applicable based on previous changes.
     * @param change - The change to verify the applicability of. Not mutated.
     * @returns false iff `change`'s description of detached nodes is inconsistent with that of changes applied
     * earlier. Returns true otherwise.
     */
    isApplicable(change) {
        var _a;
        for (const mark of change) {
            if (isActiveReattach(mark)) {
                const detachEvent = (_a = mark.detachEvent) !== null && _a !== void 0 ? _a : fail("Unable to track detached nodes");
                const rev = detachEvent.revision;
                for (let i = 0; i < mark.count; ++i) {
                    const index = detachEvent.index + i;
                    const original = { rev, index };
                    const updated = this.getUpdatedDetach(original);
                    for (const detached of this.nodes.values()) {
                        if (updated.rev === detached.rev && updated.index === detached.index) {
                            // The new change is attempting to reattach nodes in a location that has already been
                            // filled by a prior reattach.
                            return false;
                        }
                    }
                }
            }
        }
        return true;
    }
    /**
     * Creates an updated representation of the given `change` so that it refers to detached nodes using the revision
     * that last detached them.
     * @param change - The change to update. Not mutated.
     * Must be applicable (i.e., `isApplicable(change)` must be true).
     * @param genId - An ID allocator that produces ID unique within this changeset.
     * @returns A change equivalent to `change` that refers to detached nodes using the revision that last detached
     * them. May reuse parts of the input `change` structure.
     */
    update(change, genId) {
        const moveEffects = newMoveEffectTable();
        const factory = new MarkListFactory(change.revision, moveEffects);
        const iter = new StackyIterator(change.change);
        while (!iter.done) {
            // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
            const preSplit = iter.pop();
            const splitMarks = applyMoveEffectsToMark(preSplit, change.revision, moveEffects, true);
            const mark = splitMarks[0];
            for (let i = splitMarks.length - 1; i > 0; i--) {
                iter.push(splitMarks[i]);
            }
            const cloned = cloneMark(mark);
            if (isReattach(cloned) && cloned.detachEvent !== undefined) {
                let remainder = cloned;
                for (let i = 1; i < cloned.count; ++i) {
                    const [head, tail] = splitMark(remainder, change.revision, 1, genId, moveEffects, false);
                    this.updateMark(head, change.revision, moveEffects);
                    factory.push(head);
                    remainder = tail;
                }
                this.updateMark(remainder, change.revision, moveEffects);
                factory.push(remainder);
            }
            else {
                factory.push(cloned);
            }
        }
        // We may need to apply the effects of updateMoveSrcDetacher for some marks if those were located
        // before their corresponding detach mark.
        const factory2 = new MarkListFactory(change.revision, moveEffects);
        for (const mark of factory.list) {
            const splitMarks = applyMoveEffectsToMark(mark, change.revision, moveEffects, true);
            factory2.push(...splitMarks);
        }
        return Object.assign(Object.assign({}, change), { change: factory2.list });
    }
    updateMark(mark, revision, moveEffects) {
        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
        const detachEvent = mark.detachEvent;
        const original = { rev: detachEvent.revision, index: detachEvent.index };
        const updated = this.getUpdatedDetach(original);
        if (updated.rev !== original.rev || updated.index !== original.index) {
            mark.detachEvent = { revision: updated.rev, index: updated.index };
        }
    }
    getUpdatedDetach(detach) {
        let curr = detach;
        for (const eq of this.equivalences) {
            if (curr.rev === eq.old.rev && curr.index === eq.old.index) {
                curr = eq.new;
            }
        }
        return curr;
    }
}
/**
 * Checks whether `branch` changeset is consistent with a `target` changeset that is may be rebased over.
 *
 * WARNING: this code consumes O(N) space and time for marks that affect N nodes.
 * This is code is currently meant for usage in tests.
 * It should be tested and made more efficient before production use.
 *
 * @param branch - The changeset that would be rebased over `target`.
 * @param target - The changeset that `branch` would be rebased over.
 * @returns false iff `branch`'s description of detached nodes is inconsistent with that of `target`.
 * Returns true otherwise.
 */
export function areRebasable(branch, target) {
    var _a, _b;
    const indexToReattach = new Map();
    const reattachToIndex = new Map();
    let index = 0;
    for (const mark of branch) {
        if (isActiveReattach(mark)) {
            const list = getOrAddEmptyToMap(indexToReattach, index);
            for (let i = 0; i < mark.count; ++i) {
                const detachEvent = (_a = mark.detachEvent) !== null && _a !== void 0 ? _a : fail("Unable to track detached nodes");
                const entry = {
                    rev: detachEvent.revision,
                    index: detachEvent.index + i,
                };
                const key = `${entry.rev}|${entry.index}`;
                assert(!reattachToIndex.has(key), 0x506 /* First changeset as inconsistent characterization of detached nodes */);
                list.push(key);
                reattachToIndex.set(key, index);
            }
        }
        index += getInputLength(mark);
    }
    index = 0;
    let listIndex = 0;
    for (const mark of target) {
        if (isActiveReattach(mark)) {
            const list = getOrAddEmptyToMap(indexToReattach, index);
            for (let i = 0; i < mark.count; ++i) {
                const detachEvent = (_b = mark.detachEvent) !== null && _b !== void 0 ? _b : fail("Unable to track detached nodes");
                const entry = {
                    rev: detachEvent.revision,
                    index: detachEvent.index + i,
                };
                const key = `${entry.rev}|${entry.index}`;
                const indexInA = reattachToIndex.get(key);
                if (indexInA !== undefined && indexInA !== index) {
                    // change b tries to reattach the same content as change a but in a different location
                    return false;
                }
                if (list.includes(key)) {
                    while (list[listIndex] !== undefined && list[listIndex] !== key) {
                        ++listIndex;
                    }
                    if (list.slice(0, listIndex).includes(key)) {
                        // change b tries to reattach the same content as change a but in a different order
                        return false;
                    }
                }
            }
        }
        const inputLength = getInputLength(mark);
        if (inputLength > 0) {
            listIndex = 0;
        }
        index += inputLength;
    }
    return true;
}
/**
 * Checks whether sequential changesets are consistent.
 *
 * WARNING: this code consumes O(N) space and time for marks that affect N nodes.
 * This is code is currently meant for usage in tests.
 * It should be tested and made more efficient before production use.
 *
 * @param changes - The changesets that would be composed together.
 * @returns false iff the changesets in `changes` are inconsistent/incompatible in their description of detached nodes.
 * Returns true otherwise.
 */
export function areComposable(changes) {
    const tracker = new DetachedNodeTracker();
    for (const change of changes) {
        if (!tracker.isApplicable(change.change)) {
            return false;
        }
        tracker.apply(change);
    }
    return true;
}
/**
 * @alpha
 */
export function newCrossFieldTable() {
    const srcQueries = new Map();
    const dstQueries = new Map();
    const mapSrc = new Map();
    const mapDst = new Map();
    const getMap = (target) => target === CrossFieldTarget.Source ? mapSrc : mapDst;
    const getQueries = (target) => target === CrossFieldTarget.Source ? srcQueries : dstQueries;
    const table = {
        srcQueries,
        dstQueries,
        isInvalidated: false,
        mapSrc,
        mapDst,
        get: (target, revision, id, addDependency) => {
            if (addDependency) {
                addToNestedSet(getQueries(target), revision, id);
            }
            return tryGetFromNestedMap(getMap(target), revision, id);
        },
        getOrCreate: (target, revision, id, defaultValue, invalidateDependents) => {
            if (invalidateDependents && nestedSetContains(getQueries(target), revision, id)) {
                table.isInvalidated = true;
            }
            return getOrAddInNestedMap(getMap(target), revision, id, defaultValue);
        },
        reset: () => {
            table.isInvalidated = false;
            table.srcQueries.clear();
            table.dstQueries.clear();
        },
    };
    return table;
}
/**
 * @alpha
 */
export function newMoveEffectTable() {
    return newCrossFieldTable();
}
/**
 * Splits the `mark` into two marks such that the first returned mark has length `length`.
 * @param mark - The mark to split.
 * @param revision - The revision of the changeset the mark is part of.
 * @param length - The desired length for the first of the two returned marks.
 * @param genId - An ID allocator
 * @param moveEffects - The table in which to record splitting of move marks
 * @param recordMoveEffect - Whether when splitting a move an entry should be added to `moveEffects` indicating that the mark should be split (in case we process this mark again).
 * An entry is always added to `moveEffects` indicating that the opposite end of the move should be split.
 * @returns A pair of marks equivalent to the original `mark`
 * such that the first returned mark has input length `length`.
 */
export function splitMark(mark, revision, length, genId, moveEffects, recordMoveEffect = false) {
    var _a, _b, _c, _d;
    const markLength = getMarkLength(mark);
    const remainder = markLength - length;
    if (length < 1 || remainder < 1) {
        fail("Unable to split mark due to lengths");
    }
    const type = mark.type;
    switch (type) {
        case NoopMarkType:
            return [{ count: length }, { count: remainder }];
        case "Modify":
            fail("Unable to split Modify mark of length 1");
        case "Insert":
            return [
                Object.assign(Object.assign({}, mark), { content: mark.content.slice(0, length) }),
                Object.assign(Object.assign({}, mark), { content: mark.content.slice(length), id: mark.id + length }),
            ];
        case "MoveIn":
        case "ReturnTo": {
            const newId = genId();
            splitMove(moveEffects, CrossFieldTarget.Source, (_a = mark.revision) !== null && _a !== void 0 ? _a : revision, mark.id, newId, length, remainder);
            if (recordMoveEffect) {
                splitMove(moveEffects, CrossFieldTarget.Destination, (_b = mark.revision) !== null && _b !== void 0 ? _b : revision, mark.id, newId, length, remainder);
            }
            const mark1 = Object.assign(Object.assign({}, mark), { count: length });
            const mark2 = Object.assign(Object.assign({}, mark), { id: newId, count: remainder });
            if (mark.type === "ReturnTo") {
                if (mark.detachEvent !== undefined) {
                    mark2.detachEvent = splitDetachEvent(mark.detachEvent, length);
                }
                return [mark1, mark2];
            }
            return [mark1, mark2];
        }
        case "Revive": {
            const mark1 = Object.assign(Object.assign({}, mark), { content: mark.content.slice(0, length), count: length });
            const mark2 = Object.assign(Object.assign({}, mark), { content: mark.content.slice(length), count: remainder });
            if (mark.detachEvent !== undefined) {
                mark2.detachEvent = splitDetachEvent(mark.detachEvent, length);
            }
            return [mark1, mark2];
        }
        case "Delete": {
            const mark1 = Object.assign(Object.assign({}, mark), { count: length });
            const mark2 = Object.assign(Object.assign({}, mark), { count: remainder });
            if (mark.detachEvent !== undefined) {
                mark2.detachEvent = splitDetachEvent(mark.detachEvent, length);
            }
            return [mark1, mark2];
        }
        case "MoveOut":
        case "ReturnFrom": {
            // TODO: Handle detach index for ReturnFrom
            const newId = genId();
            splitMove(moveEffects, CrossFieldTarget.Destination, (_c = mark.revision) !== null && _c !== void 0 ? _c : revision, mark.id, newId, length, remainder);
            if (recordMoveEffect) {
                splitMove(moveEffects, CrossFieldTarget.Source, (_d = mark.revision) !== null && _d !== void 0 ? _d : revision, mark.id, newId, length, remainder);
            }
            const mark1 = Object.assign(Object.assign({}, mark), { count: length });
            const mark2 = Object.assign(Object.assign({}, mark), { id: newId, count: remainder });
            if (mark.detachEvent !== undefined) {
                mark2.detachEvent = splitDetachEvent(mark.detachEvent, length);
            }
            return [mark1, mark2];
        }
        default:
            unreachableCase(type);
    }
}
function splitDetachEvent(detachEvent, length) {
    return Object.assign(Object.assign({}, detachEvent), { index: detachEvent.index + length });
}
export function compareLineages(lineage1, lineage2) {
    if (lineage1 === undefined || lineage2 === undefined) {
        return 0;
    }
    const lineage1Offsets = new Map();
    for (const event of lineage1) {
        lineage1Offsets.set(event.revision, event.offset);
    }
    for (let i = lineage2.length - 1; i >= 0; i--) {
        const event2 = lineage2[i];
        const offset1 = lineage1Offsets.get(event2.revision);
        if (offset1 !== undefined) {
            const offset2 = event2.offset;
            if (offset1 < offset2) {
                return -1;
            }
            else if (offset1 > offset2) {
                return 1;
            }
        }
    }
    return 0;
}
export function getNodeChange(mark) {
    const type = mark.type;
    switch (type) {
        case NoopMarkType:
        case "MoveIn":
        case "ReturnTo":
            return undefined;
        case "Delete":
        case "Insert":
        case "Modify":
        case "MoveOut":
        case "ReturnFrom":
        case "Revive":
            return mark.changes;
        default:
            unreachableCase(type);
    }
}
export function withNodeChange(mark, changes) {
    const type = mark.type;
    switch (type) {
        case NoopMarkType:
            return changes !== undefined ? { type: "Modify", changes } : mark;
        case "MoveIn":
        case "ReturnTo":
            assert(changes === undefined, 0x6a7 /* Cannot have a node change on a MoveIn or ReturnTo mark */);
            return mark;
        case "Delete":
        case "Insert":
        case "Modify":
        case "MoveOut":
        case "ReturnFrom":
        case "Revive": {
            const newMark = Object.assign({}, mark);
            if (changes !== undefined) {
                newMark.changes = changes;
            }
            else {
                delete newMark.changes;
            }
            return newMark;
        }
        default:
            unreachableCase(type);
    }
}
export function withRevision(mark, revision) {
    if (revision === undefined) {
        return mark;
    }
    if (isNoopMark(mark)) {
        return mark;
    }
    if (isModify(mark)) {
        return mark;
    }
    const cloned = cloneMark(mark);
    cloned.revision = revision;
    return cloned;
}
export function getMarkMoveId(mark) {
    if (isMoveMark(mark)) {
        return mark.id;
    }
    return undefined;
}
//# sourceMappingURL=utils.js.map