// Drawing mixins

// generic drawing of more complex things

//
// Helper mixin for drawing visible focus rings
//
// If $target is specified, the focus ring is applied to the specified child element.
// If $outer is true, the focus ring extends outward. Otherwise, it extends inward.
// If $within is true, use focus-within instead of focus:focus-visible
//
@mixin focus-ring($target: null, $width: 2px, $offset: -$width, $outer: false, $focus-state: ':focus:focus-visible', $transition: null) {
  & #{$target} {
    outline: 0 solid transparent;
    outline-offset: if($outer, $offset + 4px, $offset + $width + 4px);
    transition: $focus_transition, $transition;
  }

  &#{$focus-state} #{$target} {
    outline-color: $focus_border_color;
    outline-width: $width;
    outline-offset: $offset;
  }
}

@mixin undershoot($p, $neighbor: false) {
//
// undershoot
//
// $p: position
// $neighbor: use ~ instead of >
//
// possible $p values:
// top, bottom, right, left
//

  $_border_pos: '';
  $_direction: '';
  $_selector: if($neighbor, '~', '>');

  @if $p==top {
    $_direction: bottom;
    $_border_pos: 0 1px;
  } @else if $p==bottom {
    $_direction: top;
    $_border_pos: 0 -1px;
  } @else if $p==left {
    $_direction: right;
    $_border_pos: 1px 0;
  } @else if $p==right {
    $_direction: left;
    $_border_pos: -1px 0;
  } @else {
    @error "Unknown position #{$p}"
  }

  #{$_selector} undershoot.#{$p} {
    box-shadow: inset $_border_pos if($contrast == 'high', $border_color, color-mix(in srgb, var(--shade-color) 75%, transparent));
    background: linear-gradient(to $_direction,
                                color-mix(in srgb, var(--shade-color) 75%, transparent),
                                transparent 4px);
  }
}

@mixin overshoot($p) {
//
// overshoot
//
// $p: position
//
// possible $p values:
// top, bottom, right, left
//

  $_small_gradient_length: 3%;
  $_big_gradient_length: 50%;

  $_small_gradient_size: 100% $_small_gradient_length;
  $_big_gradient_size: 100% $_big_gradient_length;

  @if $p==right or $p==left {
    $_small_gradient_size: $_small_gradient_length 100%;
    $_big_gradient_size: $_big_gradient_length 100%;
  }

  $_small_gradient: radial-gradient(farthest-side at $p,
                                    color-mix(in srgb, currentColor 12%, transparent) 85%,
                                    RGB(from currentColor r g b / 0));

  $_big_gradient: radial-gradient(farthest-side at $p,
                                  color-mix(in srgb, currentColor 5%, transparent),
                                  RGB(from currentColor r g b / 0));

  background-image: $_small_gradient, $_big_gradient;
  background-size: $_small_gradient_size, $_big_gradient_size;

  background-repeat: no-repeat;
  background-position: $p;

  background-color: transparent; // reset some properties to be sure to not inherit them somehow
  border: none;                  //
  box-shadow: none;              //
}

@mixin background-shadow($direction) {
  background-image:
    linear-gradient($direction,
                    color-mix(in srgb, var(--shade-color) 70%, transparent),
                    color-mix(in srgb, var(--shade-color) 14%, transparent) 40px,
                    RGB(from var(--shade-color) r g b / 0) 56px),
    linear-gradient($direction,
                    color-mix(in srgb, var(--shade-color) 40%, transparent),
                    color-mix(in srgb, var(--shade-color) 14%, transparent) 7px,
                    RGB(from var(--shade-color) r g b / 0) 24px);
}

@mixin transition-shadows() {
  > dimming {
    background: var(--shade-color);
  }

  @if $contrast == 'high' {
    > border {
      min-width: 1px;
      min-height: 1px;
      background: $border_color;
    }
  }

  > shadow {
    min-width: 56px;
    min-height: 56px;

    &.left  { @include background-shadow(to right); }
    &.right { @include background-shadow(to left); }
    &.up    { @include background-shadow(to bottom); }
    &.down  { @include background-shadow(to top); }
  }
}
