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

$fallbacks: m3-select.get-tokens();
$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)
);

.ng-select {
  --ns-indicator-icon-size: 16px;

  .mat-mdc-form-field & {
    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-multiple {
  .ng-select-value {
    box-sizing: border-box;
    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-select-value-disabled {
      opacity: 0.4;
    }
  }

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

  .ng-select-value-remove {
    display: inline-flex;
    justify-content: center;
    align-items: center;
    width: 16px;
    height: 16px;
    border-radius: 50%;

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

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

.ng-select-control {
  color: token-utils.slot(select-container-text-color, $fallbacks);
}

.ng-select-value-container {
  gap: 4px;
}

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

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

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

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

.ng-select-clear,
.ng-select-arrow {
  display: inline-flex;
  align-items: center;
  line-height: 1;
  transition: color 150ms;
}

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

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

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

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

  .ng-select-invalid & {
    color: token-utils.slot(select-invalid-arrow-color, $fallbacks);
  }

  .ng-select-opened & {
    transform: rotate(180deg);
  }
}

.ng-select-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-select-panel-header,
.ng-select-panel-footer {
  padding: 14px 16px;
}

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

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

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

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

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

  &.ng-select-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-select-option {
  position: relative;
  padding: 14px 16px;
  color: token-utils.slot(select-option-label-text-color, $fallbacks);

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

  &.ng-select-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-select-option-disabled {
    color: token-utils.slot(select-option-disabled-state-text-color, $fallbacks);
  }

  &.ng-select-option-child {
    padding-inline-start: 32px;
  }
}

.ng-select-tag-option {
  display: inline-block;
  margin-inline-end: 6px;
  font-size: 80%;
}
