"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 core_1 = require("../../core");
const util_1 = require("../../util");
const lazyField_1 = require("./lazyField");
const lazyEntity_1 = require("./lazyEntity");
/**
 * Implementation of `EditableTreeContext`.
 *
 * @remarks An editor is required to edit the EditableTrees.
 */
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.eventUnregister = [
            this.forest.on("beforeChange", () => {
                this.prepareForEdit();
            }),
        ];
    }
    /**
     * Called before editing.
     * Clears all cursors so editing can proceed.
     */
    prepareForEdit() {
        for (const target of this.withCursors) {
            target[lazyEntity_1.prepareForEditSymbol]();
        }
        (0, core_utils_1.assert)(this.withCursors.size === 0, "prepareForEdit should remove all cursors");
    }
    [util_1.disposeSymbol]() {
        this.clear();
        for (const unregister of this.eventUnregister) {
            unregister();
        }
        this.eventUnregister.length = 0;
    }
    /**
     * Release any cursors and anchors held by EditableTrees created in this context.
     * The EditableTrees 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[util_1.disposeSymbol]();
        }
        (0, core_utils_1.assert)(this.withCursors.size === 0, "free should remove all cursors");
        (0, core_utils_1.assert)(this.withAnchors.size === 0, "free should remove all anchors");
    }
    get root() {
        const cursor = this.forest.allocateCursor();
        (0, core_1.moveToDetachedField)(this.forest, cursor);
        const field = (0, lazyField_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) {
    return new Context(schema, forest, editor, nodeKeyManager, nodeKeyFieldKey);
}
exports.getTreeContext = getTreeContext;
//# sourceMappingURL=context.js.map