/*
 * Copyright (c) 2019-present Sonatype, Inc.
 * This program and the accompanying materials are made available under
 * the terms of the Eclipse Public License 2.0 which accompanies this
 * distribution and is available at https://www.eclipse.org/legal/epl-2.0/.
 */
@use '../scss-shared/nx-container-helpers';
@use '../scss-shared/nx-text-helpers';
@use '../scss-shared/nx-button-helpers';

.nx-list {
  margin: var(--nx-spacing-4x) 0 var(--nx-spacing-6x) 0;
  overflow-wrap: anywhere;
  padding: 0;

  // focused rows get a higher z-index; make a stacking context for them
  position: relative;
  z-index: 0;

  &, > ul, > ol {
    list-style: none;
  }

  > ul, > ol {
    margin-top: var(--nx-spacing-3x);
    padding: 0;
  }
}

.nx-list__item {
  &, > .nx-list__link, & > .nx-list__btn {
    --nx-padding-without-border: calc(var(--nx-spacing-4x) - 1px);

    align-items: first baseline;
    border: 1px solid transparent;
    border-top-color: var(--nx-color-border-secondary);
    box-sizing: border-box;
    display: grid;
    grid-template-columns: auto 1fr auto;
    grid-template-rows: auto auto;
    grid-template-areas:
      "indicator list-text actions"
      ". list-sub-text actions";
    margin: 0;
    // 1px of padding subtracted to account for the transparent border
    padding: var(--nx-spacing-4x) var(--nx-padding-without-border)
      var(--nx-padding-without-border) var(--nx-padding-without-border);

    > .nx-chevron {
      margin-right: 0;
    }
  }

  &:first-child {
    &, > .nx-list__link, & > .nx-list__btn {
      border-top: var(--nx-border-default);
    }
  }

  &.disabled {
    color: var(--nx-color-text-disabled);
    cursor: default;
  }

  > .nx-loading-spinner {
    grid-area: list-text;
    margin: 0;
    text-align: center;
  }
}

.nx-list__link {
  text-decoration: none;
}

.nx-list__text {
  @include nx-text-helpers.semi-bold;

  display: block;
  grid-area: list-text;
  margin: 0;
  padding-right: var(--nx-spacing-6x);
  max-width: var(--nx-width-text-wrapping);


  &.nx-truncate-ellipsis {
    // Something about the grid layout gets confused due to overflow: hidden on this element
    // and adds vertical space to the second row of the grid.
    // Experimentally, setting an explicit height here fixes that.
    height: calc(var(--nx-line-height) * 1em);
    max-width: unset;
  }

  @include nx-container-helpers.container-horizontal;
}

.nx-list__subtext {
  @include nx-text-helpers.regular;

  display: block;
  grid-area: list-sub-text;
  // top margin is measured down from the top of the objects grid row
  margin: var(--nx-spacing-4x) 0 0 0;
  max-width: var(--nx-width-text-wrapping);

  &.nx-truncate-ellipsis {
    max-width: unset;
  }
}

.nx-list__item--empty {
  display: list-item;
  text-align: center;

  .nx-list__text {
    @include nx-text-helpers.regular;
    max-width: unset;
  }
}

.nx-list__item--error {
  display: list-item;
  padding-left: 0;
  padding-right: 0;

  .nx-alert {
    margin-left: auto;
    margin-right: auto;
  }

  @include nx-container-helpers.container-vertical;
}

.nx-list--bulleted, .nx-list--numbered {
  border-top: none;
  margin: var(--nx-spacing-4x) 0 var(--nx-spacing-6x) 0;
  padding: 0 0 0 26px;

  .nx-list__item {
    border: none;
    display: list-item;
    list-style: disc outside;
    padding: 8px 0 0 0;

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

    .nx-list__text {
      display: inline-block;
      font-weight: normal;

      // this is half the value specified in the designs. Browsers calculate the distance between the bullet
      // and the text differently and this is the "least wrong" solution across all browsers.
      margin-left: -3px;
      // solves problem where bullet is beside 2nd line of wrapped text
      vertical-align: top;
    }

    .nx-list {
      margin: 0;
      padding: 0 0 0 14px;
    }

    .nx-list__item {
      list-style-type: circle;
      padding-top: var(--nx-spacing-2x);

      &:first-child {
        padding-top: var(--nx-spacing-2x);
      }

      .nx-list__text {
        @include nx-text-helpers.regular;
      }

      .nx-list__item {
        list-style-type: square;
      }
    }

    @include nx-container-helpers.container-vertical;
  }
}

.nx-list--unstyled {
  list-style-type: none;
  margin: 0;
  padding: 0;
}

/*
 * Action or Clickable Lists
 */

.nx-list__item--link, .nx-list__item--clickable {
  display: contents;

  > .nx-list__term, > .nx-list__description {
    display: contents;
  }
}

.nx-list__item--clickable, // preferred
.nx-list__item--link, // deprecated
.nx-list--clickable .nx-list__item // deprecated
{
  &, > .nx-list__term, > .nx-list__description, > .nx-list__link, > .nx-list__btn {
    &:hover {
      background-color: var(--nx-color-list-background-hover);
      color: var(--nx-color-list-text);
    }

    &:focus, &:focus-within {
      outline: var(--nx-focus-outline-width) solid var(--nx-color-interactive-border-focus);
      outline-offset: calc(0px - var(--nx-focus-outline-offset));
      position: relative;
      z-index: 1;
    }

    &:active {
      background-color: var(--nx-color-interactive-background-active);
      color: var(--nx-color-list-text);
      outline: none;
    }


    &.nx-list__item--empty {
      @include nx-text-helpers.regular;

      color: var(--nx-color-text);
    }

    &.disabled, &[disabled] {
      color: var(--nx-color-text-disabled);

      &:hover {
        background-color: transparent;
        cursor: default;
      }

      .nx-chevron {
        display: none;
      }
    }

    .nx-chevron {
      align-self: center;
      color: var(--nx-color-list-icon);
      grid-area: actions;

      // custom sizing for the icon, brings it up to 13px
      font-size: 21px;
    }
  }

  &, .nx-list__link, .nx-list__btn {
    color: inherit;
    cursor: pointer;

    &.selected {
      &, &:hover {
        background-color: var(--nx-color-interactive-background-selected);
        color: var(--nx-color-list-text-selected);
      }
    }
  }
}

.nx-list__btn {
  text-align: left;
  width: 100%;

  &, &:hover, &:active, &:focus {
    background: none;
    font: inherit;
    outline: none;
  }
}

.nx-list__actions {
  grid-area: actions;
  // list items have 16px of padding, but the buttons are supposed to be 12px from the top
  margin: -4px 0 -4px var(--nx-spacing-4x);

  .nx-btn {
    margin: 0;

    + .nx-btn {
      margin-left: 8px;
    }
  }

  .nx-btn--icon-only {
    @include nx-button-helpers.small-icon-btn;
  }

  @include nx-container-helpers.container-horizontal;
}

.nx-list--description-list {
  .nx-list__item {
    align-items: stretch;
    display: flex;
    padding: 0;
  }

  .nx-list__item--clickable {
    &:focus-within {
      .nx-list__link, .nx-list__btn {
        outline: none;
      }
    }

    &.disabled, &[disabled] {
      .nx-list__link, .nx-list__btn {
        color: var(--nx-color-text-disabled);
        cursor: default;
      }
    }
  }
}

.nx-list__term {
  &, > .nx-list__link, > .nx-list__btn {
    border: none;
    box-sizing: border-box;

    // for proper vertical alignment of text in buttons
    display: flex;
    flex: none;
    font-weight: 600;
    padding: var(--nx-spacing-4x) 0 var(--nx-padding-without-border) var(--nx-padding-without-border);
    width: 250px;
  }
}

.nx-list__description {
  &, > .nx-list__link, > .nx-list__btn {
    flex: 1;
    margin: 0;
    padding: var(--nx-spacing-4x) var(--nx-padding-without-border)
      var(--nx-padding-without-border) var(--nx-spacing-6x);

    @include nx-container-helpers.container-vertical;
  }

  > .nx-list__link, > .nx-list__btn {
    border: none;
    display: grid;
    grid-template: "list-text actions" / 1fr auto;

    > .nx-list__text {
      font-weight: normal;
    }
  }

  .nx-list__text {
    max-width: var(--nx-width-text-wrapping);
  }
}

.nx-list--numbered {
  padding: 0 0 0 var(--nx-spacing-12x);

  .nx-list__item {
    list-style: decimal outside;

    .nx-list__text {
      margin-left: 0;
    }

    .nx-list {
      padding-left: var(--nx-spacing-12x);
    }

    .nx-list__item {
      list-style: lower-alpha outside;

      &::marker {
        font-weight: 600;
      }

      .nx-list__item {
        list-style: lower-roman outside;

        &::marker {
          font-style: italic;
          font-weight: normal;
        }
      }
    }
  }
}
