"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
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) {
        this.transactionCommits = [];
        /**
         * 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)
         */
        this.transactionScopesStart = [];
        this.rebaser = rebaser;
        this.enricher = enricher;
    }
    startNewTransaction() {
        this.transactionScopesStart.push(this.transactionCommits.length);
    }
    commitCurrentTransaction() {
        const commitsCommitted = this.transactionScopesStart.pop();
        (0, internal_1.assert)(commitsCommitted !== undefined, 0x985 /* No transaction to commit */);
    }
    abortCurrentTransaction() {
        const scopeStart = this.transactionScopesStart.pop();
        (0, internal_1.assert)(scopeStart !== undefined, 0x986 /* No transaction to abort */);
        this.transactionCommits.length = scopeStart;
    }
    addTransactionStep(commit) {
        (0, internal_1.assert)(this.transactionScopesStart.length !== 0, 0x987 /* No transaction to add a step to */);
        const change = this.enricher.updateChangeEnrichments(commit.change, commit.revision);
        this.transactionCommits.push({ ...commit, change });
    }
    getComposedChange(revision) {
        (0, internal_1.assert)(this.transactionScopesStart.length === 0, 0x988 /* Transaction not committed */);
        const squashed = this.rebaser.compose(this.transactionCommits);
        const tagged = this.rebaser.changeRevision(squashed, revision);
        this.transactionCommits.length = 0;
        return tagged;
    }
}
exports.TransactionEnricher = TransactionEnricher;
//# sourceMappingURL=transactionEnricher.js.map