
// @arrow-box()
//
// Generates a box with an arrow attached on a specified side
//
// ### Example Usage:
//
//   ```
//   @include arrow-box(
//   "adde-popover",
//   $popover-placements,
//   $adde-popover-z-index,
//   $adde-popover-background-color,
//   $adde-popover-width,
//   $adde-popover-min-width,
//   $adde-popover-max-width,
//   $adde-popover-box-padding,
//   $adde-popover-border-width,
//   $adde-popover-border-style,
//   $adde-popover-border-color,
//   $adde-popover-border-radius,
//   $adde-popover-box-shadow,
//   $adde-popover-text-color,
//   $adde-popover-text-align,
//   $adde-popover-font-weight,
//   $adde-popover-font-size,
//   $adde-popover-line-height,
//   $adde-popover-font-family,
//   $adde-popover-fade-transition,
//   $adde-popover-arrow-size
//   );
//   ```
//
// ### Parameters:
//
// * $name = Class name for your arrow-box
// * $box-placements = Map of placements, each placement having a side (left, right, top, bottom) mapped to a direction (horizontal or vertical)
// * $z-index = z-index for the whole component
// * $background-color = Background color of the box and arrow
// * $max-width = max width for the main box
// * $box-padding = padding for the main box
// * $border-width = border thickness for the box and arrow
// * $border-style = border style for the box and arrow
// * $border-color = border color for the box and arrow
// * $border-radius = border radius for the main box
// * $box-shadow = box shadow for the main box
// * $text-color = content text color for the main box
// * $text-align = content text alignment for the main box
// * $font-weight = content font weight for the main box
// * $font-size = content font size for the main box
// * $line-height = content line height for the main box
// * $font-family = content font family for the main box
// * $fade-transition = fade in/out time for the whole component
// * $arrow-size = arrow size, both width and height
//
// ### Example Output:
//   ```
//   .adde-tooltip {
//   position: absolute;
//   z-index: 10;
//   background: #333333;
//   width: auto;
//   min-width: auto;
//   max-width: 200px;
//   height: auto;
//   min-height: auto;
//   max-height: auto;
//   padding: 8px;
//   border: 0 none none;
//   border-radius: 0;
//   box-shadow: none;
//   color: #ffffff;
//   text-align: left;
//   font-weight: normal;
//   font-size: 11px;
//   line-height: 13px;
//   font-family: "Univers-Next", "Helvetica Neue", "Helvetica", Arial, sans-serif;
//   transition: opacity 0.2s ease-in-out;
//   opacity: 0; }
//   .adde-tooltip .popper-arrow {
//     position: absolute;
//     width: 0;
//     height: 0;
//     margin: 8px;
//     border-width: 8px;
//     border-style: solid;
//     border-color: #333333; }
//   .adde-tooltip.is-visible {
//     opacity: 1; }
//   .adde-tooltip[x-placement^="left"] {
//     margin-right: 8px; }
//     .adde-tooltip[x-placement^="left"] .popper-arrow {
//       border-right-width: 0;
//       border-right-color: transparent;
//       right: -8px; }
//   .adde-tooltip[x-placement^="right"] {
//     margin-left: 8px; }
//     .adde-tooltip[x-placement^="right"] .popper-arrow {
//       border-left-width: 0;
//       border-left-color: transparent;
//       left: -8px; }
//   .adde-tooltip[x-placement^="top"] {
//     margin-bottom: 8px; }
//     .adde-tooltip[x-placement^="top"] .popper-arrow {
//       border-bottom-width: 0;
//       border-bottom-color: transparent;
//       bottom: -8px; }
//   .adde-tooltip[x-placement^="bottom"] {
//     margin-top: 8px; }
//     .adde-tooltip[x-placement^="bottom"] .popper-arrow {
//       border-top-width: 0;
//       border-top-color: transparent;
//       top: -8px; }
//   .adde-tooltip[x-placement^="top"] .popper-arrow, .adde-tooltip[x-placement^="bottom"] .popper-arrow {
//     margin-top: 0;
//     margin-bottom: 0;
//     border-right-color: transparent;
//     border-left-color: transparent;
//     left: calc(50% - 8px); }
//   .adde-tooltip[x-placement^="left"] .popper-arrow, .adde-tooltip[x-placement^="right"] .popper-arrow {
//     margin-left: 0;
//     margin-right: 0;
//     border-top-color: transparent;
//     border-bottom-color: transparent;
//     top: calc(50% - 8px); }
//   ```
//
// Styleguide: Adde Pop - Utils - Mixins - @arrow-box()

@mixin arrow-box(
  $name,
  $box-placements,
  $z-index,
  $background-color,
  $width,
  $min-width,
  $max-width,
  $height,
  $min-height,
  $max-height,
  $box-padding,
  $border-width,
  $border-style,
  $border-color,
  $border-radius,
  $box-shadow,
  $text-color,
  $text-align,
  $font-weight,
  $font-size,
  $line-height,
  $font-family,
  $fade-transition,
  $arrow-size
  ) {

  $has-border: non-zero-value($border-color);

  .#{$name} {
    position: absolute;
    z-index: $z-index;

    background: $background-color;
    padding: $box-padding;

    border: $border-width $border-style $border-color;
    border-radius: $border-radius;
    box-shadow: $box-shadow;

    color: $text-color;
    text-align: $text-align;
    font-weight: $font-weight;
    font-size: $font-size;
    line-height: $line-height;
    font-family: $font-family;

    transition: $fade-transition;
    opacity: 0;

    &.is-visible {
      opacity: 1;
    }

    .popper-arrow {
      position: absolute;
      width: 0;
      height: 0;
      margin: $arrow-size;
      border-width: $arrow-size;
      border-style: solid;

      @if $has-border {
        border-color: adjust-arrow-border-color($border-color);

        &::before {
          content: '';
          position: absolute;
          width: 0;
          height: 0;
          border: $arrow-size solid $background-color;
        }
      } @else {
        border-color: $background-color;
      }
    }

    @each $box-placement, $direction in $box-placements {
      // Arrow placement is opposite of box placement
      $arrow-placement: null;
      @if $box-placement == 'left' { $arrow-placement: 'right'; }
      @if $box-placement == 'right' { $arrow-placement: 'left'; }
      @if $box-placement == 'top' { $arrow-placement: 'bottom'; }
      @if $box-placement == 'bottom' { $arrow-placement: 'top'; }

      &[x-placement^='#{$box-placement}'] {
        margin-#{$arrow-placement}: $arrow-size;

        .popper-arrow {
          @extend %#{$name}-#{$direction}-presets;
          border-#{$arrow-placement}-width: 0;
          border-#{$arrow-placement}-color: transparent;
          #{$arrow-placement}: -$arrow-size;

          @if $has-border {
            &::before {
              #{$arrow-placement}: calc(-#{$arrow-size} + #{$border-width});
              border-#{$arrow-placement}-color: transparent;
            }
          }
        }
      }
    }
  }

  .#{$name}-content {
    // Initial sizing of the box is controlled with this div
    width: $width;
    min-width: $min-width;
    max-width: $max-width;
    height: $height;
    min-height: $min-height;
    max-height: $max-height;
  }

  %#{$name}-vertical-presets {
    margin-top: 0;
    margin-bottom: 0;
    border-right-color: transparent;
    border-left-color: transparent;
    left: calc(50% - #{$arrow-size});

    @if $has-border {
      &::before {
        left: -$arrow-size;
        border-right-color: transparent;
        border-left-color: transparent;
      }
    }
  }

  %#{$name}-horizontal-presets {
    margin-left: 0;
    margin-right: 0;
    border-top-color: transparent;
    border-bottom-color: transparent;
    top: calc(50% - #{$arrow-size});

    @if $has-border {
      &::before {
        top: -$arrow-size;
        border-top-color: transparent;
        border-bottom-color: transparent;
      }
    }
  }
}

// arrow-box-color-variation()
//
// Create an arrow-box variation based on color.
// Be careful to not make the selector containing this
// mixin overly specific or it will overwrite the x-placement colors,
// causing the new colors to not apply.
//
// ### Example Usage:
//
//   ```
//   @include arrow-box-color-variation(blue, white, gray);
//   ```
//
// ### Parameters:
//
// * $background-color = New background color for the box and arrow
// * $text-color = New text color for the box
// * $border-color (optional) = New border color for the box and arrow
//
// ### Example Output:
//   ```
//   background-color: blue;
//   color: white;
//   border-color: gray;
//
//   .popper-arrow {
//     border-color: gray;
//
//     &::before {
//       border-color: blue;
//     }
//   }
//   ```
//
// Styleguide: Adde Pop - Utils - Mixins - @arrow-box-color-variation()

// Dev note
// ------------------------------------
// All of this could be further simplified by use of CSS custom properties
// for the arrow coloring, but there is no support in IE (except for lastest
// version of Edge, but even that does not support pseudo elements, which we would need)
@mixin arrow-box-color-variation($background-color, $text-color, $border-color: false) {
  background-color: $background-color;
  color: $text-color;

  $has-border: non-zero-value($border-color);
  @if $has-border {
    border-color: $border-color;
  }

  .popper-arrow {
    @if $has-border {
      border-color: adjust-arrow-border-color($border-color);

      &::before {
        border-color: $background-color;
      }
    } @else {
      border-color: $background-color;
    }
  }
}

// update-arrow-color()
//
// Used when a specific arrow needs to be updated from the defaults,
// such as for when the arrow needs to match something in the box, like the header.
// Note that this uses BOX position, not the visible arrow position.
//
// ### Example Usage:
//
//   ```
//   @include update-arrow-color('left', blue, gray);
//   ```
//
// ### Parameters:
//
// * $box-position = The current position of the box you want to target (NOT the arrow). ('left', 'right', 'top', 'bottom', 'left-start', 'right-start'...etc)
// * $background-color = New background color for the arrow
// * $border-color = New border color for the arrow
//
// ### Example Output:
//   ```
//   .popper-arrow {
//     border-left-color: gray;
//
//     &::before {
//       border-left-color: blue;
//     }
//   }
//   ```
//
// Styleguide: Adde Pop - Utils - Mixins - @update-arrow-color()

// Dev note
// ------------------------------------
// All of this could be further simplified by use of CSS custom properties
// for the arrow coloring, but there is no support in IE (except for lastest
// version of Edge, but even that does not support pseudo elements, which we would need)
@mixin update-arrow-color($box-position, $background-color, $border-color: false) {
  $has-border: non-zero-value($border-color);

  .popper-arrow {
    @if $has-border {
      border-#{$box-position}-color: adjust-arrow-border-color($border-color);

      &::before {
        border-#{$box-position}-color: $background-color;
      }
    } @else {
      border-#{$box-position}-color: $background-color;
    }
  }
}

// @create-offset-box-shadow()
//
// Creates an offset box-shadow property,
// which makes one side of the box shadow smaller than the others.
//
// ### Example Usage:
//
//   ```
//   @include arrow-box("bottom", 3px, 3px, 5px, rgba(175, 175, 175, 0.1));
//   ```
//
// ### Parameters:
//
// * $direction = The direction/placement of the popper
// * $x-offset = horizontal placement offset on the x axis
// * $y-offset = vertical placement offset on the y axis
// * $spread = shadow spread
// * $color = shadow color
//
// ### Example Output:
//   ```
//   box-shadow: 0 0 3px rgba(175, 175, 175, 0.1),
//               -3px 3px 5px rgba(175, 175, 175, 0.1),
//               3px 3px 5px rgba(175, 175, 175, 0.1);
//   ```
//
// Styleguide: Adde Pop - Utils - Mixins - @create-offset-box-shadow()

@mixin create-offset-box-shadow($direction, $x-offset, $y-offset, $spread, $color) {
  $left-top:      #{-$x-offset}   #{-$y-offset};
  $left-bottom:   #{-$x-offset}   $y-offset;
  $right-top:     $x-offset       #{-$y-offset};
  $right-bottom:  $x-offset       $y-offset;

  $side-1: '';
  $side-2: '';

  @if $direction == 'bottom' {
    $side-1: $left-bottom;
    $side-2: $right-bottom;
  }
  @else if $direction == 'top' {
    $side-1: $left-top;
    $side-2: $right-top;
  }
  @else if $direction == 'left' {
    $side-1: $left-bottom;
    $side-2: $left-top;
  }
  @else if $direction == 'right' {
    $side-1: $right-bottom;
    $side-2: $right-top;
  }

  box-shadow: 0 0 $spread $color,
  $side-1 $spread $color,
  $side-2 $spread $color;
}
