/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { assert } from "@fluidframework/core-utils/internal";
import { ITreeSubscriptionCursorState, } from "../../core/index.js";
import { disposeSymbol } from "../../util/index.js";
import { flexTreeMarker, } from "./flexTreeTypes.js";
export const prepareForEditSymbol = Symbol("prepareForEdit");
export const isFreedSymbol = Symbol("isFreed");
export const tryMoveCursorToAnchorSymbol = Symbol("tryMoveCursorToAnchor");
export const forgetAnchorSymbol = Symbol("forgetAnchor");
export const cursorSymbol = Symbol("cursor");
/**
 * Symbol used to access the (generic) anchor of a {@link LazyEntity}.
 */
export const 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.
 */
export function assertFlexTreeEntityNotFreed(entity) {
    assert(entity instanceof LazyEntity, 0x8c9 /* unexpected implementation */);
    assert(!entity[isFreedSymbol](), 0x8ca /* Use after free */);
}
/**
 * This is a base class for lazy (cursor based) UntypedEntity implementations, which uniformly handles cursors and anchors.
 */
export class LazyEntity {
    context;
    schema;
    #lazyCursor;
    [anchorSymbol];
    constructor(context, schema, cursor, anchor) {
        this.context = context;
        this.schema = schema;
        this[anchorSymbol] = anchor;
        this.#lazyCursor = cursor.fork("LazyEntity Fork");
        context.withCursors.add(this);
        this.context.withAnchors.add(this);
    }
    [disposeSymbol]() {
        this.#lazyCursor.free();
        this.context.withCursors.delete(this);
        this[forgetAnchorSymbol]();
        this.context.withAnchors.delete(this);
    }
    [prepareForEditSymbol]() {
        this.#lazyCursor.clear();
        this.context.withCursors.delete(this);
    }
    [isFreedSymbol]() {
        return this.#lazyCursor.state === ITreeSubscriptionCursorState.Freed;
    }
    get [cursorSymbol]() {
        if (this.#lazyCursor.state !== ITreeSubscriptionCursorState.Current) {
            assert(this.#lazyCursor.state === ITreeSubscriptionCursorState.Cleared, 0x778 /* Unset cursor should be in cleared state */);
            assert(this[anchorSymbol] !== undefined, 0x779 /* FlexTree should have an anchor if it does not have a cursor */);
            const result = this[tryMoveCursorToAnchorSymbol](this.#lazyCursor);
            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;
    }
}
/**
 * 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