"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.areDetachedNodeIdsEqual = exports.offsetDetachId = exports.makeDetachedNodeId = exports.deltaForSet = exports.deltaForRootInitialization = exports.isEmptyFieldChanges = exports.isReplaceMark = exports.isDetachMark = exports.isAttachMark = exports.emptyFieldChanges = exports.emptyDelta = void 0;
const types_1 = require("./types");
exports.emptyDelta = {};
exports.emptyFieldChanges = {};
function isAttachMark(mark) {
    return mark.attach !== undefined && mark.detach === undefined;
}
exports.isAttachMark = isAttachMark;
function isDetachMark(mark) {
    return mark.detach !== undefined && mark.attach === undefined;
}
exports.isDetachMark = isDetachMark;
function isReplaceMark(mark) {
    return mark.detach !== undefined && mark.attach !== undefined;
}
exports.isReplaceMark = isReplaceMark;
function isEmptyFieldChanges(fieldChanges) {
    return (fieldChanges.local === undefined &&
        fieldChanges.global === undefined &&
        fieldChanges.build === undefined &&
        fieldChanges.rename === undefined);
}
exports.isEmptyFieldChanges = isEmptyFieldChanges;
function deltaForRootInitialization(content) {
    if (content.length === 0) {
        return exports.emptyDelta;
    }
    const buildId = { minor: 0 };
    const delta = {
        build: [{ id: buildId, trees: content }],
        fields: new Map([
            [
                types_1.rootFieldKey,
                {
                    local: [{ count: content.length, attach: buildId }],
                },
            ],
        ]),
    };
    return delta;
}
exports.deltaForRootInitialization = deltaForRootInitialization;
function deltaForSet(newNode, buildId, detachId) {
    const mark = { count: 1, attach: buildId };
    if (detachId !== undefined) {
        mark.detach = detachId;
    }
    return {
        build: [{ id: buildId, trees: [newNode] }],
        local: [mark],
    };
}
exports.deltaForSet = deltaForSet;
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;
//# sourceMappingURL=deltaUtil.js.map