"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.unboxedField = exports.unboxedUnion = exports.unboxedTree = void 0;
const index_js_1 = require("../../core/index.js");
const index_js_2 = require("../default-schema/index.js");
const index_js_3 = require("../typed-schema/index.js");
const lazyField_js_1 = require("./lazyField.js");
const lazyNode_js_1 = require("./lazyNode.js");
/**
 * See {@link FlexTreeUnboxNode} for documentation on what unwrapping this performs.
 */
function unboxedTree(context, schema, cursor) {
    if ((0, index_js_3.schemaIsLeaf)(schema)) {
        return cursor.value;
    }
    return (0, lazyNode_js_1.makeTree)(context, cursor);
}
exports.unboxedTree = unboxedTree;
/**
 * See {@link FlexTreeUnboxNodeUnion} for documentation on what unwrapping this performs.
 */
function unboxedUnion(context, schema, cursor) {
    const type = schema.monomorphicChildType;
    if (type !== undefined) {
        return unboxedTree(context, type, cursor);
    }
    return (0, lazyNode_js_1.makeTree)(context, cursor);
}
exports.unboxedUnion = unboxedUnion;
/**
 * @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.
 */
function unboxedField(context, schema, cursor) {
    const kind = schema.kind;
    if (kind === index_js_2.FieldKinds.required) {
        return (0, index_js_1.inCursorNode)(cursor, 0, (innerCursor) => unboxedUnion(context, schema, innerCursor));
    }
    if (kind === index_js_2.FieldKinds.optional) {
        if (cursor.getFieldLength() === 0) {
            return undefined;
        }
        return (0, index_js_1.inCursorNode)(cursor, 0, (innerCursor) => unboxedUnion(context, schema, innerCursor));
    }
    // TODO: forbidden and nodeKey
    return (0, lazyField_js_1.makeField)(context, schema, cursor);
}
exports.unboxedField = unboxedField;
//# sourceMappingURL=unboxed.js.map