@import "../../patternfly-imports";

/* stylelint-disable */
@mixin pf-mobile-layout {
  .pf-m-grid.pf-c-table { @content; }

  .pf-m-grid-md.pf-c-table {
    @media screen and (max-width: $pf-global--breakpoint--md) {
      @content;
    }
  }

  .pf-m-grid-lg.pf-c-table {
    @media screen and (max-width: $pf-global--breakpoint--lg) {
      @content;
    }
  }
}

$pf-c-table-xs-breakpoint: 380px;

.pf-c-table[class*="pf-m-grid"] {
  // ============================================================ //
  // Start non-conformant variables
  //
  // these variables do not conform to guidelines as styling targets individual elements
  // this is purposeful and necessary to avoid adding selectors to each td/th
  // ============================================================ //

  // Responsive vars
  // ==================================================================

  // Body
  --pf-c-table-tbody--responsive--MarginTop: var(--pf-global--spacer--sm);

  // Row
  --pf-c-table-tr--responsive--GridColumnGap: var(--pf-global--spacer--md);
  --pf-c-table-tr--responsive--BackgroundColor: var(--pf-global--BackgroundColor--light-100);
  --pf-c-table-tr--responsive--MarginTop: var(--pf-global--spacer--sm);
  --pf-c-table-tr--responsive--PaddingTop: var(--pf-global--spacer--xl);
  --pf-c-table-tr--responsive--PaddingRight: var(--pf-global--spacer--xl);
  --pf-c-table-tr--responsive--PaddingBottom: var(--pf-global--spacer--xl);
  --pf-c-table-tr--responsive--PaddingLeft: var(--pf-global--spacer--xl);
  --pf-c-table-tr--responsive--nested-table--PaddingTop: var(--pf-global--spacer--xl);
  --pf-c-table-tr--responsive--nested-table--PaddingRight: var(--pf-global--spacer--2xl);
  --pf-c-table-tr--responsive--nested-table--PaddingBottom: var(--pf-global--spacer--xl);
  --pf-c-table-tr--responsive--nested-table--PaddingLeft: var(--pf-global--spacer--2xl);

  // Td
  --pf-c-table-td--responsive--GridColumnGap: var(--pf-global--spacer--md);
  --pf-c-table-cell--responsive--PaddingTop: var(--pf-global--spacer--md);
  --pf-c-table-cell--responsive--PaddingBottom: var(--pf-global--spacer--sm);
  --pf-c-table-cell-th--responsive--PaddingTop: var(--pf-global--spacer--sm);
  --pf-c-table-cell--responsive--PaddingRight: 0;
  --pf-c-table-cell--responsive--PaddingLeft: 0;

  // Compact Table
  --pf-c-table--m-compact-tr-td--responsive--PaddingTop: var(--pf-global--spacer--sm);
  --pf-c-table--m-compact-tr-td--responsive--PaddingBottom: var(--pf-global--spacer--sm);

  // Expandable row content
  --pf-c-table__expandable-row-content--responsive--PaddingRight: var(--pf-global--spacer--xl);
  --pf-c-table__expandable-row-content--responsive--PaddingLeft: var(--pf-global--spacer--xl);

  // Table check
  --pf-c-table__check--responsive--MarginLeft: var(--pf-global--spacer--sm);
  --pf-c-table__check--responsive--MarginTop: #{pf-size-prem(6px)};

  // Table action
  --pf-c-table__action--responsive--MarginLeft: var(--pf-global--spacer--xl);

  // End responsive vars
  // ==================================================================
}

// Check table cell
@include pf-mobile-layout {
  display: grid;
  background-color: transparent;
  border: none;

  // Thead
  thead {
    display: none;
  }

  // Tbody
  tbody {
    display: block;
  }

  // add top margin to siblings
  tbody + tbody {
    margin-top: var(--pf-c-table-tbody--responsive--MarginTop);
  }

  // Table row
  // using background color on tr as they are presented as block level elements for responsive view
  tr {
    background-color: var(--pf-c-table-tr--responsive--BackgroundColor);

    &:not(:first-child):not(.pf-c-table__expandable-row) {
      margin-top: var(--pf-c-table-tr--responsive--MarginTop);
    }
  }

  tbody.pf-m-expanded {
    tr:not(.pf-c-table__expandable-row) {
      border-bottom: 0;
    }
  }

  // Standard table row (non-expandable)
  // exclude expandable rows
  tr:not(.pf-c-table__expandable-row) {
    display: grid;
    grid-template-columns: 1fr;
    height: auto;

    // set subsequent auto column width to max-content
    grid-auto-columns: max-content;
    grid-column-gap: var(--pf-c-table-tr--responsive--GridColumnGap);

    // set base variables to reset later
    padding: var(--pf-c-table-tr--responsive--PaddingTop) var(--pf-c-table-tr--responsive--PaddingRight) var(--pf-c-table-tr--responsive--PaddingBottom) var(--pf-c-table-tr--responsive--PaddingLeft);

    // Reset td padding
    & > * {
      padding: var(--pf-c-table-cell--responsive--PaddingTop) var(--pf-c-table-cell--responsive--PaddingRight) var(--pf-c-table-cell--responsive--PaddingBottom) var(--pf-c-table-cell--responsive--PaddingLeft);
    }

    // remove padding from first th to align with kebab
    th {
      padding-top: var(--pf-c-table-cell-th--responsive--PaddingTop);
    }
  }

  // Remove min-height on buttons
  td.pf-c-table__compound-expansion-toggle,
  th.pf-c-table__compound-expansion-toggle {
    .pf-c-button {
      min-height: 0;
    }
  }

  .pf-c-table.pf-m-compact {
    tr {
      & > * {
        padding-top: var(--pf-c-table--m-compact-tr-td--responsive--PaddingTop);
        padding-bottom: var(--pf-c-table--m-compact-tr-td--responsive--PaddingBottom);
      }
    }
  }

  // Standard td, in standard row (non-expandable)
  // exclude specifically styled tds, as they are placed explicitly within grid
  // :not(.pf-c-table__toggle):not(.pf-c-table__check):not(.pf-c-table__action):not(.pf-c-table__sort)
  tr:not(.pf-c-table__expandable-row) > :not(.pf-c-table__toggle):not(.pf-c-table__check):not(.pf-c-table__action):not(.pf-c-table__sort) {
    // responsive layout, set td to two column grid
    display: grid;
    grid-column: 1;
    grid-column-gap: var(--pf-c-table-td--responsive--GridColumnGap);
    align-items: start;

    // @mobile, breakpoint small
    @media screen and (min-width: $pf-c-table-xs-breakpoint) {
      grid-template-columns: 1fr 1.5fr;
      grid-column: 1;
    }

    // set contents of td to start at column two of td grid
    * {
      @media screen and (min-width: $pf-c-table-xs-breakpoint) {
        grid-column: 2;
      }
    }

    // @mobile only
    // set contents of ::before pseudo element to start at column one of td grid
    &::before {
      font-weight: bold;
      text-align: left;
      content: attr(data-label);

      @media screen and (min-width: $pf-c-table-xs-breakpoint) {
        font-weight: normal;
        // set ::before to span all rows, up to 100
        grid-row: 1;
      }
    }
  }

  // nested tables
  .pf-c-table {
    --pf-c-table-tr--responsive--PaddingTop: var(--pf-c-table-tr--responsive--nested-table--PaddingTop);
    --pf-c-table-tr--responsive--PaddingRight: var(--pf-c-table-tr--responsive--nested-table--PaddingRight);
    --pf-c-table-tr--responsive--PaddingBottom: var(--pf-c-table-tr--responsive--nested-table--PaddingBottom);
    --pf-c-table-tr--responsive--PaddingLeft: var(--pf-c-table-tr--responsive--nested-table--PaddingLeft);

    border: 0;

    tr:not(.pf-c-table__expandable-row) + tr:not(.pf-c-table__expandable-row) {
      --pf-c-table-tr--responsive--PaddingTop: 0;
    }
  }

  .pf-c-table__compound-expansion-toggle {
    // Compound expansion toggle
    // reset compound expansion toggle::before
    &::before {
      position: static;
      border: 0;
    }

    // reset compound expansion toggle::after
    &::after {
      top: auto;
      right: 0;
      left: 0;
    }

    // add bottom border to expansion toggle::after
    &.pf-m-expanded::after {
      border-bottom: var(--pf-c-table__compound-expansion-toggle--BorderTop--BorderWidth) solid var(--pf-c-table__compound-expansion-toggle--BorderTop--BorderColor);
    }

    // reset button padding
    .pf-c-button {
      padding: 0;
    }

    &.pf-m-expanded .pf-c-button::after,
    &:hover .pf-c-button::after {
      border-right: 0;
      border-left: 0;
    }

    &.pf-m-expanded .pf-c-button::before,
    &:hover .pf-c-button::before {
      top: auto;
      bottom: 0;
    }
  }

  // COMPOUND EXPANSION RESPONSIVE
  // Tbody
  tbody {
    position: relative;

    &::before {
      position: absolute;
      top: 0;
      bottom: 0;
      left: 0;
      z-index: 10;
      width: var(--pf-c-table__expandable-row--before--Width);
      content: "";
    }

    &.pf-m-expanded::before {
      background-color: var(--pf-c-table__expandable-row--before--BackgroundColor);
    }

    > tr > :first-child::before {
      position: static;
      width: auto;
      background-color: transparent;
    }
  }
  // COMPOUND EXPANSION RESPONSIVE END


  // Expandable row
  .pf-c-table__expandable-row {
    --pf-c-table-cell--responsive--PaddingTop: 0;
    --pf-c-table-cell--responsive--PaddingRight: 0;
    --pf-c-table-cell--responsive--PaddingBottom: 0;
    --pf-c-table-cell--responsive--PaddingLeft: 0;
    --pf-c-table-cell--PaddingRight: 0;
    --pf-c-table-cell--PaddingLeft: 0;
    --pf-c-table__expandable-row-content--PaddingRight: var(--pf-c-table__expandable-row-content--responsive--PaddingRight);
    --pf-c-table__expandable-row-content--PaddingLeft: var(--pf-c-table__expandable-row-content--responsive--PaddingLeft);

    display: block;
    border-bottom: none;
    box-shadow: none;

    // cells
    > * {
      position: static;
      display: block;
    }

    // Modifier - Expanded tr
    &.pf-m-expanded {
      border-top-color: var(--pf-c-table--BorderColor);
    }

    td:first-child::before {
      // Border treatment
      display: none;
    }
  }

  // Overflow scroll should only happen on responsive
  .pf-c-table__expandable-row-content {
    tr.pf-m-expanded & {
      max-height: var(--pf-c-table__expandable-row-content--MaxHeight);
      overflow-y: scroll;
    }
  }

  // Set defaults
  tr:not(.pf-c-table__expandable-row) {
    .pf-c-table__toggle,
    .pf-c-table__check,
    .pf-c-table__action {
      width: auto;
      padding: 0;
    }
  }

  .pf-c-table__toggle {
    grid-row-start: 20;
    grid-column: -1;
    justify-self: end;
    padding-right: 0;
  }

  // Check table cell
  .pf-c-table__check {
    margin-top: var(--pf-c-table__check--responsive--MarginTop);
    margin-left: var(--pf-c-table__check--responsive--MarginLeft);
    grid-row-start: 1;
    grid-column-start: 2;

    // if action is present, add margin left to accommodate checkbox
    & ~ .pf-c-table__action {
      margin-left: var(--pf-c-table__action--responsive--MarginLeft);
    }
  }

  // Action cell
  .pf-c-table__action {
    grid-row-start: 1;
    grid-column-start: 2;

    // @smallest breakpoint
    @media screen and (max-width: $pf-global--breakpoint--sm) {
      grid-row-start: 1;
      grid-column-start: 2;
      margin-left: 0;
    }
  }

  // Toggles
  .pf-c-table__toggle {
    .pf-c-button > * {
      transform: rotate(0deg);
    }

    .pf-c-button.pf-m-expanded > * {
      transform: rotate(180deg);
    }
  }

  // Fit content
  .pf-m-fit-content {
    width: auto;
    white-space: normal;
  }
}

/* stylelint-enable */
