import { jsxs, jsx } from 'react/jsx-runtime';
import { forwardRef } from 'react';
import { I as Icon } from './Icon-B4uCvMU9.js';
import { S as Spinner } from './Spinner-B7tTWcP6.js';
import { c as cx } from './index-De1g9FRV.js';
import { s as styles } from './Button.module-BuOc7iPY.js';
import { u as useLayoutPropsUtil } from './useLayoutPropsUtil-DjqeAJZ9.js';
import { c as childrenToString } from './childrenToString-Bz9MqbHb.js';
import { useTrackingId } from './useTrackingId.js';

const ButtonLink = forwardRef(
  (props, ref) => {
    const { layoutStyles, componentProps } = useLayoutPropsUtil(props);
    const {
      children,
      className,
      appearance = "secondary",
      size = "medium",
      icon,
      loading = false,
      disabled,
      onClick,
      onKeyUp,
      onKeyDown,
      style,
      href,
      type,
      ...rest
    } = componentProps;
    const data = {
      children: childrenToString(props.children),
      appearance,
      href,
      icon,
      size,
      type
    };
    const iconBefore = (icon2) => {
      if (icon2 && typeof icon2 === "object" && "before" in icon2) {
        return /* @__PURE__ */ jsx(
          Icon,
          {
            "aria-hidden": true,
            className: styles["icon"],
            inherit: true,
            svg: icon2.before
          }
        );
      }
      if (icon2 && typeof icon2 !== "object") {
        return /* @__PURE__ */ jsx(Icon, { "aria-hidden": true, className: styles["icon"], inherit: true, svg: icon2 });
      }
      return;
    };
    const handleOnClick = (e) => {
      if (disabled || loading) {
        e.preventDefault();
        return;
      }
      onClick?.(e);
    };
    const onKeyDownHandler = (e) => {
      if (e.code === "Space") {
        e.preventDefault();
        onKeyDown?.(e);
        return;
      }
      onKeyDown?.(e);
    };
    const onKeyUpHandler = (e) => {
      if (e.code === "Space" || e.code === "Enter") {
        e.preventDefault();
        if (onClick) {
          e.currentTarget.click();
        }
      }
      onKeyUp?.(e);
    };
    const trackingId = useTrackingId({
      name: "ButtonLink",
      data,
      hasOverride: !!props["data-tracking-id"]
    });
    const buttonClassNames = cx(className, styles["button"], {
      [styles["appearance-primary"]]: appearance === "primary",
      [styles["appearance-secondary"]]: appearance === "secondary",
      [styles["appearance-ghost"]]: appearance === "ghost",
      [styles["danger-secondary"]]: appearance === "danger-secondary",
      [styles["danger-primary"]]: appearance === "danger",
      [styles["size-xsmall"]]: size === "xsmall",
      [styles["size-small"]]: size === "small",
      [styles["size-medium"]]: size === "medium",
      [styles["size-large"]]: size === "large",
      [styles["size-xsmall-icon"]]: size === "xsmall" && !children,
      [styles["size-small-icon"]]: size === "small" && !children,
      [styles["size-medium-icon"]]: size === "medium" && !children,
      [styles["size-large-icon"]]: size === "large" && !children,
      [styles["loading"]]: loading
    });
    const styleCombined = {
      ...style,
      ...layoutStyles
    };
    return /* @__PURE__ */ jsxs(
      "a",
      {
        "data-tracking-id": trackingId,
        className: buttonClassNames,
        "aria-disabled": disabled || loading,
        "aria-busy": loading,
        "data-anv": "button-link",
        tabIndex: disabled || loading ? -1 : 0,
        onClick: handleOnClick,
        onKeyDown: onKeyDownHandler,
        onKeyUp: onKeyUpHandler,
        style: styleCombined,
        ref,
        href,
        ...rest,
        children: [
          loading ? /* @__PURE__ */ jsx(Spinner, { inherit: true, size: "small", className: styles["loading-spinner"] }) : icon ? iconBefore(icon) : null,
          children,
          typeof icon === "object" && "after" in icon ? /* @__PURE__ */ jsx(
            Icon,
            {
              className: styles["icon"],
              inherit: true,
              "aria-hidden": true,
              svg: icon.after
            }
          ) : null
        ]
      }
    );
  }
);
ButtonLink.displayName = "ButtonLink";

export { ButtonLink as B };
//# sourceMappingURL=ButtonLink-PlW08u41.js.map
