"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCrossFieldKeys = exports.withRevision = exports.withNodeChange = exports.omitMarkEffect = exports.extractMarkEffect = exports.splitMarkEffect = exports.splitMark = exports.tryMergeMarks = exports.areMergeableChangeAtoms = exports.isRemoveMark = exports.isPin = exports.isDetach = exports.compareCellsFromSameRevision = exports.areOverlappingIdRanges = exports.isNoopMark = exports.isTombstone = exports.isImpactful = exports.areOutputCellsEmpty = exports.areInputCellsEmpty = exports.markHasCellEffect = exports.markFillsCells = exports.markEmptiesCells = exports.getInputLength = exports.getOutputLength = exports.cloneCellId = exports.cloneMarkEffect = exports.cloneMark = exports.normalizeCellRename = exports.getMovedNodeId = exports.getDetachedRootId = exports.getAttachedRootId = exports.getDetachOutputCellId = exports.compareCellPositionsUsingTombstones = exports.CellOrder = exports.cellSourcesFromMarks = exports.getOutputCellId = exports.getInputCellId = exports.areEqualCellIds = exports.isActiveReattach = exports.isReattachEffect = exports.isReattach = exports.isAttach = exports.isInsert = exports.isRename = exports.isNewAttachEffect = exports.isNewAttach = exports.getNestedChanges = exports.createEmpty = exports.isEmpty = void 0;
const internal_1 = require("@fluidframework/core-utils/internal");
const index_js_1 = require("../../core/index.js");
const index_js_2 = require("../../util/index.js");
const index_js_3 = require("../modular-schema/index.js");
const types_js_1 = require("./types.js");
function isEmpty(change) {
    for (const mark of change) {
        if (mark.changes !== undefined || mark.type !== undefined) {
            return false;
        }
    }
    return true;
}
exports.isEmpty = isEmpty;
function createEmpty() {
    return [];
}
exports.createEmpty = createEmpty;
function getNestedChanges(change) {
    const output = [];
    let inputIndex = 0;
    for (const mark of change) {
        const { changes, count } = mark;
        if (changes !== undefined) {
            output.push([changes, inputIndex]);
        }
        if (!areInputCellsEmpty(mark)) {
            inputIndex += count;
        }
    }
    return output;
}
exports.getNestedChanges = getNestedChanges;
function isNewAttach(mark, revision) {
    return isNewAttachEffect(mark, mark.cellId, revision);
}
exports.isNewAttach = isNewAttach;
function isNewAttachEffect(effect, cellId, revision) {
    return (isAttach(effect) &&
        cellId !== undefined &&
        (effect.revision ?? revision) === (cellId.revision ?? revision));
}
exports.isNewAttachEffect = isNewAttachEffect;
function isRename(mark) {
    return mark.type === "Rename";
}
exports.isRename = isRename;
function isInsert(mark) {
    return mark.type === "Insert";
}
exports.isInsert = isInsert;
function isAttach(effect) {
    return effect.type === "Insert";
}
exports.isAttach = isAttach;
function isReattach(mark) {
    return isReattachEffect(mark, mark.cellId);
}
exports.isReattach = isReattach;
function isReattachEffect(effect, cellId) {
    return isAttach(effect) && !isNewAttachEffect(effect, cellId);
}
exports.isReattachEffect = isReattachEffect;
function isActiveReattach(mark) {
    return isAttach(mark) && isReattachEffect(mark, mark.cellId) && mark.cellId !== undefined;
}
exports.isActiveReattach = isActiveReattach;
function areEqualCellIds(a, b) {
    return (0, index_js_1.areEqualChangeAtomIdOpts)(a, b);
}
exports.areEqualCellIds = areEqualCellIds;
function getInputCellId(mark) {
    return mark.cellId;
}
exports.getInputCellId = getInputCellId;
function getOutputCellId(mark) {
    const type = mark.type;
    switch (type) {
        case types_js_1.NoopMarkType:
            return getInputCellId(mark);
        case "Insert":
            return undefined;
        case "Remove":
            return getDetachOutputCellId(mark);
        case "Rename":
            return mark.idOverride;
        default:
            (0, internal_1.unreachableCase)(type);
    }
}
exports.getOutputCellId = getOutputCellId;
function cellSourcesFromMarks(marks, contextGetter) {
    const set = new Set();
    for (const mark of marks) {
        const cell = contextGetter(mark);
        if (cell !== undefined) {
            set.add(cell.revision);
        }
    }
    return set;
}
exports.cellSourcesFromMarks = cellSourcesFromMarks;
var CellOrder;
(function (CellOrder) {
    CellOrder[CellOrder["SameCell"] = 0] = "SameCell";
    CellOrder[CellOrder["OldThenNew"] = 1] = "OldThenNew";
    CellOrder[CellOrder["NewThenOld"] = 2] = "NewThenOld";
})(CellOrder || (exports.CellOrder = CellOrder = {}));
/**
 * Determines the order of two cells from two changesets.
 *
 * This function makes the following assumptions:
 * 1. The cells represent the same context.
 * 2. `oldMarkCell` is from a mark in a changeset that is older than the changeset that contains the mark that
 * `newMarkCell` is from.
 * 3. In terms of sequence index, all cells located before A are also located before B,
 * and all cells located before B are also located before A.
 * 4. If a changeset has a mark/tombstone that describes a cell named in some revision R,
 * then that changeset must contain marks/tombstones for all cells named in R as well as all cells named in later
 * revisions up to its own.
 * 5. If a changeset foo is rebased over a changeset bar, then the rebased version of foo must contain tombstones or
 * marks for all cells referenced or named in bar. It has yet to be determined whether this assumption is necessary
 * for the logic below.
 *
 * @param oldMarkCell - The cell referenced or named by a mark or tombstone from the older changeset.
 * @param newMarkCell - The cell referenced or named by a mark or tombstone from the newer changeset.
 * @param oldChangeKnowledge - The set of revisions that the older changeset has cell representations for.
 * @param newChangeKnowledge - The set of revisions that the newer changeset has cell representations for.
 * @param metadata - Revision metadata for the operation being carried out.
 * @returns a {@link CellOrder} which describes how the cells are ordered relative to one-another.
 */
function compareCellPositionsUsingTombstones(oldMarkCell, newMarkCell, oldChangeKnowledge, newChangeKnowledge, metadata) {
    if ((0, index_js_1.areEqualChangeAtomIds)(oldMarkCell, newMarkCell)) {
        return CellOrder.SameCell;
    }
    const oldChangeKnowsOfNewMarkCellRevision = oldChangeKnowledge.has(newMarkCell.revision);
    const newChangeKnowsOfOldMarkCellRevision = newChangeKnowledge.has(oldMarkCell.revision);
    if (oldChangeKnowsOfNewMarkCellRevision && newChangeKnowsOfOldMarkCellRevision) {
        // If both changesets know of both cells, but we've been asked to compare different cells,
        // Then either the changesets they originate from do not represent the same context,
        // or the ordering of their cells in inconsistent.
        // The only exception to this is when we're composing anonymous changesets in a transaction.
        (0, internal_1.assert)(oldMarkCell.revision === undefined && newMarkCell.revision === undefined, 0x8a0 /* Inconsistent cell ordering */);
        // We are composing anonymous changesets in a transaction. The new changeset is creating a cell in a gap
        // where the old changeset knows of some now empty cell. We order the new cell relative to the old cell in a
        // way that is consistent with its tie-breaking behavior should the old cell be concurrently re-filled.
        // Since only tie-break left is supported at the moment, the new cell comes first.
        return CellOrder.NewThenOld;
    }
    if (newChangeKnowsOfOldMarkCellRevision) {
        // The changeset that contains `newMarkCell` has tombstones for the revision that created `oldMarkCell`,
        // so a tombstone/mark matching `oldMarkCell` must occur later in the newer changeset.
        return CellOrder.NewThenOld;
    }
    else if (oldChangeKnowsOfNewMarkCellRevision) {
        // The changeset that contains `oldMarkCell` has tombstones for revision that created `newMarkCell`,
        // so a tombstone/mark matching `newMarkCell` must occur later in the older changeset.
        return CellOrder.OldThenNew;
    }
    else {
        // These cells are only ordered through tie-breaking.
        // Since tie-breaking is hard-coded to "merge left", the younger cell comes first.
        // In the context of compose, an undefined revision means we are composing anonymous changesets into
        // a transaction, which means the cell from the newer changeset is younger.
        if (newMarkCell.revision === undefined) {
            return CellOrder.NewThenOld;
        }
        // The only case where the old mark cell should have no revision is when composing anonymous changesets
        // into a transaction, in which case the new mark cell should also have no revision, which is handled above.
        // In all other cases, the old mark cell should have a revision.
        (0, internal_1.assert)(oldMarkCell.revision !== undefined, 0x8a1 /* Old mark cell should have a revision */);
        // Note that these indices are for ordering the revisions in which the cells were named, not the revisions
        // of the changesets in which the marks targeting these cells appear.
        const revisionComparison = metadata.compareRevisions(oldMarkCell.revision, newMarkCell.revision);
        // If the metadata defines an ordering for the revisions then the cell from the newer revision comes first.
        if (revisionComparison !== 0) {
            return revisionComparison > 0 ? CellOrder.NewThenOld : CellOrder.OldThenNew;
        }
        // While it is possible for both marks to refer to cells that were named in revisions that are outside
        // the scope of the metadata, such a scenario should be handled above due to the fact that one of the two
        // changesets should have tombstones or marks for both cells.
        //
        // To see this in the context of rebase, we must consider the lowest common ancestor (LCA) of each change's
        // original (i.e., unrebased) edit with the head of the branch they will both reside on after the rebase.
        // ...─(Ti)─...─(Tj)─...─(old')─(new') <- branch both change will reside on after rebase
        //        |        └─...─(new)
        //        └─...─(old)
        // In the diagram above we can see that by the time `new` is being rebased over `old`, both changesets have
        // been rebased over, and therefore have cell information for, changes `Tj` onwards. This means that one of
        // The two changesets (the `old` one in the diagram above) will have tombstones or marks for any cells that
        // `new` refers to so long as those cells were not created on `new`'s branch.
        // Note that the change that contains the superset of cells (again, ignoring cells created on the other
        // change's branch) is not always the older change. Consider the following scenario:
        // ...─(Ti)─...─(Tj)─...─(old')─(new')
        //        |        └─...─(old)
        //        └─...─(new)
        //
        // The same scenario can arise in the context of compose (just consider composing `old'` and `new'` from
        // the examples above) with the same resolution.
        (0, internal_1.assert)(false, 0x8a2 /* Invalid cell ordering scenario */);
    }
}
exports.compareCellPositionsUsingTombstones = compareCellPositionsUsingTombstones;
/**
 * Gets the ID of the cell in the output context of the given detach `mark`.
 */
function getDetachOutputCellId(mark) {
    if (mark.cellRename !== undefined) {
        return mark.cellRename;
    }
    if (mark.detachCellId !== undefined) {
        return mark.detachCellId;
    }
    return getDetachedRootId(mark);
}
exports.getDetachOutputCellId = getDetachOutputCellId;
/**
 * Gets the ID of the node to be attached in the input context of the given detach `mark`.
 */
function getAttachedRootId(mark) {
    return (0, index_js_1.makeChangeAtomId)(mark.id, mark.revision);
}
exports.getAttachedRootId = getAttachedRootId;
/**
 * Gets the ID of the detached node in the output context of the given detach `mark`.
 */
function getDetachedRootId(mark) {
    return (0, index_js_1.makeChangeAtomId)(mark.id, mark.revision);
}
exports.getDetachedRootId = getDetachedRootId;
function getMovedNodeId(mark) {
    return (0, index_js_1.makeChangeAtomId)(mark.id, mark.revision);
}
exports.getMovedNodeId = getMovedNodeId;
/**
 * Preserves the semantics of the given `mark` but repackages it into a `DetachOfRemovedNodes` when possible.
 */
function normalizeCellRename(cellId, count, attach, detach) {
    const outputId = getDetachOutputCellId(detach);
    // Note that the output ID may be the same as the cellId. In such a scenario,
    // we output an (impact-less) Rename mark anyway (as opposed to a Skip)
    // because the resulting Rename may be rebased over other changes that rename the input cell,
    // eventually leading to an impactful rename.
    return {
        type: "Rename",
        count,
        cellId,
        idOverride: outputId,
    };
}
exports.normalizeCellRename = normalizeCellRename;
function cloneMark(mark) {
    const clone = { ...cloneMarkEffect(mark), count: mark.count };
    if (mark.cellId !== undefined) {
        clone.cellId = cloneCellId(mark.cellId);
    }
    return clone;
}
exports.cloneMark = cloneMark;
function cloneMarkEffect(effect) {
    const clone = { ...effect };
    return clone;
}
exports.cloneMarkEffect = cloneMarkEffect;
function cloneCellId(id) {
    const cloned = { ...id };
    return cloned;
}
exports.cloneCellId = cloneCellId;
/**
 * Gets the number of nodes within the output context of the mark.
 * @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.
 */
function getOutputLength(mark, ignorePairing = false) {
    return areOutputCellsEmpty(mark) ? 0 : mark.count;
}
exports.getOutputLength = getOutputLength;
/**
 * Gets the number of nodes within the input context of the mark.
 * @param mark - The mark to get the length of.
 */
function getInputLength(mark) {
    return areInputCellsEmpty(mark) ? 0 : mark.count;
}
exports.getInputLength = getInputLength;
function markEmptiesCells(mark) {
    return !areInputCellsEmpty(mark) && areOutputCellsEmpty(mark);
}
exports.markEmptiesCells = markEmptiesCells;
function markFillsCells(mark) {
    return areInputCellsEmpty(mark) && !areOutputCellsEmpty(mark);
}
exports.markFillsCells = markFillsCells;
function markHasCellEffect(mark) {
    return areInputCellsEmpty(mark) !== areOutputCellsEmpty(mark);
}
exports.markHasCellEffect = markHasCellEffect;
function areInputCellsEmpty(mark) {
    return mark.cellId !== undefined;
}
exports.areInputCellsEmpty = areInputCellsEmpty;
function areOutputCellsEmpty(mark) {
    const type = mark.type;
    switch (type) {
        case types_js_1.NoopMarkType:
            return mark.cellId !== undefined;
        case "Remove":
        case "Rename":
            return true;
        case "Insert":
            return false;
        default:
            (0, internal_1.unreachableCase)(type);
    }
}
exports.areOutputCellsEmpty = areOutputCellsEmpty;
/**
 * Returns true if and only iff the given `mark` would have impact on the field when applied.
 *
 * @remarks
 * Ignores the impact of nested changes.
 * CellRename effects are considered impactful if they actually change the ID of the cells.
 */
function isImpactful(mark) {
    const type = mark.type;
    switch (type) {
        case types_js_1.NoopMarkType:
            return false;
        case "Rename":
            return true;
        case "Remove": {
            const inputId = getInputCellId(mark);
            if (inputId === undefined) {
                return true;
            }
            const outputId = getOutputCellId(mark);
            (0, internal_1.assert)(outputId !== undefined, 0x824 /* Remove marks must have an output cell ID */);
            return !(0, index_js_1.areEqualChangeAtomIds)(inputId, outputId);
        }
        case "Insert":
            // A Revive has no impact if the nodes are already in the document.
            return mark.cellId !== undefined;
        default:
            (0, internal_1.unreachableCase)(type);
    }
}
exports.isImpactful = isImpactful;
function isTombstone(mark) {
    return mark.type === types_js_1.NoopMarkType && mark.cellId !== undefined && mark.changes === undefined;
}
exports.isTombstone = isTombstone;
function isNoopMark(mark) {
    return mark.type === types_js_1.NoopMarkType;
}
exports.isNoopMark = isNoopMark;
function areOverlappingIdRanges(id1, count1, id2, count2) {
    const lastId1 = id1 + count1 - 1;
    const lastId2 = id2 + count2 - 1;
    return (id2 <= id1 && id1 <= lastId2) || (id1 <= id2 && id2 <= lastId1);
}
exports.areOverlappingIdRanges = areOverlappingIdRanges;
function compareCellsFromSameRevision(cell1, count1, cell2, count2) {
    (0, internal_1.assert)(cell1.revision === cell2.revision, 0x85b /* Expected cells to have the same revision */);
    if (areOverlappingIdRanges(cell1.localId, count1, cell2.localId, count2)) {
        return cell1.localId - cell2.localId;
    }
    return undefined;
}
exports.compareCellsFromSameRevision = compareCellsFromSameRevision;
function isDetach(mark) {
    const type = mark?.type;
    return type === "Remove";
}
exports.isDetach = isDetach;
function isPin(mark) {
    return mark?.type === "Insert" && mark.cellId === undefined;
}
exports.isPin = isPin;
function isRemoveMark(mark) {
    return mark?.type === "Remove";
}
exports.isRemoveMark = isRemoveMark;
function areMergeableChangeAtoms(lhs, lhsCount, rhs) {
    if (lhs === undefined || rhs === undefined) {
        return lhs === undefined && rhs === undefined;
    }
    return (lhs.revision === rhs.revision &&
        (0, index_js_2.areAdjacentIntegerRanges)(lhs.localId, lhsCount, rhs.localId));
}
exports.areMergeableChangeAtoms = areMergeableChangeAtoms;
function areMergeableCellIds(lhs, lhsCount, rhs) {
    return areMergeableChangeAtoms(lhs, lhsCount, rhs);
}
/**
 * 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 `lhs` iff the function was able to mutate `lhs` to include the effects of `rhs`.
 * When `undefined` is returned, `lhs` is left untouched.
 */
function tryMergeMarks(lhs, rhs) {
    if (rhs.type !== lhs.type) {
        return undefined;
    }
    if (!areMergeableCellIds(lhs.cellId, lhs.count, rhs.cellId)) {
        return undefined;
    }
    if (rhs.changes !== undefined || lhs.changes !== undefined) {
        return undefined;
    }
    const mergedEffect = tryMergeEffects(lhs, rhs, lhs.count);
    if (mergedEffect === undefined) {
        return undefined;
    }
    return { ...lhs, ...mergedEffect, count: lhs.count + rhs.count };
}
exports.tryMergeMarks = tryMergeMarks;
function tryMergeEffects(lhs, rhs, lhsCount) {
    if (lhs.type !== rhs.type) {
        return undefined;
    }
    if (rhs.type === types_js_1.NoopMarkType) {
        return lhs;
    }
    if (lhs.revision !== rhs.revision) {
        return undefined;
    }
    if (isDetach(lhs) &&
        isDetach(rhs) &&
        !areMergeableChangeAtoms(lhs.cellRename, lhsCount, rhs.cellRename)) {
        return undefined;
    }
    const type = rhs.type;
    switch (type) {
        case "Remove": {
            const lhsDetach = lhs;
            if (lhsDetach.id + lhsCount === rhs.id &&
                areMergeableChangeAtoms(lhsDetach.cellRename, lhsCount, rhs.cellRename) &&
                areMergeableChangeAtoms(lhsDetach.detachCellId, lhsCount, rhs.detachCellId)) {
                return lhsDetach;
            }
            break;
        }
        case "Rename": {
            const lhsDetach = lhs;
            if (areMergeableChangeAtoms(lhsDetach.idOverride, lhsCount, rhs.idOverride)) {
                return lhsDetach;
            }
            break;
        }
        case "Insert": {
            const lhsInsert = lhs;
            if (lhsInsert.id + lhsCount === rhs.id &&
                areMergeableChangeAtoms(lhsInsert.detachCellId, lhsCount, rhs.detachCellId)) {
                return lhsInsert;
            }
            break;
        }
        default:
            (0, internal_1.unreachableCase)(type);
    }
    return undefined;
}
/**
 * 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`.
 */
function splitMark(mark, length) {
    const markLength = mark.count;
    const remainder = markLength - length;
    if (length < 1 || remainder < 1) {
        (0, internal_1.fail)(0xb2d /* Unable to split mark due to lengths */);
    }
    const [effect1, effect2] = splitMarkEffect(mark, length);
    const mark1 = { ...mark, ...effect1, count: length };
    const mark2 = { ...mark, ...effect2, count: remainder };
    if (mark2.cellId !== undefined) {
        mark2.cellId = splitDetachEvent(mark2.cellId, length);
    }
    return [mark1, mark2];
}
exports.splitMark = splitMark;
function splitMarkEffect(effect, length) {
    const type = effect.type;
    switch (type) {
        case types_js_1.NoopMarkType:
            return [effect, effect];
        case "Insert": {
            const effect1 = {
                ...effect,
            };
            const effect2 = {
                ...effect,
                id: effect.id + length,
            };
            if (effect.detachCellId !== undefined) {
                effect2.detachCellId = splitDetachEvent(effect.detachCellId, length);
            }
            return [effect1, effect2];
        }
        case "Remove": {
            const effect1 = { ...effect };
            const id2 = (0, index_js_2.brand)(effect.id + length);
            const effect2 = { ...effect, id: id2 };
            const effect2Remove = effect2;
            if (effect2Remove.cellRename !== undefined) {
                effect2Remove.cellRename = splitDetachEvent(effect2Remove.cellRename, length);
            }
            if (effect2Remove.detachCellId !== undefined) {
                effect2Remove.detachCellId = splitDetachEvent(effect2Remove.detachCellId, length);
            }
            return [effect1, effect2];
        }
        case "Rename": {
            const effect1 = { ...effect };
            const effect2 = { ...effect };
            const effect2Rename = effect2;
            if (effect2Rename.idOverride !== undefined) {
                effect2Rename.idOverride = splitDetachEvent(effect2Rename.idOverride, length);
            }
            return [effect1, effect2];
        }
        default:
            (0, internal_1.unreachableCase)(type);
    }
}
exports.splitMarkEffect = splitMarkEffect;
function splitDetachEvent(detachEvent, length) {
    return { ...detachEvent, localId: (0, index_js_2.brand)(detachEvent.localId + length) };
}
// TODO: Refactor MarkEffect into a field of CellMark so this function isn't necessary.
function extractMarkEffect(mark) {
    const { cellId: _cellId, count: _count, changes: _changes, ...effect } = mark;
    return effect;
}
exports.extractMarkEffect = extractMarkEffect;
// TODO: Refactor MarkEffect into a field of CellMark so this function isn't necessary.
function omitMarkEffect(mark) {
    const { cellId, count, changes } = mark;
    const noopMark = { count };
    if (cellId !== undefined) {
        noopMark.cellId = cellId;
    }
    if (changes !== undefined) {
        noopMark.changes = changes;
    }
    return noopMark;
}
exports.omitMarkEffect = omitMarkEffect;
function withNodeChange(mark, changes) {
    const newMark = { ...mark };
    if (changes !== undefined) {
        newMark.changes = changes;
    }
    else {
        delete newMark.changes;
    }
    return newMark;
}
exports.withNodeChange = withNodeChange;
function withRevision(mark, revision) {
    if (revision === undefined) {
        return mark;
    }
    const cloned = cloneMark(mark);
    addRevision(cloned, revision);
    if (cloned.cellId !== undefined &&
        cloned.cellId.revision === undefined &&
        revision !== undefined) {
        cloned.cellId.revision = revision;
    }
    return cloned;
}
exports.withRevision = withRevision;
function addRevision(effect, revision) {
    if (effect.type === types_js_1.NoopMarkType || isRename(effect)) {
        return;
    }
    (0, internal_1.assert)(effect.revision === undefined || effect.revision === revision, 0x829 /* Should not overwrite mark revision */);
    effect.revision = revision;
}
function getCrossFieldKeys(change) {
    const keys = [];
    for (const mark of change) {
        keys.push(...getCrossFieldKeysForMark(mark, mark.count));
    }
    return keys;
}
exports.getCrossFieldKeys = getCrossFieldKeys;
function getCrossFieldKeysForMark(mark, count) {
    switch (mark.type) {
        case "Insert": {
            const keys = [
                {
                    key: {
                        revision: mark.revision,
                        localId: mark.id,
                        target: index_js_3.CrossFieldTarget.Destination,
                    },
                    count,
                },
            ];
            if (mark.cellId === undefined) {
                // This is a pin, which is treated as a detach and attach.
                keys.push({
                    key: { revision: mark.revision, localId: mark.id, target: index_js_3.CrossFieldTarget.Source },
                    count,
                });
            }
            return keys;
        }
        case "Remove":
            return [
                {
                    key: {
                        revision: mark.revision,
                        localId: mark.id,
                        target: index_js_3.CrossFieldTarget.Source,
                    },
                    count,
                },
            ];
        default:
            return [];
    }
}
//# sourceMappingURL=utils.js.map