"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.nodeApi = void 0;
const feature_libraries_1 = require("../feature-libraries");
const proxies_1 = require("./proxies");
const flexNode_1 = require("./flexNode");
/**
 * The `Tree` object holds various functions for analyzing {@link TreeNode}s.
 */
exports.nodeApi = {
    schema: (node) => {
        return (0, flexNode_1.getFlexNode)(node).schema;
    },
    is: (value, schema) => {
        return (0, flexNode_1.tryGetFlexNode)(value)?.is(schema) ?? false;
    },
    parent: (node) => {
        const editNode = (0, flexNode_1.getFlexNode)(node).parentField.parent.parent;
        if (editNode !== undefined) {
            return (0, proxies_1.getOrCreateNodeProxy)(editNode);
        }
        return undefined;
    },
    key: (node) => {
        const editNode = (0, flexNode_1.getFlexNode)(node);
        const parent = exports.nodeApi.parent(node);
        if (parent !== undefined) {
            const parentSchema = exports.nodeApi.schema(parent);
            if ((0, feature_libraries_1.schemaIsFieldNode)(parentSchema)) {
                // The parent of `node` is a list
                return editNode.parentField.index;
            }
        }
        // The parent of `node` is an object, a map, or undefined (and therefore `node` is a root/detached node).
        return editNode.parentField.parent.key;
    },
    on: (node, eventName, listener) => {
        return (0, flexNode_1.getFlexNode)(node).on(eventName, listener);
    },
    status: (node) => {
        return (0, flexNode_1.getFlexNode)(node).treeStatus();
    },
};
//# sourceMappingURL=node.js.map