/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { unreachableCase } from "@fluidframework/core-utils/internal";
import { replaceAtomRevisions } from "../../core/index.js";
import { MarkListFactory } from "./markListFactory.js";
import { NoopMarkType, } from "./types.js";
export function replaceRevisions(changeset, revisionsToReplace, newRevision) {
    const updatedMarks = new MarkListFactory();
    for (const mark of changeset) {
        const updatedMark = updateMark(mark, revisionsToReplace, newRevision);
        updatedMarks.push(updatedMark);
    }
    return updatedMarks.list;
}
function updateMark(mark, revisionsToReplace, newRevision) {
    const updatedMark = { ...replaceEffectRevisions(mark, revisionsToReplace, newRevision) };
    if (mark.cellId !== undefined) {
        updatedMark.cellId = replaceAtomRevisions(mark.cellId, revisionsToReplace, newRevision);
    }
    if (mark.changes !== undefined) {
        updatedMark.changes = replaceAtomRevisions(mark.changes, revisionsToReplace, newRevision);
    }
    return updatedMark;
}
function replaceEffectRevisions(mark, revisionsToReplace, newRevision) {
    const type = mark.type;
    switch (type) {
        case NoopMarkType:
            return mark;
        case "Insert":
            return replaceRevision(mark, revisionsToReplace, newRevision);
        case "Remove":
            return replaceDetachRevisions(mark, revisionsToReplace, newRevision);
        case "Rename":
            return {
                ...mark,
                idOverride: replaceAtomRevisions(mark.idOverride, revisionsToReplace, newRevision),
            };
        default:
            unreachableCase(type);
    }
}
function replaceDetachRevisions(detach, revisionsToReplace, newRevision) {
    const updated = replaceRevision(detach, revisionsToReplace, newRevision);
    if (updated.cellRename !== undefined) {
        updated.cellRename = replaceAtomRevisions(updated.cellRename, revisionsToReplace, newRevision);
    }
    if (updated.detachCellId !== undefined) {
        updated.detachCellId = replaceAtomRevisions(updated.detachCellId, revisionsToReplace, newRevision);
    }
    return updated;
}
function replaceRevision(input, revisionsToReplace, newRevision) {
    return revisionsToReplace.has(input.revision) ? withRevision(input, newRevision) : input;
}
function withRevision(input, revision) {
    const updated = { ...input, revision };
    if (revision === undefined) {
        delete updated.revision;
    }
    return updated;
}
//# sourceMappingURL=replaceRevisions.js.map