"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
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 {
    context;
    schema;
    #lazyCursor;
    [exports.anchorSymbol];
    constructor(context, schema, cursor, anchor) {
        this.context = context;
        this.schema = schema;
        this[exports.anchorSymbol] = anchor;
        this.#lazyCursor = cursor.fork("LazyEntity Fork");
        context.withCursors.add(this);
        this.context.withAnchors.add(this);
    }
    [index_js_2.disposeSymbol]() {
        this.#lazyCursor.free();
        this.context.withCursors.delete(this);
        this[exports.forgetAnchorSymbol]();
        this.context.withAnchors.delete(this);
    }
    [exports.prepareForEditSymbol]() {
        this.#lazyCursor.clear();
        this.context.withCursors.delete(this);
    }
    [exports.isFreedSymbol]() {
        return this.#lazyCursor.state === index_js_1.ITreeSubscriptionCursorState.Freed;
    }
    get [exports.cursorSymbol]() {
        if (this.#lazyCursor.state !== index_js_1.ITreeSubscriptionCursorState.Current) {
            (0, internal_1.assert)(this.#lazyCursor.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](this.#lazyCursor);
            (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 this.#lazyCursor;
    }
}
exports.LazyEntity = LazyEntity;
/**
 * Prevent Entities from inheriting members from Object.prototype including:
 * '__defineGetter__', '__defineSetter__', '__lookupGetter__', '__lookupSetter__', '__proto__',
 * 'hasOwnProperty', 'isPrototypeOf', 'valueOf', 'propertyIsEnumerable', 'toLocaleString' and 'toString'.
 *
 * This opens up more options for field names on struct nodes.
 */
Object.setPrototypeOf(LazyEntity.prototype, null);
//# sourceMappingURL=lazyEntity.js.map