@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/rtl" as *;
@use "../../utilities/high-contrast-mode" as *;
@use "../../utilities/convert" as *;
@use "../../breakpoint" as *;
@use "../../utilities/button-reset";

$input-height: 40px !default;
$input-border: 1px solid $field-border-color;

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

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

  .#{$prefix}--search-input {
    @include reset;
    @include type.type-style("body-short-02");
    @include focus-outline("reset");

    width: 100%;
    order: 1;
    padding: 0 $spacing-08;

    //@include font-family('sans');
    //@include type-style('body-short-02');
    //height: $input-height;
    appearance: none;
    display: block;
    width: 100%;
    padding: 0 $spacing-06 0 $spacing-03;
    border: $input-border;
    order: 2;
    border-radius: 0;
    cursor: pointer;

    appearance: none;
    background-color: $fill-field;
    color: $text-body-default;
    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;
    }
  }

  .#{$prefix}--search-input--rounded {
    border-radius: 22px;
  }

  .#{$prefix}--search-input[disabled] {
    opacity: 0.5;
    cursor: not-allowed;

    &::placeholder {
      // color: $fill-field; no color needed to be set. It should inheret
    }
  }
  .#{$prefix}--search--light .#{$prefix}--search-input {
    //TODO: background: $field-02;
  }

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

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

  .#{$prefix}--search--sm .#{$prefix}--search-magnifier-icon {
    left: calcRem(8px);
  }

  // Large styles
  // V11: change lg to md
  .#{$prefix}--search--lg .#{$prefix}--search-input,
  .#{$prefix}--search--lg.#{$prefix}--search--expandable.#{$prefix}--search--expanded
    .#{$prefix}--search-input {
    height: calcRem(40px);
    // 12px padding on either side of icon + 16px icon (40px)
    padding: 0 $spacing-08;
  }

  // V11: change lg to md
  .#{$prefix}--search--lg .#{$prefix}--search-magnifier-icon {
    left: calcRem(12px);
  }

  // V11: change xl to lg
  .#{$prefix}--search--xl .#{$prefix}--search-input,
  .#{$prefix}--search--xl.#{$prefix}--search--expandable.#{$prefix}--search--expanded
    .#{$prefix}--search-input {
    height: calcRem(48px);
    // 16px padding on either side of icon + 16px icon (48px)
    padding: 0 $spacing-09;
  }

  .#{$prefix}--search-magnifier-icon {
    position: absolute;
    z-index: 2;
    top: 50%;
    left: $spacing-05;
    @include rtl() {
      left: inherit;
      right: $spacing-05;
    }
    width: calcRem(16px);
    height: calcRem(16px);
    fill: $icon-secondary;
    pointer-events: none;
    transform: translateY(-50%);
  }

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

    position: absolute;
    top: 0;
    right: 0;

    &::before {
      position: absolute;
      top: calcRem(1px);
      left: 0;
      display: block;
      width: 2px;
      height: calc(100% - 2px);
      content: "";
      transition: background-color $duration-fast-02
        motion(standard, productive);
    }

    &:hover {
      // border-bottom: 1px solid $border-strong;

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

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

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

  .#{$prefix}--search-close svg {
    fill: inherit;
  }

  .#{$prefix}--search-close,
  .#{$prefix}--search-button {
    display: flex;
    //width: calcRem(40px);
    // height: calcRem(40px);
    align-items: center;
    justify-content: center;
    border-width: 1px 0;
    border-style: solid;
    border-color: transparent;
    cursor: pointer;
    fill: $icon-primary;
    opacity: 1;
    transition: opacity $duration-fast-02 motion(standard, productive),
      background-color $duration-fast-02 motion(standard, productive),
      outline $duration-fast-02 motion(standard, productive),
      border $duration-fast-02 motion(standard, productive);
    visibility: inherit;

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

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

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

      // background-color: $background-selected;
    }
  }

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

    &:hover {
      border-bottom-color: transparent;
      background-color: transparent;
    }

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

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

  .#{$prefix}--search-close:focus,
  .#{$prefix}--search-close:active {
    &::before {
      background-color: transparent;
    }
  }

  .#{$prefix}--search-input:focus ~ .#{$prefix}--search-close:hover {
    // @include focus-outline("outline");
  }

  .#{$prefix}--search--sm {
    .#{$prefix}--search-close,
    ~ .#{$prefix}--search-button,
    &.#{$prefix}--search--expandable,
    &.#{$prefix}--search--expandable .#{$prefix}--search-magnifier {
      width: calcRem(32px);
      height: calcRem(32px);
    }

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

  // V11: change lg to md
  .#{$prefix}--search--lg {
    .#{$prefix}--search-close,
    ~ .#{$prefix}--search-button,
    &.#{$prefix}--search--expandable,
    &.#{$prefix}--search--expandable .#{$prefix}--search-magnifier {
      width: calcRem(40px);
      height: calcRem(40px);
    }

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

  // V11: change xl to lg
  .#{$prefix}--search--xl {
    .#{$prefix}--search-close,
    ~ .#{$prefix}--search-button,
    &.#{$prefix}--search--expandable,
    &.#{$prefix}--search--expandable .#{$prefix}--search-magnifier {
      width: calcRem(48px);
      height: calcRem(48px);
    }

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

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

  // V11: change xl to lg, lg to md
  .#{$prefix}--search--xl.#{$prefix}--skeleton .#{$prefix}--search-input,
  .#{$prefix}--search--lg.#{$prefix}--skeleton .#{$prefix}--search-input,
  .#{$prefix}--search--sm.#{$prefix}--skeleton .#{$prefix}--search-input {
    @include skeleton;

    width: 100%;

    &::placeholder {
      color: transparent;
    }
  }

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

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

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

    &::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 {
    width: 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;
  }

  .#{$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;
  }

  // Windows HCM fix
  .#{$prefix}--search-close svg,
  .#{$prefix}--search-magnifier-icon {
    @include high-contrast-mode("icon-fill");
  }

  // MainNavigation styles
  // TODO: Move to MainNavigation

  .#{$prefix}--search--main .#{$prefix}--search-input {
    border-radius: calcRem(4px);
    width: 100%;
    height: calcRem(30px);
    padding: 0 $spacing-07; //xl
    @include type.type-style("body-short-02");
    @include breakpoint(lg) {
      max-width: calcRem(150px);
    }
  }

  // Main Navigation styles
  .#{$prefix}--search--main .#{$prefix}--search-input,
  .#{$prefix}--search--main.#{$prefix}--search--expandable.#{$prefix}--search--expanded
    .#{$prefix}--search-input {
    height: calcRem(32px);
    border: none;
    background-color: $border-subtle;
    font-size: 0.8em;
    &:hover {
      @include focus-outline("reset");
    }
    &::placeholder {
      color: $text-body-default;
    }

    // 8px padding on either side of icon + 16px icon (32px)
    padding: 0 $spacing-07;
  }

  .#{$prefix}--search--main .#{$prefix}--search-magnifier-icon {
    left: calcRem(8px);
  }

  .#{$prefix}--search--main {
    color: $text-body-default;
    .#{$prefix}--search-close,
    ~ .#{$prefix}--search-button,
    &.#{$prefix}--search--expandable,
    &.#{$prefix}--search--expandable .#{$prefix}--search-magnifier {
      width: calcRem(32px);
      height: calcRem(32px);
    }

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