"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.replaceRevisions = void 0;
const internal_1 = require("@fluidframework/core-utils/internal");
const index_js_1 = require("../../core/index.js");
const markListFactory_js_1 = require("./markListFactory.js");
const types_js_1 = require("./types.js");
function replaceRevisions(changeset, revisionsToReplace, newRevision) {
    const updatedMarks = new markListFactory_js_1.MarkListFactory();
    for (const mark of changeset) {
        const updatedMark = updateMark(mark, revisionsToReplace, newRevision);
        updatedMarks.push(updatedMark);
    }
    return updatedMarks.list;
}
exports.replaceRevisions = replaceRevisions;
function updateMark(mark, revisionsToReplace, newRevision) {
    const updatedMark = { ...updateEffect(mark, revisionsToReplace, newRevision) };
    if (mark.cellId !== undefined) {
        updatedMark.cellId = (0, index_js_1.replaceAtomRevisions)(mark.cellId, revisionsToReplace, newRevision);
    }
    if (mark.changes !== undefined) {
        updatedMark.changes = (0, index_js_1.replaceAtomRevisions)(mark.changes, revisionsToReplace, newRevision);
    }
    return updatedMark;
}
function updateEffect(mark, revisionsToReplace, newRevision) {
    const type = mark.type;
    switch (type) {
        case types_js_1.NoopMarkType:
            return mark;
        case "AttachAndDetach":
            return {
                ...mark,
                attach: updateEffect(mark.attach, revisionsToReplace, newRevision),
                detach: updateEffect(mark.detach, revisionsToReplace, newRevision),
            };
        case "Insert":
        case "MoveIn":
        case "MoveOut":
        case "Remove":
            return updateEffectRevision(mark, revisionsToReplace, newRevision);
        default:
            (0, internal_1.unreachableCase)(type);
    }
}
function updateEffectRevision(effect, revisionsToReplace, newRevision) {
    return revisionsToReplace.has(effect.revision)
        ? effectWithRevision(effect, newRevision)
        : effect;
}
function effectWithRevision(effect, revision) {
    const updated = { ...effect, revision };
    if (revision === undefined) {
        delete updated.revision;
    }
    return updated;
}
//# sourceMappingURL=replaceRevisions.js.map