"use client";
import { jsx as _jsx } from "react/jsx-runtime";
import { List } from "../list/List.js";
import { KeyboardMovementProvider } from "../movement/useKeyboardMovementProvider.js";
import { useEnsuredId } from "../useEnsuredId.js";
import { RenderRecursively } from "../utils/RenderRecursively.js";
import { identity } from "../utils/identity.js";
import { DefaultTreeItemRenderer } from "./DefaultTreeItemRenderer.js";
import { TreeProvider } from "./TreeProvider.js";
import { tree } from "./styles.js";
import { useTreeItems } from "./useTreeItems.js";
import { useTreeMovement } from "./useTreeMovement.js";
/**
 * **Client Component**
 *
 * @example Simple Tree
 * ```tsx
 * import type { TreeData } from "@react-md/core";
 * import { Tree, useTree } from "@react-md/core";
 * import type { ReactElement } from "react";
 *
 * const data: TreeData = {
 *   "item-1": {
 *     itemId: "item-1",
 *     parentId: null,
 *     name: "Root Level Item 1",
 *   },
 *   "item-2": {
 *     itemId: "item-2",
 *     parentId: "item-1",
 *     name: "A child for the first item",
 *   },
 *   "item-3": {
 *     itemId: "item-3",
 *     parentId: "item-1",
 *     children: "Another child for the first item",
 *   },
 * };
 *
 * function Example(): ReactElement {
 *   const tree = useTree({
 *     // can enable multiple selected items
 *     // multiSelect: true,
 *
 *     // can set default expanded and selected items
 *     // defaultSelectedIds: ["item-1"],
 *     // defaultExpandedIds: ["item-1"],
 *   });
 *
 *   return (
 *     <Tree
 *       {...tree}
 *       aria-label="Tree"
 *       data={data}
 *     />
 *   );
 * }
 * ```
 *
 * @see {@link TreeProps.renderer} for a custom tree item example.
 *
 * @since 6.0.0 There was a major API change and the `id` is now `optional`.
 * In addition, the following props were removed:
 * - `itemRenderer`
 * - `labelKey`
 * - `valueKey`
 * - `getItemLabel`
 * - `getItemValue`
 * - `getItemProps`
 */ export function Tree(props) {
    const { id, data, sort = identity, rootId = null, treeRef, className, expandedIds, selectedIds, toggleTreeItemSelection, selectMultipleTreeItems, toggleTreeItemExpansion, expandMultipleTreeItems, onClick, onFocus, onKeyDown, renderer: TreeItemRenderer = DefaultTreeItemRenderer, multiSelect = false, expansionMode = "auto", expanderIcon, expanderLeft = false, linkComponent = "a", disableTransition = false, temporaryChildItems = false, ...remaining } = props;
    const treeId = useEnsuredId(id, "tree");
    const { items, treeItemChildIds } = useTreeItems({
        data,
        sort,
        rootId
    });
    const { metadataLookup, movementContext, movementProps } = useTreeMovement({
        onClick,
        onFocus,
        onKeyDown,
        data,
        expandedIds,
        selectedIds,
        treeItemChildIds,
        toggleTreeItemExpansion,
        expandMultipleTreeItems
    });
    return /*#__PURE__*/ _jsx(TreeProvider, {
        data: data,
        rootId: rootId,
        multiSelect: multiSelect,
        linkComponent: linkComponent,
        selectedIds: selectedIds,
        expandedIds: expandedIds,
        expanderIcon: expanderIcon,
        expanderLeft: expanderLeft,
        expansionMode: expansionMode,
        metadataLookup: metadataLookup,
        disableTransition: disableTransition,
        temporaryChildItems: temporaryChildItems,
        toggleTreeItemSelection: toggleTreeItemSelection,
        toggleTreeItemExpansion: toggleTreeItemExpansion,
        selectMultipleTreeItems: selectMultipleTreeItems,
        expandMultipleTreeItems: expandMultipleTreeItems,
        children: /*#__PURE__*/ _jsx(KeyboardMovementProvider, {
            value: movementContext,
            children: /*#__PURE__*/ _jsx(List, {
                ...remaining,
                ...movementProps,
                id: treeId,
                ref: treeRef,
                role: "tree",
                tabIndex: 0,
                className: tree({
                    className
                }),
                children: /*#__PURE__*/ _jsx(RenderRecursively, {
                    items: items,
                    render: TreeItemRenderer,
                    getItemKey: (options)=>options.item.itemId
                })
            })
        })
    });
}

//# sourceMappingURL=Tree.js.map