"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.sequenceFieldToDelta = void 0;
const internal_1 = require("@fluidframework/core-utils/internal");
const index_js_1 = require("../../util/index.js");
const deltaUtils_js_1 = require("../deltaUtils.js");
const types_js_1 = require("./types.js");
const utils_js_1 = require("./utils.js");
function sequenceFieldToDelta(change, deltaFromChild) {
    const deltaMarks = [];
    for (const mark of change) {
        const deltaMark = { count: mark.count };
        const inputCellId = (0, utils_js_1.getInputCellId)(mark);
        const changes = mark.changes;
        if (changes !== undefined && inputCellId === undefined) {
            const nestedDelta = deltaFromChild(changes);
            if (nestedDelta.size > 0) {
                deltaMark.fields = nestedDelta;
            }
        }
        if (!(0, utils_js_1.areInputCellsEmpty)(mark) && !(0, utils_js_1.areOutputCellsEmpty)(mark)) {
            // Since each cell is associated with exactly one node,
            // the cell starting end ending populated means the cell content has not changed.
            deltaMarks.push(deltaMark);
        }
        else {
            const type = mark.type;
            // Inline into `switch(mark.type)` once we upgrade to TS 4.7
            switch (type) {
                case "Remove": {
                    const newDetachId = (0, utils_js_1.getDetachedRootId)(mark);
                    if (inputCellId === undefined) {
                        deltaMark.detach = (0, deltaUtils_js_1.nodeIdFromChangeAtom)(newDetachId);
                        deltaMarks.push(deltaMark);
                    }
                    break;
                }
                case "Insert": {
                    const buildId = (0, deltaUtils_js_1.nodeIdFromChangeAtom)((0, utils_js_1.getAttachedRootId)(mark));
                    deltaMark.attach = buildId;
                    deltaMarks.push(deltaMark);
                    break;
                }
                case types_js_1.NoopMarkType:
                    if (inputCellId === undefined) {
                        deltaMarks.push(deltaMark);
                    }
                    break;
                case "Rename":
                    (0, internal_1.assert)(mark.cellId !== undefined, 0x9f9 /* Renames should only target empty cells */);
                    break;
                default:
                    (0, internal_1.unreachableCase)(type);
            }
        }
    }
    // Remove trailing no-op marks
    while ((0, index_js_1.hasSome)(deltaMarks)) {
        const lastMark = (0, index_js_1.getLast)(deltaMarks);
        if (lastMark.attach !== undefined ||
            lastMark.detach !== undefined ||
            lastMark.fields !== undefined) {
            break;
        }
        deltaMarks.pop();
    }
    return deltaMarks;
}
exports.sequenceFieldToDelta = sequenceFieldToDelta;
//# sourceMappingURL=sequenceFieldToDelta.js.map