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

@use '../list-box';
@use '../../config' as *;
@use '../../layer' as *;
@use '../../motion' as *;
@use '../../spacing' as *;
@use '../../theme' as *;
@use '../../type' as *;
@use '../../utilities/box-shadow' as *;
@use '../../utilities/convert';
@use '../../utilities/focus-outline' as *;
@use '../../utilities/high-contrast-mode' as *;
@use '../../utilities/skeleton' as *;
@use '../../utilities/z-index' as *;

/// Dropdown styles
/// @access public
/// @group dropdown
@mixin dropdown {
  .#{$prefix}--dropdown__wrapper--inline {
    display: inline-grid;
    align-items: center;
    grid-gap: 0 convert.to-rem(8px);
    grid-template: auto / auto min-content auto;

    &:has(.#{$prefix}--label.#{$prefix}--visually-hidden) {
      grid-template: auto / auto;
    }

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

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

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

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

    position: relative;
    display: block;
    border: none;
    background-color: $field;
    block-size: convert.to-rem(40px);
    border-block-end: 1px solid $border-strong;
    color: $text-primary;
    cursor: pointer;
    inline-size: 100%;
    list-style: none;
    outline: 2px solid transparent;
    transition: background-color $duration-fast-01 motion(standard, productive);

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

  // Menu's triggering element updated to button with Downshift v5 upgrade
  .#{$prefix}--dropdown .#{$prefix}--list-box__field {
    text-align: start;
  }

  .#{$prefix}--dropdown--lg {
    block-size: convert.to-rem(48px);
    max-block-size: convert.to-rem(48px);
  }

  .#{$prefix}--dropdown--lg .#{$prefix}--dropdown__arrow {
    inset-block-start: convert.to-rem(16px);
  }

  .#{$prefix}--dropdown--sm {
    block-size: convert.to-rem(32px);
    max-block-size: convert.to-rem(32px);
  }

  .#{$prefix}--dropdown--sm .#{$prefix}--dropdown__arrow {
    inset-block-start: convert.to-rem(8px);
  }

  .#{$prefix}--dropdown--open {
    border-block-end-color: $border-subtle;
  }

  .#{$prefix}--dropdown--open .#{$prefix}--list-box__field {
    outline: none;
  }

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

  .#{$prefix}--dropdown--invalid {
    @include focus-outline('invalid');

    .#{$prefix}--dropdown-text {
      padding-inline-end: convert.to-rem(56px);
    }

    + .#{$prefix}--form-requirement {
      display: inline-block;
      color: $text-error;
      max-block-size: convert.to-rem(200px);
    }
  }

  .#{$prefix}--dropdown__invalid-icon {
    position: absolute;
    fill: $support-error;
    inset-block-start: 50%;
    inset-inline-end: $spacing-08;
    transform: translateY(-50%);
  }

  .#{$prefix}--dropdown--open:hover {
    background-color: $field;
  }

  .#{$prefix}--dropdown--open:focus {
    outline: 1px solid transparent;
  }

  .#{$prefix}--dropdown--open .#{$prefix}--dropdown-list {
    @include box-shadow;

    // 40px item height * 5.5 items shown
    max-block-size: convert.to-rem(220px);
    transition: max-height $duration-fast-02 motion(entrance, productive);
  }

  .#{$prefix}--dropdown--light {
    background-color: $field-02;

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

  .#{$prefix}--dropdown--up .#{$prefix}--dropdown-list {
    inset-block-end: 2rem;
  }

  .#{$prefix}--dropdown__arrow {
    position: absolute;
    fill: $icon-primary;
    inset-block-start: convert.to-rem(13px);
    inset-inline-end: 1rem;
    pointer-events: none;
    transform-origin: 50% 45%;
    transition: transform $duration-fast-02 motion(standard, productive);
  }

  button.#{$prefix}--dropdown-text {
    border: none;
    // button-reset mixin contradicts with cds--dropdown-text styles
    background: none;
    color: $text-primary;
    inline-size: 100%;
    text-align: start;

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

  .#{$prefix}--dropdown-text {
    @include type-style('body-compact-01');

    display: block;
    overflow: hidden;
    // Account for the border in `.cds--dropdown`
    block-size: calc(100% + 1px);
    // 2rem + SVG width
    padding-inline: $spacing-05 convert.to-rem(42px);
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .#{$prefix}--dropdown-list {
    @include reset;
    @include focus-outline('reset');
    @include box-shadow;
    @include type-style('body-compact-01');

    position: absolute;
    z-index: z('dropdown');
    display: flex;
    // NOTE: IE, Edge, and Safari do not support two value `overflow` shorthand.
    overflow: hidden auto;
    flex-direction: column;
    background-color: $layer;
    inline-size: 100%;
    list-style: none;
    max-block-size: 0;
    transition: max-height $duration-fast-02 motion(standard, productive);
  }

  // V11: Possibly deprecate
  .#{$prefix}--dropdown--light .#{$prefix}--dropdown-list {
    background-color: $layer;
  }

  .#{$prefix}--dropdown:not(.#{$prefix}--dropdown--open)
    .#{$prefix}--dropdown-item {
    visibility: hidden;
  }

  .#{$prefix}--dropdown-item {
    position: relative;
    opacity: 0;
    transition:
      visibility $duration-fast-01 motion(standard, productive),
      opacity $duration-fast-01 motion(standard, productive),
      background-color $duration-fast-01 motion(standard, productive);
    visibility: inherit;

    &:hover {
      background-color: $layer-hover;

      + .#{$prefix}--dropdown-item .#{$prefix}--dropdown-link {
        border-color: transparent;
      }
    }

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

    &:first-of-type .#{$prefix}--dropdown-link {
      border-block-start-color: transparent;
    }
  }

  .#{$prefix}--dropdown-item:last-of-type .#{$prefix}--dropdown-link {
    border-block-end: none;
  }

  .#{$prefix}--dropdown-link {
    @include focus-outline('reset');

    display: block;
    overflow: hidden;
    padding: convert.to-rem(11px) 0;
    border: 1px solid transparent;
    margin: 0 $spacing-05;
    block-size: convert.to-rem(40px);
    border-block-start-color: $border-subtle;
    color: $text-secondary;
    font-weight: normal;
    line-height: 1rem;
    text-decoration: none;
    text-overflow: ellipsis;
    white-space: nowrap;

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

  // V11: Possibly deprecate
  .#{$prefix}--dropdown--light .#{$prefix}--dropdown-link {
    border-block-start-color: $border-subtle-02;
  }

  .#{$prefix}--dropdown--sm .#{$prefix}--dropdown-link {
    block-size: convert.to-rem(32px);
    padding-block: convert.to-rem(7px) convert.to-rem(7px);
  }

  .#{$prefix}--dropdown--focused,
  .#{$prefix}--dropdown-link:focus {
    @include focus-outline('outline');

    padding: convert.to-rem(11px) convert.to-rem(16px);
    margin: 0;
  }

  // We don't want to apply focus styles via focus selector when using the aria-activedescendant structure
  .#{$prefix}--dropdown-list[aria-activedescendant]
    .#{$prefix}--dropdown-link:focus {
    // Copied from .cds--dropdown-link styles
    padding: convert.to-rem(11px) 0;
    margin: 0 $spacing-05;
    outline: none;
  }

  // Need added weight for item that is :focused and .cds--dropdown--focused
  .#{$prefix}--dropdown-list[aria-activedescendant]
    .#{$prefix}--dropdown--focused:focus {
    // copied from default focus styles
    @include focus-outline('outline');

    padding: convert.to-rem(11px) convert.to-rem(16px);
    margin: 0;
  }

  // Don't want to allow multiple elements have a "selected" style. Not sure why active
  // had unique styles initially but creating an overwrite for the latest HTML markup for
  // backwards compatibility. For the next major release it would be possible to clean up
  // the HTML structure to prevent the user of :active and :focus styles which is creating
  // these duplicated styles in the list. The Carbon 10 version of dropdown is already
  // supporting 2 very different HTML structures.
  .#{$prefix}--dropdown-list[aria-activedescendant]
    .#{$prefix}--dropdown-item:active {
    background-color: inherit;
  }

  .#{$prefix}--dropdown-item:hover .#{$prefix}--dropdown-link {
    border-block-end-color: $layer-hover;
  }

  .#{$prefix}--dropdown--open .#{$prefix}--dropdown__arrow {
    transform: rotate(-180deg);
  }

  .#{$prefix}--dropdown--open.#{$prefix}--dropdown--sm
    .#{$prefix}--dropdown-list {
    // 32px item height * 5.5 items shown
    max-block-size: convert.to-rem(176px);
  }

  .#{$prefix}--dropdown--open .#{$prefix}--dropdown-item {
    opacity: 1;
  }

  .#{$prefix}--dropdown--disabled {
    border-block-end-color: transparent;

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

    &:focus {
      outline: none;
    }

    // TODO: remove in v11
    .#{$prefix}--dropdown-text,
    .#{$prefix}--list-box__label {
      color: $text-disabled;
    }

    // TODO: remove in v11
    .#{$prefix}--dropdown__arrow,
    .#{$prefix}--list-box__menu-icon svg {
      fill: $icon-disabled;
    }

    // V11: Possibly deprecate
    &.#{$prefix}--dropdown--light:hover {
      background-color: $field-02;
    }
  }

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

  .#{$prefix}--dropdown--auto-width {
    inline-size: auto;
    max-inline-size: convert.to-rem(400px);
  }

  .#{$prefix}--dropdown--inline {
    display: inline-block;
    background-color: transparent;
    border-block-end-color: transparent;
    inline-size: auto;
    justify-self: start;
    transition: background $duration-fast-01 motion(entrance, productive);

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

    &.#{$prefix}--dropdown--disabled {
      background-color: transparent;
    }

    .#{$prefix}--dropdown__arrow {
      inset-block-start: convert.to-rem(8px);
      inset-inline-end: convert.to-rem(8px);
    }
  }

  .#{$prefix}--dropdown--inline.#{$prefix}--dropdown--open {
    background-color: transparent;
  }

  .#{$prefix}--dropdown--inline .#{$prefix}--dropdown-text {
    display: inline-block;
    overflow: visible;
    padding: convert.to-rem(7px) $spacing-07 convert.to-rem(7px) $spacing-04;
    block-size: convert.to-rem(32px);
    color: $text-primary;
  }

  .#{$prefix}--dropdown--inline.#{$prefix}--dropdown--disabled
    .#{$prefix}--dropdown-text {
    color: $text-disabled;
  }

  .#{$prefix}--dropdown--inline.#{$prefix}--dropdown--disabled:focus
    .#{$prefix}--dropdown-text {
    outline: 0;
  }

  .#{$prefix}--dropdown--inline.#{$prefix}--dropdown--invalid
    .#{$prefix}--dropdown__invalid-icon {
    inset-inline-end: convert.to-rem(32px);
  }

  .#{$prefix}--dropdown--inline.#{$prefix}--dropdown--invalid
    .#{$prefix}--dropdown-text {
    padding-inline-end: convert.to-rem(56px);
  }

  .#{$prefix}--dropdown--inline.#{$prefix}--dropdown--open:focus
    .#{$prefix}--dropdown-list {
    @include box-shadow;
  }

  .#{$prefix}--dropdown--inline .#{$prefix}--dropdown-link {
    font-weight: normal;
  }

  .#{$prefix}--dropdown--show-selected .#{$prefix}--dropdown--selected {
    display: block;
    background-color: $layer-selected;
    color: $text-primary;

    &:hover {
      background-color: $layer-selected-hover;
    }

    .#{$prefix}--dropdown-link {
      border-block-start-color: transparent;
    }

    + .#{$prefix}--dropdown-item .#{$prefix}--dropdown-link {
      border-block-start-color: transparent;
    }

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

  // Skeleton State
  .#{$prefix}--dropdown-v2.#{$prefix}--skeleton,
  .#{$prefix}--dropdown.#{$prefix}--skeleton {
    @include skeleton;
  }

  // Windows HCM fix

  .#{$prefix}--dropdown .#{$prefix}--list-box__field {
    @include high-contrast-mode('outline');
  }

  .#{$prefix}--list-box__menu-item__option {
    @include high-contrast-mode {
      outline: none;
    }
  }

  // readonly
  .#{$prefix}--dropdown--readonly,
  .#{$prefix}--dropdown--readonly:hover {
    background-color: transparent;
    border-block-end-color: $border-subtle;
  }
  .#{$prefix}--dropdown--inline.#{$prefix}--dropdown--readonly {
    border-block-end-color: transparent;
  }

  .#{$prefix}--dropdown--readonly .#{$prefix}--list-box__field,
  .#{$prefix}--dropdown--readonly .#{$prefix}--list-box__menu-icon {
    cursor: default;
  }

  .#{$prefix}--dropdown--readonly .#{$prefix}--list-box__menu-icon svg {
    fill: $icon-disabled;
  }
}
