/** @license MIT | @cube-dev/ui-kit v0.0.0-canary-ad23b00 | Cube Dev Team */
import { mergeProps as mergeProps$1 } from "../../../utils/react/mergeProps.js";
import { useEvent } from "../../../_internal/hooks/use-event.js";
import { LoadingIcon } from "../../../icons/LoadingIcon.js";
import { DirectionIcon } from "../../../icons/DirectionIcon.js";
import { TreeNodeCheckboxWrapper, TreeNodeRow, TreeNodeToggle, TreeNodeTogglePlaceholder, TreeRowItem } from "./styled.js";
import { __Checkbox } from "../../fields/Checkbox/Checkbox.js";
import { memo, useMemo, useRef } from "react";
import { jsx } from "react/jsx-runtime";
import { useTreeItem } from "react-aria";

//#region src/components/content/Tree/TreeNode.tsx
const ROW_LEVEL_OFFSET = 0;
function TreeNodeInner(props) {
	const { node, data, state, isCheckable, isExpanded, isIndeterminate, isChecked, isLoading, isBlockNode, onToggleChecked, rowStyles } = props;
	const rowRef = useRef(null);
	const { rowProps, gridCellProps, expandButtonProps } = useTreeItem({ node }, state, rowRef);
	const isDisabled = state.disabledKeys.has(node.key);
	const isSelected = state.selectionManager.isSelected(node.key);
	/**
	* `useTreeItem` reports `aria-expanded` only on rows that *can* expand
	* (i.e. have child nodes in the collection). For lazy rows with no
	* children loaded yet but `isLeaf !== true`, we still want to render
	* a toggle so the consumer can fire `loadData`.
	*/
	const isLeaf = data.isLeaf === true || data.isLeaf !== false && !node.hasChildNodes;
	const isRowCheckable = isCheckable && data.isCheckable !== false;
	const handleCheckboxClick = useEvent((e) => {
		e.stopPropagation();
	});
	const handleCheckboxChange = useEvent(() => {
		onToggleChecked(String(node.key));
	});
	const rowMods = useMemo(() => ({
		checked: isChecked,
		indeterminate: isIndeterminate,
		expanded: isExpanded,
		disabled: isDisabled,
		loading: isLoading,
		leaf: isLeaf,
		"block-node": isBlockNode,
		"has-checkbox": isRowCheckable
	}), [
		isChecked,
		isIndeterminate,
		isExpanded,
		isDisabled,
		isLoading,
		isLeaf,
		isBlockNode,
		isRowCheckable
	]);
	const itemMods = useMemo(() => ({
		checked: isChecked,
		indeterminate: isIndeterminate,
		expanded: isExpanded,
		loading: isLoading,
		leaf: isLeaf,
		"has-checkbox": isRowCheckable
	}), [
		isChecked,
		isIndeterminate,
		isExpanded,
		isLoading,
		isLeaf,
		isRowCheckable
	]);
	/**
	* Indent each row by its depth. `node.level` is 0-based at the root.
	* Pushed down via a CSS custom property so the styled element can
	* compute padding without re-extracting styles per row.
	*/
	const levelStyle = useMemo(() => ({ ["--tree-level"]: String((node.level ?? ROW_LEVEL_OFFSET) - ROW_LEVEL_OFFSET) }), [node.level]);
	/**
	* `aria-checked` for treegrid rows that act as multi-state checkboxes.
	* Only set when the tree is checkable, so we don't pollute the row
	* with checkbox semantics in select-only mode.
	*/
	const ariaChecked = isRowCheckable ? isIndeterminate ? "mixed" : isChecked ? "true" : "false" : void 0;
	const finalRowProps = mergeProps$1(rowProps, { "aria-checked": ariaChecked });
	/**
	* Always render a toggle slot — even for leaf rows — so siblings
	* at the same indent level visually align. Leaves get a
	* non-interactive placeholder (a plain div) so user clicks can't
	* trigger anything.
	*/
	const toggleNode = isLeaf ? /* @__PURE__ */ jsx(TreeNodeTogglePlaceholder, {
		"aria-hidden": true,
		"data-element": "Toggle"
	}) : /* @__PURE__ */ jsx(TreeNodeToggle, {
		...expandButtonProps,
		tabIndex: -1,
		"data-element": "Toggle",
		children: isLoading ? /* @__PURE__ */ jsx(LoadingIcon, {}) : /* @__PURE__ */ jsx(DirectionIcon, { to: isExpanded ? "bottom" : "right" })
	});
	const checkboxNode = isRowCheckable ? /* @__PURE__ */ jsx(TreeNodeCheckboxWrapper, {
		"data-element": "Checkbox",
		role: "presentation",
		onClick: handleCheckboxClick,
		onKeyDown: handleCheckboxClick,
		children: /* @__PURE__ */ jsx(__Checkbox, {
			isSelected: isChecked,
			isIndeterminate,
			isDisabled: isDisabled || data.isCheckboxDisabled,
			"aria-label": typeof data.title === "string" ? data.title : String(node.key),
			onChange: handleCheckboxChange
		})
	}) : null;
	return /* @__PURE__ */ jsx(TreeNodeRow, {
		...finalRowProps,
		ref: rowRef,
		mods: rowMods,
		style: levelStyle,
		"data-element": "Row",
		"data-qa-key": String(node.key),
		children: /* @__PURE__ */ jsx(TreeRowItem, {
			...gridCellProps,
			isSelected,
			mods: itemMods,
			icon: toggleNode,
			prefix: checkboxNode,
			styles: rowStyles,
			children: data.title
		})
	});
}
const TreeNode = memo(TreeNodeInner);

//#endregion
export { TreeNode };
//# sourceMappingURL=TreeNode.js.map