/*
 * Search field for finding a module.
 *
 * HTML:
 *
 * <form className='this-is-the-module-we-mixin-for'>
 *   <input type='search' ... />
 *   <button type='reset' className='reset'><i className='icon-close'/></button>
 * </div>
 */
@mixin module-search-field {
  position: relative;

  input {
    // left padding for "search" icon; right padding for "close" button
    padding: 0.5rem 3.4rem 0.5rem;
    display: block;
    width: 100%;
    color: $brand-text--dark;
    background: white;
    border-radius: 2rem;
    border: 2px solid transparent;

    &:focus {
      border-color: $brand-orange--medium;
    }

    //search placeholder when empty
    &::placeholder {
      // TODO white+opacity is not white. Do not use opacity: figure out the correct color and write it opaquely.
      color: $placeholder;
      font-weight: bold;
      opacity: 0.9;
    }
  }

  &::before {
    // "search" icon
    display: inline-block;
    position: absolute;
    top: 0.7rem;
    left: 1.4rem;
    font-size: 1.6rem;
    font-family: 'Workbench';
    color: $item-border;
    content: '\e920'; // .icon-search
    opacity: 1;
  }

  button.reset {
    border: 0;
    background: transparent;
    position: absolute;
    right: 1.4rem;
    top: 0.9rem;
    font-size: 1.4rem;
    margin: 0;
    padding: 0;
    text-shadow: none;
    color: $placeholder;
    opacity: 0;
    &:hover {
      color: $brand-text--dark;
      opacity: 1;
    }
  }

  &:hover button.reset {
    opacity: 0.5;
  }
}

.module-search-popper {
  z-index: $zindex-popover;
  $row-height: 4rem;
  $padding-horizontal: 2rem;

  position: absolute; // so Popper can position it
  top: 0; // Popper needs this during preventOverflow calculations
  left: 0; // Popper needs this during preventOverflow calculations

  // make sure we never get taller than the max-height Popper.js sets on us.
  // height: 100% won't do. Flex will.
  display: flex;
  flex-direction: column;

  // We'll add box-shadow here. The border-radius is
  // specified in .module-search-result-groups; this one has no effect except
  // to render the box-shadow correctly.

  box-shadow: $overlay-shadow;

  .module-search-popup {
    // never get taller than the (correctly-sized) .module-search-popper
    display: flex;
    flex-direction: column;
    max-height: 80%;
    overflow: hidden;
  }

  .module-search-popup {
    // position: absolute is set by Popper
    @include content-1;
    box-shadow: $overlay-shadow;
    border: 1px solid $item-border;
    border-radius: 0.5rem;
  }

  // Make scrollbar always visible on Mac, as user tests show they don't know there's more menu otherwise
  ::-webkit-scrollbar {
    -webkit-appearance: none;
    width: 7px;
  }
  ::-webkit-scrollbar-thumb {
    border-radius: 5px;
    background-color: $light-gray;
  }

  ul,
  li {
    // reset styles. (we'll re-change this later.)
    display: block;
    margin: 0;
    padding: 0;
  }

  &[data-popper-placement='top'] {
    // Backwards case: there isn't enough room below the module to render a list of
    // search results. So we'll display the search results _above_ the module ...
    // and therefore we show the <input> at the bottom, where the button is.

    // Search <input> appears at the _bottom_ of the search popup
    .module-search-field {
      order: 2;
      border-bottom-left-radius: 0.5rem;
      border-bottom-right-radius: 0.5rem;
    }

    .module-search-result-groups {
      order: 1;
      border-top-left-radius: 0.5rem;
      border-top-right-radius: 0.5rem;
      border-bottom: 0px;
    }
  }

  &[data-popper-placement='bottom'] {
    // Search <input> appears at the _top_ of the search popup
    .module-search-field {
      border-top-left-radius: 0.5rem;
      border-top-right-radius: 0.5rem;
    }

    .module-search-result-groups {
      border-bottom-left-radius: 0.5rem;
      border-bottom-right-radius: 0.5rem;
      border-top: 0px;
    }
  }

  .module-search-field {
    flex: 0 0 auto;
    background: white;
    padding: 1rem;
    color: $input-alt;
    box-shadow: $element-shadow;
    @include module-search-field;

    input {
      border-color: $item-border;
      background-color: $dark-accent--soft;
    }

    &::before {
      // padding:1rem means we need to move icon-search by 1rem
      margin-top: 1rem;
      margin-left: 1rem;
      color: $light-gray;
    }

    button.reset {
      // padding:1rem means we need to move icon-close by 1rem
      margin-top: 1rem;
      margin-right: 1rem;
    }
  }

  .lesson-highlight input[type='search'] {
    border-color: $brand-learn;
  }

  .module-search-result-groups {
    flex: 1 1 auto;
    overflow-y: auto;

    //border: 1px solid $heading-bg;

    // background-color goes here, NOT .module-search-popup. That's because this
    // element has a border-radius (the rule is elsewhere); its parent can't have
    // a background or it would appear behind the curved border.
    background-color: $white;
  }

  .module-search-result-group {
    margin-bottom: 1.5rem;

    li {
      margin: 0;
      padding: 0;
      display: block;
    }
    &:not(:first-child) {
      border-top: 1px solid $dark-accent--soft-M;
    }
  }

  h4 {
    @include info-2;
    margin: 0;
    padding: $padding-horizontal;
    line-height: 1;
    color: $brand-text--medium;
    text-transform: uppercase;
  }

  button.module-search-result {
    display: block;
    margin: 0;
    padding: 0 $padding-horizontal;
    appearance: none;
    -webkit-appearance: none;
    -moz-appearance: none;
    height: $row-height;
    width: 100%;
    display: flex;
    align-items: center;
    border: 2px solid transparent;
    color: $brand-text--dark;
    cursor: pointer;
    background: transparent;
    box-shadow: none;

    i {
      width: 1.5rem;
      margin-right: 1rem;
    }

    &:hover {
      color: $brand-text--darker;
      background-color: $dark-accent--soft;
    }

    &:active {
      background-color: $dark-accent--medium;
    }

    &.lesson-highlight {
      background-color: $brand-learn--medium;
    }
  }
}

.module-search-result-description {
  position: absolute; // so Popper can position it
  top: 0; // Popper needs this during preventOverflow calculations
  left: 0; // Popper needs this during preventOverflow calculations

  border: $module-popover-border-width solid $brand-orange;
  border-radius: 5px;
  background: white;
  box-shadow: $menu-shadow;
  width: 18rem;
  padding: 1rem;
  z-index: $zindex-popover; // show up above .module-search-popper

  h3 {
    @include content-1;
    padding: 0;
    margin: 0 0 0.5rem;
    border: 0;
    background: none;
    color: $brand-text--dark;
  }

  p {
    @include info-3;
    margin: 0;
    padding: 0;
  }

  .arrow {
    // https://popper.js.org/docs/v2/tutorial/#arrow
    // ... but we'll show up on top, not behind, to look fancy
    &,
    &::before {
      position: absolute;
      width: 1.2rem;
      height: 1.2rem;
    }

    left: -0.6rem;

    &::before {
      content: '';
      transform: rotate(45deg);
      background: white;
      border: $module-popover-border-width solid $brand-orange;
      border-top-color: transparent;
      border-right-color: transparent;
    }
    &::after {
      left: $module-popover-border-width;
      border-width: 8px 8px 8px 0;
    }
  }
}
