/*!
 * 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 _BranchCommitEnricher_transactionEnricher, _BranchCommitEnricher_enricher, _BranchCommitEnricher_preparedCommits, _BranchCommitEnricher_getOuterTransactionChange;
import { assert } from "@fluidframework/core-utils/internal";
import { replaceChange, } from "../core/index.js";
import { TransactionEnricher } from "./transactionEnricher.js";
/**
 * Utility for enriching commits from a {@link Branch} before these commits are applied and submitted.
 */
export class BranchCommitEnricher {
    constructor(rebaser, enricher) {
        _BranchCommitEnricher_transactionEnricher.set(this, void 0);
        _BranchCommitEnricher_enricher.set(this, void 0);
        /**
         * Maps each local commit to the corresponding enriched commit.
         * @remarks
         * Entries are added when the commits are {@link BranchCommitEnricher.processChange | processed during a change}.
         * Each entry is removed when it is {@link BranchCommitEnricher.enrich | retrieved}.
         * In the event that an entry is not explicitly removed, it will eventually be {@link WeakMap | dropped from memory} along with the associated commit.
         */
        _BranchCommitEnricher_preparedCommits.set(this, new Map());
        /**
         * If defined, a top-level transaction has been {@link BranchCommitEnricher.commitTransaction | committed} since the last {@link BranchCommitEnricher.processChange | change has been processed}.
         * Calling this function will compute the composition of that transaction's commits.
         * @remarks This function will be reset to undefined after each {@link BranchCommitEnricher.processChange | change is processed}.
         */
        _BranchCommitEnricher_getOuterTransactionChange.set(this, void 0);
        __classPrivateFieldSet(this, _BranchCommitEnricher_enricher, enricher, "f");
        __classPrivateFieldSet(this, _BranchCommitEnricher_transactionEnricher, new TransactionEnricher(rebaser, __classPrivateFieldGet(this, _BranchCommitEnricher_enricher, "f")), "f");
    }
    /**
     * Process the given change, preparing new commits for {@link BranchCommitEnricher.enrich | enrichment}.
     * @param change - The change to process.
     * @param isAttached - Whether or not the SharedTree is attached to the service.
     */
    processChange(change) {
        if (change.type === "append") {
            for (const newCommit of change.newCommits) {
                const newChange = __classPrivateFieldGet(this, _BranchCommitEnricher_getOuterTransactionChange, "f")?.call(this, newCommit.revision) ??
                    __classPrivateFieldGet(this, _BranchCommitEnricher_enricher, "f").updateChangeEnrichments(newCommit.change, newCommit.revision);
                __classPrivateFieldGet(this, _BranchCommitEnricher_preparedCommits, "f").set(newCommit, replaceChange(newCommit, newChange));
            }
        }
        __classPrivateFieldSet(this, _BranchCommitEnricher_getOuterTransactionChange, undefined, "f");
    }
    /**
     * Retrieves the enriched version of the given commit.
     * @param commit - A commit {@link BranchCommitEnricher.processChange | processed during the most recent change}.
     * @remarks A commit can only be enriched once - subsequent calls to this method with the same commit will throw an error.
     */
    enrich(commit) {
        const prepared = __classPrivateFieldGet(this, _BranchCommitEnricher_preparedCommits, "f").get(commit);
        assert(prepared !== undefined, 0x980 /* Unknown commit */);
        __classPrivateFieldGet(this, _BranchCommitEnricher_preparedCommits, "f").delete(commit);
        return prepared;
    }
    /**
     * Notify the enricher that a new transaction has started.
     * @remarks This may be called multiple times without calling {@link BranchCommitEnricher.commitTransaction | commitTransaction}, producing "nested transactions".
     */
    startTransaction() {
        __classPrivateFieldGet(this, _BranchCommitEnricher_transactionEnricher, "f").startTransaction();
    }
    /**
     * Commit the current transaction.
     * @remarks This should be called _before_ the corresponding transaction commit change is {@link BranchCommitEnricher.processChange | processed}.
     */
    commitTransaction() {
        __classPrivateFieldSet(this, _BranchCommitEnricher_getOuterTransactionChange, __classPrivateFieldGet(this, _BranchCommitEnricher_transactionEnricher, "f").commitTransaction(), "f");
    }
    /**
     * Notify the enricher that the current transaction has been aborted.
     * @remarks This will throw an error if there is no ongoing transaction.
     */
    abortTransaction() {
        __classPrivateFieldGet(this, _BranchCommitEnricher_transactionEnricher, "f").abortTransaction();
    }
    /**
     * Add new transaction commits to the current transaction.
     * @param newCommits - The new commits to add.
     * @remarks This will throw an error if there is no ongoing transaction.
     */
    addTransactionCommits(newCommits) {
        assert(__classPrivateFieldGet(this, _BranchCommitEnricher_transactionEnricher, "f").isTransacting(), 0xa97 /* Not in transaction */);
        for (const commit of newCommits) {
            __classPrivateFieldGet(this, _BranchCommitEnricher_transactionEnricher, "f").addTransactionStep(commit);
        }
    }
}
_BranchCommitEnricher_transactionEnricher = new WeakMap(), _BranchCommitEnricher_enricher = new WeakMap(), _BranchCommitEnricher_preparedCommits = new WeakMap(), _BranchCommitEnricher_getOuterTransactionChange = new WeakMap();
//# sourceMappingURL=branchCommitEnricher.js.map