@use "sass:list";
@use "sass:map";
@use "sass:math";
@use "sass:meta";

// button
@mixin button-unstyled {
  border: 0;
  padding: 0;
  background-color: transparent;
  color: inherit;
}

@mixin button-base(
  $padding-vertical: $button-padding-vertical,
  $padding-horizontal: $button-padding-horizontal,
  $transition: $button-transition,
  $cursor-pointer: $button-cursor-pointer
) {
  display: inline-block;
  vertical-align: middle;
  padding: $padding-vertical $padding-horizontal;
  transition: $transition;
  text-align: center;
  text-decoration: none;
  white-space: normal;
  user-select: none;
  touch-action: manipulation;

  @if ($cursor-pointer) {
    cursor: pointer;
  }
}

@mixin button-style(
  $bg: $button-bg,
  $color: $button-color,
  $hover-bg: $button-hover-bg,
  $hover-color: $button-hover-color,
  $hover-border-color: $button-hover-border-color,
  $active-bg: $button-active-bg,
  $active-color: $button-active-color,
  $border-width: $button-border-width,
  $border-color: $button-border-color,
  $border-radius: $button-border-radius
) {
  border: $border-width solid $border-color;
  border-radius: $border-radius;
  background-color: $bg;
  color: $color;

  &:hover,
  &:focus {
    outline: 0;
    text-decoration: none;
    border-color: $hover-border-color;
    background-color: $hover-bg;
    color: $hover-color;
  }

  &:active {
    background-color: $active-bg;
    color: $active-color;
  }
}

@mixin button-disabled($disabled-opacity: $button-disabled-opacity) {
  &.disabled,
  &:disabled {
    opacity: $disabled-opacity;
    cursor: default;
    pointer-events: none;
  }
}

@mixin button {
  @include button-base;
  @include button-style;
  @include button-disabled;
}


// list
@mixin list-unstyled {
  padding-left: 0;
  list-style: none;
}

@mixin list-inline($spacing: 1rem, $center: false, $child-selector: 'li') {
  @include list-unstyled;

  display: flex;
  flex-wrap: wrap;
  margin-left: $spacing * -1;

  @if $center {
    justify-content: center;
  }

  > #{$child-selector} {
    margin-left: $spacing;
  }
}


// clearfix
@mixin clearfix {
  &::after {
    content: '';
    display: block;
    clear: both;
  }
}


// text
@mixin text-hide {
  text-indent: 110%;
  white-space: nowrap;
  overflow: hidden;
  color: transparent;
  text-shadow: none;
}

@mixin text-truncate {
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow: hidden;
}

@mixin text-hyphenate {
  overflow-wrap: break-word;
  word-wrap: break-word;
  hyphens: auto;
}


// visibility
@mixin sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
}

@mixin sr-only-focusable {
  &:active,
  &:focus {
    position: static;
    width: auto;
    height: auto;
    overflow: visible;
    clip: auto;
    white-space: inherit;
  }
}


// beautiful underline
@mixin underline($color: currentColor, $distance: 0, $width: 100%, $height: 1px) {
  padding-bottom: $distance;
  background-image: linear-gradient($color, $color);
  background-size: $width $height;
  background-position: 0 100%;
  background-repeat: no-repeat;
  text-decoration: none;
}


// target HiDPI screens
@mixin hidpi($ratio: 1.5) {
  @media (min-resolution: math.round($ratio * 96dpi)), (min-resolution: $ratio * 1dppx) {
    @content;
  }
}


// scale headings using modular scale
@mixin scale-headings($scale: $type-scale-base, $headings: $headings) {
  @if (meta.type-of($scale) == 'map') {
    @each $breakpoint, $size in $scale {
      @include mq($breakpoint) {
        @include loop-headings($size);
      }
    }
  }
  @else {
    @include loop-headings($scale);
  }
}


// helper mixin for scale-headings
@mixin loop-headings($size) {
  $max-scale: 0;

  @each $heading in $headings {
    #{$heading} {
      font-size: to-rem(modular-scale($max-scale, $size));
    }

    $max-scale: $max-scale + 1;
  }
}


// triangle mixin adapted from bourbon
@mixin triangle($direction, $width, $height, $color: currentColor) {
  border-style: solid;
  height: 0;
  width: 0;

  @if $direction == 'up' {
    border-color: transparent transparent $color;
    border-width: 0 ($width * 0.5) $height;
  }
  @else if $direction == 'up-right' {
    border-color: transparent $color transparent transparent;
    border-width: 0 $width $width 0;
  }
  @else if $direction == 'right' {
    border-color: transparent transparent transparent $color;
    border-width: ($height * 0.5) 0 ($height * 0.5) $width;
  }
  @else if $direction == 'down-right' {
    border-color: transparent transparent $color;
    border-width: 0 0 $width $width;
  }
  @else if $direction == 'down' {
    border-color: $color transparent transparent;
    border-width: $height ($width * 0.5) 0;
  }
  @else if $direction == 'down-left' {
    border-color: transparent transparent transparent $color;
    border-width: $width 0 0 $width;
  }
  @else if $direction == 'left' {
    border-color: transparent $color transparent transparent;
    border-width: ($height * 0.5) $width ($height * 0.5) 0;
  }
  @else if $direction == 'up-left' {
    border-color: $color transparent transparent;
    border-width: $width $width 0 0;
  }
}


// loop all breakpoints and output content
@mixin loop-breakpoints($breakpoints: $mq-breakpoints, $inclusive: true, $mq: true) {
  $breakpoint-keys: map.keys($breakpoints);

  @for $i from 1 through list.length($breakpoint-keys) {
    $breakpoint: list.nth($breakpoint-keys, $i) !global;
    $is-first-breakpoint: $breakpoint == list.nth($breakpoint-keys, 1) !global;
    $is-last-breakpoint: $breakpoint == list.nth($breakpoint-keys, list.length($breakpoint-keys)) !global;

    @if $mq {
      @if $inclusive {
        // first breakpoint
        @if $is-first-breakpoint {
          @content;
        }
        // remaining breakpoints
        @else {
          @include mq($breakpoint, $breakpoints: $breakpoints) {
            @content;
          }
        }
      }
      @else {
        // first breakpoint
        @if $is-first-breakpoint {
          @if list.length($breakpoint-keys) > 1 {
            @include mq($until: list.nth($breakpoint-keys, $i + 1), $breakpoints: $breakpoints) {
              @content;
            }
          }
          @else {
            @content;
          }
        }
        // last breakpoint
        @else if $is-last-breakpoint {
          @include mq($breakpoint, $breakpoints: $breakpoints) {
            @content;
          }
        }
        // remaining breakpoints
        @else {
          @include mq($breakpoint, list.nth($breakpoint-keys, $i + 1), $breakpoints: $breakpoints) {
            @content;
          }
        }
      }
    }
    @else {
      @content;
    }
  }
}


// Usage example:
//
// body {
//  @include fluid-calc('font-size', (1rem, 1.25rem), (min: 400px, max: 960px));
// }
@mixin fluid-calc($prop, $sizes, $breakpoints: $fluid-breakpoints) {
  @if ($prop and meta.type-of($sizes) == 'list') {
    $breakpoints: map.values($breakpoints);
    $min-breakpoint: list.nth($breakpoints, 1);
    $max-breakpoint: list.nth($breakpoints, list.length($breakpoints));
    $min-size: list.nth($sizes, 1);
    $max-size: list.nth($sizes, list.length($sizes));

    #{$prop}: $min-size;

    @include mq($min-breakpoint) {
      #{$prop}: calc(#{$min-size} + #{strip-unit($max-size - $min-size)} * (100vw - #{to-rem($min-breakpoint)}) / #{strip-unit(to-rem($max-breakpoint) - to-rem($min-breakpoint))});
    }

    @include mq($max-breakpoint) {
      #{$prop}: $max-size;
    }
  }
}
