/**
 * @license EUPL-1.2
 * Copyright (c) 2021 The Knights Who Say NIH! B.V.
 * Copyright (c) 2021 Gemeente Utrecht
 */

// Avoid false positive for new CSS function `max()`
/* stylelint-disable scss/no-global-function-names */

@import "~@utrecht/focus-ring-css/src/mixin";

/*

# CSS implementation

## `text-decoration-skip`

`text-decoration-skip` can be helpful to avoid making some texts unreadable.
For example by obscuring Arabic diacritics.

However, the combination of a greater thickness and skipping this thick underline
leads to a very unappealing rendering of the underline. To avoid this,
`text-decoration-skip` is disabled for interactive states.

For design token configurations that have identical thickness for normal and interactive
states, this will lead to the (undesirable) effect that the focus/hover effect is switching
from an interrupted to an uninterrupted underline (for some texts).

Apart from making `skip-ink` configurable for normal/focus/hover, there is no good solution yet.

---

Disabling `text-decoration-skip` for interactive states obscures some texts, and we assume for now
that moving the pointer away from a link or having focus elsewhere first is simple enough to
not make this too inconvenient.

---

Some folks implement the underline of links using `border-bottom` or even using a finely crafted
`linear-gradient()` with a solid color at the bottom and transparent behind the text. These approaches
would unfortunately not be able to provide the improved readability of `text-decoration-skip`.

## `text-decoration-thickness`

Varying `text-decoration-thickness` can be used to distinguish interactive states.

---

`text-decoration-thickness` appears to have rendering differences between Chrome and Safari.
In Safari the line becomes thicker with extra pixels added to the bottom, while in Chrome
the underline offset also seems to increase along with the thickness, which effectively means
the underline is closer to the next line than in Safari.

---

It might be nice to use font-relative units for `text-decoration-thickness`, and that is why we
use the `max()` function to ensure the underline remains visible for every font size.

## `transition`

`text-decoration-thickness` could be a candidate for animating between interactive states,
however browsers don't seem to have implemented great looking supixel tweening yet.

`text-decoration-skip` also makes the transition more challenging to implement.

*/

@mixin utrecht-link {
  --utrecht-icon-size: var(--utrecht-link-icon-size, 1em);

  color: var(--_utrecht-link-state-color, var(--utrecht-link-color, var(--_utrecht-link-forced-colors-color)));
}

@mixin utrecht-link--any-link {
  /* Only underline `<a href="...">...</a>`, which matches `a:any-link`.
   * Do not underline `<a name="">Anchor</a>`.
   */
  --_utrecht-link-forced-colors-color: linktext;

  text-decoration-color: var(
    --_utrecht-link-state-text-decoration-color,
    var(--utrecht-link-text-decoration-color, currentColor)
  );
  text-decoration-line: var(--_utrecht-link-state-text-decoration, var(--utrecht-link-text-decoration, underline));
  text-decoration-skip-ink: all;
  text-decoration-thickness: max(
    var(--_utrecht-link-state-text-decoration-thickness, var(--utrecht-link-text-decoration-thickness)),
    1px
  );
  text-underline-offset: var(--utrecht-link-text-underline-offset);
}

@mixin utrecht-link--visited {
  --_utrecht-link-forced-colors-color: visitedtext;
  --_utrecht-link-state-color: var(--utrecht-link-visited-color);
}

@mixin utrecht-link--hover {
  --_utrecht-link-forced-colors-color: linktext;
  --_utrecht-link-state-color: var(--utrecht-link-hover-color);
  --_utrecht-link-state-text-decoration: var(--utrecht-link-hover-text-decoration);
  --_utrecht-link-state-text-decoration-thickness: var(--utrecht-link-hover-text-decoration-thickness);

  text-decoration-skip: none;
  text-decoration-skip-ink: none;
}

@mixin utrecht-link--active {
  --_utrecht-link-forced-colors-color: activetext;
  --_utrecht-link-state-color: var(--utrecht-link-active-color);
}

@mixin utrecht-link--focus {
  --_utrecht-link-state-color: var(--utrecht-link-focus-color);

  background-color: var(--utrecht-link-focus-background-color, transparent);
  text-decoration-skip: none;
  text-decoration-skip-ink: none;
  @include utrecht-focus;
}

@mixin utrecht-link--placeholder {
  --_utrecht-link-forced-colors-color: GrayText;
  --_utrecht-link-state-color: var(--utrecht-link-placeholder-color);

  cursor: var(--utrecht-action-disabled-cursor, not-allowed);
  font-weight: var(--utrecht-link-placeholder-font-weight);
  text-decoration-line: none;
}

@mixin utrecht-link--current {
  cursor: var(--utrecht-action-inert-cursor, normal);
  font-weight: var(--utrecht-link-current-font-weight);
}

@mixin utrecht-link--focus-visible {
  /*
   * WCAG SC 2.4.12: Focus Not Obscured
   * Use `z-index` to ensure the focus ring is stacked above adjecent elements with a `background`
   */
  @include utrecht-focus-visible;

  /*
   * The following tokens are deprecated, but we keep them for backwards compatibility for now:
   * 
   * `--utrecht-link-focus-text-decoration`
   * `--utrecht-link-focus-text-decoration-thickness`
   */
  --_utrecht-link-state-text-decoration: var(
    --utrecht-link-focus-visible-text-decoration,
    var(--utrecht-link-focus-text-decoration)
  );
  --_utrecht-link-state-text-decoration-thickness: var(
    --utrecht-link-focus-visible-text-decoration-thickness,
    var(--utrecht-link-focus-text-decoration-thickness)
  );

  z-index: var(--utrecht-stack-focus-z-index, 1);
}

/**
 * Simulate forced-colors mode.
 */
@mixin utrecht-link--forced-colors {
  /* Some others choose `transparent` to trigger `currentColor` for `inverse-outline-color`,
   * however this doesn't guarantee significant contrast between `outline-color` and `inverse-outline-color`.
   * That's why we use `Highlight` vs `HighlightText`.
   */
  --utrecht-focus-outline-color: Highlight;
  --utrecht-focus-inverse-outline-color: HighlightText;
  --utrecht-link-color: linktext;
  --utrecht-link-hover-color: linktext;
  --utrecht-link-focus-color: linktext;
  --utrecht-link-active-color: activetext;
  --utrecht-link-visited-color: visitedtext;
  --utrecht-link-placeholder-color: GrayText;
}

/* stylelint-disable-next-line block-no-empty */
@mixin utrecht-link--mailto {
}

/* stylelint-disable-next-line block-no-empty */
@mixin utrecht-link--external {
}

@mixin utrecht-link--telephone {
  white-space: nowrap;
}

/* stylelint-disable-next-line block-no-empty */
@mixin utrecht-link--alternate-lang {
}

/* stylelint-disable-next-line block-no-empty */
@mixin utrecht-link--current-lang {
}

/**
 * Link for elements such as `<img>` or `<article>`, that are not inline elements or plain text.
 * 
 * Changing `display: inline` to `inline-block` ensures the focus outline is rendered around the entire box.
 */
@mixin utrecht-link--box-content {
  color: unset;
  display: inline-block;
  text-decoration-line: unset;
  text-decoration-skip-ink: unset;
  text-decoration-thickness: unset;
  text-underline-offset: unset;
}

@mixin utrecht-link--html-a {
  &:visited {
    @include utrecht-link--visited;
  }

  &:any-link:hover {
    @include utrecht-link--hover;
  }

  &:any-link:active {
    @include utrecht-link--active;
  }

  &:any-link:focus {
    @include utrecht-link--focus;
  }

  &:any-link:focus-visible {
    @include utrecht-link--focus-visible;
  }
}
