/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { unreachableCase } from "@fluidframework/common-utils";
import { Delta, isSkipMark } from "../core";
/**
 * Converts a `Delta.FieldMarks` whose tree content is represented with by `TIn` instances
 * into a `Delta.FieldMarks`whose tree content is represented with by `TOut` instances.
 *
 * This function is useful for converting `Delta`s that represent tree content with cursors
 * into `Delta`s that represent tree content with a deep-comparable representation of the content.
 * See {@link assertDeltaEqual}.
 * @param fields - The Map of fields to convert. Not mutated.
 * @param func - The functions used to map tree content.
 */
export function mapFieldMarks(fields, func) {
    const out = new Map();
    for (const [k, v] of fields) {
        out.set(k, mapMarkList(v, func));
    }
    return out;
}
/**
 * Converts a `Delta.MarkList` whose tree content is represented with by `TIn` instances
 * into a `Delta.MarkList`whose tree content is represented with by `TOut` instances.
 *
 * This function is useful for converting `Delta`s that represent tree content with cursors
 * into `Delta`s that represent tree content with a deep-comparable representation of the content.
 * See {@link assertMarkListEqual}.
 * @param list - The list of marks to convert. Not mutated.
 * @param func - The functions used to map tree content.
 */
export function mapMarkList(list, func) {
    return list.map((mark) => mapMark(mark, func));
}
/**
 * Converts a `Delta.Mark` whose tree content is represented with by `TIn` instances
 * into a `Delta.Mark`whose tree content is represented with by `TOut` instances.
 *
 * This function is useful for converting `Delta`s that represent tree content with cursors
 * into `Delta`s that represent tree content with a deep-comparable representation of the content.
 * See {@link assertMarkListEqual}.
 * @param mark - The mark to convert. Not mutated.
 * @param func - The functions used to map tree content.
 */
export function mapMark(mark, func) {
    if (isSkipMark(mark)) {
        return mark;
    }
    const type = mark.type;
    switch (type) {
        case Delta.MarkType.Insert: {
            return Object.assign(Object.assign({ type: Delta.MarkType.Insert }, mapModifications(mark, func)), { content: mark.content.map(func) });
        }
        case Delta.MarkType.Modify: {
            return Object.assign({ type: Delta.MarkType.Modify }, mapModifications(mark, func));
        }
        case Delta.MarkType.MoveOut: {
            return Object.assign({ type: Delta.MarkType.MoveOut, count: mark.count, moveId: mark.moveId }, mapModifications(mark, func));
        }
        case Delta.MarkType.Delete: {
            return Object.assign({ type: Delta.MarkType.Delete, count: mark.count }, mapModifications(mark, func));
        }
        case Delta.MarkType.MoveIn:
            return mark;
        default:
            unreachableCase(type);
    }
}
function mapModifications(mark, func) {
    const out = {};
    if (mark.fields !== undefined) {
        out.fields = mapFieldMarks(mark.fields, func);
    }
    if (Object.prototype.hasOwnProperty.call(mark, "setValue")) {
        out.setValue = mark.setValue;
    }
    return out;
}
export function populateChildModifications(modifications, deltaMark) {
    if (Object.prototype.hasOwnProperty.call(modifications, "setValue")) {
        deltaMark.setValue = modifications.setValue;
    }
    if (modifications.fields !== undefined) {
        deltaMark.fields = modifications.fields;
    }
}
//# sourceMappingURL=deltaUtils.js.map