/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { assert, fail } from "@fluidframework/core-utils/internal";
import { UsageError } from "@fluidframework/telemetry-utils/internal";
import { inCursorNode, iterateCursorField, mapCursorField, rootFieldKey, } from "../../core/index.js";
import { disposeSymbol, getOrCreate } from "../../util/index.js";
import { combineChunks } from "../chunked-forest/index.js";
import { FieldKinds, MappedEditBuilder, } from "../default-schema/index.js";
import { cursorForMapTreeField } from "../mapTreeCursor.js";
import { FlexTreeEntityKind, TreeStatus, flexTreeMarker, flexTreeSlot, } from "./flexTreeTypes.js";
import { LazyEntity } from "./lazyEntity.js";
import { getOrCreateHydratedFlexTreeNode } from "./lazyNode.js";
import { indexForAt, treeStatusFromAnchorCache } from "./utilities.js";
/**
 * Reuse fields.
 * Since field currently own cursors and register themselves for disposal when the node hit end of life,
 * not reusing them results in memory leaks every time the field is accessed.
 * Since the fields stay alive until the node is end of life reusing them this way is safe.
 *
 * This ins't a perfect solution:
 *
 * - This can cause leaks, like map nodes will keep all accessed field objects around. Since other things cause this same leak already, its not too bad.
 * - This does not cache the root.
 * - Finding the parent anchor to do the caching on has significant cost.
 *
 * Despite these limitations, this cache provides a large performance win in some common cases (over 10x), especially with how simple tree requests far more field objects than necessary currently.
 */
const fieldCache = new WeakMap();
export function makeField(context, schema, cursor) {
    const fieldAnchor = cursor.buildFieldAnchor();
    let usedAnchor = false;
    const makeFlexTreeField = () => {
        usedAnchor = true;
        const field = new (kindToClass.get(schema) ?? fail(0xb0f /* missing field implementation */))(context, schema, cursor, fieldAnchor);
        return field;
    };
    if (fieldAnchor.parent === undefined) {
        return makeFlexTreeField();
    }
    // For the common case (all but roots), cache field associated with its node's anchor and field key.
    const anchorNode = context.checkout.forest.anchors.locate(fieldAnchor.parent) ??
        fail(0xb10 /* missing anchor */);
    // Since anchor-set could be reused across a flex tree context getting disposed, key off the flex tree node not the anchor.
    const cacheKey = anchorNode.slots.get(flexTreeSlot);
    // If there is no flex tree parent node, skip caching: this is not expected to be a hot path, but should probably be fixed at some point.
    if (cacheKey === undefined) {
        return makeFlexTreeField();
    }
    const innerCache = getOrCreate(fieldCache, cacheKey, () => new Map());
    const result = getOrCreate(innerCache, fieldAnchor.fieldKey, makeFlexTreeField);
    if (!usedAnchor) {
        // The anchor must be disposed to avoid leaking. In the case of a cache hit,
        // we are not transferring ownership to a new FlexTreeField, so it must be disposed of here to avoid the leak.
        context.checkout.forest.anchors.forget(fieldAnchor.parent);
    }
    return result;
}
/**
 * Base type for fields implementing {@link FlexTreeField} using cursors.
 */
export class LazyField extends LazyEntity {
    get [flexTreeMarker]() {
        return FlexTreeEntityKind.Field;
    }
    constructor(context, schema, cursor, fieldAnchor) {
        super(context, cursor, fieldAnchor);
        this.schema = schema;
        assert(cursor.mode === 1 /* CursorLocationType.Fields */, 0x77b /* must be in fields mode */);
        this.key = cursor.getFieldKey();
        // Fields currently live as long as their parent does.
        // For root fields, this means forever, but other cases can be cleaned up when their parent anchor is deleted.
        if (fieldAnchor.parent !== undefined) {
            const anchorNode = context.checkout.forest.anchors.locate(fieldAnchor.parent) ??
                fail(0xb11 /* parent anchor node should always exist since field is under a node */);
            this.offAfterDestroy = anchorNode.events.on("afterDestroy", () => {
                this[disposeSymbol]();
            });
        }
    }
    is(kind) {
        assert(this.context.schemaPolicy.fieldKinds.get(kind.identifier) === kind, 0xa26 /* Narrowing must be done to a kind that exists in this context */);
        return this.schema === kind.identifier;
    }
    get parent() {
        if (this.anchor.parent === undefined) {
            return undefined;
        }
        const cursor = this.cursor;
        cursor.exitField();
        const output = getOrCreateHydratedFlexTreeNode(this.context, cursor);
        cursor.enterField(this.key);
        return output;
    }
    tryMoveCursorToAnchor(cursor) {
        return this.context.checkout.forest.tryMoveCursorToField(this.anchor, cursor);
    }
    forgetAnchor() {
        this.offAfterDestroy?.();
        if (this.anchor.parent === undefined)
            return;
        this.context.checkout.forest.anchors.forget(this.anchor.parent);
    }
    get length() {
        return this.cursor.getFieldLength();
    }
    atIndex(index) {
        return inCursorNode(this.cursor, index, (cursor) => unboxedFlexNode(this.context, cursor, this.anchor));
    }
    boxedAt(index) {
        const finalIndex = indexForAt(index, this.length);
        if (finalIndex === undefined) {
            return undefined;
        }
        return inCursorNode(this.cursor, finalIndex, (cursor) => getOrCreateHydratedFlexTreeNode(this.context, cursor));
    }
    map(callbackfn) {
        return mapCursorField(this.cursor, (cursor) => callbackfn(unboxedFlexNode(this.context, cursor, this.anchor), cursor.fieldIndex));
    }
    [Symbol.iterator]() {
        return iterateCursorField(this.cursor, (cursor) => getOrCreateHydratedFlexTreeNode(this.context, cursor));
    }
    getFieldPath() {
        return { parent: this.parent?.anchorNode, field: this.key };
    }
    /**
     * Returns the path to this field to use for editing. Throws iff this path is not {@link TreeStatus#InDocument}.
     * This path is not valid to hold onto across edits: this must be recalled for each edit.
     */
    getFieldPathForEditing() {
        if (!this.isFreed() &&
            // Only allow editing if we are the root document field...
            ((this.parent === undefined && this.anchor.fieldKey === rootFieldKey) ||
                // ...or are under a node in the document
                (this.parent !== undefined &&
                    treeStatusFromAnchorCache(this.parent.anchorNode) === TreeStatus.InDocument))) {
            return this.getFieldPath();
        }
        throw new UsageError("Editing only allowed on fields with TreeStatus.InDocument status");
    }
    getEditor() {
        return new MappedEditBuilder(this.context.checkout.editor, (cursor) => combineChunks(this.context.checkout.forest.chunkField(cursor)));
    }
}
export class LazySequence extends LazyField {
    constructor() {
        super(...arguments);
        this.editor = {
            insert: (index, newContent) => {
                this.sequenceEditor().insert(index, cursorForMapTreeField(newContent));
            },
            remove: (index, count) => {
                this.sequenceEditor().remove(index, count);
            },
        };
    }
    at(index) {
        const finalIndex = indexForAt(index, this.length);
        if (finalIndex === undefined) {
            return undefined;
        }
        return this.atIndex(finalIndex);
    }
    get asArray() {
        return this.map((x) => x);
    }
    sequenceEditor() {
        const fieldPath = this.getFieldPathForEditing();
        return this.getEditor().sequenceField(fieldPath);
    }
}
export class LazyValueField extends LazyField {
    constructor() {
        super(...arguments);
        this.editor = {
            set: (newContent) => {
                this.valueFieldEditor().set(cursorForMapTreeField([newContent]));
            },
        };
    }
    valueFieldEditor() {
        const fieldPath = this.getFieldPathForEditing();
        const fieldEditor = this.getEditor().valueField(fieldPath);
        return fieldEditor;
    }
    get content() {
        return this.atIndex(0);
    }
}
export class LazyOptionalField extends LazyField {
    constructor() {
        super(...arguments);
        this.editor = {
            set: (newContent, wasEmpty) => {
                this.optionalEditor().set(newContent === undefined ? newContent : cursorForMapTreeField([newContent]), wasEmpty);
            },
        };
    }
    optionalEditor() {
        const fieldPath = this.getFieldPathForEditing();
        const fieldEditor = this.getEditor().optionalField(fieldPath);
        return fieldEditor;
    }
    get content() {
        return this.length === 0 ? undefined : this.atIndex(0);
    }
}
export class LazyForbiddenField extends LazyField {
}
const builderList = [
    [FieldKinds.forbidden.identifier, LazyForbiddenField],
    [FieldKinds.optional.identifier, LazyOptionalField],
    [FieldKinds.sequence.identifier, LazySequence],
    [FieldKinds.required.identifier, LazyValueField],
    [FieldKinds.identifier.identifier, LazyValueField],
];
const kindToClass = new Map(builderList);
/**
 * Returns the flex tree node, or the value if it has one.
 */
export function unboxedFlexNode(context, cursor, fieldAnchor) {
    const value = cursor.value;
    if (value !== undefined) {
        return value;
    }
    // Try accessing cached child node via anchors.
    // This avoids O(depth) related costs from getOrCreateHydratedFlexTreeNode in the cached case.
    const anchor = fieldAnchor.parent;
    let child;
    if (anchor === undefined) {
        child = context.checkout.forest.anchors.find({
            parent: undefined,
            parentField: fieldAnchor.fieldKey,
            parentIndex: cursor.fieldIndex,
        });
    }
    else {
        const anchorNode = context.checkout.forest.anchors.locate(anchor);
        assert(anchorNode !== undefined, 0xa4c /* missing anchor */);
        child = anchorNode.childIfAnchored(fieldAnchor.fieldKey, cursor.fieldIndex);
    }
    if (child !== undefined) {
        const cached = child.slots.get(flexTreeSlot);
        if (cached !== undefined) {
            return cached;
        }
    }
    return getOrCreateHydratedFlexTreeNode(context, cursor);
}
//# sourceMappingURL=lazyField.js.map