import { jsx, jsxs, Fragment } from "react/jsx-runtime";
import clsx from "clsx";
import { forwardRef, useContext } from "react";
import { atoms } from "../../css/atoms/atoms.mjs";
import { useBackgroundLightness, useBackground } from "../Box/BackgroundContext.mjs";
import { useLinkComponent } from "../../../../side-effects/lib/components/BraidProvider/BraidProvider.mjs";
import { HeadingContext } from "../Heading/Heading.mjs";
import { TextContext } from "../Accordion/AccordionItem.mjs";
import { AvoidWidowIcon } from "../private/AvoidWidowIcon/AvoidWidowIcon.mjs";
import { buildDataAttributes } from "../Accordion/Accordion.mjs";
import { virtualTouchable } from "../private/touchable/virtualTouchable.mjs";
import { weakLink, regularLinkLightMode, regularLinkDarkMode, base, visitedLightMode, visitedDarkMode } from "./TextLink.css.mjs";
import { fontWeight } from "../../css/typography.css.mjs";
const isPlainBackground = (backgroundContext, contrast) => contrast === "light" && (backgroundContext === "body" || backgroundContext === "surface" || backgroundContext === "neutralLight") || contrast === "dark" && (backgroundContext === "bodyDark" || backgroundContext === "surfaceDark" || backgroundContext === "neutral");
const useLinkStyles = ({
  reset = "a",
  weight,
  showVisited = false,
  hitArea = "standard"
}) => {
  const backgroundLightness = useBackgroundLightness();
  const backgroundContext = useBackground();
  const textContext = useContext(TextContext);
  const headingContext = useContext(HeadingContext);
  const isWeakDueToTextTone = !headingContext && (textContext == null ? void 0 : textContext.tone) !== "neutral" && (textContext == null ? void 0 : textContext.tone) !== "secondary";
  const linkStyles = weight === "weak" || isWeakDueToTextTone ? weakLink : [
    isPlainBackground(backgroundContext.lightMode, "light") || weight === "regular" ? regularLinkLightMode[backgroundLightness.lightMode] : weakLink,
    isPlainBackground(backgroundContext.darkMode, "dark") || weight === "regular" ? regularLinkDarkMode[backgroundLightness.darkMode] : weakLink
  ];
  return clsx(
    base,
    linkStyles,
    weight !== "weak" ? fontWeight.medium : void 0,
    showVisited ? [
      visitedLightMode[backgroundLightness.lightMode],
      visitedDarkMode[backgroundLightness.darkMode]
    ] : "",
    reset !== false ? atoms({
      reset: typeof reset === "string" ? reset : "a"
    }) : null,
    atoms({
      cursor: "pointer"
    }),
    hitArea === "large" && virtualTouchable()
  );
};
const TextLinkContent = ({
  icon,
  iconPosition = "leading",
  children
}) => /* @__PURE__ */ jsxs(Fragment, { children: [
  icon && iconPosition === "leading" ? /* @__PURE__ */ jsx(AvoidWidowIcon, { iconPosition, children: icon }) : null,
  children,
  icon && iconPosition === "trailing" ? /* @__PURE__ */ jsx(AvoidWidowIcon, { iconPosition, children: icon }) : null
] });
const TextLink = forwardRef(
  ({
    weight,
    showVisited,
    hitArea,
    data,
    icon,
    children,
    iconPosition,
    ...restProps
  }, ref) => {
    const LinkComponent = useLinkComponent(ref);
    const classes = useLinkStyles({
      weight,
      showVisited,
      hitArea
    });
    return /* @__PURE__ */ jsx(
      LinkComponent,
      {
        ref,
        ...restProps,
        className: classes,
        ...buildDataAttributes({ data, validateRestProps: false }),
        children: /* @__PURE__ */ jsx(TextLinkContent, { icon, iconPosition, children })
      }
    );
  }
);
TextLink.displayName = "TextLink";
export {
  TextLink,
  TextLinkContent,
  useLinkStyles
};
