import { jsxs, Fragment, jsx } from 'react/jsx-runtime';
import { forwardRef } from 'react';
import { Link } from 'react-router-dom';
import { c as cn } from './utils-BJnb9o5c.js';
import { r as renderIcon } from './renderIcon-C6twJSqH.js';
import { Button as Button$1 } from './primitives/Button.js';
import { Spinner } from './primitives/Spinner.js';
import { TooltipProvider, Tooltip, TooltipTrigger, TooltipContent } from './primitives/Tooltip.js';

const ICON_ONLY_SIZE_MAP = {
  xs: "icon-xs",
  sm: "icon-sm",
  default: "icon",
  lg: "icon-lg"
};
const SPINNER_SIZE_MAP = {
  xs: "size-3",
  sm: "size-3.5",
  default: "size-3.5",
  lg: "size-4",
  icon: "size-3.5",
  "icon-xs": "size-3",
  "icon-sm": "size-3.5",
  "icon-lg": "size-4"
};
const TOOLTIP_SIDE_MAP = {
  top: "top",
  bottom: "bottom",
  left: "left",
  right: "right",
  "top-start": "top",
  "top-end": "top",
  "bottom-start": "bottom",
  "bottom-end": "bottom",
  "left-start": "left",
  "left-end": "left",
  "right-start": "right",
  "right-end": "right"
};

const Button = forwardRef(
  ({
    variant = "default",
    size = "default",
    label,
    icon = null,
    iconPosition = "right",
    loading = false,
    disabled = false,
    onClick,
    to,
    href,
    type = "button",
    fullWidth = false,
    className,
    tooltipProps = null,
    asChild = false,
    children,
    ...otherProps
  }, ref) => {
    const isIconSize = size.startsWith("icon");
    const renderLabel = isIconSize ? null : label || children;
    const isIconOnly = !renderLabel && !!icon;
    const resolvedSize = isIconOnly ? ICON_ONLY_SIZE_MAP[size] ?? size : size;
    const isDisabled = disabled || loading;
    const handleClick = (e) => {
      if (loading || disabled) {
        e.preventDefault();
        return;
      }
      onClick?.(e);
    };
    const iconElement = icon ? renderIcon(icon) : null;
    const buttonContent = /* @__PURE__ */ jsxs(Fragment, { children: [
      loading && /* @__PURE__ */ jsx("span", { className: "absolute inset-0 flex items-center justify-center", children: /* @__PURE__ */ jsx(
        Spinner,
        {
          "aria-hidden": "true",
          className: SPINNER_SIZE_MAP[resolvedSize]
        }
      ) }),
      /* @__PURE__ */ jsxs(
        "span",
        {
          className: cn(
            "inline-flex items-center gap-1.5",
            loading && "invisible"
          ),
          children: [
            iconPosition === "left" && iconElement && /* @__PURE__ */ jsx("span", { "data-icon": "inline-start", children: iconElement }),
            renderLabel && /* @__PURE__ */ jsx("span", { children: renderLabel }),
            iconPosition === "right" && iconElement && /* @__PURE__ */ jsx("span", { "data-icon": "inline-end", children: iconElement })
          ]
        }
      )
    ] });
    const mergedClassName = cn(
      fullWidth && "w-full",
      loading && "relative pointer-events-none",
      className
    );
    let buttonElement;
    if (asChild) {
      buttonElement = /* @__PURE__ */ jsx(
        Button$1,
        {
          ref,
          asChild: true,
          variant,
          size: resolvedSize,
          className: mergedClassName,
          disabled: isDisabled,
          ...otherProps,
          children
        }
      );
    } else if (!disabled && to) {
      buttonElement = /* @__PURE__ */ jsx(
        Button$1,
        {
          asChild: true,
          variant,
          size: resolvedSize,
          className: mergedClassName,
          disabled: isDisabled,
          ...otherProps,
          children: /* @__PURE__ */ jsx(
            Link,
            {
              innerRef: ref,
              to,
              onClick: handleClick,
              children: buttonContent
            }
          )
        }
      );
    } else if (!disabled && href) {
      buttonElement = /* @__PURE__ */ jsx(
        Button$1,
        {
          asChild: true,
          variant,
          size: resolvedSize,
          className: mergedClassName,
          disabled: isDisabled,
          ...otherProps,
          children: /* @__PURE__ */ jsx(
            "a",
            {
              ref,
              href,
              onClick: handleClick,
              children: buttonContent
            }
          )
        }
      );
    } else {
      buttonElement = /* @__PURE__ */ jsx(
        Button$1,
        {
          ref,
          type,
          variant,
          size: resolvedSize,
          className: mergedClassName,
          disabled: isDisabled,
          onClick: handleClick,
          ...otherProps,
          children: buttonContent
        }
      );
    }
    if (tooltipProps) {
      const { content, position, disabled: tooltipDisabled } = tooltipProps;
      if (tooltipDisabled || !content) {
        return /* @__PURE__ */ jsx(Fragment, { children: buttonElement });
      }
      const side = position ? TOOLTIP_SIDE_MAP[position] : void 0;
      const triggerContent = isDisabled ? /* @__PURE__ */ jsx("span", { className: "inline-flex", children: buttonElement }) : buttonElement;
      return /* @__PURE__ */ jsx(TooltipProvider, { children: /* @__PURE__ */ jsxs(Tooltip, { children: [
        /* @__PURE__ */ jsx(TooltipTrigger, { asChild: true, children: triggerContent }),
        /* @__PURE__ */ jsx(TooltipContent, { side, children: content })
      ] }) });
    }
    return /* @__PURE__ */ jsx(Fragment, { children: buttonElement });
  }
);
Button.displayName = "Button";

export { Button as B };
//# sourceMappingURL=Button-BCWJgHDy.js.map
