"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
    if (kind === "m") throw new TypeError("Private method is not writable");
    if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
    if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
    return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
    if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
    if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
    return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _LazyEntity_lazyCursor;
Object.defineProperty(exports, "__esModule", { value: true });
exports.LazyEntity = exports.assertFlexTreeEntityNotFreed = exports.anchorSymbol = exports.cursorSymbol = exports.forgetAnchorSymbol = exports.tryMoveCursorToAnchorSymbol = exports.isFreedSymbol = exports.prepareForEditSymbol = void 0;
const internal_1 = require("@fluidframework/core-utils/internal");
const index_js_1 = require("../../core/index.js");
const index_js_2 = require("../../util/index.js");
const flexTreeTypes_js_1 = require("./flexTreeTypes.js");
exports.prepareForEditSymbol = Symbol("prepareForEdit");
exports.isFreedSymbol = Symbol("isFreed");
exports.tryMoveCursorToAnchorSymbol = Symbol("tryMoveCursorToAnchor");
exports.forgetAnchorSymbol = Symbol("forgetAnchor");
exports.cursorSymbol = Symbol("cursor");
/**
 * Symbol used to access the (generic) anchor of a {@link LazyEntity}.
 */
exports.anchorSymbol = Symbol("anchor");
/**
 * Assert `entity` is not deleted.
 * @privateRemarks
 * This can be faster than getting the tree status and checking that since that can require computing removed vs deleted.
 * TODO: provide a non implementation dependent way to leverage this optimization.
 */
function assertFlexTreeEntityNotFreed(entity) {
    (0, internal_1.assert)(entity instanceof LazyEntity, 0x8c9 /* unexpected implementation */);
    (0, internal_1.assert)(!entity[exports.isFreedSymbol](), 0x8ca /* Use after free */);
}
exports.assertFlexTreeEntityNotFreed = assertFlexTreeEntityNotFreed;
/**
 * This is a base class for lazy (cursor based) UntypedEntity implementations, which uniformly handles cursors and anchors.
 */
class LazyEntity {
    constructor(context, cursor, anchor) {
        this.context = context;
        _LazyEntity_lazyCursor.set(this, void 0);
        this[exports.anchorSymbol] = anchor;
        __classPrivateFieldSet(this, _LazyEntity_lazyCursor, cursor.fork("LazyEntity Fork"), "f");
        context.withCursors.add(this);
        this.context.withAnchors.add(this);
    }
    [(_LazyEntity_lazyCursor = new WeakMap(), index_js_2.disposeSymbol)]() {
        __classPrivateFieldGet(this, _LazyEntity_lazyCursor, "f").free();
        this.context.withCursors.delete(this);
        this[exports.forgetAnchorSymbol]();
        this.context.withAnchors.delete(this);
    }
    [exports.prepareForEditSymbol]() {
        __classPrivateFieldGet(this, _LazyEntity_lazyCursor, "f").clear();
        this.context.withCursors.delete(this);
    }
    [exports.isFreedSymbol]() {
        return __classPrivateFieldGet(this, _LazyEntity_lazyCursor, "f").state === index_js_1.ITreeSubscriptionCursorState.Freed;
    }
    get [exports.cursorSymbol]() {
        if (__classPrivateFieldGet(this, _LazyEntity_lazyCursor, "f").state !== index_js_1.ITreeSubscriptionCursorState.Current) {
            (0, internal_1.assert)(__classPrivateFieldGet(this, _LazyEntity_lazyCursor, "f").state === index_js_1.ITreeSubscriptionCursorState.Cleared, 0x778 /* Unset cursor should be in cleared state */);
            (0, internal_1.assert)(this[exports.anchorSymbol] !== undefined, 0x779 /* FlexTree should have an anchor if it does not have a cursor */);
            const result = this[exports.tryMoveCursorToAnchorSymbol](__classPrivateFieldGet(this, _LazyEntity_lazyCursor, "f"));
            (0, internal_1.assert)(result === 1 /* TreeNavigationResult.Ok */, 0x77a /* It is invalid to access a FlexTree node which no longer exists */);
            this.context.withCursors.add(this);
        }
        return __classPrivateFieldGet(this, _LazyEntity_lazyCursor, "f");
    }
}
exports.LazyEntity = LazyEntity;
//# sourceMappingURL=lazyEntity.js.map