@use "sass:list";
@use "sass:map";
@use "sass:math";
@use './variables';

$cuts: 8, 16, 24, 32, 40, 48;
$shadows: (
  'sm': variables.$drop-shadow-sm,
  'md': variables.$drop-shadow-md,
  'lg': variables.$drop-shadow-lg,
  'xl': variables.$drop-shadow-xl
);

@mixin input-transition {
  transition: all 75ms;
}

@mixin input-transition-long {
  transition: all 250ms;
}

@mixin text-ellipsis {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

@mixin drop-shadow($shadow: 'md') {
  filter: #{map.get($shadows, $shadow)};
}

@mixin miter-original($i, $inner: false) {
  clip-path: polygon(
    0 0,
    100% 0,
    100% calc(100% - #{list.nth($cuts, $i)}px + #{if($inner, 0.5px, 0px)}),
    calc(100% - #{list.nth($cuts, $i)}px + #{if($inner, 0.5px, 0px)}) 100%,
    0 100%
  );
}

@mixin firefox-only {
  @at-root {
    @-moz-document url-prefix() {
      & {
        @content;
      }
    }
  }
}

@mixin safari-only {
  @media not all and (min-resolution: 0.001dpcm) {
    & {
      @content;
    }
  }
}

@mixin edge-only {
  @supports (-ms-ime-align: auto) {
    & {
      @content;
    }
  }
}

@mixin miter-clip($cut-length: 1, $border-width: 1) {
  $cut: list.nth($cuts, $cut-length);
  $hypotenuse: math.ceil(
    math.hypot($cut + $border-width * 0.25, $cut + $border-width * 0.25)
  );

  --end: 100%;

  @include firefox-only {
    --end: calc(100% + 0.5px);
  }

  @include safari-only {
    --end: calc(100% + 0.5px);
  }

  $end: var(--end);

  $miter-x: calc(#{$end} - #{$cut}px);
  $miter-y: calc(#{$end} - #{$cut}px);
  $polygon: polygon(
    $miter-x $end,
    $end $miter-y,
    $end 0,
    0 0,
    0 $end,
    $miter-x $end
  );

  clip-path: stroke-box $polygon;
  -webkit-clip-path: $polygon;
}

@mixin miter($cut-length: 1, $border-width: 1) {
  $cut: list.nth($cuts, $cut-length);
  $hypotenuse: math.ceil(
    math.hypot($cut + $border-width * 0.25, $cut + $border-width * 0.25)
  );

  --end: 100%;

  @include firefox-only {
    --end: calc(100% + 0.5px);
  }

  @include safari-only {
    --end: calc(100% + 0.5px);
  }

  $end: var(--end);

  $miter-x: calc(#{$end} - #{$cut}px);
  $miter-y: calc(#{$end} - #{$cut}px);
  $polygon: polygon(
    $miter-x $end,
    $end $miter-y,
    $end 0,
    0 0,
    0 $end,
    $miter-x $end
  );

  > :first-child {
    clip-path: stroke-box $polygon;
    -webkit-clip-path: $polygon;
    position: relative;

    &:after {
      content: '';
      height: #{$hypotenuse}px;
      width: 0;
      padding: 0;
      right: #{$cut - $border-width - $border-width * 0.25}px;
      bottom: -#{$border-width + $border-width * 0.25}px;
      position: absolute;
      border-color: inherit;
      border: inherit;
      border-bottom: none;
      border-right: none;
      border-top: none;

      // Increasing the border width of this slightly gives a more consistent look on the diagonal
      border-width: #{$border-width + $border-width * 0.25}px;
      transform: rotate(45deg);
      transform-origin: bottom;
      user-select: none;
      -webkit-user-select: none;
    }
  }
}

@mixin skeleton-loader {
  overflow: hidden;
  position: relative;

  &::before,
  &::after {
    content: '';
    top: 0;
    right: 0;
    left: 0;
    bottom: 0;
    position: absolute;
    z-index: 2;
  }

  &::before {
    background-color: variables.$secondary-hover;
  }

  &::after {
    transform: translateX(-100%);
    background-image: linear-gradient(
      90deg,
      rgba(#fff, 0) 0,
      rgba(#fff, 0.2) 20%,
      rgba(#fff, 0.5) 60%,
      rgba(#fff, 0)
    );
  }

  &::after {
    transform: translateX(-100%);
    background-image: linear-gradient(
      90deg,
      rgba(#fff, 0) 0,
      rgba(#fff, 0.2) 20%,
      rgba(#fff, 0.5) 60%,
      rgba(#fff, 0)
    );
    animation: skeleton-loader-animation 2s cubic-bezier(0, 0.35, 1, 0.35)
      infinite;
  }
}

@keyframes skeleton-loader-animation {
  100% {
    transform: translateX(100%);
  }
}

@mixin miter-bordered() {
  position: relative;
  clip-path: var(--miter-clip-path);
  
 &::before {
    content: '';
    display: block;
    position: absolute;
    background-color: var(--miter-border-color);
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 1000;
    pointer-events: none;
    clip-path: var(--miter-clip-path-border);
  }
}

@mixin miter-bordered-rounded() {
  position: relative;
  clip-path: var(--miter-clip-path-rounded);
  
 &::before {
    content: '';
    display: block;
    position: absolute;
    background-color: var(--miter-border-color);
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 1000;
    pointer-events: none;
    clip-path: var(--miter-clip-path-border-rounded);
  }
}
