@use './focus';

$ods-link-font-weight: var(--ods-semantic-font-weight-semibold);
// Unlike the colors, not declared by the mixin, only read with a fallback: a value set on a link or
// on any ancestor (a container, :root) is inherited instead of being reset on every link. Kept out of
// ./style (index.scss hides them): the custom properties are the API.
$ods-link-underline-thickness: var(--ods-link-underline-thickness, 1px);
$ods-link-underline-thickness-hover: var(--ods-link-underline-thickness-hover, 2px);

@mixin ods-link() {
  --ods-link-column-gap: var(--ods-semantic-size-space-gap-inline);
  --ods-link-row-gap: var(--ods-semantic-size-space-gap-block);
  --ods-link-text-color: var(--ods-semantic-color-content-brand);
  // Same value as at rest: the hover signal is the thicker underline, not a color change.
  --ods-link-text-color-hover: var(--ods-semantic-color-content-brand);
  --ods-link-text-color-visited: var(--ods-semantic-color-interaction-brand-visited);

  display: inline-flex;
  position: relative;
  flex-flow: row;
  gap: var(--ods-link-row-gap) var(--ods-link-column-gap);
  align-items: center;
  width: max-content;
  text-decoration: none;
  color: var(--ods-link-text-color);
  font-weight: $ods-link-font-weight;
  cursor: pointer;

  // The underline is there at rest, so that a link is not told by its color alone (RGAA 10.6,
  // WCAG 1.4.1), and thickens on hover.
  &::after {
    position: absolute;
    bottom: 0;
    left: 0;
    background-color: currentcolor;
    width: 100%;
    height: $ods-link-underline-thickness;
    content: "";
  }

  &:hover,
  &:focus-visible {
    color: var(--ods-link-text-color-hover);

    &::after {
      height: $ods-link-underline-thickness-hover;
    }
  }

  &:focus-visible {
    @include focus.ods-focus();
  }

  // After :hover, so that a visited link keeps its color on hover, as in 19.7.3.
  &:visited {
    color: var(--ods-link-text-color-visited);
  }

  // Forced colors repaint the background of the line with the canvas color: draw it with the link
  // color of the user's palette, so that the underline stays the cue.
  @media (forced-colors: active) {
    &::after {
      background-color: LinkText;
    }
  }
}
