"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.sequenceFieldEditor = void 0;
const common_utils_1 = require("@fluidframework/common-utils");
const treeTextCursor_1 = require("../treeTextCursor");
const util_1 = require("../../util");
const markListFactory_1 = require("./markListFactory");
exports.sequenceFieldEditor = {
    buildChildChange: (index, change) => markAtIndex(index, { count: 1, changes: change }),
    insert: (index, cursors, id) => {
        const mark = {
            type: "Insert",
            count: cursors.length,
            content: cursors.map(treeTextCursor_1.jsonableTreeFromCursor),
            cellId: { localId: id },
        };
        return markAtIndex(index, mark);
    },
    delete: (index, count, id) => count === 0 ? [] : markAtIndex(index, { type: "Delete", count, id }),
    revive: (index, count, detachEvent, reviver, isIntention = false) => {
        (0, common_utils_1.assert)(detachEvent.revision !== undefined, 0x724 /* Detach event must have a revision */);
        const mark = {
            type: "Revive",
            content: reviver(detachEvent.revision, detachEvent.localId, count),
            count,
            cellId: detachEvent,
        };
        if (!isIntention) {
            mark.inverseOf = detachEvent.revision;
        }
        return count === 0 ? [] : markAtIndex(index, mark);
    },
    move(sourceIndex, count, destIndex, id) {
        const moveOut = {
            type: "MoveOut",
            id,
            count,
        };
        const moveIn = {
            type: "MoveIn",
            id,
            count,
            cellId: { localId: id },
        };
        return [markAtIndex(sourceIndex, moveOut), markAtIndex(destIndex, moveIn)];
    },
    return(sourceIndex, count, destIndex, detachEvent) {
        if (count === 0) {
            return [];
        }
        const id = (0, util_1.brand)(0);
        const returnFrom = {
            type: "ReturnFrom",
            id,
            count,
        };
        const returnTo = {
            type: "ReturnTo",
            id,
            count,
            cellId: detachEvent,
        };
        const factory = new markListFactory_1.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] : [{ count: index }, mark];
}
//# sourceMappingURL=sequenceFieldEditor.js.map