//
// Copyright IBM Corp. 2016, 2018
//
// This source code is licensed under the Apache-2.0 license found in the
// LICENSE file in the root directory of this source tree.
//

//-----------------------------
// List Box
//-----------------------------

@import '../../globals/scss/vars';
@import '../../globals/scss/css--helpers';
@import '../../globals/scss/helper-mixins';
@import '../../globals/scss/layout';
@import '../../globals/scss/vendor/@carbon/elements/scss/import-once/import-once';
@import '../../globals/scss/css--reset';

/// @type Number
/// @access private
/// @group list-box
$list-box-width: 100%;

/// @type Number
/// @access private
/// @group list-box
$list-box-height: rem(40px);

/// @type Number
/// @access private
/// @group list-box
$list-box-inline-height: $list-box-height;

/// @type Number
/// @access private
/// @group list-box
$list-box-menu-width: rem(300px);

/// List box styles
/// @access private
/// @group list-box
@mixin listbox {
  // The overall container element for a `list-box`. Has two variants,
  // `disabled` and `inline`.
  .#{$prefix}--list-box__wrapper--inline {
    display: inline-grid;
    grid-gap: rem(4px);
    grid-template: auto auto / auto auto;
    align-items: center;

    .#{$prefix}--label {
      @include type-style('body-short-01');
    }

    .#{$prefix}--label,
    .#{$prefix}--form__helper-text,
    .#{$prefix}--form-requirement {
      margin: 0;
    }

    .#{$prefix}--form__helper-text {
      max-width: none;
    }

    .#{$prefix}--form-requirement {
      grid-column: 2;
    }
  }

  .#{$prefix}--list-box {
    @include reset;

    position: relative;
    width: $list-box-width;
    height: rem(40px);
    max-height: rem(40px);
    color: $text-01;
    background-color: $field-01;
    border: none;
    border-bottom: 1px solid $ui-04;
    cursor: pointer;
    transition: all $duration--fast-01 motion(standard, productive);

    &:hover {
      background-color: $hover-ui;
    }
  }

  .#{$prefix}--list-box--xl {
    height: rem(48px);
    max-height: rem(48px);
  }

  .#{$prefix}--list-box--sm {
    height: rem(32px);
    max-height: rem(32px);
  }

  .#{$prefix}--list-box--expanded {
    border-bottom-color: $ui-03;
  }

  .#{$prefix}--list-box--expanded:hover {
    background-color: $field-01;
  }

  .#{$prefix}--list-box--expanded:hover.#{$prefix}--list-box--light:hover {
    background-color: $field-02;
  }

  .#{$prefix}--list-box .#{$prefix}--text-input {
    height: 100%;
  }

  // invalid states
  .#{$prefix}--list-box__invalid-icon {
    position: absolute;
    top: 50%;
    right: $carbon--spacing-08;
    transform: translateY(-50%);
    fill: $support-01;
  }

  .#{$prefix}--list-box[data-invalid] .#{$prefix}--list-box__field {
    padding-right: carbon--mini-units(8);
    border-bottom: 0;
  }

  .#{$prefix}--list-box[data-invalid].#{$prefix}--list-box--inline
    .#{$prefix}--list-box__field {
    padding-right: carbon--mini-units(7);
  }

  // Light variation for 'list-box'
  .#{$prefix}--list-box--light {
    background-color: $field-02;
  }

  .#{$prefix}--list-box--light .#{$prefix}--list-box__menu {
    background: $field-02;
  }

  .#{$prefix}--list-box--light .#{$prefix}--list-box__menu-item__option {
    border-top-color: $decorative-01;
  }

  .#{$prefix}--list-box--light.#{$prefix}--list-box--expanded {
    border-bottom-color: transparent;
  }

  // Disabled state for `list-box`
  .#{$prefix}--list-box--disabled:hover {
    background-color: $field-01;
  }

  .#{$prefix}--list-box--light.#{$prefix}--list-box--disabled {
    background-color: $field-02;
  }

  .#{$prefix}--list-box--disabled,
  .#{$prefix}--list-box--disabled .#{$prefix}--list-box__field,
  .#{$prefix}--list-box--disabled .#{$prefix}--list-box__field:focus {
    border-bottom-color: transparent;
    outline: none;
  }

  .#{$prefix}--list-box--disabled .#{$prefix}--list-box__label,
  .#{$prefix}--list-box--disabled.#{$prefix}--list-box--inline
    .#{$prefix}--list-box__label {
    color: $disabled-02;
  }

  .#{$prefix}--list-box--disabled .#{$prefix}--list-box__menu-icon > svg {
    fill: $disabled-02;
  }

  .#{$prefix}--list-box--disabled,
  .#{$prefix}--list-box--disabled .#{$prefix}--list-box__field,
  .#{$prefix}--list-box--disabled .#{$prefix}--list-box__menu-icon {
    cursor: not-allowed;
  }

  .#{$prefix}--list-box--disabled .#{$prefix}--list-box__menu-item,
  .#{$prefix}--list-box--disabled .#{$prefix}--list-box__menu-item:hover,
  .#{$prefix}--list-box--disabled
    .#{$prefix}--list-box__menu-item--highlighted {
    color: $disabled-02;
    text-decoration: none;
  }

  .#{$prefix}--list-box--disabled .#{$prefix}--list-box__selection:hover {
    cursor: not-allowed;
  }

  // disabled && invalid
  .#{$prefix}--list-box--disabled.#{$prefix}--list-box[data-invalid]
    .#{$prefix}--list-box__field {
    padding-right: $carbon--spacing-09;
  }

  .#{$prefix}--list-box--disabled.#{$prefix}--list-box[data-invalid].#{$prefix}--list-box--inline
    .#{$prefix}--list-box__field {
    padding-right: carbon--mini-units(4);
  }

  // Inline variant for a `list-box`
  .#{$prefix}--list-box.#{$prefix}--list-box--inline {
    background-color: transparent;
    border-width: 0;

    &:hover {
      background-color: $hover-ui;
    }
  }

  .#{$prefix}--list-box.#{$prefix}--list-box--inline.#{$prefix}--list-box--expanded {
    border-bottom-width: 0;
  }

  .#{$prefix}--list-box.#{$prefix}--list-box--inline.#{$prefix}--list-box--expanded
    .#{$prefix}--list-box__field[aria-expanded='true'] {
    border-width: 0;
  }

  .#{$prefix}--list-box.#{$prefix}--list-box--inline.#{$prefix}--list-box--disabled:hover {
    background-color: transparent;
  }

  .#{$prefix}--list-box.#{$prefix}--list-box--inline.#{$prefix}--list-box--expanded:hover {
    background-color: $field-02;
  }

  .#{$prefix}--list-box.#{$prefix}--list-box--inline
    .#{$prefix}--list-box__field {
    padding: 0 carbon--mini-units(4) 0 $carbon--spacing-03;
  }

  .#{$prefix}--list-box.#{$prefix}--list-box--inline
    .#{$prefix}--list-box__menu-icon {
    right: $carbon--spacing-03;
  }

  .#{$prefix}--list-box.#{$prefix}--list-box--inline
    .#{$prefix}--list-box__invalid-icon {
    right: $carbon--spacing-07;
  }

  .#{$prefix}--list-box--inline .#{$prefix}--list-box__label {
    color: $text-01;
  }

  .#{$prefix}--list-box--inline .#{$prefix}--list-box__field {
    height: 100%;
  }

  .#{$prefix}--dropdown--inline .#{$prefix}--list-box__field {
    max-width: rem(480px);
  }

  .#{$prefix}--dropdown--inline .bx--list-box__menu {
    min-width: rem(288px);
    max-width: rem(480px);
  }

  // The field we use for input, showing selection, etc.
  .#{$prefix}--list-box__field {
    @include button-reset;

    position: relative;
    display: inline-flex;
    align-items: center;
    // Account for the border in `.bx--list-box`
    height: calc(100% + 1px);
    padding: 0 $carbon--spacing-09 0 $carbon--spacing-05;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    vertical-align: top;
    outline: none;
    cursor: pointer;
  }

  .#{$prefix}--list-box__field:focus {
    @include focus-outline('outline');
  }

  .#{$prefix}--list-box__field[disabled] {
    color: $disabled-02;
    outline: none;
  }

  // populated input field
  .#{$prefix}--list-box__field .#{$prefix}--text-input {
    padding-right: carbon--mini-units(9);
  }

  // invalid && populated input field
  .#{$prefix}--list-box[data-invalid]
    .#{$prefix}--list-box__field
    .#{$prefix}--text-input {
    // to account for clear input button outline
    padding-right: rem(98px);
  }

  .#{$prefix}--list-box[data-invalid]
    .#{$prefix}--list-box__field
    .#{$prefix}--text-input
    + .#{$prefix}--list-box__invalid-icon {
    // to account for clear input button outline
    right: rem(66px);
  }

  // empty input field
  .#{$prefix}--list-box__field .#{$prefix}--text-input--empty {
    padding-right: $carbon--spacing-09;
  }

  // invalid && empty input field
  .#{$prefix}--list-box[data-invalid]
    .#{$prefix}--list-box__field
    .#{$prefix}--text-input--empty {
    padding-right: carbon--mini-units(9);
  }

  .#{$prefix}--list-box[data-invalid]
    .#{$prefix}--list-box__field
    .#{$prefix}--text-input--empty
    + .#{$prefix}--list-box__invalid-icon {
    // to account for clear input button outline
    right: rem(40px);
  }

  // Label for a `list-box__field`
  .#{$prefix}--list-box__label {
    @include type-style('body-short-01');

    overflow: hidden;
    color: $text-01;
    white-space: nowrap;
    text-overflow: ellipsis;
    user-select: none;
  }

  // Menu status inside of a `list-box__field`
  .#{$prefix}--list-box__menu-icon {
    position: absolute;
    top: 0;
    right: $carbon--spacing-05;
    display: flex;
    align-items: center;
    height: 100%;
    cursor: pointer;
    transition: transform $duration--fast-01 motion(standard, productive);
  }

  .#{$prefix}--list-box__menu-icon > svg {
    height: 100%;
    fill: $icon-01;
  }

  .#{$prefix}--list-box__menu-icon--open {
    transform: rotate(180deg);
  }

  // Selection indicator for a `list-box__field`
  .#{$prefix}--list-box__selection {
    position: absolute;
    top: 50%;
    /* to preserve .5rem space between icons according to spec top/transform used to center the combobox clear selection icon in IE11 */
    right: rem(33px);
    display: flex;
    align-items: center;
    justify-content: center;
    width: rem(30px);
    height: rem(30px);
    transform: translateY(-50%);
    cursor: pointer;
    transition: background-color $duration--fast-01 motion(standard, productive);
    user-select: none;

    &:focus {
      @include focus-outline('outline');
    }
  }

  .#{$prefix}--list-box__selection > svg {
    fill: $icon-02;
  }

  .#{$prefix}--list-box--disabled .#{$prefix}--list-box__selection:focus {
    outline: none;
  }

  // Modifier for a selection to show that multiple selections have been made
  .#{$prefix}--list-box__selection--multi {
    @include type-style('label-01');

    position: static;
    top: auto;
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: auto;
    height: rem(24px);
    margin-right: rem(10px);
    padding: rem(8px);
    // Align with hover circle of X button
    padding-right: rem(2px);
    color: $inverse-01;
    line-height: 0;
    background-color: $inverse-02;
    border-radius: rem(12px);
    transform: none;
  }

  .#{$prefix}--list-box__selection--multi > svg {
    width: rem(20px);
    height: rem(20px);
    margin-left: rem(4px);
    padding: rem(2px);
    fill: $inverse-01;

    &:hover {
      background-color: $hover-secondary;
      border-radius: 50%;
    }
  }

  .#{$prefix}--list-box--disabled
    .#{$prefix}--list-box__selection--multi
    > svg {
    fill: $disabled-02;

    &:hover {
      background-color: initial;
    }
  }

  .#{$prefix}--list-box__selection--multi:focus,
  .#{$prefix}--list-box__selection--multi:hover {
    outline: none;
  }

  // Descendant of a `list-box` that displays a list of options to select
  .#{$prefix}--list-box__menu {
    @include box-shadow();

    position: absolute;
    right: 0;
    left: 0;
    z-index: z('dropdown');
    width: $list-box-width;
    overflow-y: auto;
    background-color: $ui-01;
    transition: max-height $duration--fast-02 motion(standard, productive);

    &:focus {
      // remove default browser focus in firefox
      @include focus-outline('border');
    }
  }

  .#{$prefix}--list-box
    .#{$prefix}--list-box__field[aria-expanded='false']
    + .#{$prefix}--list-box__menu {
    max-height: 0;
  }

  .#{$prefix}--list-box--expanded .#{$prefix}--list-box__menu {
    // 40px item height * 5.5 items shown
    max-height: rem(220px);
  }

  .#{$prefix}--list-box--expanded.#{$prefix}--list-box--xl
    .#{$prefix}--list-box__menu {
    // 48px item height * 5.5 items shown
    max-height: rem(264px);
  }

  .#{$prefix}--list-box--expanded.#{$prefix}--list-box--sm
    .#{$prefix}--list-box__menu {
    // 32px item height * 5.5 items shown
    max-height: rem(176px);
  }

  // Descendant of a `list-box__menu` that represents a selection for a control
  .#{$prefix}--list-box__menu-item {
    @include type-style('body-short-01');

    position: relative;
    height: rem(40px);
    color: $text-02;
    cursor: pointer;
    transition: background $duration--fast-01 motion(standard, productive);
    user-select: none;

    &:hover {
      background-color: $hover-ui;
    }

    &:active {
      background-color: $selected-ui;
    }
  }

  .#{$prefix}--list-box--sm .#{$prefix}--list-box__menu-item {
    height: rem(32px);
  }

  .#{$prefix}--list-box--xl .#{$prefix}--list-box__menu-item {
    height: rem(48px);
  }

  .#{$prefix}--list-box--disabled .#{$prefix}--list-box__menu-item:hover {
    background-color: transparent;
  }

  .#{$prefix}--list-box--light .#{$prefix}--list-box__menu-item:active {
    background-color: $selected-light-ui;
  }

  .#{$prefix}--list-box--disabled
    .#{$prefix}--list-box__menu-item__option:hover {
    border-top-color: $ui-03;
  }

  .#{$prefix}--list-box__menu-item:first-of-type
    .#{$prefix}--list-box__menu-item__option {
    border-top-color: transparent;
  }

  .#{$prefix}--list-box__menu-item:hover
    .#{$prefix}--list-box__menu-item__option {
    color: $text-01;
  }

  .#{$prefix}--list-box__menu-item:hover
    + .#{$prefix}--list-box__menu-item
    .#{$prefix}--list-box__menu-item__option {
    border-top-color: transparent;
  }

  .#{$prefix}--list-box--disabled
    .#{$prefix}--list-box__menu-item:hover
    + .#{$prefix}--list-box__menu-item
    .#{$prefix}--list-box__menu-item__option {
    border-top-color: $ui-03;
  }

  .#{$prefix}--list-box__menu-item__option {
    @include focus-outline('reset');

    display: block;
    height: rem(40px);
    margin: 0 $carbon--spacing-05;
    padding: rem(11px) 0;
    padding-right: $carbon--spacing-06;
    overflow: hidden;
    color: $text-02;
    font-weight: normal;
    line-height: 1rem;
    white-space: nowrap;
    text-decoration: none;
    text-overflow: ellipsis;
    border-top: 1px solid transparent;
    border-top-color: $ui-03;
    border-bottom: 1px solid transparent;
    transition: border-color $duration--fast-01 motion(standard, productive),
      color $duration--fast-01 motion(standard, productive);

    &:focus {
      @include focus-outline('outline');

      margin: 0;
      padding: rem(11px) rem(16px);
      border-color: transparent;
    }

    &:hover {
      color: $text-01;
      border-color: transparent;
    }
  }

  .#{$prefix}--list-box--sm .#{$prefix}--list-box__menu-item__option {
    height: rem(32px);
    padding-top: rem(7px);
    padding-bottom: rem(7px);
  }

  .#{$prefix}--list-box--xl .#{$prefix}--list-box__menu-item__option {
    height: rem(48px);
    padding-top: rem(15px);
    padding-bottom: rem(15px);
  }

  .#{$prefix}--list-box--disabled
    .#{$prefix}--list-box__menu-item:hover
    .#{$prefix}--list-box__menu-item__option,
  .#{$prefix}--list-box--disabled .#{$prefix}--list-box__menu-item__option {
    color: $disabled-02;
  }

  .#{$prefix}--list-box.#{$prefix}--list-box--inline
    .#{$prefix}--list-box__menu-item__option {
    margin: 0 $carbon--spacing-03;

    &:focus {
      margin: 0;
      padding-right: $carbon--spacing-03;
      padding-left: $carbon--spacing-03;
    }
  }

  .#{$prefix}--list-box__menu-item--highlighted {
    color: $text-01;
    background-color: $hover-ui;
    border-color: transparent;
  }

  .#{$prefix}--list-box__menu-item--highlighted
    .#{$prefix}--list-box__menu-item__option,
  .#{$prefix}--list-box__menu-item--highlighted
    + .#{$prefix}--list-box__menu-item
    .#{$prefix}--list-box__menu-item__option {
    border-top-color: transparent;
  }

  .#{$prefix}--list-box__menu-item--highlighted
    .#{$prefix}--list-box__menu-item__option {
    color: $text-01;
  }

  .#{$prefix}--list-box__menu-item--active {
    color: $text-01;
    background-color: $selected-ui;
    border-bottom-color: $selected-ui;
  }

  .#{$prefix}--list-box--light .#{$prefix}--list-box__menu-item--active {
    background-color: $selected-light-ui;
    border-bottom-color: $selected-light-ui;
  }

  .#{$prefix}--list-box__menu-item--active:hover {
    background-color: $hover-ui;
    border-bottom-color: $hover-ui;
  }

  .#{$prefix}--list-box__menu-item--active
    .#{$prefix}--list-box__menu-item__option {
    color: $text-01;
  }

  // Hide top border if previous list item is selected
  .#{$prefix}--list-box__menu-item--active
    + .#{$prefix}--list-box__menu-item
    > .#{$prefix}--list-box__menu-item__option {
    border-top-color: transparent;
  }

  .#{$prefix}--list-box__menu-item__selected-icon {
    position: absolute;
    top: 50%;
    right: rem(16px);
    display: none;
    transform: translateY(-50%);
    fill: $icon-01;
  }

  .#{$prefix}--list-box--inline
    .#{$prefix}--list-box__menu-item__selected-icon {
    right: rem(8px);
  }

  .#{$prefix}--list-box__menu-item--active
    .#{$prefix}--list-box__menu-item__selected-icon {
    display: block;
  }

  .#{$prefix}--list-box__menu-item .#{$prefix}--checkbox-label {
    width: 100%;
  }

  .#{$prefix}--list-box__menu-item .#{$prefix}--checkbox-label-text {
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }

  // Dropdown top orientation modifiers
  .#{$prefix}--list-box--up .#{$prefix}--list-box__menu {
    bottom: 2.5rem;
  }

  .#{$prefix}--list-box--up.#{$prefix}--dropdown--sm
    .#{$prefix}--list-box__menu,
  .#{$prefix}--list-box--up.#{$prefix}--list-box--sm
    .#{$prefix}--list-box__menu {
    bottom: 2rem;
  }

  .#{$prefix}--list-box--up.#{$prefix}--dropdown--xl
    .#{$prefix}--list-box__menu,
  .#{$prefix}--list-box--up.#{$prefix}--list-box--xl
    .#{$prefix}--list-box__menu {
    bottom: 3rem;
  }

  // Tweaks for descendants
  // When handling input, we need to target nodes that specifically opt-in to
  // the type text in order to make sure the text input is styled
  // correctly.
  // TODO: remove [role='combobox'] in v11
  .#{$prefix}--list-box input[role='combobox'],
  .#{$prefix}--list-box input[type='text'] {
    min-width: 0;
    background-color: inherit;
  }
}

@include exports('list-box') {
  @include listbox;
}
