"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.invert = void 0;
const internal_1 = require("@fluidframework/core-utils/internal");
const index_js_1 = require("../../util/index.js");
const index_js_2 = require("../modular-schema/index.js");
const markListFactory_js_1 = require("./markListFactory.js");
const types_js_1 = require("./types.js");
const utils_js_1 = require("./utils.js");
/**
 * Inverts a given changeset.
 * @param change - The changeset to produce the inverse of.
 * @param isRollback - Whether the inverse is being produced for a rollback.
 * @param genId - The ID allocator to use for generating new IDs.
 * @param revision - The revision to use for the inverse changeset.
 * @param crossFieldManager - The cross-field manager to use for tracking cross-field changes.
 * @returns The inverse of the given `change` such that the inverse can be applied after `change`.
 *
 * WARNING! This implementation is incomplete:
 * - Support for slices is not implemented.
 */
function invert(change, isRollback, genId, revision, crossFieldManager) {
    return invertMarkList(change, isRollback, crossFieldManager, revision);
}
exports.invert = invert;
function invertMarkList(markList, isRollback, crossFieldManager, revision) {
    const inverseMarkList = new markListFactory_js_1.MarkListFactory();
    for (const mark of markList) {
        const inverseMarks = invertMark(mark, isRollback, crossFieldManager, revision);
        inverseMarkList.push(...inverseMarks);
    }
    return inverseMarkList.list;
}
function invertMark(mark, isRollback, crossFieldManager, revision) {
    if (!(0, utils_js_1.isImpactful)(mark)) {
        const inputId = (0, utils_js_1.getInputCellId)(mark);
        return [invertNodeChangeOrSkip(mark.count, mark.changes, inputId)];
    }
    const type = mark.type;
    switch (type) {
        case types_js_1.NoopMarkType: {
            return [mark];
        }
        case "Rename": {
            const inputId = (0, utils_js_1.getInputCellId)(mark);
            (0, internal_1.assert)(inputId !== undefined, 0x9f5 /* Rename mark must have cell ID */);
            const inverse = {
                type: "Rename",
                count: mark.count,
                cellId: mark.idOverride,
                // Unlike a remove or move-out, which follow a node, there is no way for this mark to assign the original input cell ID to another cell.
                // This means it should be safe to always restore the input cell ID (as opposed to only doing it on rollbacks).
                // Despite that, we still only do it on rollback for the sake of consistency: once a cell has been assigned an ID,
                // the only way for that cell to be assigned that ID again is if it is rolled back to that state.
                idOverride: isRollback ? inputId : { localId: inputId.localId },
            };
            return [(0, utils_js_1.withNodeChange)(inverse, mark.changes)];
        }
        case "Remove": {
            (0, internal_1.assert)(mark.revision !== undefined, 0x5a1 /* Unable to revert to undefined revision */);
            const outputId = (0, utils_js_1.getOutputCellId)(mark);
            const inputId = (0, utils_js_1.getInputCellId)(mark);
            let inverse;
            if (inputId === undefined) {
                inverse = {
                    type: "Insert",
                    id: mark.id,
                    cellId: outputId,
                    count: mark.count,
                    revision,
                };
            }
            else {
                inverse = {
                    type: "Remove",
                    id: mark.id,
                    cellId: outputId,
                    count: mark.count,
                    revision,
                };
                if (isRollback) {
                    inverse.idOverride = inputId;
                }
            }
            return [(0, utils_js_1.withNodeChange)(inverse, mark.changes)];
        }
        case "Insert": {
            const inputId = (0, utils_js_1.getInputCellId)(mark);
            (0, internal_1.assert)(inputId !== undefined, 0x80c /* Active inserts should target empty cells */);
            const removeMark = {
                type: "Remove",
                count: mark.count,
                id: inputId.localId,
                revision,
            };
            if (isRollback) {
                removeMark.idOverride = inputId;
            }
            const inverse = (0, utils_js_1.withNodeChange)(removeMark, mark.changes);
            return [inverse];
        }
        case "MoveOut": {
            if (mark.changes !== undefined) {
                (0, internal_1.assert)(mark.count === 1, 0x6ed /* Mark with changes can only target a single cell */);
                const endpoint = (0, utils_js_1.getEndpoint)(mark);
                crossFieldManager.set(index_js_2.CrossFieldTarget.Destination, endpoint.revision, endpoint.localId, mark.count, mark.changes, true);
            }
            const cellId = (0, utils_js_1.getDetachOutputCellId)(mark) ?? {
                revision: mark.revision ?? (0, index_js_1.fail)("Revision must be defined"),
                localId: mark.id,
            };
            const moveIn = {
                type: "MoveIn",
                id: mark.id,
                revision,
            };
            if (mark.finalEndpoint !== undefined) {
                moveIn.finalEndpoint = {
                    localId: mark.finalEndpoint.localId,
                    revision: mark.revision,
                };
            }
            let effect = moveIn;
            const inputId = (0, utils_js_1.getInputCellId)(mark);
            if (inputId !== undefined) {
                const detach = {
                    type: "Remove",
                    id: mark.id,
                    revision,
                };
                if (isRollback) {
                    detach.idOverride = inputId;
                }
                effect = {
                    type: "AttachAndDetach",
                    attach: moveIn,
                    detach,
                };
            }
            return [{ ...effect, count: mark.count, cellId }];
        }
        case "MoveIn": {
            const inputId = (0, utils_js_1.getInputCellId)(mark);
            (0, internal_1.assert)(inputId !== undefined, 0x89e /* Active move-ins should target empty cells */);
            const invertedMark = {
                type: "MoveOut",
                id: mark.id,
                count: mark.count,
                revision,
            };
            if (isRollback) {
                invertedMark.idOverride = inputId;
            }
            if (mark.finalEndpoint) {
                invertedMark.finalEndpoint = {
                    localId: mark.finalEndpoint.localId,
                    revision: mark.revision,
                };
            }
            return applyMovedChanges(invertedMark, mark.revision, crossFieldManager);
        }
        case "AttachAndDetach": {
            const attach = {
                count: mark.count,
                cellId: mark.cellId,
                ...mark.attach,
            };
            const idAfterAttach = (0, utils_js_1.getOutputCellId)(attach);
            // We put `mark.changes` on the detach so that if it is a move source
            // the changes can be sent to the endpoint.
            const detach = {
                count: mark.count,
                cellId: idAfterAttach,
                changes: mark.changes,
                ...mark.detach,
            };
            const attachInverses = invertMark(attach, isRollback, crossFieldManager, revision);
            const detachInverses = invertMark(detach, isRollback, crossFieldManager, revision);
            if (detachInverses.length === 0) {
                return attachInverses;
            }
            (0, internal_1.assert)((0, index_js_1.hasSingle)(detachInverses), 0x80d /* Only expected MoveIn marks to be split when inverting */);
            let detachInverse = detachInverses[0];
            (0, internal_1.assert)((0, utils_js_1.isAttach)(detachInverse), 0x80e /* Inverse of a detach should be an attach */);
            const inverses = [];
            for (const attachInverse of attachInverses) {
                let detachInverseCurr = detachInverse;
                if (attachInverse.count !== detachInverse.count) {
                    [detachInverseCurr, detachInverse] = (0, utils_js_1.splitMark)(detachInverse, attachInverse.count);
                }
                if (attachInverse.type === types_js_1.NoopMarkType) {
                    if (attachInverse.changes !== undefined) {
                        (0, internal_1.assert)(detachInverseCurr.changes === undefined, 0x80f /* Unexpected node changes */);
                        detachInverseCurr.changes = attachInverse.changes;
                    }
                    inverses.push(detachInverseCurr);
                    continue;
                }
                (0, internal_1.assert)((0, utils_js_1.isDetach)(attachInverse), 0x810 /* Inverse of an attach should be a detach */);
                (0, internal_1.assert)(detachInverseCurr.cellId !== undefined, 0x9f6 /* Expected empty cell */);
                const inverted = (0, utils_js_1.normalizeCellRename)(detachInverseCurr.cellId, attachInverse.count, (0, utils_js_1.extractMarkEffect)(detachInverseCurr), (0, utils_js_1.extractMarkEffect)(attachInverse));
                if (detachInverse.changes !== undefined) {
                    inverted.changes = detachInverse.changes;
                }
                if (attachInverse.changes !== undefined) {
                    (0, internal_1.assert)(inverted.changes === undefined, 0x811 /* Unexpected node changes */);
                    inverted.changes = attachInverse.changes;
                }
                inverses.push(inverted);
            }
            return inverses;
        }
        default:
            (0, internal_1.unreachableCase)(type);
    }
}
function applyMovedChanges(mark, revision, manager) {
    // Although this is a source mark, we query the destination because this was a destination mark during the original invert pass.
    const entry = manager.get(index_js_2.CrossFieldTarget.Destination, revision, mark.id, mark.count, true);
    if (entry.length < mark.count) {
        const [mark1, mark2] = (0, utils_js_1.splitMark)(mark, entry.length);
        const mark1WithChanges = entry.value !== undefined
            ? (0, utils_js_1.withNodeChange)(mark1, entry.value)
            : mark1;
        return [mark1WithChanges, ...applyMovedChanges(mark2, revision, manager)];
    }
    if (entry.value !== undefined) {
        manager.onMoveIn(entry.value);
        return [(0, utils_js_1.withNodeChange)(mark, entry.value)];
    }
    return [mark];
}
function invertNodeChangeOrSkip(count, changes, cellId) {
    if (changes !== undefined) {
        (0, internal_1.assert)(count === 1, 0x66c /* A modify mark must have length equal to one */);
        const noop = {
            count,
            changes,
        };
        if (cellId !== undefined) {
            noop.cellId = cellId;
        }
        return noop;
    }
    if (cellId !== undefined) {
        return { count, cellId };
    }
    return { count };
}
//# sourceMappingURL=invert.js.map