/*
 * 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';
@use '../scss-shared/_nx-table-variables';

.nx-table {
  background-color: var(--nx-color-component-background);
  border-spacing: 0;
  border: var(--nx-border-default);
  border-radius: var(--nx-border-radius);
  display: table;
  margin: var(--nx-spacing-3x) 0 var(--nx-spacing-6x) 0;
  overflow: hidden;
  width: 100%;

  > .nx-h2 {
    text-align: left;
    margin: var(--nx-spacing-3x) 0 var(--nx-spacing-6x) 0;
  }

  > tbody > .nx-table-row {
    > .nx-cell {
      $border-width: 1px;

      border-width: $border-width;
      border-color: transparent;
      border-style: none;

      border-bottom-style: solid;
      border-bottom-color: var(--nx-color-border-secondary);
      border-top-style: solid;

      // we want the perceived padding to be the standard spacing size. As the top border is generally invisible,
      // it should be counted as padding from that perspective
      padding-top: calc(var(--nx-spacing-3x) - #{$border-width});

      &:first-child {
        border-left-style: solid;
        padding-left: calc(var(--nx-spacing-4x) - #{$border-width});
      }

      &:last-child {
        border-right-style: solid;
        padding-right: calc(var(--nx-spacing-4x) - #{$border-width});
      }

      // Used for table cells container meta-information such as the fact that the table is empty, or
      // that the data is loading. These cells are expected to take up the entire row using colspan attributes
      &.nx-cell--meta-info {
        padding-top: calc(var(--nx-spacing-4x) - 1px);
        padding-bottom: var(--nx-spacing-4x);
        text-align: center;

        > * {
          margin-left: auto;
          margin-right: auto;
        }
      }
    }

    &:last-child {
      > .nx-cell {
        border-bottom-style: none;
        height: calc(#{nx-table-variables.$table-cell-min-height} - 1px);
      }

      > :first-child {
        border-bottom-left-radius: var(--nx-border-radius);
      }

      > :last-child {
        border-bottom-right-radius: var(--nx-border-radius);
      }
    }

    &.nx-clickable {
      &:hover {
        color: var(--nx-color-text-stark);
        outline: var(--nx-outline-width-table-row-hover) solid var(--nx-color-border);
        outline-offset: calc(0px - var(--nx-focus-outline-offset));
        > .nx-cell {
          background-color: var(--nx-color-table-row-background-hover);
        }
      }

      &.selected {
        color: var(--nx-color-text-stark);
        > .nx-cell {
          background-color: var(--nx-color-interactive-background-selected);
        }
      }

      &:focus-within {
        outline: var(--nx-focus-outline-width) solid var(--nx-color-interactive-border-focus);
        outline-offset: calc(0px - var(--nx-focus-outline-offset));
      }
      
      &:active {
        color: var(--nx-color-text-stark);
        outline: 0;
        > .nx-cell {
          background-color: var(--nx-color-interactive-background-active);
        }
      }
    }
  }
}

.nx-table-container {
  background-color: var(--nx-color-component-background);
  border: var(--nx-border-default);
  border-radius: var(--nx-border-radius);
  box-sizing: content-box;
  display: flex;
  flex-direction: column;
  overflow: hidden;

  // create a stacking context so the sticky content doesn't appear over other content outside the container
  position: relative;
  z-index: 0;

  // for specificity
  &.nx-scrollable {
    overflow-y: auto;
  }

  // NOTE: these styles used to be on .nx-table--scrollable
  > .nx-table {
    border-style: none;
    border-radius: 0;
    margin: 0;
    overflow: visible;

    // In Chrome (at least), tables are forced to have a whole number of pixels for their width. This means that
    // if a container has a fractional number of pixels width, the table width will be truncated down and a visible
    // gap will appear.  This calc ensures that when in a fractional-width container, the table will get an extra
    // pixel of width
    width: calc(100% + 0.9999px);

    > thead > :first-child {
      > .nx-cell {
        &:first-child, &:last-child {
          border-radius: 0;
        }
      }
    }

    > tbody > :last-child {
      > .nx-cell {
        border-bottom-style: solid;
        height: nx-table-variables.$table-cell-min-height;

        &:first-child, &:last-child {
          border-radius: 0;
        }
      }
    }

    .nx-cell--header {
      position: sticky;
      top: 0;
      z-index: 1;
    }

    &:last-child > tbody > :last-child > .nx-cell {
      border-bottom-style: none;
    }
  }
}

// deprecated
.nx-scrollable--table-container {
  @extend .nx-table-container;
}

.nx-table-container__footer {
  background-color: var(--nx-color-component-background);
  border-top: var(--nx-border-default);
  margin-top: auto;

  // when following a table with no space between, merge the table's bottom border and the pagination's top border
  transform: translateY(-1px);

  // subtract 1 from bottom padding to counteract the translateY above
  padding: nx-table-variables.$table-footer-vertical-padding var(--nx-spacing-6x)
    calc(#{nx-table-variables.$table-footer-vertical-padding} - 1px) var(--nx-spacing-6x);
  position: sticky;
  bottom: 0;
  z-index: 1;

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

// nested and located this far down the file for specificity reasons
.nx-table {
  .nx-table-row--filter-header {
    > .nx-cell {
      top: nx-table-variables.$table-cell-header-height;
    }

    .nx-filter-input {
      width: 250px;
    }
  }
}

.nx-table--fixed-layout {
  table-layout: fixed;

  .nx-cell--row-btn {
    // unfortunate magic number for the intended width when the icon is a chevron. If it is a different icon, the
    // width will have to be explicitly overridden. `width: min-content` would have been ideal here in conjunction with
    // a hidden copy of the button in the header row, but it does
    // not work consistently across browsers: it works in firefox but in chrome it seems equivalent to `auto`, and
    // in safari is seems equivalent to `0`.
    width: 37px;
  }
}

.nx-cell {
  box-sizing: border-box;

  // min-height doesn't work on table cells, and height works roughly the way min-height ought to
  height: nx-table-variables.$table-cell-min-height;
  padding: nx-table-variables.$table-cell-vertical-padding var(--nx-spacing-3x);
  vertical-align: top;

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

  &:first-child {
    padding-left: var(--nx-spacing-4x);
  }

  &:last-child {
    padding-right: var(--nx-spacing-4x);
  }

  > .nx-btn-bar {
    // buttons should be 8px from row edges even though row padding is 12px
    margin-top: calc(0px - var(--nx-spacing-base));
    margin-bottom: calc(0px - var(--nx-spacing-base));
  }

  &:not(.nx-cell--header) > .nx-btn-bar {
    .nx-btn--icon-only {
      @include nx-button-helpers.small-icon-btn;
    }
  }
}

.nx-cell--label {
  @include nx-text-helpers.semi-bold;
}

.nx-cell--header {
  @include nx-text-helpers.bold;
  background-color: var(--nx-color-table-header-background);
  border-top: none;
  box-sizing: border-box;

  color: var(--nx-color-text-stark);
  font-size: var(--nx-font-size-s);
  height: nx-table-variables.$table-cell-header-height;
  text-align: left;
  text-transform: uppercase;
  white-space: nowrap;
  vertical-align: middle;

  &:focus {
    outline: var(--nx-focus-outline-width) solid var(--nx-color-interactive-border-focus);
    outline-offset: calc(0px - var(--nx-focus-outline-offset));
  }
}

//Allows making content visible only to screen readers but not visually
//Applied to the <th> when dealing with clickable rows
.nx-cell__row-btn-header-text {
  @include nx-text-helpers.visually-hidden;
}

// columns whose content is numerical
.nx-cell--num {
  text-align: center;
}


.nx-cell--icon {
  color: var(--nx-color-text-stark);
  text-align: center;
}

.nx-cell--row-btn {
  @extend .nx-cell--icon;

  // get the table to size the column as small as possible around the icon
  width: 1px;
}

// deprecated, just use .nx-cell--icon
.nx-cell--chevron {
  @extend .nx-cell--row-btn;
}

.nx-cell--sortable {
  user-select: none;
  -ms-user-select: none;
}

.nx-table-row--filter-header > .nx-cell {
  background-color: var(--nx-color-component-background);
  border-bottom: var(--nx-border-default);
  padding-top: var(--nx-spacing-4x);
  padding-bottom: var(--nx-spacing-4x);
}

.nx-cell__sort-icons {
  cursor: pointer;
  font-size: 16px;
  margin-left: var(--nx-spacing-base);

  > :nth-child(2) path {
    color: var(--nx-color-table-sort-icon-active);
  }
}

// make button take up entire cell including its padding box
@mixin btn-cell-sizing($left-padding, $right-padding) {
  margin-left: calc(0px - #{$left-padding});
  margin-right: calc(0px - #{$right-padding});
  padding-left: $left-padding;
  padding-right: $right-padding;
  width: calc(100% + #{$left-padding} + #{$right-padding});
}

.nx-cell__sort-btn, .nx-cell__row-btn {
  box-sizing: border-box;
  cursor: pointer;
  text-align: inherit;
  text-transform: uppercase;

  // first-child and last-child added for specificity
  &, &:first-child, &:last-child {
    @include btn-cell-sizing(var(--nx-spacing-3x), var(--nx-spacing-3x));

    margin-top: calc(0px - var(--nx-spacing-3x));
    margin-bottom: calc(0px - var(--nx-spacing-3x));
    padding-top: var(--nx-spacing-3x);
    padding-bottom: var(--nx-spacing-3x);
  }

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

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

.nx-cell {
  .nx-cell__sort-btn {
    height: nx-table-variables.$table-cell-header-height;
    position: relative;
    z-index: 0;

    &::before {
      box-sizing: border-box;
      border-radius: 4px;
      content: '';
      // offset by 1px on top and 2px on the bottom
      height: calc(100% - 3px);
      position: absolute;
      left: 1px;
      top: 1px;
      width: calc(100% - 2px);
      z-index: -1;
    }

    &:hover {
      &::before {
        background-color: var(--nx-color-table-header-button-background-hover);
      }
    }
    &:focus {
      &::before {
        border: var(--nx-focus-outline-width) solid var(--nx-color-interactive-border-focus);
      }
    }
    &:active {
      &::before {
        background-color: var(--nx-color-table-header-button-background-active);
        border: none;
      }
    }
  }
}

// adjust button sizing for ends of rows, where padding is different and there are borders
.nx-cell {
  $edge-spacing: calc(var(--nx-spacing-4x) - 1px);

  &:first-child {
    > .nx-cell__row-btn {
      @include btn-cell-sizing($edge-spacing, var(--nx-spacing-3x));
    }
    > .nx-cell__sort-btn {
      @include btn-cell-sizing(var(--nx-spacing-4x), var(--nx-spacing-3x));
    }
  }

  &:last-child {
    > .nx-cell__row-btn {
      @include btn-cell-sizing(var(--nx-spacing-3x), $edge-spacing);
    }
    > .nx-cell__sort-btn {
      @include btn-cell-sizing(var(--nx-spacing-3x), var(--nx-spacing-4x));
    }
  }

  &:first-child:last-child {
    > .nx-cell__row-btn {
      @include btn-cell-sizing($edge-spacing, $edge-spacing);
    }
    > .nx-cell__sort-btn {
      @include btn-cell-sizing(var(--nx-spacing-4x), var(--nx-spacing-4x));
    }
  }
}
