"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
    if (kind === "m") throw new TypeError("Private method is not writable");
    if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
    if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
    return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
    if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
    if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
    return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _TransactionEnricher_rebaser, _TransactionEnricher_enricher, _TransactionEnricher_transactionCommits, _TransactionEnricher_transactionScopesStart;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TransactionEnricher = void 0;
const internal_1 = require("@fluidframework/core-utils/internal");
/**
 * Utility for producing an enriched commit out of multiple transaction steps
 */
class TransactionEnricher {
    constructor(rebaser, enricher) {
        _TransactionEnricher_rebaser.set(this, void 0);
        _TransactionEnricher_enricher.set(this, void 0);
        _TransactionEnricher_transactionCommits.set(this, []);
        /**
         * The number of commits before the start of each active transaction scope.
         * For a stack of `n` transaction scopes, the array will contain `n` integers,
         * where the integer at index `i` is the number of commits made before the start of the `i`th transaction scope
         * (therefore, the first element in the array, if present, is always 0)
         */
        _TransactionEnricher_transactionScopesStart.set(this, []);
        __classPrivateFieldSet(this, _TransactionEnricher_rebaser, rebaser, "f");
        __classPrivateFieldSet(this, _TransactionEnricher_enricher, enricher, "f");
    }
    isTransacting() {
        return __classPrivateFieldGet(this, _TransactionEnricher_transactionScopesStart, "f").length !== 0;
    }
    startTransaction() {
        __classPrivateFieldGet(this, _TransactionEnricher_transactionScopesStart, "f").push(__classPrivateFieldGet(this, _TransactionEnricher_transactionCommits, "f").length);
    }
    /**
     * Commits the current transaction.
     * @returns A function which can be used to compute the composed change for that transaction's commits. Undefined if the transaction is still ongoing or contained no committed changes.
     */
    commitTransaction() {
        const commitsCommitted = __classPrivateFieldGet(this, _TransactionEnricher_transactionScopesStart, "f").pop();
        (0, internal_1.assert)(commitsCommitted !== undefined, 0x985 /* No transaction to commit */);
        if (__classPrivateFieldGet(this, _TransactionEnricher_transactionScopesStart, "f").length === 0) {
            if (__classPrivateFieldGet(this, _TransactionEnricher_transactionCommits, "f").length === 0) {
                return undefined;
            }
            const transactionCommits = __classPrivateFieldGet(this, _TransactionEnricher_transactionCommits, "f");
            __classPrivateFieldSet(this, _TransactionEnricher_transactionCommits, [], "f");
            return (revision) => __classPrivateFieldGet(this, _TransactionEnricher_rebaser, "f").changeRevision(__classPrivateFieldGet(this, _TransactionEnricher_rebaser, "f").compose(transactionCommits), revision);
        }
    }
    abortTransaction() {
        const scopeStart = __classPrivateFieldGet(this, _TransactionEnricher_transactionScopesStart, "f").pop();
        (0, internal_1.assert)(scopeStart !== undefined, 0x986 /* No transaction to abort */);
        __classPrivateFieldGet(this, _TransactionEnricher_transactionCommits, "f").length = scopeStart;
    }
    addTransactionStep(commit) {
        (0, internal_1.assert)(__classPrivateFieldGet(this, _TransactionEnricher_transactionScopesStart, "f").length !== 0, 0x987 /* No transaction to add a step to */);
        const change = __classPrivateFieldGet(this, _TransactionEnricher_enricher, "f").updateChangeEnrichments(commit.change, commit.revision);
        __classPrivateFieldGet(this, _TransactionEnricher_transactionCommits, "f").push({ ...commit, change });
    }
}
exports.TransactionEnricher = TransactionEnricher;
_TransactionEnricher_rebaser = new WeakMap(), _TransactionEnricher_enricher = new WeakMap(), _TransactionEnricher_transactionCommits = new WeakMap(), _TransactionEnricher_transactionScopesStart = new WeakMap();
//# sourceMappingURL=transactionEnricher.js.map