/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { defaultSchemaPolicy, FieldKinds } from "../../feature-libraries/index.js";
import { brand } from "../../util/index.js";
import { Context, getKernel, isTreeNode, UnhydratedContext, } from "../core/index.js";
import { getUnhydratedContext } from "../createContext.js";
import { createFromCursor } from "./create.js";
import { treeNodeApi } from "./treeNodeApi.js";
/**
 * Extensions to {@link (Tree:variable)} which are not yet stable.
 * @see {@link (TreeBeta:interface)}.
 * @beta
 */
export const TreeBeta = {
    on(node, eventName, listener) {
        return treeNodeApi.on(node, eventName, listener);
    },
    clone(node) {
        // The only non-TreeNode cases are {@link TreeLeafValue} and `undefined` (for an empty optional field) which can be returned as is.
        if (!isTreeNode(node)) {
            return node;
        }
        const kernel = getKernel(node);
        const cursor = kernel.getOrCreateInnerNode().borrowCursor();
        // To handle when the node transitively contains unknown optional fields,
        // derive the context from the source node's stored schema which has stored schema for any such fields and their contents.
        const flexContext = new UnhydratedContext(defaultSchemaPolicy, kernel.context.flexContext.schema);
        const context = new Context(flexContext, getUnhydratedContext(kernel.schema).schema);
        const fieldSchema = {
            kind: FieldKinds.required.identifier,
            types: new Set([brand(kernel.schema.identifier)]),
            persistedMetadata: undefined,
        };
        return createFromCursor(kernel.schema, cursor, fieldSchema, context);
    },
};
//# sourceMappingURL=treeBeta.js.map