@import './shadows';
@import './state';

@mixin Dropdown {
  display: inline-block;
  position: relative;
  margin: 0;
}

@mixin DropdownCaret($direction: 'se') {
  position: absolute;
  overflow: hidden;
  height: 0.5rem;
  width: 1rem;

  &::before {
    background: $white;
    content: '';

    pointer-events: none;
    height: 1rem;
    width: 1rem;

    border: 1px solid transparentize($black, 0.8);
    transform: rotate(45deg);
    position: absolute;
    display: block;
  }

  // North
  @if ($direction == 'n') {
    margin-right: -0.5rem;
    bottom: -0.5rem;
    right: 50%;
    left: auto;
    top: auto;

    &::before {
      border-bottom-right-radius: 3px;
      bottom: 0.25rem;
      right: auto;
      left: auto;
      top: auto;
    }
  }

  // West
  @if ($direction == 'w') {
    margin-bottom: -0.5rem;
    height: 1rem;
    width: 0.5rem;
    bottom: 50%;
    right: -0.5rem;
    left: auto;
    top: auto;

    &::before {
      border-top-right-radius: 3px;
      bottom: auto;
      right: 0.25rem;
      left: auto;
      top: auto;
    }
  }

  // South
  @if ($direction == 's') {
    margin-right: -0.5rem;
    bottom: auto;
    right: 50%;
    left: auto;
    top: -0.5rem;

    &::before {
      border-top-left-radius: 3px;
      bottom: auto;
      right: auto;
      left: auto;
      top: 0.25rem;
    }
  }

  // East
  @if ($direction == 'e') {
    margin-bottom: -0.5rem;
    height: 1rem;
    width: 0.5rem;
    bottom: 50%;
    right: auto;
    left: -0.5rem;
    top: auto;

    &::before {
      border-bottom-right-radius: 3px;
      bottom: auto;
      right: auto;
      left: 0.25rem;
      top: auto;
    }
  }

  // South East
  @if ($direction == 'se') {
    bottom: auto;
    right: auto;
    left: 1rem;
    top: -0.5rem;

    &::before {
      border-top-left-radius: 3px;
      bottom: auto;
      right: auto;
      left: auto;
      top: 0.25rem;
    }
  }

  // North East
  @if ($direction == 'ne') {
    bottom: -0.5rem;
    right: auto;
    left: 1rem;
    top: auto;

    &::before {
      border-bottom-right-radius: 3px;
      bottom: 0.25rem;
      right: auto;
      left: auto;
      top: auto;
    }
  }

  // North West
  @if ($direction == 'nw') {
    bottom: -0.5rem;
    right: 1rem;
    left: auto;
    top: auto;

    &::before {
      border-bottom-right-radius: 3px;
      bottom: 0.25rem;
      right: auto;
      left: auto;
      top: auto;
    }
  }

  // South West
  @if ($direction == 'sw') {
    bottom: auto;
    right: 1rem;
    left: auto;
    top: -0.5rem;

    &::before {
      border-top-left-radius: 3px;
      bottom: auto;
      right: auto;
      left: auto;
      top: 0.25rem;
    }
  }
}

@mixin DropdownMenu {
  border: 1px solid transparentize($black, 0.8);
  background-color: $white;
  white-space: nowrap;
  border-radius: 3px;
  min-width: 10rem;

  padding-bottom: 0.375rem;
  padding-top: 0.375rem;
  position: absolute;
  z-index: 1030;

  will-change: transform, opacity;
  transition: 0.2s cubic-bezier(0.2, 0, 0.13, 1.25);
  pointer-events: none;
  opacity: 0;

  @include shadow(3);

  // South East (Default)
  &,
  &--se,
  &--southeast {
    transform: translateY(1rem);
    bottom: auto;
    right: auto;
    left: 0;
    top: 100%;

    .caret {
      @include DropdownCaret(se);
    }
  }

  &--se,
  &--southeast {
    margin-top: 0.5rem;
  }

  // South West
  &--sw,
  &--southwest {
    transform: translateY(1rem);
    bottom: auto;
    right: 0;
    left: auto;
    top: 100%;
    margin-top: 0.5rem;

    .caret {
      @include DropdownCaret(sw);
    }
  }

  // Start Irregular Positioning

  // North
  &--n,
  &--north {
    transform: translateY(-1rem) translateX(50%);
    bottom: 100%;
    right: 50%;
    left: auto;
    top: auto;
    margin-bottom: 0.5rem;

    .#{$dropdown-class + $open-class} & {
      transform: translateX(50%);
    }

    .caret {
      @include DropdownCaret(n);
    }
  }

  // West
  &--w,
  &--west {
    transform: translateX(-1rem) translateY(50%);
    bottom: 50%;
    right: 100%;
    left: auto;
    top: auto;
    margin-right: 0.5rem;

    .#{$dropdown-class + $open-class} & {
      transform: translateY(50%);
    }

    .caret {
      @include DropdownCaret(w);
    }
  }

  // South
  &--s,
  &--south {
    transform: translateY(1rem) translateX(50%);
    bottom: auto;
    right: 50%;
    left: auto;
    top: 100%;
    margin-top: 0.5rem;

    .#{$dropdown-class + $open-class} & {
      transform: translateX(50%);
    }

    .caret {
      @include DropdownCaret(s);
    }
  }

  // East
  &--e,
  &--east {
    transform: translateX(1rem) translateY(50%);
    bottom: 50%;
    right: auto;
    left: 100%;
    top: auto;
    margin-left: 0.5rem;

    .#{$dropdown-class + $open-class} & {
      transform: translateY(50%);
    }

    .caret {
      @include DropdownCaret(e);
    }
  }

  // North East
  &--ne,
  &--northeast {
    transform: translateY(-1rem);
    bottom: 100%;
    right: auto;
    left: 0;
    top: auto;
    margin-bottom: 0.5rem;

    .caret {
      @include DropdownCaret(ne);
    }
  }

  // North West
  &--nw,
  &--northwest {
    transform: translateY(-1rem);
    bottom: 100%;
    right: 0;
    left: auto;
    top: auto;
    margin-bottom: 0.5rem;

    .caret {
      @include DropdownCaret(nw);
    }
  }

  hr {
    margin: 0.375rem 0.75rem;
  }
}

@mixin DropdownHeader {
  font-weight: $dropdown-header-font-weight;
  font-size: $dropdown-header-font-size;
  padding: $dropdown-header-padding;
  color: $dropdown-header-color;
  text-transform: uppercase;
}

@mixin DropdownItem {
  box-sizing: border-box;
  font-weight: $dropdown-item-font-weight;
  font-size: $dropdown-item-font-size;
  padding: $dropdown-item-padding;
  border-radius: 0;
  text-align: left;
  cursor: pointer;
  line-height: 1;
  display: block;
  color: $black;
  background: 0;
  width: 100%;
  outline: 0;
  border: 0;

  touch-action: manipulation;
  transition: none;

  -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
  -webkit-font-smoothing: antialiased;
  -webkit-appearance: none;

  @include hover {
    background-color: $blue;
    color: $white;
  }

  @each $state, $color in $states {
    @include state($state) {
      color: $color;

      @include hover {
        background-color: $color;
        color: _text-set($color);
      }
    }
  }
}
