"use client";
import { getSpacing } from "../../core/utils/get-size/get-size.mjs";
import { createVarsResolver } from "../../core/styles-api/create-vars-resolver/create-vars-resolver.mjs";
import { useProps } from "../../core/MantineProvider/use-props/use-props.mjs";
import { useStyles } from "../../core/styles-api/use-styles/use-styles.mjs";
import { factory } from "../../core/factory/factory.mjs";
import { Box } from "../../core/Box/Box.mjs";
import { TreeNode } from "./TreeNode.mjs";
import { useTree } from "./use-tree.mjs";
import Tree_module_default from "./Tree.module.mjs";
import { useEffect, useMemo, useRef } from "react";
import { useClickOutside, useMergedRef } from "@mantine/hooks";
import { jsx } from "react/jsx-runtime";
//#region packages/@mantine/core/src/components/Tree/Tree.tsx
function getFlatValues(data) {
	return data.reduce((acc, item) => {
		acc.push(item.value);
		if (item.children) acc.push(...getFlatValues(item.children));
		return acc;
	}, []);
}
const defaultProps = {
	expandOnClick: true,
	allowRangeSelection: true,
	expandOnSpace: true
};
const varsResolver = createVarsResolver((_theme, { levelOffset }) => ({ root: { "--level-offset": getSpacing(levelOffset) } }));
const Tree = factory((_props) => {
	const props = useProps("Tree", defaultProps, _props);
	const { classNames, className, style, styles, unstyled, vars, data, expandOnClick, tree, renderNode, selectOnClick, clearSelectionOnOutsideClick, allowRangeSelection, expandOnSpace, levelOffset, checkOnSpace, keepMounted, onDragDrop, allowDrop, withDragHandle, withLines, attributes, ref, ...others } = props;
	const defaultController = useTree();
	const controller = tree || defaultController;
	const dragStateRef = useRef({
		draggedValue: null,
		currentDropTarget: null
	});
	const getStyles = useStyles({
		name: "Tree",
		classes: Tree_module_default,
		props,
		className,
		style,
		classNames,
		styles,
		unstyled,
		attributes,
		vars,
		varsResolver
	});
	const mergedRef = useMergedRef(ref, useClickOutside(() => clearSelectionOnOutsideClick && controller.clearSelected()));
	const flatValues = useMemo(() => getFlatValues(data), [data]);
	useEffect(() => {
		controller.initialize(data);
	}, [data]);
	const nodes = data.map((node, index) => /* @__PURE__ */ jsx(TreeNode, {
		node,
		getStyles,
		rootIndex: index,
		expandOnClick,
		selectOnClick,
		controller,
		renderNode,
		flatValues,
		allowRangeSelection,
		expandOnSpace,
		checkOnSpace,
		keepMounted,
		onDragDrop,
		allowDrop,
		withDragHandle,
		dragStateRef,
		data
	}, node.value));
	return /* @__PURE__ */ jsx(Box, {
		component: "ul",
		ref: mergedRef,
		...getStyles("root"),
		...others,
		role: "tree",
		"aria-multiselectable": controller.multiple,
		"data-tree-root": true,
		"data-with-lines": withLines || void 0,
		children: nodes
	});
});
Tree.displayName = "@mantine/core/Tree";
Tree.classes = Tree_module_default;
Tree.varsResolver = varsResolver;
//#endregion
export { Tree };

//# sourceMappingURL=Tree.mjs.map