/*
*
* Select 2
* --------------------------------------------------
*/


// Mixins
// -------------------------

@mixin input-defaults {
  background-color: $input-bg;
  border: 1px solid $input-border;
  border-radius: $input-border-radius;
  color: $input-color;
  font-size: $input-font-size;
  font-weight: $input-font-weight;
  line-height: $input-line-height;
}

@mixin validation-state-focus($color) {
  $shadow: inset 0 1px 1px rgba(0, 0, 0, .075), 0 0 6px lighten($color, 20%);

  &.select2-drop-active {
    border-color: darken($color, 10%);

    &.select2-drop.select2-drop-above {
      border-top-color: darken($color, 10%);
    }
  }
}

// dropdown arrow when dropdown is open
@mixin dropdown-arrow {
  .select2-selection--single {
    /**
     * Make the dropdown arrow point up while the dropdown is visible.
     */

    .select2-selection__arrow b {
      border-color: transparent transparent $gray-dark transparent;
      border-width: 0 $caret-width-large $caret-width-large $caret-width-large;
    }
  }
}

// Base Styles
// -------------------------

.select2 {
  width: 100% !important;
}

.select2-container {
  box-sizing: border-box;
  display: inline-block;
  margin: 0;
  position: relative;
  vertical-align: middle;

  .select2-selection--single {
    box-sizing: border-box;
    cursor: pointer;
    display: block;
    height: 50px;
    user-select: none;
    -webkit-user-select: none;

    .select2-selection__rendered {
      display: block;
      padding-left: 20px;
      padding-right: 20px;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }

    .select2-selection__clear {
      position: relative;
    }
  }

  &[dir="rtl"] {
    .select2-selection--single {
      .select2-selection__rendered {
        padding-left: 20px;
        padding-right: 20px;
      }
    }
  }

  .select2-selection--multiple {
    box-sizing: border-box;
    cursor: pointer;
    display: block;
    min-height: 32px;
    user-select: none;
    -webkit-user-select: none;

    .select2-selection__rendered {
      display: inline-block;
      overflow: hidden;
      padding-left: 20px;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
  }

  .select2-search--inline {
    float: left;

    .select2-search__field {
      box-sizing: border-box;
      border: none;
      margin-top: 5px;
      padding: 0;

      &::-webkit-search-cancel-button {
        -webkit-appearance: none;
      }
    }
  }

}

.select2-dropdown {
  position: absolute;
  margin-top: 2px;
  left: -100000px;
  z-index: 16;
  display: block;
  box-sizing: border-box;
  width: 100%;
  background-color: $body-bg;
  border: 1px solid $secondary-6;
  border-radius: $input-border-radius;
  box-shadow: $shadow-area-sm;
}

.select2-results {
  display: block;
}

.select2-results__options {
  padding: 10px 0;
  list-style: none;
  margin: 0;
}

.select2-results__option {
  padding: 10px 20px;
  font-size: 18px;
  transition: .3s;
  user-select: none;
  color: $gray-darker;
  -webkit-user-select: none;

  &[aria-selected] {
    cursor: pointer;
  }
}

.select2-container--open .select2-dropdown {
  left: 0;
}

.select2-search--dropdown {
  display: block;
  padding: 4px;

  .select2-search__field {
    width: 100%;
    padding: 4px;
    box-sizing: border-box;

    &::-webkit-search-cancel-button {
      -webkit-appearance: none;
    }
  }

  &.select2-search--hide {
    display: none;
  }
}

.select2-close-mask {
  border: 0;
  margin: 0;
  padding: 0;
  display: block;
  position: fixed;
  left: 0;
  top: 0;
  min-height: 100%;
  min-width: 100%;
  height: auto;
  width: auto;
  opacity: 0;
  z-index: 99;
  background-color: $white;
  filter: alpha(opacity=0);
}

.select2-hidden-accessible {
  border: 0 !important;
  clip: rect(0 0 0 0) !important;
  height: 1px !important;
  margin: -1px !important;
  overflow: hidden !important;
  padding: 0 !important;
  position: absolute !important;
  width: 1px !important;
}

.select2 + .form-validation {
  z-index: 14;
}

// Select 2 Bootstrap Theme
// -------------------------

.select2-container--bootstrap {
  display: block;

  .select2-selection {
    @include input-defaults;
    outline: 0;
  }

  // Search field in the Select2 dropdown.
  .select2-search--dropdown {
    .select2-search__field {
      @include input-defaults;
    }
  }

  // No outline for all search fields - in the dropdown
  // and inline in multi Select2s.
  .select2-search__field {
    outline: 0;
    font-size: $input-font-size;

    &::-webkit-input-placeholder {
      color: $input-color-placeholder;
    }

    &:-moz-placeholder {
      color: $input-color-placeholder;
    }

    &::-moz-placeholder {
      color: $input-color-placeholder;
      opacity: 1;
    }

    &:-ms-input-placeholder {
      color: $input-color-placeholder;
    }
  }

  // Adjust Select2's choices hover and selected styles to match
  // Bootstrap 3's default dropdown styles.
  .select2-results__option {

    &[role=group] {
      padding: 0;
    }

    // Disabled results.
    &[aria-disabled=true] {
      color: $gray-light;
      cursor: not-allowed;
    }

    // Hover state.
    &[aria-selected=true] {
      color: $gray-1;
    }

    // Selected state.
    &--highlighted[aria-selected] {
      color: $secondary;
    }

    .select2-results__option {
      padding: 10px 20px;

      .select2-results__group {
        padding-left: 0;
      }
 
      .select2-results__option {
        margin-left: -15px;
        padding-left: 30px;
      }
    }
  }

  .select2-results__group {
    color: $gray-light;
    display: block;
    padding: 10px 15px;
    font-weight: 700;
    font-size: 16px;
    line-height: (30/16);
    white-space: nowrap;
  }

  &.select2-container--focus,
  &.select2-container--open {
    .select2-selection {
      transition: border-color ease-in-out .15s, box-shadow ease-in-out .15s;
    }
  }

  &.select2-container--open {

    // Make the dropdown arrow point up while the dropdown is visible.
    .select2-selection .select2-selection__arrow b {
      border-color: transparent transparent $primary transparent;
      border-width: 0 5px 5px 5px;
    }
  }

  // Clear the selection.
  .select2-selection__clear {
    color: $gray-dark;
    cursor: pointer;
    float: right;
    font-weight: 400;
    margin-right: 10px;

    &:hover {
      color: $primary;
    }
  }

  // Address disabled Select2 styles.
  &.select2-container--disabled {

    .select2-selection {
      border-color: #ccc;
      box-shadow: none;
    }

    .select2-selection,
    .select2-search__field {
      cursor: not-allowed;
    }

    .select2-selection,
    .select2-selection--multiple .select2-selection__choice {
      background-color: $gray-lighter;
    }

    .select2-selection__clear,
    .select2-selection--multiple .select2-selection__choice__remove {
      display: none;
    }
  }

  // Dropdown border color and box-shadow.
  .select2-dropdown {
    overflow-x: hidden;
  }

  // Limit the dropdown height.
  .select2-results > .select2-results__options {
    max-height: 215px;
    overflow-y: auto;
  }

  // Single select
  .select2-selection--single {
    text-align: left;
    height: 60px;
    line-height: 59px;
    padding: 0 44px 0 24px;
  
    // Adjust the single Select2's dropdown arrow button appearance.
    .select2-selection__arrow {
      position: absolute;
      bottom: 0;
      right: 15px;
      top: 0;

      b {
        &:before {
          position: absolute;
          top: 50%;
          font-weight: 400;
          margin-top: -10px;
          right: 0;
          height: 20px;
          line-height: 20px;
          content: '\f236';
          font-family: 'Material Design Icons';
          font-size: 20px;
        }
      }
    }

    .select2-selection__rendered {
      color: $gray;
      padding: 0;
    }

    .select2-selection__placeholder {
      color: $gray-light;
    }
  }

  // Mutiple
  .select2-selection--multiple {
    min-height: 50px;

    .select2-selection__rendered {
      box-sizing: border-box;
      display: block;
      line-height: 24px;
      list-style: none;
      margin: 0;
      overflow: hidden;
      padding: 0;
      width: 100%;
      text-overflow: ellipsis;
      white-space: nowrap;
    }

    .select2-selection__placeholder {
      color: $gray-light;
      float: left;
      margin-top: 5px;
    }

    // Make Multi Select2's choices match Bootstrap 3's default button styles.
    .select2-selection__choice {
      color: $input-color;
      background: $black;
      border: 1px solid $black;
      border-radius: 3px;
      cursor: default;
      float: left;
      margin: 0;
      padding: 0 15px;
    }

    // Minus 2px borders.
    .select2-search--inline {
      .select2-search__field {
        background: transparent;
        padding: 0 15px;
        height: 50px - 2;
        line-height: 24px;
        margin-top: 0;
        min-width: 5em;
      }
    }

    .select2-selection__choice__remove {
      color: $gray-dark;
      cursor: pointer;
      display: inline-block;
      font-weight: bold;
      margin-right: 10px / 2;

      &:hover {
        color: $primary;
      }
    }

    // Clear the selection.
    .select2-selection__clear {
      margin-top: 10px;
    }
  }

  // RTL SUPPORT
  &[dir="rtl"] {

    // Single Select2
    .select2-selection--single {
      padding-left: 15px + 4px *3;
      padding-right: 15px;
      .select2-selection__rendered {
        padding-right: 0;
        padding-left: 0;
        text-align: right; /* 1 */
      }

      .select2-selection__clear {
        float: left;
      }

      .select2-selection__arrow {
        left: 15px;
        right: auto;

        b {
          margin-left: 0;
        }
      }
    }

    // Multiple Select2
    .select2-selection--multiple {
      .select2-selection__choice,
      .select2-selection__placeholder {
        float: right;
      }

      .select2-selection__choice {
        margin-left: 0;
        margin-right: 15px/2;
      }

      .select2-selection__choice__remove {
        margin-left: 2px;
        margin-right: auto;
      }
    }
  }
}

// Address Bootstrap's validation states
.has-warning {
  @include validation-state-focus(#8a6d3b);
}

.has-error {
  @include validation-state-focus(#a94442);
}

.has-success {
  @include validation-state-focus(#3c763d);
}

// Select2 widgets in Bootstrap Input Groups
.input-group .select2-container--bootstrap {
  display: table;
  table-layout: fixed;
  position: relative;
  z-index: 2;
  float: left;
  width: 100%;
  margin-bottom: 0;
}

.input-group.select2-bootstrap-prepend .select2-container--bootstrap {
  .select2-selection {
    order-bottom-left-radius: 0;
    border-top-left-radius: 0;
  }
}

.input-group.select2-bootstrap-append .select2-container--bootstrap {
  .select2-selection {
    order-bottom-right-radius: 0;
    border-top-right-radius: 0;
  }
}

// Adjust alignment of Bootstrap buttons in Bootstrap Input Groups to address
.select2-bootstrap-append,
.select2-bootstrap-prepend {
  .select2-container--bootstrap,
  .input-group-btn,
  .input-group-btn .btn {
    vertical-align: top;
  }
}

// Temporary fix for https://github.com/select2/select2-bootstrap-theme/issues/9
.form-control.select2-hidden-accessible {
  position: absolute !important;
  width: 1px !important;
}

// Display override for inline forms
.form-inline .select2-container--bootstrap {
  display: inline-block;
}