"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.SharedTreeMutableChangeEnricher = exports.SharedTreeReadonlyChangeEnricher = 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 SharedTreeReadonlyChangeEnricher {
    /**
     * @param forest - The state based on which to enrich changes.
     * Exclusively owned by the constructed instance.
     * @param schema - The schema that corresponds to the forest.
     * @param removedRoots - The set of removed roots based on which to enrich changes.
     * Exclusively owned by the constructed instance.
     */
    constructor(forest, schema, removedRoots, idCompressor) {
        this.forest = forest;
        this.schema = schema;
        this.removedRoots = removedRoots;
        this.idCompressor = idCompressor;
        this.getDetachedRoot = (id) => {
            const root = this.removedRoots.tryGetEntry(id);
            if (root !== undefined) {
                const cursor = this.forest.getCursorAboveDetachedFields();
                const parentField = this.removedRoots.toFieldKey(root);
                cursor.enterField(parentField);
                cursor.enterNode(0);
                return (0, index_js_2.chunkTree)(cursor, {
                    policy: index_js_2.defaultChunkPolicy,
                    idCompressor: this.idCompressor,
                });
            }
            return undefined;
        };
    }
    fork() {
        return new SharedTreeMutableChangeEnricher(this.forest.clone(this.schema, new index_js_1.AnchorSet()), this.schema, this.removedRoots.clone());
    }
    updateChangeEnrichments(change) {
        return (0, sharedTreeChangeFamily_js_1.updateRefreshers)(change, this.getDetachedRoot, index_js_2.relevantRemovedRoots, index_js_2.updateRefreshers);
    }
}
exports.SharedTreeReadonlyChangeEnricher = SharedTreeReadonlyChangeEnricher;
class SharedTreeMutableChangeEnricher extends SharedTreeReadonlyChangeEnricher {
    applyTipChange(change, revision) {
        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.forest.acquireVisitor();
                    (0, index_js_1.visitDelta)(delta, visitor, this.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);
            }
        }
    }
    [index_js_3.disposeSymbol]() {
        // TODO: in the future, forest and/or its AnchorSet may require disposal.
    }
}
exports.SharedTreeMutableChangeEnricher = SharedTreeMutableChangeEnricher;
//# sourceMappingURL=sharedTreeChangeEnricher.js.map