/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { inCursorNode } from "../../core/index.js";
import { FieldKinds } from "../default-schema/index.js";
import { schemaIsLeaf, } from "../typed-schema/index.js";
import { makeField } from "./lazyField.js";
import { makeTree } from "./lazyNode.js";
/**
 * See {@link FlexTreeUnboxNode} for documentation on what unwrapping this performs.
 */
export function unboxedTree(context, schema, cursor) {
    if (schemaIsLeaf(schema)) {
        return cursor.value;
    }
    return makeTree(context, cursor);
}
/**
 * See {@link FlexTreeUnboxNodeUnion} for documentation on what unwrapping this performs.
 */
export function unboxedUnion(context, schema, cursor) {
    const type = schema.monomorphicChildType;
    if (type !== undefined) {
        return unboxedTree(context, type, cursor);
    }
    return makeTree(context, cursor);
}
/**
 * @param context - the common context of the field.
 * @param schema - the TreeFieldStoredSchema of the field.
 * @param cursor - the cursor, which must point to the field being proxified.
 */
export function unboxedField(context, schema, cursor) {
    const kind = schema.kind;
    if (kind === FieldKinds.required) {
        return inCursorNode(cursor, 0, (innerCursor) => unboxedUnion(context, schema, innerCursor));
    }
    if (kind === FieldKinds.optional) {
        if (cursor.getFieldLength() === 0) {
            return undefined;
        }
        return inCursorNode(cursor, 0, (innerCursor) => unboxedUnion(context, schema, innerCursor));
    }
    // TODO: forbidden and nodeKey
    return makeField(context, schema, cursor);
}
//# sourceMappingURL=unboxed.js.map