"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.deltaFieldChangesHaveVisibleChanges = exports.deltaFieldMapHasVisibleChanges = exports.areDetachedNodeIdsEqual = exports.offsetDetachId = exports.makeDetachedNodeId = exports.deltaForRootInitialization = exports.emptyDelta = void 0;
const types_js_1 = require("./types.js");
exports.emptyDelta = {};
function deltaForRootInitialization(content) {
    if (content.topLevelLength === 0) {
        return exports.emptyDelta;
    }
    const buildId = { minor: 0 };
    const delta = {
        build: [{ id: buildId, trees: content }],
        fields: new Map([
            [types_js_1.rootFieldKey, { marks: [{ count: content.topLevelLength, attach: buildId }] }],
        ]),
    };
    return delta;
}
exports.deltaForRootInitialization = deltaForRootInitialization;
function makeDetachedNodeId(major, minor) {
    const out = { minor };
    if (major !== undefined) {
        out.major = major;
    }
    return out;
}
exports.makeDetachedNodeId = makeDetachedNodeId;
function offsetDetachId(id, offset) {
    if (id === undefined) {
        return undefined;
    }
    return {
        ...id,
        minor: id.minor + offset,
    };
}
exports.offsetDetachId = offsetDetachId;
function areDetachedNodeIdsEqual(a, b) {
    return a.major === b.major && a.minor === b.minor;
}
exports.areDetachedNodeIdsEqual = areDetachedNodeIdsEqual;
/**
 * Returns true if a delta field map contains any changes that would be visible in the document (eg, an insert, move, edit)
 * @param fields - Delta FieldMap to check for visible changes
 * @returns True if change map contains any changes that would be visible in the document, false otherwise
 */
function deltaFieldMapHasVisibleChanges(fields) {
    if (fields === undefined || fields.size === 0) {
        return false;
    }
    for (const [, fieldChanges] of fields) {
        if (deltaFieldChangesHaveVisibleChanges(fieldChanges)) {
            return true;
        }
    }
    return false;
}
exports.deltaFieldMapHasVisibleChanges = deltaFieldMapHasVisibleChanges;
/**
 * Returns true if the given field changes contains any changes that would be visible in the document (eg, an insert, move, edit)
 * @param fieldChanges - Field changes to check for visible changes
 * @returns True if the field changes contain any changes that would be visible in the document, false otherwise
 */
function deltaFieldChangesHaveVisibleChanges(fieldChanges) {
    for (const mark of fieldChanges.marks) {
        if (mark.attach !== undefined ||
            mark.detach !== undefined ||
            deltaFieldMapHasVisibleChanges(mark.fields)) {
            return true;
        }
    }
    return false;
}
exports.deltaFieldChangesHaveVisibleChanges = deltaFieldChangesHaveVisibleChanges;
//# sourceMappingURL=deltaUtil.js.map