"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCheckoutFlexTreeView = exports.CheckoutFlexTreeView = void 0;
const internal_1 = require("@fluidframework/core-utils/internal");
const index_js_1 = require("../feature-libraries/index.js");
const index_js_2 = require("../simple-tree/index.js");
const index_js_3 = require("../util/index.js");
/**
 * An editable view of a (version control style) branch of a shared tree.
 */
class CheckoutFlexTreeView {
    constructor(
    /**
     * Access non-view schema specific aspects of this branch.
     *
     * @remarks
     * This is a non-owning reference: disposing of this view does not impact the branch.
     */
    checkout, schema, nodeKeyManager, onDispose) {
        this.checkout = checkout;
        this.schema = schema;
        this.nodeKeyManager = nodeKeyManager;
        this.onDispose = onDispose;
        this.context = (0, index_js_1.getTreeContext)(schema, this.checkout, nodeKeyManager);
        contextToTreeViewMap.set(this.context, this);
        this.flexTree = this.context.root;
    }
    [index_js_3.disposeSymbol]() {
        for (const anchorNode of this.checkout.forest.anchors) {
            (0, index_js_2.tryDisposeTreeNode)(anchorNode);
        }
        this.context[index_js_3.disposeSymbol]();
        this.onDispose?.();
    }
    /**
     * Spawn a new view which is based off of the current state of this view.
     * Any mutations of the new view will not apply to this view until the new view is merged back into this view via `merge()`.
     */
    fork() {
        const branch = this.checkout.branch();
        return new CheckoutFlexTreeView(branch, this.schema, this.nodeKeyManager);
    }
}
exports.CheckoutFlexTreeView = CheckoutFlexTreeView;
/**
 * Maps the context of every {@link CheckoutFlexTreeView} to the view.
 * In practice, this allows the view or checkout to be obtained from a flex node by first getting the context from the flex node and then using this map.
 */
const contextToTreeViewMap = new WeakMap();
/**
 * Retrieve the {@link CheckoutFlexTreeView | view} for the given {@link FlexTreeHydratedContext | context}.
 * @remarks Every {@link CheckoutFlexTreeView} is associated with its context upon creation.
 */
function getCheckoutFlexTreeView(context) {
    const view = contextToTreeViewMap.get(context);
    (0, internal_1.assert)(view !== undefined, 0xa41 /* Expected view to be registered for context */);
    return view;
}
exports.getCheckoutFlexTreeView = getCheckoutFlexTreeView;
//# sourceMappingURL=checkoutFlexTreeView.js.map