import { jsxs, jsx } from "react/jsx-runtime";
import assert from "assert";
import { forwardRef, cloneElement } from "react";
import { Box } from "../Box/Box.mjs";
import { buildDataAttributes } from "../Accordion/Accordion.mjs";
import { iconContainerSize, iconSize } from "../../hooks/useIcon/index.mjs";
import { virtualTouchable } from "../private/touchable/virtualTouchable.mjs";
import { useButtonStyles, ButtonOverlays, Bleed } from "../Button/Button.mjs";
import { Text } from "../Accordion/AccordionItem.mjs";
import { TooltipRenderer } from "../TooltipRenderer/TooltipRenderer.mjs";
import { button } from "./ButtonIcon.css.mjs";
const buttonIconVariants = ["soft", "transparent"];
const buttonIconTones = ["neutral", "secondary"];
const padding = "xsmall";
const PrivateButtonIcon = forwardRef(
  ({
    icon,
    label,
    id,
    size = "standard",
    tone = "neutral",
    variant = "soft",
    type = "button",
    bleed,
    tooltipPlacement,
    onClick,
    onMouseDown,
    onKeyUp,
    onKeyDown,
    "aria-haspopup": ariaHasPopUp,
    "aria-expanded": ariaExpanded,
    tabIndex,
    data,
    ...restProps
  }, forwardedRef) => {
    const {
      className: buttonClasses,
      width: _,
      ...buttonStyleProps
    } = useButtonStyles({
      variant,
      tone: "neutral",
      size: "standard",
      radius: "full"
    });
    assert(
      icon.props.size === void 0,
      "Icons cannot set the 'size' prop when passed to a ButtonIcon component"
    );
    const button$1 = /* @__PURE__ */ jsxs(
      Box,
      {
        component: "button",
        type,
        id,
        zIndex: 0,
        title: !id ? label : void 0,
        ref: forwardedRef,
        "aria-label": label,
        "aria-haspopup": ariaHasPopUp,
        "aria-expanded": ariaExpanded,
        padding,
        onClick,
        onKeyUp,
        onKeyDown,
        onMouseDown,
        className: [buttonClasses, button, virtualTouchable()],
        tabIndex,
        ...buildDataAttributes({ data, validateRestProps: restProps }),
        ...buttonStyleProps,
        children: [
          /* @__PURE__ */ jsx(
            ButtonOverlays,
            {
              variant,
              tone: "neutral",
              radius: "full",
              keyboardFocusable: typeof tabIndex === "undefined" || tabIndex >= 0,
              forceActive: ariaExpanded === "true" || ariaExpanded === true
            }
          ),
          /* @__PURE__ */ jsx(
            Box,
            {
              component: "span",
              display: "block",
              position: "relative",
              className: size === "large" ? iconContainerSize() : iconSize({ crop: true }),
              children: cloneElement(icon, { tone: icon.props.tone || tone, size: "fill" })
            }
          )
        ]
      }
    );
    const shouldBleed = typeof bleed === "undefined" && variant === "transparent" || bleed;
    return shouldBleed ? /* @__PURE__ */ jsx(Bleed, { space: padding, children: button$1 }) : button$1;
  }
);
const ButtonIcon = forwardRef(
  ({ id, label, tooltipPlacement, ...restProps }, forwardedRef) => {
    if (!id) {
      return /* @__PURE__ */ jsx(PrivateButtonIcon, { label, ref: forwardedRef, ...restProps });
    }
    return /* @__PURE__ */ jsx(
      TooltipRenderer,
      {
        id: `${id}-tooltip`,
        tooltip: /* @__PURE__ */ jsx(Text, { children: label }),
        placement: tooltipPlacement,
        children: ({ triggerProps: { ref: triggerRef, ...triggerProps } }) => /* @__PURE__ */ jsx(
          PrivateButtonIcon,
          {
            id,
            label,
            ref: (node) => {
              if (forwardedRef) {
                if (typeof forwardedRef === "function") {
                  forwardedRef(node);
                } else {
                  forwardedRef.current = node;
                }
              }
              triggerRef(node);
            },
            ...triggerProps,
            ...restProps
          }
        )
      }
    );
  }
);
ButtonIcon.displayName = "ButtonIcon";
export {
  ButtonIcon,
  buttonIconTones,
  buttonIconVariants
};
