/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { fail } from "../../../util";
import { schemaIsFieldNode } from "../../typed-schema";
import { getProxyForNode } from "./proxies";
import { getTreeNode } from "./types";
/**
 * The `node` object holds various functions for analyzing {@link SharedTreeNode}s.
 * @alpha
 */
export const nodeApi = {
    schema: (node) => {
        return assertTreeNode(node).schema;
    },
    is: (value, schema) => {
        return getTreeNode(value)?.is(schema) ?? false;
    },
    parent: (node) => {
        const treeNode = assertTreeNode(node).parentField.parent.parent;
        if (treeNode !== undefined) {
            return getProxyForNode(treeNode);
        }
        return undefined;
    },
    key: (node) => {
        const treeNode = assertTreeNode(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 treeNode.parentField.index;
            }
        }
        // The parent of `node` is an object, a map, or undefined (and therefore `node` is a root/detached node).
        return treeNode.parentField.parent.key;
    },
    on: (node, eventName, listener) => {
        return assertTreeNode(node).on(eventName, listener);
    },
    status: (node) => {
        return assertTreeNode(node).treeStatus();
    },
};
function assertTreeNode(node) {
    return getTreeNode(node) ?? fail("Expected a SharedTreeNode");
}
//# sourceMappingURL=node.js.map