"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.getOrCreateNodeFromInnerNode = void 0;
const index_js_1 = require("../../util/index.js");
const treeNodeKernel_js_1 = require("./treeNodeKernel.js");
const schemaCaching_js_1 = require("./schemaCaching.js");
const unhydratedFlexTree_js_1 = require("./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.
 */
function getOrCreateNodeFromInnerNode(flexNode) {
    const cached = flexNode instanceof unhydratedFlexTree_js_1.UnhydratedFlexTreeNode
        ? treeNodeKernel_js_1.mapTreeNodeToProxy.get(flexNode)
        : flexNode.anchorNode.slots.get(treeNodeKernel_js_1.proxySlot);
    if (cached !== undefined) {
        return cached;
    }
    const classSchema = (0, schemaCaching_js_1.getSimpleNodeSchemaFromInnerNode)(flexNode) ?? (0, index_js_1.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);
    }
}
exports.getOrCreateNodeFromInnerNode = getOrCreateNodeFromInnerNode;
//# sourceMappingURL=getOrCreateNode.js.map