/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { unreachableCase } from "@fluidframework/core-utils/internal";
import { AnchorSet, tagChange, visitDelta, } from "../core/index.js";
import { chunkTree, defaultChunkPolicy, getBuildsIds, intoDelta, relevantRemovedRoots, updateRefreshers as updateDataChangeRefreshers, } from "../feature-libraries/index.js";
import { disposeSymbol } from "../util/index.js";
import { updateRefreshers } from "./sharedTreeChangeFamily.js";
export class SharedTreeReadonlyChangeEnricher {
    /**
     * @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.
     */
    constructor(borrowedForest, schema, borrowedRemovedRoots, idCompressor) {
        this.borrowedForest = borrowedForest;
        this.schema = schema;
        this.borrowedRemovedRoots = borrowedRemovedRoots;
        this.idCompressor = idCompressor;
        this.getDetachedRoot = (id) => {
            const root = this.borrowedRemovedRoots.tryGetEntry(id);
            if (root !== undefined) {
                const cursor = this.borrowedForest.getCursorAboveDetachedFields();
                const parentField = this.borrowedRemovedRoots.toFieldKey(root);
                cursor.enterField(parentField);
                cursor.enterNode(0);
                return chunkTree(cursor, {
                    policy: defaultChunkPolicy,
                    idCompressor: this.idCompressor,
                });
            }
            return undefined;
        };
    }
    fork() {
        return new SharedTreeMutableChangeEnricher(this.borrowedForest.clone(this.schema, new AnchorSet()), this.schema, this.borrowedRemovedRoots.clone());
    }
    updateChangeEnrichments(change) {
        return updateRefreshers(change, getBuildsIds, this.getDetachedRoot, relevantRemovedRoots, updateDataChangeRefreshers);
    }
}
export class SharedTreeMutableChangeEnricher extends SharedTreeReadonlyChangeEnricher {
    /**
     * @param forest - The state based on which to enrich changes.
     * 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.
     * Owned by the constructed instance.
     * @param idCompressor - The id compressor to use when chunking trees.
     */
    constructor(forest, schema, removedRoots, idCompressor) {
        super(forest, schema, removedRoots, idCompressor);
        this.forest = forest;
        this.removedRoots = removedRoots;
    }
    applyTipChange(change, revision) {
        for (const dataOrSchemaChange of change.changes) {
            const type = dataOrSchemaChange.type;
            switch (type) {
                case "data": {
                    const delta = intoDelta(tagChange(dataOrSchemaChange.innerChange, revision));
                    const visitor = this.forest.acquireVisitor();
                    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:
                    unreachableCase(type);
            }
        }
    }
    [disposeSymbol]() {
        // TODO: in the future, forest and/or its AnchorSet may require disposal.
    }
}
//# sourceMappingURL=sharedTreeChangeEnricher.js.map