/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { fail } from "@fluidframework/core-utils/internal";
import { FieldKinds, isFlexTreeNode, } from "../feature-libraries/index.js";
import { getOrCreateNodeFromInnerNode } from "./core/index.js";
/**
 * Retrieve the associated {@link TreeNode} for the given field's content.
 */
export function tryGetTreeNodeForField(field) {
    function tryToUnboxLeaves(flexField) {
        const maybeContent = flexField.content;
        return isFlexTreeNode(maybeContent)
            ? getOrCreateNodeFromInnerNode(maybeContent)
            : maybeContent;
    }
    switch (field.schema) {
        case FieldKinds.required.identifier: {
            const typedField = field;
            return tryToUnboxLeaves(typedField);
        }
        case FieldKinds.optional.identifier: {
            const typedField = field;
            return tryToUnboxLeaves(typedField);
        }
        case FieldKinds.identifier.identifier: {
            // Identifier fields are just value fields that hold strings
            return field.content;
        }
        default:
            fail(0xadf /* invalid field kind */);
    }
}
//# sourceMappingURL=getTreeNodeForField.js.map