/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { jsonableTreeFromCursor } from "../treeTextCursor";
import { brand } from "../../util";
import { MarkListFactory } from "./markListFactory";
export const sequenceFieldEditor = {
    buildChildChange: (index, change) => markAtIndex(index, { type: "Modify", changes: change }),
    insert: (index, cursors, id) => markAtIndex(index, {
        type: "Insert",
        content: Array.isArray(cursors)
            ? cursors.map(jsonableTreeFromCursor)
            : [jsonableTreeFromCursor(cursors)],
        id,
    }),
    delete: (index, count) => count === 0 ? [] : markAtIndex(index, { type: "Delete", count }),
    revive: (index, count, detachedBy, reviver, detachIndex, isIntention) => {
        // Revives are typically created to undo a delete from the prior revision.
        // When that's the case, we know the content used to be at the index at which it is being revived.
        const computedDetachIndex = detachIndex !== null && detachIndex !== void 0 ? detachIndex : index;
        const mark = {
            type: "Revive",
            content: reviver(detachedBy, computedDetachIndex, count),
            count,
            detachedBy,
            detachIndex: computedDetachIndex,
        };
        if (isIntention) {
            mark.isIntention = true;
        }
        return count === 0 ? [] : markAtIndex(index, mark);
    },
    move(sourceIndex, count, destIndex, id) {
        const moveOut = {
            type: "MoveOut",
            id,
            count,
        };
        const moveIn = {
            type: "MoveIn",
            id,
            count,
        };
        return [markAtIndex(sourceIndex, moveOut), markAtIndex(destIndex, moveIn)];
    },
    return(sourceIndex, count, destIndex, detachedBy, detachIndex) {
        if (count === 0) {
            return [];
        }
        const id = brand(0);
        const returnFrom = {
            type: "ReturnFrom",
            id,
            count,
            detachedBy,
        };
        const returnTo = {
            type: "ReturnTo",
            id,
            count,
            detachedBy,
            // Returns are typically created to undo a move from the prior revision.
            // When that's the case, we know the content used to be at the index to which it is being returned.
            detachIndex: detachIndex !== null && detachIndex !== void 0 ? detachIndex : destIndex,
        };
        const factory = new MarkListFactory();
        if (sourceIndex < destIndex) {
            factory.pushOffset(sourceIndex);
            factory.pushContent(returnFrom);
            factory.pushOffset(destIndex - sourceIndex);
            factory.pushContent(returnTo);
        }
        else {
            factory.pushOffset(destIndex);
            factory.pushContent(returnTo);
            factory.pushOffset(sourceIndex - destIndex);
            factory.pushContent(returnFrom);
        }
        return factory.list;
    },
};
function markAtIndex(index, mark) {
    return index === 0 ? [mark] : [index, mark];
}
//# sourceMappingURL=sequenceFieldEditor.js.map