/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { schemaIsFieldNode, } from "../feature-libraries";
import { getOrCreateNodeProxy } from "./proxies";
import { getFlexNode, tryGetFlexNode } from "./flexNode";
/**
 * The `Tree` object holds various functions for analyzing {@link TreeNode}s.
 */
export const nodeApi = {
    schema: (node) => {
        return getFlexNode(node).schema;
    },
    is: (value, schema) => {
        return tryGetFlexNode(value)?.is(schema) ?? false;
    },
    parent: (node) => {
        const editNode = getFlexNode(node).parentField.parent.parent;
        if (editNode !== undefined) {
            return getOrCreateNodeProxy(editNode);
        }
        return undefined;
    },
    key: (node) => {
        const editNode = getFlexNode(node);
        const parent = nodeApi.parent(node);
        if (parent !== undefined) {
            const parentSchema = nodeApi.schema(parent);
            if (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 getFlexNode(node).on(eventName, listener);
    },
    status: (node) => {
        return getFlexNode(node).treeStatus();
    },
};
//# sourceMappingURL=node.js.map