@use '@angular/material' as mat;
@use '../core/tokens/token-utils';
@use './m3-select';

$fallbacks: m3-select.get-tokens();

@mixin rtl {
  @at-root [dir='rtl'] #{&} {
    @content;
  }
}

.ng-select {
  $filled-padding-top: var(--mat-form-field-filled-with-label-container-padding-top, 24px);
  $filled-padding-bottom: var(--mat-form-field-filled-with-label-container-padding-bottom, 8px);
  $vertical-padding: var(--mat-form-field-container-vertical-padding, 16px);
  $text-line-height: var(--mat-form-field-container-text-line-height, var(--mat-sys-body-large-line-height));

  padding: $filled-padding-top 16px $filled-padding-bottom;
  margin: calc($filled-padding-top * -1) -16px calc($filled-padding-bottom * -1);

  .mdc-text-field--outlined &,
  .mdc-text-field--no-label & {
    padding-top: $vertical-padding;
    padding-bottom: $vertical-padding;
    margin-top: calc($vertical-padding * -1);
    margin-bottom: calc($vertical-padding * -1);
  }

  .ng-select-container {
    align-items: center;
    color: token-utils.slot(select-container-text-color, $fallbacks);

    .ng-value-container {
      align-items: center;
      gap: 4px;

      .ng-input > input {
        height: $text-line-height;
        color: inherit;
        font: inherit;
      }
    }

    .ng-clear-wrapper {
      display: inline-flex;
      justify-content: center;
      align-items: center;
      width: 24px;
      height: $text-line-height;
    }
  }

  .ng-placeholder {
    transition: opacity 200ms;
    opacity: 1;
    color: token-utils.slot(select-placeholder-text-color, $fallbacks);

    .mat-form-field-hide-placeholder & {
      opacity: 0;
    }
  }

  .ng-has-value .ng-placeholder {
    display: none;
  }

  .ng-clear-wrapper {
    color: token-utils.slot(select-clear-icon-color, $fallbacks);

    &:hover .ng-clear {
      color: token-utils.slot(select-clear-icon-hover-color, $fallbacks);
    }
  }

  &.ng-select-disabled {
    .ng-value {
      color: token-utils.slot(select-disabled-text-color, $fallbacks);
    }
  }

  &.ng-select-opened {
    .ng-arrow-wrapper .ng-arrow {
      top: -2px;
      border-width: 0 5px 5px;
    }
  }

  &.ng-select-single {
    &.ng-select-filtered .ng-placeholder {
      display: initial;
      visibility: hidden;
    }
  }

  &.ng-select-multiple {
    .ng-select-container .ng-value-container .ng-value {
      display: inline-flex;
      align-items: center;
      height: $text-line-height;
      padding: 0 calc(($text-line-height - 16px) / 2);
      border-radius: 9999px;
      font-size: 0.875em;
      background-color: token-utils.slot(select-multiple-value-background-color, $fallbacks);
      border: 1px solid token-utils.slot(select-multiple-value-outline-color, $fallbacks);

      &.ng-value-disabled {
        opacity: 0.4;
      }

      .ng-value-label {
        display: inline-block;
        margin: 0 4px;
        line-height: 16px;
      }

      .ng-value-icon {
        width: 16px;
        height: 16px;
        line-height: 16px;
        border-radius: 50%;
        text-align: center;

        &:hover {
          background-color: token-utils.slot(select-multiple-value-icon-hover-background-color, $fallbacks);
        }
      }
    }

    &.ng-select-disabled .ng-select-container .ng-value-container .ng-value {
      border-color: token-utils.slot(select-multiple-value-disabled-outline-color, $fallbacks);
    }
  }

  .ng-arrow-wrapper {
    width: 10px;
  }

  $enabled-arrow-color: token-utils.slot(select-enabled-arrow-color, $fallbacks);
  $disabled-arrow-color: token-utils.slot(select-disabled-arrow-color, $fallbacks);
  $invalid-arrow-color: token-utils.slot(select-invalid-arrow-color, $fallbacks);

  .ng-arrow {
    border-width: 5px 5px 2px;
    border-style: solid;
    border-color: $enabled-arrow-color transparent transparent;
  }

  &.ng-select-disabled .ng-arrow {
    border-color: $disabled-arrow-color transparent transparent;
  }

  &.ng-select-invalid .ng-arrow {
    border-color: $invalid-arrow-color transparent transparent;
  }

  &.ng-select-opened .ng-arrow {
    border-color: transparent transparent $enabled-arrow-color;
  }

  &.ng-select-opened.ng-select-invalid .ng-arrow {
    border-color: transparent transparent $invalid-arrow-color;
  }
}

.ng-dropdown-panel {
  background-color: token-utils.slot(select-panel-background-color, $fallbacks);

  &.ng-select-bottom {
    top: 100%;
    border-bottom-left-radius: token-utils.slot(select-container-shape, $fallbacks);
    border-bottom-right-radius: token-utils.slot(select-container-shape, $fallbacks);
    box-shadow: token-utils.slot(select-container-elevation-shadow, $fallbacks);
  }

  &.ng-select-top {
    bottom: 100%;
    border-top-left-radius: token-utils.slot(select-container-shape, $fallbacks);
    border-top-right-radius: token-utils.slot(select-container-shape, $fallbacks);
    box-shadow: token-utils.slot(select-container-elevation-shadow, $fallbacks);
  }

  .ng-dropdown-header,
  .ng-dropdown-footer {
    padding: 14px 16px;
  }

  .ng-dropdown-header {
    border-bottom: 1px solid token-utils.slot(select-panel-divider-color, $fallbacks);
  }

  .ng-dropdown-footer {
    border-top: 1px solid token-utils.slot(select-panel-divider-color, $fallbacks);
  }

  .ng-dropdown-panel-items {
    .ng-optgroup {
      padding: 14px 16px;
      font-weight: 500;
      user-select: none;
      cursor: pointer;
      color: token-utils.slot(select-optgroup-label-text-color, $fallbacks);

      &.ng-option-disabled {
        cursor: default;
      }

      &.ng-option-marked {
        background-color: token-utils.slot(select-option-hover-state-background-color, $fallbacks);
      }

      &.ng-option-selected {
        background-color: token-utils.slot(select-option-selected-state-background-color, $fallbacks);
        color: token-utils.slot(select-option-selected-state-text-color, $fallbacks);
      }
    }

    .ng-option {
      position: relative;
      padding: 14px 16px;
      text-overflow: ellipsis;
      text-decoration: none;
      text-align: left;
      white-space: nowrap;
      overflow: hidden;
      color: token-utils.slot(select-option-label-text-color, $fallbacks);

      &.ng-option-marked {
        background-color: token-utils.slot(select-option-hover-state-background-color, $fallbacks);
      }

      &.ng-option-selected {
        background-color: token-utils.slot(select-option-selected-state-background-color, $fallbacks);
        color: token-utils.slot(select-option-selected-state-text-color, $fallbacks);
      }

      &.ng-option-disabled {
        color: token-utils.slot(select-option-disabled-state-text-color, $fallbacks);
      }

      @include rtl {
        text-align: right;
      }

      &.ng-option-child {
        padding-left: 32px;

        @include rtl {
          padding-right: 32px;
          padding-left: 0;
        }
      }

      .ng-tag-label {
        margin-right: 6px;
        font-size: 80%;

        @include rtl {
          margin-left: 6px;
          margin-right: 0;
        }
      }
    }
  }
}
