/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { defaultSchemaPolicy, FieldKinds, isTreeValue, } from "../../feature-libraries/index.js";
import { brand } from "../../util/index.js";
import { Context, getKernel, getOrCreateNodeFromInnerNode, isTreeNode, UnhydratedContext, } from "../core/index.js";
import { getUnhydratedContext } from "../createContext.js";
import { unhydratedFlexTreeFromInsertable, } from "../unhydratedFlexTreeFromInsertable.js";
import { conciseFromCursor } from "./conciseTree.js";
import { createFromCursor } from "./create.js";
import { treeNodeApi } from "./treeNodeApi.js";
import { cursorFromVerbose } from "./verboseTree.js";
/**
 * Borrow a cursor from a node.
 * @remarks
 * The cursor must be put back to its original location before the node is used again.
 */
export function borrowCursorFromTreeNodeOrValue(node) {
    if (isTreeValue(node)) {
        return cursorFromVerbose(node, {});
    }
    const kernel = getKernel(node);
    const cursor = kernel.getInnerNode().borrowCursor();
    return cursor;
}
export function importConcise(schema, data) {
    // Create the tree content from insertable data
    const mapTree = unhydratedFlexTreeFromInsertable(data, schema);
    const result = mapTree === undefined ? undefined : getOrCreateNodeFromInnerNode(mapTree);
    return result;
}
export function exportConcise(node, options) {
    if (!isTreeNode(node)) {
        return node;
    }
    const config = { ...options };
    const kernel = getKernel(node);
    const cursor = borrowCursorFromTreeNodeOrValue(node);
    return conciseFromCursor(cursor, kernel.context, config);
}
/**
 * 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);
    },
    importConcise,
    exportConcise,
    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.getInnerNode().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);
    },
    create(schema, data) {
        const mapTree = unhydratedFlexTreeFromInsertable(data, schema);
        const result = mapTree === undefined ? undefined : getOrCreateNodeFromInnerNode(mapTree);
        return result;
    },
};
//# sourceMappingURL=treeBeta.js.map