//
// Basic OUDS Web table
//
%active {
  --#{$prefix}table-color-state: var(--#{$prefix}color-content-default);
  --#{$prefix}table-bg-state: var(--#{$prefix}table-active-bg);
}

.table {
  // Reset needed for nesting tables
  --#{$prefix}table-color-type: initial;
  --#{$prefix}table-bg-type: initial;
  --#{$prefix}table-color-state: initial;
  --#{$prefix}table-bg-state: initial;
  // End of reset
  --#{$prefix}table-color: #{$table-color};
  --#{$prefix}table-bg: #{$table-bg};
  --#{$prefix}table-border-color: #{$table-border-color};
  --#{$prefix}table-accent-bg: #{$table-accent-bg};
  --#{$prefix}table-striped-color: #{$table-striped-color};
  --#{$prefix}table-striped-bg: #{$table-striped-bg};
  --#{$prefix}table-active-color: #{$table-active-color};
  --#{$prefix}table-active-bg: #{$table-active-bg};
  --#{$prefix}table-hover-color: #{$table-hover-color};
  --#{$prefix}table-hover-bg: #{$table-hover-bg};

  width: 100%;
  margin-bottom: $spacer;
  vertical-align: $table-cell-vertical-align;

  // OUDS mod
  //// Only rows have border
  //// Set line-height everywhere

  // stylelint-disable selector-no-qualifying-type
  th[scope="col"],
  th[scope="colgroup"] {
    border-bottom: $table-header-border-width solid var(--#{$prefix}table-border-color);
  }

  tr  {
    border-bottom: $table-border-width solid var(--#{$prefix}table-border-color);
  }

  // Target th & td
  // We need the child combinator to prevent styles leaking to nested tables which doesn't have a `.table` class.
  // We use the universal selectors here to simplify the selector (else we would need 6 different selectors).
  // Another advantage is that this generates less code and makes the selector less specific making it easier to override.
  // stylelint-disable-next-line selector-max-universal
  > :not(caption) > * > * {
    padding: $table-cell-padding-y $table-cell-padding-x calc($table-cell-padding-y + 1px);
    line-height: $table-line-height;
    // Following the precept of cascades: https://codepen.io/miriamsuzanne/full/vYNgodb
    color: var(--#{$prefix}table-color-state, var(--#{$prefix}table-color-type, var(--#{$prefix}table-color)));
    background-color: var(--#{$prefix}table-bg);
    box-shadow: inset 0 0 0 9999px var(--#{$prefix}table-bg-state, var(--#{$prefix}table-bg-type, var(--#{$prefix}table-accent-bg)));
  }

  > thead > tr {
    border-bottom-width: calc(#{$table-border-width} * 2);
  }

  > tbody > th {
    font-weight: $font-weight-normal;
  }
  // End mod

  > tbody {
    vertical-align: inherit;
  }

  > thead {
    vertical-align: bottom;
  }

  // OUDS mod
  // When using checkboxes in the first column, force width to ensure correct spacing on this column
  tr > *:has(.control-item-indicator[type="checkbox"]:not([role="switch"]), .control-item-indicator[type="radio"]) {
    width: 1%;
    padding: 0;
    vertical-align: middle;
  }

  // Extend checkboxes and radios click area
  tr .radio-button-standalone,
  tr .checkbox-standalone {
    width: stretch;
  }

  // Automatically highlight rows with checked checkboxes or radios
  tbody tr:has(.control-item-indicator[type="checkbox"]:not([role="switch"]):checked, .control-item-indicator[type="radio"]:checked) {
    @extend %active;
  }

  // Use negative margins on icons in tables to preserve row height
  svg,
  img {
    margin-top: $table-cell-icon-margin-top;
    margin-bottom: $table-cell-icon-margin-bottom;
  }

  .table-cell-component {
    padding-block: 0;
    vertical-align: middle;

    > * {
      vertical-align: middle;
    }
  }
  // End mod
}

.table-group-divider {
  border-top: calc(#{$table-border-width} * 2) solid $table-group-separator-color;
}

//
// Change placement of captions with a class
//

.caption-bottom {
  caption-side: bottom; // OUDS mod
}

// OUDS mod: no .table-bordered

// OUDS mod: no .table-borderless

// Zebra-striping
//
// Default zebra-stripe styles (alternating gray and transparent backgrounds)

// For rows
.table-striped {
  > tbody > tr:nth-of-type(#{$table-striped-order}) > * {
    --#{$prefix}table-color-type: var(--#{$prefix}table-striped-color);
    --#{$prefix}table-bg-type: var(--#{$prefix}table-striped-bg);
  }
}

// For columns
.table-striped-columns {
  > :not(caption) > tr > :nth-child(#{$table-striped-columns-order}) {
    --#{$prefix}table-color-type: var(--#{$prefix}table-striped-color);
    --#{$prefix}table-bg-type: var(--#{$prefix}table-striped-bg);
  }
}

// Active table
//
// The `.table-active` class can be added to highlight rows or cells
.table-active[data-bs-theme="dark"] {
  @extend %active;
}

// Hover effect
//
// Placed here since it has to come after the potential zebra striping

.table-hover {
  > tbody > tr:hover > * {
    --#{$prefix}table-color-state: var(--#{$prefix}table-hover-color);
    --#{$prefix}table-bg-state: var(--#{$prefix}table-hover-bg);
  }
}

// Table functional colors
@each $table-state in ("info", "positive", "warning", "negative") {
  .table-#{$table-state} {
    --#{$prefix}table-bg: var(--#{$prefix}color-surface-status-#{$table-state}-muted);
  }
}

// Responsive tables
//
// Generate series of `.table-responsive-*` classes for configuring the screen
// size of where your table will overflow.

@each $breakpoint in map-keys($grid-breakpoints) {
  @include media-breakpoint-down($breakpoint) {
    $breakpoint-prefix: breakpoint-prefix($breakpoint, $grid-breakpoints);
    .#{$breakpoint-prefix}table-responsive {
      overflow-x: auto;
      -webkit-overflow-scrolling: touch;
    }

    @if $enable-bootstrap-compatibility {
      .table-responsive#{breakpoint-infix($breakpoint, $grid-breakpoints)} {
        @extend .#{$breakpoint-prefix}table-responsive;
      }
    }
  }
}
