//
// 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.
//

//-----------------------------
// Search
//-----------------------------

@use '../text-input';
@use '../../theme' as *;
@use '../../config' as *;
@use '../../type';
@use '../../motion' as *;
@use '../../spacing' as *;
@use '../../utilities/placeholder-colors' as *;
@use '../../utilities/focus-outline' as *;
@use '../../utilities/visually-hidden' as *;
@use '../../utilities/component-reset' as *;
@use '../../utilities/skeleton' as *;
@use '../../utilities/convert';
@use '../../utilities/button-reset';

/// Search styles
/// @access public
/// @group search
@mixin search {
  .#{$prefix}--search {
    position: relative;
    display: flex;
    align-items: center;
    inline-size: 100%;
  }

  .#{$prefix}--search .#{$prefix}--label {
    @include visually-hidden;
  }

  .#{$prefix}--search-input {
    @include reset;
    @include type.type-style('body-compact-01');
    @include focus-outline('reset');

    order: 1;
    padding: 0 $spacing-08;
    border: none;
    appearance: none;
    background-color: $field;
    border-block-end: 1px solid $border-strong;
    color: $text-primary;

    inline-size: 100%;
    text-overflow: ellipsis;
    transition:
      background-color $duration-fast-02 motion(standard, productive),
      outline $duration-fast-02 motion(standard, productive);

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

    &::placeholder {
      @include placeholder-colors;
    }

    &::-ms-clear {
      display: none;
    }

    &::-webkit-search-decoration,
    &::-webkit-search-cancel-button,
    &::-webkit-search-results-button,
    &::-webkit-search-results-decoration {
      display: none;
      -webkit-appearance: none;
      appearance: none;
    }
  }

  .#{$prefix}--search-input[disabled] {
    background-color: $field;
    border-block-end: 1px solid transparent;
    color: $text-disabled;
    cursor: not-allowed;

    &::placeholder {
      color: $field;
    }
  }
  .#{$prefix}--search--light .#{$prefix}--search-input {
    background: $field-02;
  }

  .#{$prefix}--search--light .#{$prefix}--search-close::before {
    background: $field-02;
  }

  // Small styles
  .#{$prefix}--search--sm .#{$prefix}--search-input,
  .#{$prefix}--search--sm.#{$prefix}--search--expandable.#{$prefix}--search--expanded
    .#{$prefix}--search-input {
    // 8px padding on either side of icon + 16px icon (32px)
    padding: 0 $spacing-07;
    block-size: convert.to-rem(32px);
  }

  .#{$prefix}--search--sm .#{$prefix}--search-magnifier-icon {
    inset-inline-start: convert.to-rem(8px);
  }

  // Medium styles
  .#{$prefix}--search--md .#{$prefix}--search-input,
  .#{$prefix}--search--md.#{$prefix}--search--expandable.#{$prefix}--search--expanded
    .#{$prefix}--search-input {
    // 12px padding on either side of icon + 16px icon (40px)
    padding: 0 $spacing-08;
    block-size: convert.to-rem(40px);
  }

  .#{$prefix}--search--md .#{$prefix}--search-magnifier-icon {
    inset-inline-start: convert.to-rem(12px);
  }

  // Large styles
  .#{$prefix}--search--lg .#{$prefix}--search-input,
  .#{$prefix}--search--lg.#{$prefix}--search--expandable.#{$prefix}--search--expanded
    .#{$prefix}--search-input {
    // 16px padding on either side of icon + 16px icon (48px)
    padding: 0 $spacing-09;
    block-size: convert.to-rem(48px);
  }

  .#{$prefix}--search-magnifier-icon {
    @include reset;

    position: absolute;
    z-index: 2;
    block-size: convert.to-rem(16px);
    fill: $icon-secondary;
    inline-size: convert.to-rem(16px);
    inset-block-start: 50%;
    inset-inline-start: $spacing-05;
    pointer-events: none;
    transform: translateY(-50%);
  }

  .#{$prefix}--search-close {
    @include button-reset.reset(false);
    @include focus-outline('reset');

    position: absolute;
    inset-block-start: 0;
    inset-inline-end: 0;

    &:hover {
      border-block-end: 1px solid $border-strong;
    }
  }

  .#{$prefix}--search-button {
    flex-shrink: 0;
    background-color: $field;
    margin-inline-start: $spacing-01;

    svg {
      fill: currentColor;
      vertical-align: middle;
    }
  }

  // Keep the clear icon above the hover fill overlay.
  .#{$prefix}--search-close svg {
    position: relative;
    z-index: 1;
    fill: inherit;
  }

  .#{$prefix}--search-close,
  .#{$prefix}--search-button {
    display: flex;
    align-items: center;
    justify-content: center;
    border-width: 0 0 1px;
    border-style: solid;
    border-color: transparent;
    block-size: convert.to-rem(40px);
    cursor: pointer;
    fill: $icon-primary;
    inline-size: convert.to-rem(40px);
    opacity: 1;
    visibility: inherit;

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

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

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

      background-color: $background-selected;
    }
  }

  // Preserve the field focus outline while the clear button is hovered.
  .#{$prefix}--search-input:focus ~ .#{$prefix}--search-close:hover {
    background-color: transparent;
    border-block-end-color: transparent;
    outline: none;

    &::after {
      position: absolute;
      background-color: $field-hover;
      content: '';
      inset-block: $spacing-01;
      inset-inline: 0 $spacing-01;
      pointer-events: none;
    }
  }

  .#{$prefix}--search--disabled .#{$prefix}--search-close,
  .#{$prefix}--search--disabled.#{$prefix}--search--expandable
    .#{$prefix}--search-magnifier {
    cursor: not-allowed;
    outline: none;

    &:hover {
      background-color: transparent;
      border-block-end-color: transparent;
    }

    &:hover::before {
      background-color: transparent;
    }
  }

  .#{$prefix}--search--disabled svg {
    fill: $icon-on-color-disabled;
  }

  // Small
  .#{$prefix}--search--sm {
    .#{$prefix}--search-close,
    ~ .#{$prefix}--search-button,
    &.#{$prefix}--search--expandable,
    &.#{$prefix}--search--expandable .#{$prefix}--search-magnifier {
      block-size: convert.to-rem(32px);
      inline-size: convert.to-rem(32px);
    }

    &.#{$prefix}--search--expandable .#{$prefix}--search-input::placeholder {
      padding: 0 convert.to-rem(32px);
    }
  }

  // Medium
  .#{$prefix}--search--md {
    .#{$prefix}--search-close,
    ~ .#{$prefix}--search-button,
    &.#{$prefix}--search--expandable,
    &.#{$prefix}--search--expandable .#{$prefix}--search-magnifier {
      block-size: convert.to-rem(40px);
      inline-size: convert.to-rem(40px);
    }

    &.#{$prefix}--search--expandable .#{$prefix}--search-input::placeholder {
      padding: 0 convert.to-rem(40px);
    }
  }

  // Large
  .#{$prefix}--search--lg {
    .#{$prefix}--search-close,
    ~ .#{$prefix}--search-button,
    &.#{$prefix}--search--expandable,
    &.#{$prefix}--search--expandable .#{$prefix}--search-magnifier {
      block-size: convert.to-rem(48px);
      inline-size: convert.to-rem(48px);
    }

    &.#{$prefix}--search--expandable .#{$prefix}--search-input::placeholder {
      padding: 0 convert.to-rem(48px);
    }
  }

  .#{$prefix}--search-close--hidden {
    opacity: 0;
    visibility: hidden;
  }

  .#{$prefix}--search--lg.#{$prefix}--skeleton .#{$prefix}--search-input,
  .#{$prefix}--search--md.#{$prefix}--skeleton .#{$prefix}--search-input,
  .#{$prefix}--search--sm.#{$prefix}--skeleton .#{$prefix}--search-input {
    @include skeleton;

    inline-size: 100%;

    &::placeholder {
      color: transparent;
    }
  }

  .#{$prefix}--search--expandable {
    transition: width $duration-fast-01 motion(standard, productive);
  }

  .#{$prefix}--search--expandable.#{$prefix}--search--expanded {
    inline-size: 100%;
  }

  .#{$prefix}--search--expandable .#{$prefix}--search-input {
    padding: 0;
    inline-size: 0;
    transition: transform $duration-fast-01 motion(standard, productive);

    &::placeholder {
      position: relative;
      opacity: 0;
      transition-duration: $duration-fast-01;
      transition-property: padding, opacity;
      transition-timing-function: motion(standard, productive);
    }
  }

  .#{$prefix}--search--expandable.#{$prefix}--search--expanded
    .#{$prefix}--search-input {
    inline-size: 100%;
    transition: padding $duration-fast-01 motion(standard, productive);

    &::placeholder {
      position: relative;
      padding: 0;
      opacity: 1;
    }
  }

  .#{$prefix}--search--expandable .#{$prefix}--search-magnifier {
    position: absolute;
    cursor: pointer;

    &:focus {
      outline: 2px solid $focus;
    }
  }

  .#{$prefix}--search--expandable .#{$prefix}--search-magnifier:hover {
    background-color: $background-hover;
  }

  .#{$prefix}--search--expandable.#{$prefix}--search--expanded
    .#{$prefix}--search-magnifier {
    pointer-events: none;
  }

  .#{$prefix}--search--expandable .#{$prefix}--search-magnifier-icon {
    fill: $icon-primary;
  }

  .#{$prefix}--search--expandable.#{$prefix}--search--expanded
    .#{$prefix}--search-magnifier-icon {
    fill: $icon-secondary;
  }

  .#{$prefix}--search--expandable.#{$prefix}--search--disabled svg {
    fill: $icon-disabled;
  }

  .#{$prefix}--search-magnifier-tooltip {
    display: flex;
    align-items: center;
    justify-content: center;
    .#{$prefix}--search-magnifier {
      position: relative;
    }
  }
}
