/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { fail } from "../../util/index.js";
import { mapTreeNodeToProxy, proxySlot } from "./treeNodeKernel.js";
import { getSimpleNodeSchemaFromInnerNode } from "./schemaCaching.js";
import { UnhydratedFlexTreeNode } from "./unhydratedFlexTree.js";
/**
 * Returns the TreeNode or TreeValue for the provided {@link InnerNode}.
 * This will allocate a new one if needed, and otherwise return one from cache.
 * @remarks
 * This supports both hydrated and unhydrated nodes.
 */
export function getOrCreateNodeFromInnerNode(flexNode) {
    const cached = flexNode instanceof UnhydratedFlexTreeNode
        ? mapTreeNodeToProxy.get(flexNode)
        : flexNode.anchorNode.slots.get(proxySlot);
    if (cached !== undefined) {
        return cached;
    }
    const classSchema = getSimpleNodeSchemaFromInnerNode(flexNode) ?? fail("Missing schema");
    const node = flexNode;
    // eslint-disable-next-line unicorn/prefer-ternary
    if (typeof classSchema === "function") {
        return new classSchema(node);
    }
    else {
        return classSchema.create(flexNode);
    }
}
//# sourceMappingURL=getOrCreateNode.js.map