/*!
 * 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;
import { assert } from "@fluidframework/core-utils/internal";
import { ITreeSubscriptionCursorState, } from "../../core/index.js";
import { disposeSymbol } from "../../util/index.js";
import { flexTreeMarker, } from "./flexTreeTypes.js";
/**
 * 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.isFreed(), 0x8ca /* Use after free */);
}
/**
 * This is a base class for lazy (cursor based) UntypedEntity implementations, which uniformly handles cursors and anchors.
 */
export class LazyEntity {
    constructor(context, cursor, anchor) {
        this.context = context;
        _LazyEntity_lazyCursor.set(this, void 0);
        this.anchor = anchor;
        __classPrivateFieldSet(this, _LazyEntity_lazyCursor, cursor.fork("LazyEntity Fork"), "f");
        context.withCursors.add(this);
        this.context.withAnchors.add(this);
    }
    borrowCursor() {
        return this.cursor;
    }
    [(_LazyEntity_lazyCursor = new WeakMap(), disposeSymbol)]() {
        __classPrivateFieldGet(this, _LazyEntity_lazyCursor, "f").free();
        this.context.withCursors.delete(this);
        this.forgetAnchor();
        this.context.withAnchors.delete(this);
    }
    prepareForEdit() {
        __classPrivateFieldGet(this, _LazyEntity_lazyCursor, "f").clear();
        this.context.withCursors.delete(this);
    }
    isFreed() {
        return __classPrivateFieldGet(this, _LazyEntity_lazyCursor, "f").state === ITreeSubscriptionCursorState.Freed;
    }
    get cursor() {
        if (__classPrivateFieldGet(this, _LazyEntity_lazyCursor, "f").state !== ITreeSubscriptionCursorState.Current) {
            assert(__classPrivateFieldGet(this, _LazyEntity_lazyCursor, "f").state === ITreeSubscriptionCursorState.Cleared, 0x778 /* Unset cursor should be in cleared state */);
            assert(this.anchor !== undefined, 0x779 /* FlexTree should have an anchor if it does not have a cursor */);
            const result = this.tryMoveCursorToAnchor(__classPrivateFieldGet(this, _LazyEntity_lazyCursor, "f"));
            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");
    }
}
//# sourceMappingURL=lazyEntity.js.map