@use '../core/style/vendor-prefixes';
@use '../core/tokens/token-utils';
@use './m3-list';

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

// Includes the structural styles for the list that were inherited from MDC.
@mixin private-list-inherited-structural-styles {
  .mdc-list {
    margin: 0;
    padding: 8px 0;
    list-style-type: none;

    &:focus {
      outline: none;
    }
  }

  .mdc-list-item {
    display: flex;
    position: relative;
    justify-content: flex-start;
    overflow: hidden;
    padding: 0;
    align-items: stretch;
    cursor: pointer;
    padding-left: 16px;
    padding-right: 16px;

    background-color: token-utils.slot(list-list-item-container-color, $fallbacks);
    border-radius: token-utils.slot(list-list-item-container-shape, $fallbacks);

    &.mdc-list-item--selected {
      background-color: token-utils.slot(list-list-item-selected-container-color, $fallbacks);
    }

    &:focus {
      outline: 0;
    }

    &.mdc-list-item--disabled {
      cursor: auto;
    }

    &.mdc-list-item--with-one-line {
      height: token-utils.slot(list-list-item-one-line-container-height, $fallbacks);

      .mdc-list-item__start {
        align-self: center;
        margin-top: 0;
      }

      .mdc-list-item__end {
        align-self: center;
        margin-top: 0;
      }
    }

    &.mdc-list-item--with-two-lines {
      height: token-utils.slot(list-list-item-two-line-container-height, $fallbacks);

      .mdc-list-item__start {
        align-self: flex-start;
        margin-top: 16px;
      }

      .mdc-list-item__end {
        align-self: center;
        margin-top: 0;
      }
    }

    &.mdc-list-item--with-three-lines {
      height: token-utils.slot(list-list-item-three-line-container-height, $fallbacks);

      .mdc-list-item__start {
        align-self: flex-start;
        margin-top: 16px;
      }

      .mdc-list-item__end {
        align-self: flex-start;
        margin-top: 16px;
      }
    }

    // Not used in Material, but some internal tests seem to depend on it.
    &.mdc-list-item--selected::before,
    &.mdc-list-item--selected:focus::before,
    &:not(.mdc-list-item--selected):focus::before {
      position: absolute;
      box-sizing: border-box;
      width: 100%;
      height: 100%;
      top: 0;
      left: 0;
      content: '';
      pointer-events: none;
    }
  }

  a.mdc-list-item {
    color: inherit;
    text-decoration: none;
  }

  .mdc-list-item__start {
    fill: currentColor;
    flex-shrink: 0;
    pointer-events: none;

    .mdc-list-item--with-leading-icon & {
      color: token-utils.slot(list-list-item-leading-icon-color, $fallbacks);
      width: token-utils.slot(list-list-item-leading-icon-size, $fallbacks);
      height: token-utils.slot(list-list-item-leading-icon-size, $fallbacks);
      margin-left: 16px;
      margin-right: 32px;
    }

    [dir='rtl'] .mdc-list-item--with-leading-icon & {
      margin-left: 32px;
      margin-right: 16px;
    }

    .mdc-list-item--with-leading-icon:hover & {
      color: token-utils.slot(list-list-item-hover-leading-icon-color, $fallbacks);
    }

    // This is the same in RTL, but we need the specificity.
    .mdc-list-item--with-leading-avatar & {
      width: token-utils.slot(list-list-item-leading-avatar-size, $fallbacks);
      height: token-utils.slot(list-list-item-leading-avatar-size, $fallbacks);
      margin-left: 16px;
      margin-right: 16px;
      border-radius: 50%;
    }

    .mdc-list-item--with-leading-avatar &,
    [dir='rtl'] .mdc-list-item--with-leading-avatar & {
      margin-left: 16px;
      margin-right: 16px;
      border-radius: 50%;
    }
  }

  .mdc-list-item__end {
    flex-shrink: 0;
    pointer-events: none;

    .mdc-list-item--with-trailing-meta & {
      font-family: token-utils.slot(list-list-item-trailing-supporting-text-font, $fallbacks);
      line-height: token-utils.slot(
          list-list-item-trailing-supporting-text-line-height, $fallbacks);
      font-size: token-utils.slot(list-list-item-trailing-supporting-text-size, $fallbacks);
      font-weight: token-utils.slot(list-list-item-trailing-supporting-text-weight, $fallbacks);
      letter-spacing: token-utils.slot(
          list-list-item-trailing-supporting-text-tracking, $fallbacks);
    }

    .mdc-list-item--with-trailing-icon & {
      color: token-utils.slot(list-list-item-trailing-icon-color, $fallbacks);
      width: token-utils.slot(list-list-item-trailing-icon-size, $fallbacks);
      height: token-utils.slot(list-list-item-trailing-icon-size, $fallbacks);
    }

    .mdc-list-item--with-trailing-icon:hover & {
      color: token-utils.slot(list-list-item-hover-trailing-icon-color, $fallbacks);
    }

    // For some reason this has an increased specificity just for the `color`.
    // Keeping it in place for now to reduce the amount of screenshot diffs.
    .mdc-list-item.mdc-list-item--with-trailing-meta & {
      color: token-utils.slot(list-list-item-trailing-supporting-text-color, $fallbacks);
    }

    .mdc-list-item--selected.mdc-list-item--with-trailing-icon & {
      color: token-utils.slot(list-list-item-selected-trailing-icon-color, $fallbacks);
    }
  }

  .mdc-list-item__content {
    text-overflow: ellipsis;
    white-space: nowrap;
    overflow: hidden;
    align-self: center;
    flex: 1;
    pointer-events: none;

    .mdc-list-item--with-two-lines &,
    .mdc-list-item--with-three-lines & {
      align-self: stretch;
    }
  }

  .mdc-list-item__primary-text {
    text-overflow: ellipsis;
    white-space: nowrap;
    overflow: hidden;

    color: token-utils.slot(list-list-item-label-text-color, $fallbacks);
    font-family: token-utils.slot(list-list-item-label-text-font, $fallbacks);
    line-height: token-utils.slot(list-list-item-label-text-line-height, $fallbacks);
    font-size: token-utils.slot(list-list-item-label-text-size, $fallbacks);
    font-weight: token-utils.slot(list-list-item-label-text-weight, $fallbacks);
    letter-spacing: token-utils.slot(list-list-item-label-text-tracking, $fallbacks);

    .mdc-list-item:hover & {
      color: token-utils.slot(list-list-item-hover-label-text-color, $fallbacks);
    }

    .mdc-list-item:focus & {
      color: token-utils.slot(list-list-item-focus-label-text-color, $fallbacks);
    }

    .mdc-list-item--with-two-lines &,
    .mdc-list-item--with-three-lines & {
      display: block;
      margin-top: 0;
      line-height: normal;
      margin-bottom: -20px;
    }

    .mdc-list-item--with-two-lines &::before,
    .mdc-list-item--with-three-lines &::before {
      display: inline-block;
      width: 0;
      height: 28px;
      content: '';
      vertical-align: 0;
    }

    .mdc-list-item--with-two-lines &::after,
    .mdc-list-item--with-three-lines &::after {
      display: inline-block;
      width: 0;
      height: 20px;
      content: '';
      vertical-align: -20px;
    }
  }

  .mdc-list-item__secondary-text {
    text-overflow: ellipsis;
    white-space: nowrap;
    overflow: hidden;
    display: block;
    margin-top: 0;
    color: token-utils.slot(list-list-item-supporting-text-color, $fallbacks);
    font-family: token-utils.slot(list-list-item-supporting-text-font, $fallbacks);
    line-height: token-utils.slot(list-list-item-supporting-text-line-height, $fallbacks);
    font-size: token-utils.slot(list-list-item-supporting-text-size, $fallbacks);
    font-weight: token-utils.slot(list-list-item-supporting-text-weight, $fallbacks);
    letter-spacing: token-utils.slot(list-list-item-supporting-text-tracking, $fallbacks);

    &::before {
      display: inline-block;
      width: 0;
      height: 20px;
      content: '';
      vertical-align: 0;
    }

    .mdc-list-item--with-three-lines & {
      white-space: normal;
      line-height: 20px;
    }

    .mdc-list-item--with-overline & {
      white-space: nowrap;
      line-height: auto;
    }
  }

  .mdc-list-item--with-leading-radio,
  .mdc-list-item--with-leading-checkbox,
  .mdc-list-item--with-leading-icon,
  .mdc-list-item--with-leading-avatar {
    &.mdc-list-item {
      padding-left: 0;
      padding-right: 16px;

      [dir='rtl'] & {
        padding-left: 16px;
        padding-right: 0;
      }
    }

    &.mdc-list-item--with-two-lines {
      .mdc-list-item__primary-text {
        display: block;
        margin-top: 0;
        line-height: normal;
        margin-bottom: -20px;

        // This was used by MDC to set the text baseline. We should figure out a way to
        // remove it, because it can introduce unnecessary whitespace at the beginning
        // of the element.
        &::before {
          display: inline-block;
          width: 0;
          height: 32px;
          content: '';
          vertical-align: 0;
        }

        &::after {
          display: inline-block;
          width: 0;
          height: 20px;
          content: '';
          vertical-align: -20px;
        }
      }

      &.mdc-list-item--with-trailing-meta {
        .mdc-list-item__end {
          display: block;
          margin-top: 0;
          line-height: normal;

          &::before {
            display: inline-block;
            width: 0;
            height: 32px;
            content: '';
            vertical-align: 0;
          }
        }
      }
    }
  }

  .mdc-list-item--with-trailing-icon {
    &.mdc-list-item {
      // This is the same in RTL, but we need the specificity.
      &, [dir='rtl'] & {
        padding-left: 0;
        padding-right: 0;
      }
    }

    .mdc-list-item__end {
      margin-left: 16px;
      margin-right: 16px;
    }
  }

  .mdc-list-item--with-trailing-meta {
    &.mdc-list-item {
      padding-left: 16px;
      padding-right: 0;

      [dir='rtl'] & {
        padding-left: 0;
        padding-right: 16px;
      }
    }

    .mdc-list-item__end {
      @include vendor-prefixes.user-select(none);
      margin-left: 28px;
      margin-right: 16px;

      [dir='rtl'] & {
        margin-left: 16px;
        margin-right: 28px;
      }
    }

    &.mdc-list-item--with-three-lines .mdc-list-item__end,
    &.mdc-list-item--with-two-lines .mdc-list-item__end {
      display: block;
      line-height: normal;
      align-self: flex-start;
      margin-top: 0;

      &::before {
        display: inline-block;
        width: 0;
        height: 28px;
        content: '';
        vertical-align: 0;
      }
    }
  }

  .mdc-list-item--with-leading-radio,
  .mdc-list-item--with-leading-checkbox {
    .mdc-list-item__start {
      margin-left: 8px;
      margin-right: 24px;

      [dir='rtl'] & {
        margin-left: 24px;
        margin-right: 8px;
      }
    }

    &.mdc-list-item--with-two-lines {
      .mdc-list-item__start {
        align-self: flex-start;
        margin-top: 8px;
      }
    }
  }

  .mdc-list-item--with-trailing-radio,
  .mdc-list-item--with-trailing-checkbox {
    &.mdc-list-item {
      padding-left: 16px;
      padding-right: 0;

      [dir='rtl'] & {
        padding-left: 0;
        padding-right: 16px;
      }
    }

    &.mdc-list-item--with-leading-icon,
    &.mdc-list-item--with-leading-avatar {
      padding-left: 0;

      [dir='rtl'] & {
        padding-right: 0;
      }
    }

    .mdc-list-item__end {
      margin-left: 24px;
      margin-right: 8px;

      [dir='rtl'] & {
        margin-left: 8px;
        margin-right: 24px;
      }
    }

    &.mdc-list-item--with-three-lines .mdc-list-item__end {
      align-self: flex-start;
      margin-top: 8px;
    }
  }

  .mdc-list-group__subheader {
    margin: 0.75rem 16px;
  }

  .mdc-list-item--disabled {
    .mdc-list-item__start,
    .mdc-list-item__content,
    .mdc-list-item__end {
      opacity: 1;
    }

    .mdc-list-item__primary-text,
    .mdc-list-item__secondary-text {
      opacity: token-utils.slot(list-list-item-disabled-label-text-opacity, $fallbacks);
    }

    &.mdc-list-item--with-leading-icon .mdc-list-item__start {
      color: token-utils.slot(list-list-item-disabled-leading-icon-color, $fallbacks);
      opacity: token-utils.slot(list-list-item-disabled-leading-icon-opacity, $fallbacks);
    }

    &.mdc-list-item--with-trailing-icon .mdc-list-item__end {
      color: token-utils.slot(list-list-item-disabled-trailing-icon-color, $fallbacks);
      opacity: token-utils.slot(list-list-item-disabled-trailing-icon-opacity, $fallbacks);
    }
  }

  .mat-mdc-list-item.mat-mdc-list-item-both-leading-and-trailing {
    &, [dir='rtl'] & {
      padding-left: 0;
      padding-right: 0;
    }
  }
}
