/*
 * Lists
 *
 */

@use '../../../style/elements/ui-spacing.scss';
@import '../../../style/core/utilities.scss';
@import '../../../components/space/style/space-mixins.scss';

@mixin listDefaults() {
  & ul,
  & ol {
    margin-top: 0;
    margin-bottom: 1.5rem;
    padding-left: 2rem; // to adjust the left spacing of child lists
  }

  & li,
  &--outside li {
    position: relative;

    &::before {
      position: absolute;
      left: -10rem;
      width: 10rem;
      padding-right: 0.25rem;
      text-align: right;
    }
  }
  &--inside li {
    &::before {
      position: relative;
      left: 0;
      width: auto;
      padding-right: 0;
      text-align: initial;
    }
  }

  .dnb-anchor {
    font-size: inherit;
  }
}

@mixin ulStyle() {
  @include spaceReset();

  padding: 0;
  &:not([class*='dnb-space__left']) {
    padding-left: 2rem;
  }

  font-size: var(--font-size-basis);
  line-height: var(--line-height-basis);

  @include listDefaults();

  & li {
    display: list-item;

    margin-top: 0.5rem;
    margin-bottom: 0.5rem;

    // NB: If we set this, it will force other places, to do so
    // list-style-type: inherit;
  }
}

@mixin olStyle() {
  @include ulStyle();

  @include listDefaults();

  & > li {
    list-style-type: decimal;
  }

  // custom counter
  &--nested,
  &--nested ol {
    counter-reset: item;
  }
  &--nested li {
    display: block; // instead of list-item;

    &::before {
      content: counters(item, '.') '. '; // with dot
      counter-increment: item;
    }

    & li::before {
      content: counters(item, '.') ' '; // no dot
      counter-increment: item;
    }
  }

  &[type] {
    li {
      display: list-item;
      list-style-type: inherit;

      &::before,
      & li::before {
        content: none;
      }
    }
  }
}

@mixin dlStyle() {
  --column-gap: 1rem;
  --row-gap: 0.5rem;

  &:not([class*='dnb-space__top']) {
    margin-top: 0;
  }
  &:not([class*='dnb-space__bottom']) {
    margin-bottom: 0;
  }
  padding: 0;

  font-size: var(--font-size-basis);
  line-height: var(--line-height-basis);

  & dt {
    padding: 0;
    font-weight: var(--font-weight-medium);
  }
  & dd ~ dt {
    margin-top: var(--row-gap);
  }
  & dt:not(:first-of-type) {
    margin-top: calc(var(--row-gap) + 1rem);
  }
  & dd {
    padding: 0;
    margin: 0.5rem 0 1rem;

    // In case we really want / need to have a indented definition content
    // margin-left: 2rem;
  }
  & > dd > dl,
  &:not([class*='dnb-space']) > dd > dl {
    margin-top: calc(var(--row-gap) + 0.5rem);
    margin-left: 2rem;
  }

  // Inline
  @include allAbove(small) {
    &__layout--horizontal {
      --dt-max-width: 40%;
      --dd-max-width: calc(60% - 1rem); // minus margin-right and the gap

      // We cannot use gap: 0.5rem because of our wrapper "__item"
      display: flex;
      flex-wrap: wrap;
      max-width: var(--prose-max-width); // to enhance readability
      width: 100%;

      & dt {
        margin-top: 0;
        margin-right: calc(var(--column-gap) - 0.5rem);
        max-width: var(--dt-max-width);
      }

      & dd {
        width: var(--dd-max-width);
        align-self: end;
      }

      & dt:not(:first-of-type),
      & dd,
      & dd ~ dt:not(:first-of-type) {
        margin-top: var(--row-gap);
        margin-bottom: 0;
        &:first-of-type {
          margin-top: 0;
        }
      }
    }

    dd#{&}__item {
      visibility: hidden;
      user-select: none;
      flex-basis: 100%; // ensures we always break to a new line, no matter what the available space is
      height: 0;
      margin: 0;
    }
    &:not(#{&}__layout--horizontal) dd#{&}__item {
      display: none;
    }

    &__layout--grid {
      --dt-max-width: 15ch;
      --dd-max-width: 40ch;

      display: grid;
      grid-template-columns: max-content 1fr;
      column-gap: var(--column-gap);
      row-gap: var(--row-gap);

      /* stylelint-disable */
      & dt {
        max-width: var(--dt-max-width);
      }
      & dt:not(:first-of-type) {
        margin-top: 0;
      }
      & dd {
        max-width: var(--dd-max-width);
        align-self: end;
      }
      & dt,
      & dd,
      & dd ~ dt {
        margin: 0;
      }
      /* stylelint-enable */
    }
  }
}

@mixin listSpacing() {
  @include ui-spacing.defaultSpacing();
  & > li {
    margin-top: 1rem;
    margin-bottom: 1rem;

    /* stylelint-disable-next-line */
    & > p {
      margin-top: 1rem;
    }
  }
  & li > ul,
  &:not([class*='dnb-space']) li > ul,
  & li > ol,
  &:not([class*='dnb-space']) li > ol {
    margin-top: -0.5rem;
  }
}

@mixin dlSpacing() {
  @include ui-spacing.defaultSpacing();
}

@mixin ulTag() {
  ul {
    @include ulStyle();
  }
  .dnb-spacing ul {
    @include listSpacing();
  }
}

@mixin olTag() {
  ol {
    @include olStyle();
  }
  .dnb-spacing ol {
    @include listSpacing();
  }
}

@mixin dlTag() {
  dl {
    @include dlStyle();
  }
  .dnb-spacing {
    dl {
      @include dlSpacing();
    }
  }
}

// Un-styled list helper
@mixin unstyledList() {
  list-style-type: none;

  @include spaceReset();
  &:not([class*='dnb-space__left']) {
    padding-left: 0;
  }

  > li {
    list-style-type: none;

    &::before {
      content: none;
    }
  }
  > dl,
  > dd {
    margin-left: 0;
  }
}
