"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.getTreeContext = exports.Context = void 0;
const core_utils_1 = require("@fluidframework/core-utils");
const index_js_1 = require("../../core/index.js");
const index_js_2 = require("../node-key/index.js");
const index_js_3 = require("../../util/index.js");
const lazyField_js_1 = require("./lazyField.js");
const lazyEntity_js_1 = require("./lazyEntity.js");
const nodeKeys_js_1 = require("./nodeKeys.js");
/**
 * Implementation of `FlexTreeContext`.
 *
 * @remarks An editor is required to edit the FlexTree.
 */
class Context {
    /**
     * @param forest - the Forest
     * @param editor - an editor that makes changes to the forest.
     * @param nodeKeys - an object which handles node key generation and conversion
     * @param nodeKeyFieldKey - an optional field key under which node keys are stored in this tree.
     * If present, clients may query the {@link LocalNodeKey} of a node directly via the {@link localNodeKeySymbol}.
     */
    constructor(schema, forest, editor, nodeKeys, nodeKeyFieldKey) {
        this.schema = schema;
        this.forest = forest;
        this.editor = editor;
        this.nodeKeys = nodeKeys;
        this.nodeKeyFieldKey = nodeKeyFieldKey;
        this.withCursors = new Set();
        this.withAnchors = new Set();
        this.disposed = false;
        this.eventUnregister = [
            this.forest.on("beforeChange", () => {
                this.prepareForEdit();
            }),
        ];
    }
    /**
     * Called before editing.
     * Clears all cursors so editing can proceed.
     */
    prepareForEdit() {
        (0, core_utils_1.assert)(this.disposed === false, 0x802 /* use after dispose */);
        for (const target of this.withCursors) {
            target[lazyEntity_js_1.prepareForEditSymbol]();
        }
        (0, core_utils_1.assert)(this.withCursors.size === 0, 0x773 /* prepareForEdit should remove all cursors */);
    }
    [index_js_3.disposeSymbol]() {
        (0, core_utils_1.assert)(this.disposed === false, 0x803 /* double dispose */);
        this.disposed = true;
        this.clear();
        for (const unregister of this.eventUnregister) {
            unregister();
        }
        this.eventUnregister.length = 0;
    }
    /**
     * Release any cursors and anchors held by tree entities created in this context.
     * Ensures the cashed references to those entities on the Anchors are also cleared.
     * The tree entities are invalid to use after this, but the context may still be used
     * to create new trees starting from the root.
     */
    clear() {
        for (const target of this.withAnchors) {
            target[index_js_3.disposeSymbol]();
        }
        (0, core_utils_1.assert)(this.withCursors.size === 0, 0x774 /* free should remove all cursors */);
        (0, core_utils_1.assert)(this.withAnchors.size === 0, 0x775 /* free should remove all anchors */);
    }
    get root() {
        (0, core_utils_1.assert)(this.disposed === false, 0x804 /* use after dispose */);
        const cursor = this.forest.allocateCursor();
        (0, index_js_1.moveToDetachedField)(this.forest, cursor);
        const field = (0, lazyField_js_1.makeField)(this, this.schema.rootFieldSchema, cursor);
        cursor.free();
        return field;
    }
    on(eventName, listener) {
        return this.forest.on(eventName, listener);
    }
}
exports.Context = Context;
/**
 * A simple API for a Forest to interact with the tree.
 *
 * @param forest - the Forest
 * @param editor - an editor that makes changes to the forest.
 * @param nodeKeyManager - an object which handles node key generation and conversion.
 * @param nodeKeyFieldKey - an optional field key under which node keys are stored in this tree.
 * If present, clients may query the {@link LocalNodeKey} of a node directly via the {@link localNodeKeySymbol}.
 * @returns {@link EditableTreeContext} which is used to manage the cursors and anchors within the EditableTrees:
 * This is necessary for supporting using this tree across edits to the forest, and not leaking memory.
 */
function getTreeContext(schema, forest, editor, nodeKeyManager, nodeKeyFieldKey) {
    const nodeKeys = new nodeKeys_js_1.SimpleNodeKeys(new index_js_2.NodeKeyIndex(nodeKeyFieldKey), nodeKeyManager);
    const context = new Context(schema, forest, editor, nodeKeys, nodeKeyFieldKey);
    nodeKeys.map.scanKeys(context);
    context.on("afterChange", () => {
        nodeKeys.map.scanKeys(context);
    });
    return context;
}
exports.getTreeContext = getTreeContext;
//# sourceMappingURL=context.js.map