import { jsxs, jsx } from 'react/jsx-runtime';
import * as React from 'react';
import { forwardRef } from 'react';
import { s as styles } from './Link.module-JgOOqNcL.js';
import { I as Icon } from './Icon-B4uCvMU9.js';
import { c as cx } from './index-De1g9FRV.js';
import { u as useLayoutPropsUtil } from './useLayoutPropsUtil-DjqeAJZ9.js';
import { c as childrenToString } from './childrenToString-Bz9MqbHb.js';
import { useTrackingId } from './useTrackingId.js';

const SvgOpenInNew = (props) => /* @__PURE__ */ React.createElement("svg", { xmlns: "http://www.w3.org/2000/svg", width: "1em", height: "1em", viewBox: "0 0 24 24", ...props }, /* @__PURE__ */ React.createElement("path", { d: "M18 19H6c-.55 0-1-.45-1-1V6c0-.55.45-1 1-1h5c.55 0 1-.45 1-1s-.45-1-1-1H5a2 2 0 0 0-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2v-6c0-.55-.45-1-1-1s-1 .45-1 1v5c0 .55-.45 1-1 1zM14 4c0 .55.45 1 1 1h2.59l-9.13 9.13a.996.996 0 1 0 1.41 1.41L19 6.41V9c0 .55.45 1 1 1s1-.45 1-1V4c0-.55-.45-1-1-1h-5c-.55 0-1 .45-1 1z" }));

const LinkHeadless = forwardRef(
  ({ children, ...props }, ref) => {
    return /* @__PURE__ */ jsxs(
      "a",
      {
        rel: "noopener noreferrer",
        "data-anv": "link-headless",
        ref,
        ...props,
        children: [
          children,
          props.target === "_blank" && /* @__PURE__ */ jsx(Icon, { className: styles["open-in-new"], inherit: true, svg: SvgOpenInNew })
        ]
      }
    );
  }
);
LinkHeadless.displayName = "LinkHeadless";

const useLinkStyles = (props) => {
  const { layoutStyles, componentProps } = useLayoutPropsUtil(props);
  const { className, appearance = "primary", ghost, style } = componentProps;
  const linkClassNames = cx([styles["link"]], className, {
    [styles["ghost"]]: ghost,
    [styles["secondary"]]: appearance === "secondary"
  });
  const styleCombined = Object.assign({}, style, layoutStyles);
  return { linkClassNames, styleCombined };
};

const Link$1 = forwardRef((props, ref) => {
  const { appearance, className, ghost, style, children, ...rest } = props;
  const { linkClassNames, styleCombined } = useLinkStyles({
    appearance,
    className,
    ghost,
    style
  });
  return /* @__PURE__ */ jsx(
    LinkHeadless,
    {
      className: linkClassNames,
      "data-anv": "link",
      ref,
      style: styleCombined,
      ...rest,
      children
    }
  );
});
Link$1.displayName = "Link";

const Link = forwardRef(
  (props, ref) => {
    const { onClick, pathname, children, search, ...rest } = props;
    const data = {
      appearance: props.appearance,
      ghost: props.ghost,
      children: childrenToString(children)
    };
    const handleClick = (...args) => {
      if (pathname) {
        const to = pathname + (search ?? "");
        args[0].preventDefault();
        history.pushState(null, "", to);
        onClick?.(...args);
      } else {
        onClick?.(...args);
      }
    };
    const trackingId = useTrackingId({
      name: "Link",
      data,
      hasOverride: !!rest["data-tracking-id"]
    });
    return /* @__PURE__ */ jsx(
      Link$1,
      {
        onClick: handleClick,
        ref,
        "data-tracking-id": trackingId,
        ...rest,
        children
      }
    );
  }
);
Link.displayName = Link$1.displayName;

export { Link as L, useLinkStyles as u };
//# sourceMappingURL=Link-C7dtI5K6.js.map
