"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.SharedTreeChangeEnricher = void 0;
const internal_1 = require("@fluidframework/core-utils/internal");
const index_js_1 = require("../core/index.js");
const index_js_2 = require("../feature-libraries/index.js");
const index_js_3 = require("../util/index.js");
const sharedTreeChangeFamily_js_1 = require("./sharedTreeChangeFamily.js");
class SharedTreeChangeEnricher {
    /**
     * @param borrowedForest - The state based on which to enrich changes.
     * Not owned by the constructed instance.
     * @param schema - The schema that corresponds to the forest.
     * @param borrowedRemovedRoots - The set of removed roots based on which to enrich changes.
     * Not owned by the constructed instance.
     * @param idCompressor - The id compressor to use when chunking trees.
     * @param onEnrichCommit - Optional callback invoked whenever a commit is enriched.
     * @param onRefresherAdded - Optional callback invoked whenever a refresher is added during enrichment.
     * @param onForkState - Optional callback invoked whenever the enricher forks its state.
     * @param onApplyChange - Optional callback invoked whenever a change is applied to the owned state.
     */
    constructor(borrowedForest, borrowedRemovedRoots, schema, idCompressor, onEnrichCommit, onRefresherAdded, onForkState, onApplyChange) {
        this.schema = schema;
        this.idCompressor = idCompressor;
        this.onEnrichCommit = onEnrichCommit;
        this.onRefresherAdded = onRefresherAdded;
        this.onForkState = onForkState;
        this.onApplyChange = onApplyChange;
        /**
         * Queue of changes to be applied before querying for detached roots.
         */
        this.changeQueue = [];
        this.borrowed = {
            forest: borrowedForest,
            removedRoots: borrowedRemovedRoots,
        };
    }
    enrich(change) {
        this.onEnrichCommit?.();
        return (0, sharedTreeChangeFamily_js_1.updateRefreshers)(change, (id) => this.getDetachedRoot(id), index_js_2.relevantRemovedRoots, index_js_2.updateRefreshers);
    }
    getDetachedRoot(id) {
        this.purgeChangeQueue();
        const state = this.owned ?? this.borrowed;
        const root = state.removedRoots.tryGetEntry(id);
        if (root !== undefined) {
            const cursor = state.forest.getCursorAboveDetachedFields();
            const parentField = state.removedRoots.toFieldKey(root);
            cursor.enterField(parentField);
            cursor.enterNode(0);
            this.onRefresherAdded?.();
            return (0, index_js_2.chunkTree)(cursor, {
                policy: index_js_2.defaultChunkPolicy,
                idCompressor: this.idCompressor,
            });
        }
        return undefined;
    }
    enqueueChange(change) {
        this.changeQueue.push(typeof change === "function" ? change : () => change);
    }
    purgeChangeQueue() {
        if (this.changeQueue.length === 0) {
            return;
        }
        if (this.owned === undefined) {
            this.onForkState?.();
            this.owned = {
                forest: this.borrowed.forest.clone(this.schema),
                removedRoots: this.borrowed.removedRoots.clone(),
            };
        }
        for (const getChange of this.changeQueue) {
            const { change, revision } = getChange();
            this.onApplyChange?.();
            for (const dataOrSchemaChange of change.changes) {
                const type = dataOrSchemaChange.type;
                switch (type) {
                    case "data": {
                        const delta = (0, index_js_2.intoDelta)((0, index_js_1.tagChange)(dataOrSchemaChange.innerChange, revision));
                        const visitor = this.owned.forest.acquireVisitor();
                        (0, index_js_1.visitDelta)(delta, visitor, this.owned.removedRoots, revision);
                        visitor.free();
                        break;
                    }
                    case "schema": {
                        // This enricher doesn't need to maintain schema information.
                        // Note that the refreshers being generated through `updateChangeEnrichments` will be encoded using
                        // the schema that was used in the input context of the data changeset these refreshers are on.
                        // See the encoding logic in SharedTreeCore for details.
                        break;
                    }
                    default: {
                        (0, internal_1.unreachableCase)(type);
                    }
                }
            }
        }
        this.changeQueue.length = 0;
    }
    [index_js_3.disposeSymbol]() {
        // TODO: in the future, forest and/or its AnchorSet may require disposal.
    }
}
exports.SharedTreeChangeEnricher = SharedTreeChangeEnricher;
//# sourceMappingURL=sharedTreeChangeEnricher.js.map