/**
 * Table
 * @define table
 */

@use 'sass:map';
@use '@ecl/grid/mixins/breakpoints';
@use '@ecl/utility-screen-reader/screen-reader';
@use '@ecl/mixins-color/mixins' as mixins-color;

// Exposed variables
$theme: null !default;
$table: null !default;

.ecl-table,
%ecl-table {
  border-collapse: collapse;
  color: map.get($table, 'table', 'color');
  margin: 0;
  width: 100%;
  font: map.get($table, 'table', 'font');
}

.ecl-table-responsive {
  overflow-x: auto;
}

.ecl-table__head,
%ecl-table__head {
  border-block-end: map.get($table, 'head', 'border');
  border-inline-end: 1px solid map.get($table, 'head', 'background');
  background-color: map.get($table, 'head', 'background');
  display: table-header-group;
}

.ecl-table__header,
%ecl-table__header {
  border-inline-end: map.get($table, 'header', 'separator-desktop');
  border-inline-start: map.get($table, 'header', 'separator-desktop');
  font: map.get($table, 'header', 'font');
  font-weight: map.get($table, 'header', 'font-weight');
  min-width: 12.1875rem;
  padding: map.get($table, 'header', 'padding');
  text-align: start;
  vertical-align: top;

  &:first-child {
    border-inline-start-color: map.get($table, 'head', 'border-inline-color');
  }

  &:last-child {
    border-inline-end-color: map.get($table, 'head', 'border-inline-color');
  }
}

.ecl-table__row,
%ecl-table__row {
  border-block-end: map.get($table, 'row-border');
  border-block-start-width: 0;

  .ecl-table__body &:first-child {
    border-block-start: map.get($table, 'head', 'head-border-top');
  }

  // stylelint-disable-next-line no-descending-specificity
  .ecl-table__head + .ecl-table__body &:first-child {
    border-block-start-width: 0;
  }
}

// stylelint-disable-next-line no-descending-specificity
.ecl-table__head .ecl-table__row:first-child {
  border-block-end: map.get($table, 'header', 'border');

  .ecl-table__header {
    padding-block-start: var(--s-m);
  }
}

.ecl-table__cell,
%ecl-table__cell {
  border-inline-start: map.get($table, 'cell', 'separator');
  display: table-cell;
  padding: map.get($table, 'cell', 'padding-desktop');

  &:last-child {
    border-inline-end: map.get($table, 'cell', 'separator');
  }
}

.ecl-table__caption {
  caption-side: bottom;
  color: map.get($table, 'caption', 'color');
  margin-block-start: var(--s-xs);
  text-align: start;
  font: map.get($table, 'caption', 'font');
}

// Zebra
.ecl-table--zebra {
  // stylelint-disable-next-line no-descending-specificity
  .ecl-table__row:nth-of-type(even) {
    background-color: map.get($table, 'zebra-background');
  }
}

// Multi
// stylelint-disable-next-line no-descending-specificity
.ecl-table__head .ecl-table__row:not(:first-child) {
  background-color: map.get($table, 'multi-head', 'background-empty');
  border-block-end-width: 0;
}

.ecl-table__head
  .ecl-table__row:not(:first-child)
  .ecl-table__header:not(:empty) {
  background-color: map.get($table, 'multi-head', 'background');
  border-block-start: map.get($table, 'header', 'separator-desktop');

  &:last-child {
    border-inline-end: none;
  }
}

// Sort Table
.ecl-table__header--sortable {
  background-clip: padding-box;
  block-size: 1px;
  cursor: pointer;
}

.ecl-table__arrow {
  align-self: flex-start;
  background-color: transparent;
  border: 0;
  cursor: pointer;
  display: flex;
  flex-shrink: 0;
  height: auto;
  padding: 0.25rem;
  width: auto;

  @include mixins-color.focus-default;

  &:hover {
    background-color: map.get($table, 'sort', 'background-color-hover');
  }
}

.ecl-table__header-inner {
  block-size: 100%;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.ecl-table__header-label {
  display: flex;
  gap: var(--s-xs);
  justify-content: space-between;
}

.ecl-table__icon {
  color: map.get($table, 'sort', 'icon-color');
  height: 1.25rem;
  width: 1.25rem;
}

.ecl-table__header[aria-sort='ascending'] {
  .ecl-table__icon {
    color: map.get($table, 'sort', 'icon-color-active');
  }
}

.ecl-table__header[aria-sort='descending'] {
  .ecl-table__icon {
    color: map.get($table, 'sort', 'icon-color-active');
    transform: rotate(180deg);
  }
}

// Filter Table
.ecl-table__header--filterable {
  block-size: 1px;
}

.ecl-table__filter {
  display: block;
  margin-block-start: var(--s-xs);
}

.ecl-table__filter-input {
  --ecl-text-field-height: 2.25rem;

  width: 100%;
}

.ecl-table__filter-label {
  @include screen-reader.sr-only;
}

// Enhanced table (mobile)
/* stylelint-disable-next-line order/order */
@include breakpoints.down('m') {
  .ecl-table:not(.ecl-table--simple) {
    .ecl-table__head {
      display: none;
    }

    .ecl-table__row {
      border-block-end: none;
    }

    .ecl-table__cell {
      border-inline-start: map.get(
        $table,
        'cell',
        'border-inline-start-mobile'
      );
      border-inline-end: map.get($table, 'cell', 'separator');
      border-block-start: map.get($table, 'row-border');
      display: flex;
      padding: map.get($table, 'cell', 'padding-mobile');

      &::before {
        background-color: map.get($table, 'head', 'background');
        border-block-end: map.get($table, 'header', 'separator-desktop');
        border-inline-end: 2px solid map.get($table, 'head', 'background');
        content: attr(data-ecl-table-header);
        display: block;
        flex-basis: 10rem;
        flex-grow: 0;
        flex-shrink: 0;
        font-weight: map.get($table, 'header', 'font-weight');
        margin-block-end: map.get($table, 'cell', 'margin-mobile', 'bottom');
        margin-inline-end: map.get($table, 'cell', 'margin-mobile', 'end');
        margin-inline-start: map.get($table, 'cell', 'margin-mobile', 'start');
        margin-block-start: map.get($table, 'cell', 'margin-mobile', 'top');
        padding: map.get($table, 'cell', 'padding-mobile');
      }

      &:first-child::before {
        margin-block-start: calc(
          map.get($table, 'cell', 'margin-mobile', 'top') - 1px
        );
      }

      &:last-of-type {
        border-block-end: map.get($table, 'row-border');
        margin-block-end: var(--s-xs);

        &::before {
          border-block-end: none;
        }
      }
    }

    .ecl-table__row:last-of-type .ecl-table__cell:last-of-type {
      margin-block-end: 0;
    }

    .ecl-table__cell[data-ecl-table-header-group] {
      display: grid;
      grid-template-columns:
        calc(10rem + #{map.get($table, 'multi-head', 'position')} * 2 + 2px)
        1fr;
      grid-template-rows: auto 1fr;
      position: relative;

      &::after {
        background-color: map.get($table, 'head', 'background');
        border-block-end: map.get($table, 'header', 'group-separator');
        border-block-start: map.get($table, 'header', 'group-separator');
        content: attr(data-ecl-table-header-group);
        display: block;
        font-weight: map.get($table, 'header', 'font-weight');
        grid-column: span 2;
        inset-inline: calc(-1 * #{map.get($table, 'multi-head', 'position')});
        margin-block-end: -2px;
        order: -1;
        padding: map.get($table, 'cell', 'padding-mobile');
        position: relative;
        top: calc(-1 * #{map.get($table, 'multi-head', 'position')} - 2px);
        width: 100%;
      }
    }

    .ecl-table__cell--group::before {
      border-inline-end: map.get($table, 'header', 'separator-mobile');
    }
  }

  .ecl-table--zebra:not(.ecl-table--simple) {
    .ecl-table__row:nth-of-type(even) {
      background-color: transparent;
    }

    // stylelint-disable-next-line no-descending-specificity
    .ecl-table__cell:nth-of-type(even) {
      background-color: map.get($table, 'zebra-background');
    }
  }
}
