/*!
 * 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";
import { isDetach, isRename } from "./utils.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 = { ...updateEffect(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 updateEffect(input, revisionsToReplace, newRevision) {
    const mark = isDetach(input) || isRename(input)
        ? updateIdOverride(input, revisionsToReplace, newRevision)
        : input;
    const type = mark.type;
    switch (type) {
        case "Rename":
        case NoopMarkType:
            return mark;
        case "AttachAndDetach":
            return {
                ...mark,
                attach: updateEffect(mark.attach, revisionsToReplace, newRevision),
                detach: updateEffect(mark.detach, revisionsToReplace, newRevision),
            };
        case "MoveIn":
        case "MoveOut":
            return updateMoveEffect(
            // For some reason, TypeScript is not able to infer that `mark` cannot be a `NoopMark` here.
            mark, revisionsToReplace, newRevision);
        case "Insert":
        case "Remove":
            return updateRevision(mark, revisionsToReplace, newRevision);
        default:
            unreachableCase(type);
    }
}
function updateIdOverride(effect, revisionsToReplace, newRevision) {
    if (effect.idOverride !== undefined) {
        const idOverride = replaceAtomRevisions(effect.idOverride, revisionsToReplace, newRevision);
        return { ...effect, idOverride };
    }
    else {
        return effect;
    }
}
function updateMoveEffect(effect, revisionsToReplace, newRevision) {
    return effect.finalEndpoint !== undefined
        ? updateRevision({
            ...effect,
            finalEndpoint: updateRevision(effect.finalEndpoint, revisionsToReplace, newRevision),
        }, revisionsToReplace, newRevision)
        : updateRevision(effect, revisionsToReplace, newRevision);
}
function updateRevision(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