'use strict';

var jsxRuntime = require('react/jsx-runtime');
var React = require('react');
var reactRouterDom = require('react-router-dom');
var utils = require('./utils-BhM0B89p.js');
var renderIcon = require('./renderIcon-BRrpZu9a.js');
var primitives_Button = require('./primitives/Button.js');
var primitives_Spinner = require('./primitives/Spinner.js');
var primitives_Tooltip = require('./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_VARIANTS = [
  "default",
  "secondary",
  "destructive",
  "destructive-soft",
  "outline",
  "ghost",
  "link"
];
const BUTTON_SIZES = [
  "default",
  "xs",
  "sm",
  "lg",
  "icon",
  "icon-xs",
  "icon-sm",
  "icon-lg"
];
const Button = React.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,
    trailing,
    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.renderIcon(icon) : null;
    const buttonContent = /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
      loading && /* @__PURE__ */ jsxRuntime.jsx("span", { className: "absolute inset-0 flex items-center justify-center", children: /* @__PURE__ */ jsxRuntime.jsx(
        primitives_Spinner.Spinner,
        {
          "aria-hidden": "true",
          className: SPINNER_SIZE_MAP[resolvedSize]
        }
      ) }),
      /* @__PURE__ */ jsxRuntime.jsxs(
        "span",
        {
          className: utils.cn(
            "inline-flex items-center gap-1.5",
            loading && "invisible"
          ),
          children: [
            iconPosition === "left" && iconElement && /* @__PURE__ */ jsxRuntime.jsx("span", { "data-icon": "inline-start", children: iconElement }),
            renderLabel && /* @__PURE__ */ jsxRuntime.jsx("span", { children: renderLabel }),
            iconPosition === "right" && iconElement && /* @__PURE__ */ jsxRuntime.jsx("span", { "data-icon": "inline-end", children: iconElement }),
            trailing && /* @__PURE__ */ jsxRuntime.jsx("span", { "data-icon": "inline-end", children: trailing })
          ]
        }
      )
    ] });
    const mergedClassName = utils.cn(
      fullWidth && "w-full",
      loading && "relative pointer-events-none",
      variant === "link" && "[table_&]:max-w-full [table_&]:whitespace-normal",
      className
    );
    let buttonElement;
    if (asChild) {
      buttonElement = /* @__PURE__ */ jsxRuntime.jsx(
        primitives_Button.Button,
        {
          ref,
          asChild: true,
          variant,
          size: resolvedSize,
          className: mergedClassName,
          disabled: isDisabled,
          ...otherProps,
          children
        }
      );
    } else if (!disabled && to) {
      buttonElement = /* @__PURE__ */ jsxRuntime.jsx(
        primitives_Button.Button,
        {
          asChild: true,
          variant,
          size: resolvedSize,
          className: mergedClassName,
          disabled: isDisabled,
          ...otherProps,
          children: /* @__PURE__ */ jsxRuntime.jsx(
            reactRouterDom.Link,
            {
              innerRef: ref,
              to,
              onClick: handleClick,
              children: buttonContent
            }
          )
        }
      );
    } else if (!disabled && href) {
      buttonElement = /* @__PURE__ */ jsxRuntime.jsx(
        primitives_Button.Button,
        {
          asChild: true,
          variant,
          size: resolvedSize,
          className: mergedClassName,
          disabled: isDisabled,
          ...otherProps,
          children: /* @__PURE__ */ jsxRuntime.jsx(
            "a",
            {
              ref,
              href,
              onClick: handleClick,
              children: buttonContent
            }
          )
        }
      );
    } else {
      buttonElement = /* @__PURE__ */ jsxRuntime.jsx(
        primitives_Button.Button,
        {
          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__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: buttonElement });
      }
      const side = position ? TOOLTIP_SIDE_MAP[position] : void 0;
      const triggerContent = isDisabled ? /* @__PURE__ */ jsxRuntime.jsx("span", { className: "inline-flex", children: buttonElement }) : buttonElement;
      return /* @__PURE__ */ jsxRuntime.jsx(primitives_Tooltip.TooltipProvider, { children: /* @__PURE__ */ jsxRuntime.jsxs(primitives_Tooltip.Tooltip, { children: [
        /* @__PURE__ */ jsxRuntime.jsx(primitives_Tooltip.TooltipTrigger, { asChild: true, children: triggerContent }),
        /* @__PURE__ */ jsxRuntime.jsx(primitives_Tooltip.TooltipContent, { side, children: content })
      ] }) });
    }
    return /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: buttonElement });
  }
);
Button.displayName = "Button";

exports.BUTTON_SIZES = BUTTON_SIZES;
exports.BUTTON_VARIANTS = BUTTON_VARIANTS;
exports.Button = Button;
//# sourceMappingURL=Button-8VH9p9RB.js.map
