/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { assert } from "@fluidframework/core-utils";
import { UnassignedSequenceNumber, UniversalSequenceNumber } from "./constants.mjs";
import { createMap } from "./properties.mjs";
/**
 * @alpha
 */
export var PropertiesRollback;
(function (PropertiesRollback) {
    /** Not in a rollback */
    PropertiesRollback[PropertiesRollback["None"] = 0] = "None";
    /** Rollback */
    PropertiesRollback[PropertiesRollback["Rollback"] = 1] = "Rollback";
})(PropertiesRollback || (PropertiesRollback = {}));
/**
 * @alpha
 */
export class PropertiesManager {
    ackPendingProperties(annotateOp) {
        this.decrementPendingCounts(annotateOp.props);
    }
    decrementPendingCounts(props) {
        for (const key of Object.keys(props)) {
            if (this.pendingKeyUpdateCount?.[key] !== undefined) {
                assert(this.pendingKeyUpdateCount[key] > 0, 0x05c /* "Trying to update more annotate props than do exist!" */);
                this.pendingKeyUpdateCount[key]--;
                if (this.pendingKeyUpdateCount?.[key] === 0) {
                    // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
                    delete this.pendingKeyUpdateCount[key];
                }
            }
        }
    }
    addProperties(oldProps, newProps, seq, collaborating = false, rollback = PropertiesRollback.None) {
        // eslint-disable-next-line import/no-deprecated
        this.pendingKeyUpdateCount ?? (this.pendingKeyUpdateCount = createMap());
        // Clean up counts for rolled back edits before modifying oldProps
        if (collaborating && rollback === PropertiesRollback.Rollback) {
            this.decrementPendingCounts(newProps);
        }
        const shouldModifyKey = (key) => {
            if (seq === UnassignedSequenceNumber ||
                seq === UniversalSequenceNumber ||
                this.pendingKeyUpdateCount?.[key] === undefined) {
                return true;
            }
            return false;
        };
        const deltas = {};
        for (const key of Object.keys(newProps)) {
            if (collaborating) {
                if (seq === UnassignedSequenceNumber) {
                    if (this.pendingKeyUpdateCount?.[key] === undefined) {
                        this.pendingKeyUpdateCount[key] = 0;
                    }
                    this.pendingKeyUpdateCount[key]++;
                }
                else if (!shouldModifyKey(key)) {
                    continue;
                }
            }
            const previousValue = oldProps[key];
            // The delta should be null if undefined, as that's how we encode delete
            deltas[key] = previousValue === undefined ? null : previousValue;
            const newValue = newProps[key];
            if (newValue === null) {
                // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
                delete oldProps[key];
            }
            else {
                oldProps[key] = newValue;
            }
        }
        return deltas;
    }
    copyTo(oldProps, newProps, newManager) {
        if (oldProps) {
            // eslint-disable-next-line no-param-reassign, import/no-deprecated
            newProps ?? (newProps = createMap());
            if (!newManager) {
                throw new Error("Must provide new PropertyManager");
            }
            for (const key of Object.keys(oldProps)) {
                newProps[key] = oldProps[key];
            }
            // eslint-disable-next-line import/no-deprecated
            newManager.pendingKeyUpdateCount = createMap();
            for (const key of Object.keys(this.pendingKeyUpdateCount)) {
                newManager.pendingKeyUpdateCount[key] = this.pendingKeyUpdateCount[key];
            }
        }
        return newProps;
    }
    hasPendingProperties() {
        return Object.keys(this.pendingKeyUpdateCount).length > 0;
    }
    hasPendingProperty(key) {
        return (this.pendingKeyUpdateCount?.[key] ?? 0) > 0;
    }
}
//# sourceMappingURL=segmentPropertiesManager.mjs.map