"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.populateChildModifications = exports.mapMark = exports.mapMarkList = exports.mapFieldMarks = void 0;
const common_utils_1 = require("@fluidframework/common-utils");
const core_1 = require("../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.
 */
function mapFieldMarks(fields, func) {
    const out = new Map();
    for (const [k, v] of fields) {
        out.set(k, mapMarkList(v, func));
    }
    return out;
}
exports.mapFieldMarks = mapFieldMarks;
/**
 * 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.
 */
function mapMarkList(list, func) {
    return list.map((mark) => mapMark(mark, func));
}
exports.mapMarkList = mapMarkList;
/**
 * 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.
 */
function mapMark(mark, func) {
    if ((0, core_1.isSkipMark)(mark)) {
        return mark;
    }
    const type = mark.type;
    switch (type) {
        case core_1.Delta.MarkType.Insert: {
            return Object.assign(Object.assign({ type: core_1.Delta.MarkType.Insert }, mapModifications(mark, func)), { content: mark.content.map(func) });
        }
        case core_1.Delta.MarkType.Modify: {
            return Object.assign({ type: core_1.Delta.MarkType.Modify }, mapModifications(mark, func));
        }
        case core_1.Delta.MarkType.MoveOut: {
            return Object.assign({ type: core_1.Delta.MarkType.MoveOut, count: mark.count, moveId: mark.moveId }, mapModifications(mark, func));
        }
        case core_1.Delta.MarkType.Delete: {
            return Object.assign({ type: core_1.Delta.MarkType.Delete, count: mark.count }, mapModifications(mark, func));
        }
        case core_1.Delta.MarkType.MoveIn:
            return mark;
        default:
            (0, common_utils_1.unreachableCase)(type);
    }
}
exports.mapMark = mapMark;
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;
}
function populateChildModifications(modifications, deltaMark) {
    if (Object.prototype.hasOwnProperty.call(modifications, "setValue")) {
        deltaMark.setValue = modifications.setValue;
    }
    if (modifications.fields !== undefined) {
        deltaMark.fields = modifications.fields;
    }
}
exports.populateChildModifications = populateChildModifications;
//# sourceMappingURL=deltaUtils.js.map