@use 'sass:color';
@use 'sass:math';

@use '../tools/colors';
@use '../tools/variables' as tools-variables;

@use '../ui/config/speed.scss';
@use '../ui/config/variables.scss';
@use '../ui/config/border-radius.scss';
@use '../ui/config/spacing.scss';

$interactive-list--padding--vertical: 0;
$interactive-list--padding--horizontal: 0;

$interactive-list--item--foreground: colors.$textbox--foreground;

$interactive-list--item--detail--tertiary--foreground: color.adjust(
  $interactive-list--item--foreground,
  $lightness: -10%
);

$interactive-list--item--icon--fill: rgba($interactive-list--item--foreground, 0.5);
$interactive-list--item--icon--fill--hover: #fff;

$interactive-list--item--padding--horizontal: variables.$form--element--padding--x;
$interactive-list--item--padding--vertical: math.div(tools-variables.$spacing-unit * 3, 10);

$interactive-list--detail-list--item--padding--horizontal: math.div(tools-variables.$spacing-unit * 1, 5);
$interactive-list--detail-list--item--padding--vertical: 0;

.interactive-list {
  appearance: none;
  outline: none;
  background: variables.$input--inverse--background;
  border: variables.$form--element--border-width solid variables.$input--inverse--border;
  border-radius: variables.$form--element--border-radius;
  box-shadow: variables.$form--element--inverse--box-shadow;
  color: variables.$input--inverse--foreground;
  display: block;
  height: auto;
  padding: $interactive-list--padding--vertical $interactive-list--padding--horizontal;
  transition: all speed.$speed--x-fast;
  width: 100%;

  &::placeholder {
    color: colors.$textbox--placeholder;
    font-style: italic;
    transition: color 0.25s;
  }

  &::selection {
    color: colors.$textbox--selection--foreground;
    background: colors.$textbox--selection--background;
  }

  &:focus {
    background: colors.$textbox--active--background;
    border-color: colors.$textbox--active--border;
    color: colors.$textbox--active--foreground;

    &::placeholder {
      color: colors.$textbox--active--placeholder;
    }
  }

  &--loading {
    height: 60px;
  }

  &__item {
    border-bottom: 1px solid color.adjust(variables.$input--inverse--background, $lightness: -3%);
    display: flex;
    overflow: hidden;
    padding: $interactive-list--item--padding--vertical $interactive-list--item--padding--horizontal;
    text-overflow: ellipsis;
    transition: background speed.$speed--x-fast;
    white-space: nowrap;
    width: 100%;

    &:last-child {
      border-bottom: none;
    }

    &--stacked-content {
      align-items: center;

      .interactive-list {
        &__label {
          align-items: stretch;
          flex-direction: column;
        }
      }
    }
  }

  &__label {
    align-items: center;
    display: flex;
    flex: 1 1 auto;
    overflow: hidden;
    text-align: left;
    text-overflow: ellipsis;

    &__text {
      flex: 1 1 auto;
    }

    &__tag {
      flex: 0 0 auto;

      &.tag {
        color: color.adjust(colors.$darkest-grey--hard, $lightness: 50%);
        margin-right: 0;
        padding-bottom: math.div(spacing.$spacing--xx-small, 2);
        padding-top: math.div(spacing.$spacing--xx-small, 2);
      }
    }
  }

  &__loading-indicator {
    height: 100%;
    left: 0;
    position: absolute;
    top: 0;
    transition: opacity 0.25s;
    width: 100%;

    .icon {
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
    }

    &-enter {
      opacity: 0;

      &-active {
        opacity: 1;
      }
    }

    &-exit {
      opacity: 1;

      &-active {
        opacity: 0;
      }
    }
  }

  &__icon {
    border-radius: border-radius.$border-radius--small;
    flex: 0 0 auto;
    height: spacing.$spacing--large;
    position: relative;
    width: spacing.$spacing--large;

    .icon {
      height: 12px;
      width: 12px;
      fill: $interactive-list--item--icon--fill;
      transition: fill speed.$speed--x-fast;
    }

    &--action {
      transition: background speed.$speed--xx-fast;

      &:focus {
        outline: none;
        -webkit-tap-highlight-color: transparent;
      }

      &:focus-visible,
      &:hover {
        background: colors.$darkest-grey--hard;

        .icon {
          filter: drop-shadow(0 1px 0 rgba(colors.$darkest-grey, 0.8));
          fill: $interactive-list--item--icon--fill--hover;
        }
      }

      &--warning {
        &:focus-visible,
        &:hover {
          background: colors.$red;
        }
      }

      & + .interactive-list__icon--action {
        margin-left: spacing.$spacing--x-small;
      }
    }
  }

  &__detail {
    &--primary {
      font-weight: 700;
    }

    &--tertiary {
      color: $interactive-list--item--detail--tertiary--foreground;
    }
  }

  &__detail-list {
    display: flex;
    flex: 1 1 auto;
    max-width: 100%;

    &__item {
      flex: 0 0 auto;
      max-width: 100%;
      padding: $interactive-list--detail-list--item--padding--vertical
        $interactive-list--detail-list--item--padding--horizontal;

      &:first-child {
        padding-left: 0;
      }

      &:last-child {
        padding-right: 0;
      }

      &--overflow {
        flex: 0 1 auto;
        overflow: hidden;
        text-overflow: ellipsis;
      }
    }
  }
}
