"use client";
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { cnb } from "cnbuilder";
import { cloneElement, isValidElement } from "react";
import { IconRotator } from "../icon/IconRotator.js";
import { getIcon } from "../icon/config.js";
import { useTreeContext } from "./TreeProvider.js";
/**
 * **Client Component**
 *
 * @internal
 * @since 6.0.0 Updated to support the new `expansionMode` behavior.
 */ export function TreeItemExpander(props) {
    const { itemId, isLeft = false, addon, expanded, disabled, isLeafNode, className } = props;
    const { expanderIcon, expanderLeft, expansionMode, disableTransition, toggleTreeItemExpansion } = useTreeContext();
    const icon = getIcon("dropdown", expanderIcon);
    if (isLeafNode || expanderLeft !== isLeft) {
        if (/*#__PURE__*/ isValidElement(addon)) {
            return /*#__PURE__*/ cloneElement(addon, {
                className
            });
        }
        return /*#__PURE__*/ _jsx(_Fragment, {
            children: addon
        });
    }
    const isCloneable = /*#__PURE__*/ isValidElement(icon);
    let clickProps;
    if (expansionMode === "manual" && !disabled) {
        clickProps = {
            onClick (event) {
                // do not select the tree item if the icon is clicked
                event.stopPropagation();
                event.preventDefault();
                toggleTreeItemExpansion(itemId);
            }
        };
    }
    return /*#__PURE__*/ _jsxs(_Fragment, {
        children: [
            /*#__PURE__*/ _jsx(IconRotator, {
                className: cnb(!addon && className),
                ...!isCloneable ? clickProps : undefined,
                rotated: expanded,
                forceIconWrap: !isCloneable,
                disableTransition: disableTransition,
                children: clickProps && isCloneable ? /*#__PURE__*/ cloneElement(icon, clickProps) : icon
            }),
            addon
        ]
    });
}

//# sourceMappingURL=TreeItemExpander.js.map