/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { assert } from "@fluidframework/common-utils";
import { fail } from "../../util";
import { CrossFieldTarget, } from "../modular-schema";
import { NoopMarkType } from "./format";
import { MarkListFactory } from "./markListFactory";
import { areInputCellsEmpty, getInputLength, isConflictedReattach, isReattachConflicted, splitMark, withNodeChange, } from "./utils";
/**
 * Inverts a given changeset.
 * @param change - The changeset to produce the inverse of.
 * @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.
 */
export function invert(change, invertChild, reviver, genId, crossFieldManager) {
    return invertMarkList(change.change, change.revision, reviver, invertChild, crossFieldManager);
}
export function amendInvert(invertedChange, originalRevision, reviver, genId, crossFieldManager) {
    return amendMarkList(invertedChange, originalRevision, crossFieldManager);
}
function invertMarkList(markList, revision, reviver, invertChild, crossFieldManager) {
    const inverseMarkList = new MarkListFactory();
    let inputIndex = 0;
    for (const mark of markList) {
        const inverseMarks = invertMark(mark, inputIndex, revision, reviver, invertChild, crossFieldManager);
        inverseMarkList.push(...inverseMarks);
        inputIndex += getInputLength(mark);
    }
    return inverseMarkList.list;
}
function invertMark(mark, inputIndex, revision, reviver, invertChild, crossFieldManager) {
    switch (mark.type) {
        case NoopMarkType: {
            const inverse = { ...mark };
            if (mark.changes !== undefined) {
                if (mark.cellId === undefined) {
                    inverse.changes = invertChild(mark.changes, inputIndex);
                }
                else {
                    // TODO: preserve modifications to the removed nodes.
                    delete inverse.changes;
                }
            }
            return [inverse];
        }
        case "Insert": {
            assert(mark.cellId !== undefined, 0x72c /* Insert marks must have a cellId */);
            if (mark.transientDetach !== undefined) {
                assert(revision !== undefined, 0x720 /* Unable to revert to undefined revision */);
                return [
                    withNodeChange({
                        type: "Revive",
                        cellId: {
                            revision: mark.transientDetach.revision ?? revision,
                            localId: mark.transientDetach.localId,
                        },
                        content: reviver(revision, inputIndex, mark.content.length),
                        count: mark.content.length,
                        inverseOf: mark.revision ?? revision,
                        transientDetach: {
                            revision: mark.cellId.revision ?? revision,
                            localId: mark.cellId.localId,
                        },
                    }, invertNodeChange(mark.changes, inputIndex, invertChild)),
                ];
            }
            else {
                const inverse = withNodeChange({ type: "Delete", count: mark.content.length, id: mark.cellId.localId }, invertNodeChange(mark.changes, inputIndex, invertChild));
                return [inverse];
            }
        }
        case "Delete": {
            assert(revision !== undefined, 0x5a1 /* Unable to revert to undefined revision */);
            if (mark.cellId === undefined) {
                const inverse = withNodeChange({
                    type: "Revive",
                    cellId: { revision: mark.revision ?? revision, localId: mark.id },
                    content: reviver(revision, inputIndex, mark.count),
                    count: mark.count,
                    inverseOf: mark.revision ?? revision,
                }, invertNodeChange(mark.changes, inputIndex, invertChild));
                return [inverse];
            }
            // TODO: preserve modifications to the removed nodes.
            return [];
        }
        case "Revive": {
            if (!isReattachConflicted(mark)) {
                assert(mark.cellId !== undefined, 0x707 /* Active reattach should have a detach event */);
                if (mark.transientDetach !== undefined) {
                    assert(revision !== undefined, 0x721 /* Unable to revert to undefined revision */);
                    return [
                        withNodeChange({
                            type: "Revive",
                            cellId: {
                                revision: mark.transientDetach.revision ?? revision,
                                localId: mark.transientDetach.localId,
                            },
                            content: reviver(revision, inputIndex, mark.count),
                            count: mark.count,
                            inverseOf: mark.revision ?? revision,
                            transientDetach: {
                                revision: mark.revision ?? revision,
                                localId: mark.cellId.localId,
                            },
                        }, invertNodeChange(mark.changes, inputIndex, invertChild)),
                    ];
                }
                const inverse = withNodeChange({
                    type: "Delete",
                    count: mark.count,
                    id: mark.cellId.localId,
                }, invertNodeChange(mark.changes, inputIndex, invertChild));
                return [inverse];
            }
            return mark.transientDetach !== undefined
                ? invertMark({
                    type: "Delete",
                    count: mark.count,
                    revision: mark.transientDetach.revision ?? revision,
                    changes: mark.changes,
                    id: mark.transientDetach.localId,
                }, inputIndex, revision, reviver, invertChild, crossFieldManager)
                : [
                    invertNodeChangeOrSkip(mark.count, mark.changes, inputIndex, invertChild, mark.cellId),
                ];
        }
        case "MoveOut":
        case "ReturnFrom": {
            if (areInputCellsEmpty(mark)) {
                // TODO: preserve modifications to the removed nodes.
                return [];
            }
            if (mark.type === "ReturnFrom" && mark.isDstConflicted) {
                // The nodes were present but the destination was conflicted, the mark had no effect on the nodes.
                return [invertNodeChangeOrSkip(mark.count, mark.changes, inputIndex, invertChild)];
            }
            if (mark.changes !== undefined) {
                assert(mark.count === 1, 0x6ed /* Mark with changes can only target a single cell */);
                crossFieldManager.set(CrossFieldTarget.Destination, mark.revision ?? revision, mark.id, mark.count, invertChild(mark.changes, inputIndex), true);
            }
            return [
                {
                    type: "ReturnTo",
                    id: mark.id,
                    count: mark.count,
                    cellId: {
                        revision: mark.revision ?? revision ?? fail("Revision must be defined"),
                        localId: mark.id,
                    },
                },
            ];
        }
        case "MoveIn":
        case "ReturnTo": {
            if (mark.isSrcConflicted) {
                return mark.type === "ReturnTo" && mark.cellId === undefined
                    ? [{ count: mark.count }]
                    : [];
            }
            if (mark.type === "ReturnTo") {
                if (mark.cellId === undefined) {
                    // The nodes were already attached, so the mark did not affect them.
                    return [{ count: mark.count }];
                }
                else if (isConflictedReattach(mark)) {
                    // The nodes were not attached and could not be attached.
                    return [];
                }
            }
            const invertedMark = {
                type: "ReturnFrom",
                id: mark.id,
                count: mark.count,
            };
            return applyMovedChanges(invertedMark, revision, crossFieldManager);
        }
        default:
            fail("Not implemented");
    }
}
function amendMarkList(marks, revision, crossFieldManager) {
    const factory = new MarkListFactory();
    for (const mark of marks) {
        if (mark.type === "MoveOut" || mark.type === "ReturnFrom") {
            factory.push(...applyMovedChanges(mark, revision, crossFieldManager));
        }
        else {
            factory.push(mark);
        }
    }
    return factory.list;
}
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(CrossFieldTarget.Destination, mark.revision ?? revision, mark.id, mark.count, true);
    if (entry === undefined) {
        return [mark];
    }
    if (entry.start > mark.id) {
        // The entry does not apply to the first cell in the mark.
        const [mark1, mark2] = splitMark(mark, entry.start - mark.id);
        return [mark1, ...applyMovedChanges(mark2, revision, manager)];
    }
    else if (entry.start + entry.length < mark.id + mark.count) {
        // The entry applies to the first cell in the mark, but not the mark's entire range.
        const [mark1, mark2] = splitMark(mark, entry.start + entry.length - mark.id);
        return [withNodeChange(mark1, entry.value), ...applyMovedChanges(mark2, revision, manager)];
    }
    else {
        // The entry applies to all cells in the mark.
        return [withNodeChange(mark, entry.value)];
    }
}
function invertNodeChangeOrSkip(length, changes, index, inverter, detachEvent) {
    if (changes !== undefined) {
        assert(length === 1, 0x66c /* A modify mark must have length equal to one */);
        const noop = {
            count: 1,
            changes: inverter(changes, index),
        };
        if (detachEvent !== undefined) {
            noop.cellId = detachEvent;
        }
        return noop;
    }
    return { count: detachEvent === undefined ? length : 0 };
}
function invertNodeChange(change, index, inverter) {
    return change === undefined ? undefined : inverter(change, index);
}
//# sourceMappingURL=invert.js.map