//
// Copyright IBM Corp. 2016, 2018
//
// This source code is licensed under the Apache-2.0 license found in the
// LICENSE file in the root directory of this source tree.
//

@import '../../globals/scss/vendor/@carbon/elements/scss/import-once/import-once';
@import '../../globals/scss/vars';
@import '../../globals/scss/helper-mixins';
@import 'mixins';

/// Data table core styles
/// @access private
/// @group data-table
@mixin data-table-core {
  //----------------------------------------------------------------------------
  // Container
  //----------------------------------------------------------------------------
  .#{$prefix}--data-table-container {
    // Allow space for focus styles
    padding-top: $spacing-01;
  }

  .#{$prefix}--data-table-content {
    overflow-x: auto;
  }

  //----------------------------------------------------------------------------
  // Table title text
  //----------------------------------------------------------------------------
  .#{$prefix}--data-table-header {
    padding: $spacing-05 0 $spacing-06 $spacing-05;
    background: $ui-01;
  }

  .#{$prefix}--data-table-header__title {
    @include type-style('productive-heading-03');

    color: $text-01;
  }

  .#{$prefix}--data-table-header__description {
    @include type-style('body-short-01');

    color: $text-02;
  }

  //----------------------------------------------------------------------------
  // Data table
  //----------------------------------------------------------------------------
  .#{$prefix}--data-table {
    width: 100%;
    border-collapse: collapse;
    border-spacing: 0;
  }

  .#{$prefix}--data-table thead {
    @include type-style('productive-heading-01');

    background-color: $ui-03;
  }

  .#{$prefix}--data-table tbody {
    @include type-style('body-short-01');

    width: 100%;
    background-color: $ui-01;
  }

  .#{$prefix}--data-table tr {
    width: 100%;
    height: $layout-04;
    border: none;
  }

  .#{$prefix}--data-table tbody tr,
  .#{$prefix}--data-table tbody tr td,
  .#{$prefix}--data-table tbody tr th {
    transition: background-color $duration--fast-01 motion(entrance, productive);
  }

  .#{$prefix}--data-table tbody tr:hover {
    background: $hover-field;
  }

  .#{$prefix}--data-table tbody tr:hover td,
  .#{$prefix}--data-table tbody tr:hover th {
    color: $text-01;
    background: $hover-field;
    border-top: 1px solid $hover-field;
    border-bottom: 1px solid $hover-field;
  }

  .#{$prefix}--data-table th,
  .#{$prefix}--data-table td {
    text-align: left;
    vertical-align: middle;
  }

  .#{$prefix}--data-table td {
    padding-right: $spacing-05;
    padding-left: $spacing-05;
  }

  .#{$prefix}--data-table th {
    color: $text-01;
    background-color: $ui-03;
  }

  .#{$prefix}--data-table th:last-of-type {
    // Do not use `position: relative`, as its behavior is undefined for many table elements: https://www.w3.org/TR/CSS21/visuren.html#propdef-position
    position: static;
    width: auto;
  }

  .#{$prefix}--data-table .#{$prefix}--table-header-label {
    padding-right: $spacing-05;
    padding-left: $spacing-05;
    text-align: left;
  }

  .#{$prefix}--data-table td,
  .#{$prefix}--data-table tbody th {
    padding-right: $spacing-05;
    padding-left: $spacing-05;
    color: $text-02;
    background: $ui-01;
    border-top: 1px solid $ui-01;
    border-bottom: 1px solid $ui-03;

    + td:first-of-type {
      padding-left: $spacing-04;
    }
  }

  @supports (-moz-appearance: none) {
    .#{$prefix}--data-table td {
      // Fix to show borders in ff
      background-clip: padding-box;
    }
  }

  // Overflow Menu Overrides
  .#{$prefix}--data-table
    td.#{$prefix}--table-column-menu
    .#{$prefix}--overflow-menu[aria-expanded='false']:focus {
    @include focus-outline('outline');
  }

  .#{$prefix}--data-table
    td.#{$prefix}--table-column-menu
    .#{$prefix}--overflow-menu[aria-expanded='true']:focus {
    outline: none;
  }

  @media screen and (hover: hover),
    (-ms-high-contrast: active),
    (-ms-high-contrast: none) {
    .#{$prefix}--data-table
      td.#{$prefix}--table-column-menu
      .#{$prefix}--overflow-menu
      .#{$prefix}--overflow-menu__icon {
      opacity: 0;
    }
  }

  .#{$prefix}--data-table
    td.#{$prefix}--table-column-menu
    .#{$prefix}--overflow-menu.#{$prefix}--overflow-menu--open
    .#{$prefix}--overflow-menu__icon {
    opacity: 1;
  }

  .#{$prefix}--data-table.#{$prefix}--data-table--visible-overflow-menu
    td.#{$prefix}--table-column-menu
    .#{$prefix}--overflow-menu
    .#{$prefix}--overflow-menu__icon,
  .#{$prefix}--data-table
    td.#{$prefix}--table-column-menu
    .#{$prefix}--overflow-menu:hover
    .#{$prefix}--overflow-menu__icon,
  .#{$prefix}--data-table
    td.#{$prefix}--table-column-menu
    .#{$prefix}--overflow-menu:focus
    .#{$prefix}--overflow-menu__icon,
  .#{$prefix}--data-table
    tr:hover
    td.#{$prefix}--table-column-menu
    .#{$prefix}--overflow-menu
    .#{$prefix}--overflow-menu__icon {
    opacity: 1;
  }

  // .#{$prefix}--table-row--menu-option // removed by sam because we want SVG to be centered everywhere, not only in table row-overflow menus
    .#{$prefix}--overflow-menu-options__btn
    .#{$prefix}--overflow-menu-options__option-content
    svg {
    position: relative;
    // Used to center svg without setting display flex //display block needed for overflow text truncation
    top: rem(3px);
    margin-right: $spacing-03;
  }

  .#{$prefix}--data-table .#{$prefix}--overflow-menu,
  .#{$prefix}--data-table .#{$prefix}--overflow-menu__trigger {
    &:hover {
      background-color: $hover-selected-ui;
    }
  }

  .#{$prefix}--data-table--selected .#{$prefix}--overflow-menu,
  .#{$prefix}--data-table--selected .#{$prefix}--overflow-menu__trigger {
    &:hover {
      background-color: $hover-field;
    }
  }

  .#{$prefix}--data-table--compact td.#{$prefix}--table-column-menu,
  .#{$prefix}--data-table--short td.#{$prefix}--table-column-menu {
    height: rem(24px);
    padding-top: 0;
    padding-bottom: 0;
  }

  .#{$prefix}--data-table--short td.#{$prefix}--table-column-menu {
    height: rem(32px);
  }

  .#{$prefix}--data-table--tall .#{$prefix}--table-column-menu {
    padding-top: $spacing-03;
  }

  //----------------------------------------------------------------------------
  //ZEBRA
  //----------------------------------------------------------------------------

  .#{$prefix}--data-table--zebra
    tbody
    tr:not(.#{$prefix}--parent-row):nth-child(even)
    td {
    border-bottom: 1px solid $ui-01;
  }

  .#{$prefix}--data-table--zebra
    tbody
    tr:not(.#{$prefix}--parent-row):nth-child(odd)
    td {
    background-color: $data-table-zebra-color;
    border-top: 1px solid $data-table-zebra-color;
    border-bottom: 1px solid $data-table-zebra-color;
  }

  .#{$prefix}--data-table--zebra
    tbody
    tr:not(.#{$prefix}--parent-row):hover
    td {
    background-color: $hover-field;
    border-top: 1px solid $hover-field;
    border-bottom: 1px solid $hover-field;
  }

  //----------------------------------------------------------------------------
  // Select
  //----------------------------------------------------------------------------
  .#{$prefix}--table-column-checkbox .#{$prefix}--checkbox-label {
    padding-left: $spacing-05;
  }

  .#{$prefix}--data-table th.#{$prefix}--table-column-checkbox {
    // Do not use `position: relative`, as its behavior is undefined for many table elements: https://www.w3.org/TR/CSS21/visuren.html#propdef-position
    position: static;
    // 16px padding left + 8px padding right + 20px checkbox width
    width: rem(44px);
    padding-right: $spacing-05;
    padding-left: $spacing-05;
    background: $ui-03;
    transition: background-color $duration--fast-01 motion(entrance, productive);
  }

  .#{$prefix}--data-table thead th.#{$prefix}--table-column-checkbox,
  .#{$prefix}--data-table tbody td.#{$prefix}--table-column-checkbox,
  .#{$prefix}--data-table thead th.#{$prefix}--table-expand,
  .#{$prefix}--data-table tbody td.#{$prefix}--table-expand {
    min-width: 0;
    // spacing between checkbox / chevron and next cell should be 16px / 1rem
    // adjacent cell has 16px / 1rem padding-left though, hence the removal of padding-right here
    padding-right: 0;
    padding-left: $spacing-05;
  }

  .#{$prefix}--data-table thead th.#{$prefix}--table-column-checkbox,
  .#{$prefix}--data-table tbody td.#{$prefix}--table-column-checkbox {
    // 16px padding left + 20px checkbox width
    width: rem(36px);
  }

  .#{$prefix}--data-table thead th.#{$prefix}--table-expand,
  .#{$prefix}--data-table tbody td.#{$prefix}--table-expand {
    // 16px padding left + 16px checkbox width
    width: rem(32px);
  }

  .#{$prefix}--data-table--tall .#{$prefix}--table-column-checkbox {
    padding-top: rem(13px);
  }

  .#{$prefix}--data-table--tall .#{$prefix}--table-column-radio {
    padding-top: $spacing-05;
  }

  .#{$prefix}--date-table tbody th.#{$prefix}--table-column-checkbox:hover {
    background: $data-table-column-hover;
  }

  //----------------------------------------------------------------------------
  // Radio
  //----------------------------------------------------------------------------
  .#{$prefix}--table-column-radio {
    width: 48px;
  }

  .#{$prefix}--table-column-radio .#{$prefix}--radio-button__appearance {
    margin-right: rem(-2px);
  }

  // default selected row + zebra select - even child
  .#{$prefix}--data-table--zebra
    tbody
    tr:nth-child(odd).#{$prefix}--data-table--selected
    td,
  tr.#{$prefix}--data-table--selected td {
    color: $text-01;
    background-color: $selected-ui;
    border-top: 1px solid $selected-ui;
    // Bottom border acts as separator from other rows
    border-bottom: 1px solid $active-ui;
  }

  // First row
  .#{$prefix}--data-table--zebra
    tbody
    tr:first-of-type:nth-child(odd).#{$prefix}--data-table--selected
    td,
  tr.#{$prefix}--data-table--selected:first-of-type td {
    // Top border acts as separator from thead
    border-top: 1px solid $active-ui;
  }

  // last row + zebra select last
  .#{$prefix}--data-table--zebra
    tbody
    tr:last-of-type:nth-child(odd).#{$prefix}--data-table--selected
    td,
  .#{$prefix}--data-table--zebra
    tbody
    tr:last-of-type:nth-child(even).#{$prefix}--data-table--selected
    td,
  tr.#{$prefix}--data-table--selected:last-of-type td {
    // Doesn't need separators
    border-top: 1px solid $selected-ui;
    border-bottom: 1px solid $selected-ui;
  }

  // zebra select - odd child
  .#{$prefix}--data-table--zebra
    tbody
    tr:nth-child(even).#{$prefix}--data-table--selected
    td {
    border-bottom: 1px solid $active-ui;
  }

  .#{$prefix}--data-table--zebra
    tbody
    tr:nth-child(even).#{$prefix}--data-table--selected:hover
    td {
    border-bottom: 1px solid $data-table-column-hover;
  }

  // hover + zebra select - even child
  .#{$prefix}--data-table--zebra
    tbody
    tr:nth-child(odd).#{$prefix}--data-table--selected:hover
    td,
  .#{$prefix}--data-table tbody .#{$prefix}--data-table--selected:hover td {
    color: $text-01;
    background: $data-table-column-hover;
    border-top: 1px solid $data-table-column-hover;
    border-bottom: 1px solid $data-table-column-hover;
  }

  // selected overflow menu
  .#{$prefix}--data-table--selected
    .#{$prefix}--overflow-menu
    .#{$prefix}--overflow-menu__icon {
    opacity: 1;
  }

  //----------------------------------------------------------------------------
  // Compact
  //----------------------------------------------------------------------------
  .#{$prefix}--data-table--compact thead tr,
  .#{$prefix}--data-table--compact tbody tr,
  .#{$prefix}--data-table--compact tbody tr th {
    height: rem(24px);
  }

  .#{$prefix}--data-table--compact .#{$prefix}--table-header-label {
    padding-top: rem(2px);
    padding-bottom: rem(2px);
  }

  .#{$prefix}--data-table--compact td,
  .#{$prefix}--data-table--compact tbody tr th {
    padding-top: rem(2px);
    padding-bottom: rem(2px);
  }

  .#{$prefix}--data-table--compact .#{$prefix}--overflow-menu {
    width: rem(32px);
    height: 100%;
  }

  .#{$prefix}--data-table.#{$prefix}--data-table--compact
    .#{$prefix}--table-column-checkbox {
    padding-top: 0;
    padding-bottom: 0;
  }

  .#{$prefix}--data-table.#{$prefix}--data-table--compact
    .#{$prefix}--table-column-checkbox
    .#{$prefix}--checkbox-label {
    height: rem(23px);
    // 24px row - 1px border
    min-height: rem(23px);
  }

  //----------------------------------------------------------------------------
  // Short
  //----------------------------------------------------------------------------
  .#{$prefix}--data-table--short thead tr,
  .#{$prefix}--data-table--short tbody tr,
  .#{$prefix}--data-table--short tbody tr th {
    height: rem(32px);
  }

  .#{$prefix}--data-table--short .#{$prefix}--table-header-label {
    padding-top: rem(7px);
    padding-bottom: rem(7px);
  }

  .#{$prefix}--data-table--short td,
  .#{$prefix}--data-table--short tbody tr th {
    padding-top: rem(7px);
    padding-bottom: rem(6px);
  }

  .#{$prefix}--data-table.#{$prefix}--data-table--short
    .#{$prefix}--table-column-checkbox {
    padding-top: rem(3px);
    padding-bottom: rem(3px);
  }

  .#{$prefix}--data-table--short .#{$prefix}--overflow-menu {
    height: 100%;
  }

  //----------------------------------------------------------------------------
  // Tall
  //----------------------------------------------------------------------------
  .#{$prefix}--data-table--tall thead tr,
  .#{$prefix}--data-table--tall tbody tr,
  .#{$prefix}--data-table--tall tbody tr th {
    height: rem(64px);
  }

  .#{$prefix}--data-table--tall .#{$prefix}--table-header-label {
    padding-top: $spacing-05;
    padding-bottom: $spacing-05;
  }

  .#{$prefix}--data-table--tall td,
  .#{$prefix}--data-table--tall tbody tr th {
    padding-top: 1rem;
  }

  .#{$prefix}--data-table--tall th,
  .#{$prefix}--data-table--tall td {
    vertical-align: top;
  }

  .#{$prefix}--data-table--cell-secondary-text {
    @include type-style('label-01');
  }

  //----------------------------------------------------------------------------
  // Static
  //----------------------------------------------------------------------------
  .#{$prefix}--data-table--static {
    width: auto;
  }

  // -------------
  // Sticky header
  // -------------
  .#{$prefix}--data-table_inner-container {
    background-color: $ui-03;
    transform: translateZ(0);
  }

  .#{$prefix}--data-table--sticky-header {
    display: block;
    // max-height: rem(300px);
    overflow-y: scroll;

    thead,
    tbody,
    tr,
    th,
    td {
      display: flex;
    }

    thead {
      position: sticky;
      top: 0;
      z-index: 1;
      width: 100%;
      overflow: scroll;
      will-change: transform;
      // Hides ie scrollbar
      -ms-overflow-style: none;
    }

    thead tr th {
      border-bottom: 1px solid $active-ui;
    }

    tbody {
      flex-direction: column;
      overflow-x: scroll;
      // Hides ie scrollbar
      -ms-overflow-style: none;
      will-change: transform;
    }

    tr.#{$prefix}--parent-row.#{$prefix}--expandable-row {
      height: auto;
      min-height: 3rem;
    }

    tr.#{$prefix}--expandable-row:not(.#{$prefix}--parent-row) {
      height: auto;
    }

    .#{$prefix}--table-expand {
      max-width: rem(48px);
    }

    thead .#{$prefix}--table-expand {
      align-items: center;
    }

    .#{$prefix}--parent-row {
      min-height: 3rem;
    }

    // .#{$prefix}--parent-row td {
    //   padding: 1rem;
    // }

    &:not(.#{$prefix}--data-table--compact):not(.#{$prefix}--data-table--tall):not(.#{$prefix}--data-table--short)
      td:not(.#{$prefix}--table-column-menu):not(.#{$prefix}--table-column-checkbox) {
      padding-top: rem(14px);
    }

    // Taken from L125 _data-table-expandable
    // Used to hide white line when parent row is hovered when child is expanded
    tr.#{$prefix}--parent-row.#{$prefix}--expandable-row:hover
      + tr[data-child-row]
      td {
      border-top: 1px solid $hover-ui;
    }

    tr.#{$prefix}--expandable-row:last-of-type {
      overflow: hidden;
    }

    tr.#{$prefix}--data-table--selected:first-of-type td {
      border-top: none;
    }

    // Selectable fix
    thead th.#{$prefix}--table-column-checkbox,
    tbody tr td.#{$prefix}--table-column-checkbox {
      align-items: center;
      width: rem(36px);
      min-width: rem(36px);
    }

    &.#{$prefix}--data-table--tall thead th.#{$prefix}--table-column-checkbox,
    &.#{$prefix}--data-table--tall td.#{$prefix}--table-column-checkbox {
      align-items: flex-start;
    }

    // Overflow fix
    /* When using sticky header, with a selection element in the first column, we need to set the last item to a fixed width to match the table body. We only want this to happen when the last table header does not have any text */
    th.#{$prefix}--table-column-checkbox ~ th:last-of-type:empty {
      max-width: rem(64px);
    }

    th:empty:not(.#{$prefix}--table-expand) {
      max-width: 2.25rem;
    }

    td.#{$prefix}--table-column-menu {
      align-items: center;
      height: auto;
      padding-top: 0;
    }

    //hides webkit scrollbar
    thead::-webkit-scrollbar,
    tbody::-webkit-scrollbar {
      display: none;
    }

    //hides ff scrollbar
    @-moz-document url-prefix() {
      thead,
      tbody {
        scrollbar-width: none;
      }
    }

    tbody tr:last-of-type {
      border-bottom: 0;
    }

    th:not(.#{$prefix}--table-column-checkbox):not(.#{$prefix}--table-column-menu):not(.#{$prefix}--table-expand-v2):not(.#{$prefix}--table-column-icon),
    td:not(.#{$prefix}--table-column-checkbox):not(.#{$prefix}--table-column-menu):not(.#{$prefix}--table-expand-v2):not(.#{$prefix}--table-column-icon) {
      width: 100%;
      min-width: 0;
    }

    &.#{$prefix}--data-table--compact tr:not(.#{$prefix}--expandable-row),
    &.#{$prefix}--data-table--short tr:not(.#{$prefix}--expandable-row),
    &.#{$prefix}--data-table--tall tr:not(.#{$prefix}--expandable-row) {
      height: auto;
    }

    // Compact
    &.#{$prefix}--data-table--compact tr:not(.#{$prefix}--expandable-row) {
      min-height: rem(24px);
    }

    // Short
    &.#{$prefix}--data-table--short tr:not(.#{$prefix}--expandable-row) {
      min-height: rem(32px);
    }

    // Tall
    &.#{$prefix}--data-table--tall tr:not(.#{$prefix}--expandable-row) {
      min-height: rem(64px);
    }

    // Expansion overrides
    &.#{$prefix}--data-table--compact tr td.#{$prefix}--table-expand {
      padding-top: rem(4px);
    }

    &.#{$prefix}--data-table--short tr td.#{$prefix}--table-expand {
      padding-top: rem(8px);
    }

    .#{$prefix}--table-header-label {
      @include text-overflow;

      max-width: calc(100% - 10px);
      // Needed to reduce 1px jump when toggling between variations
      padding-top: rem(15px);
      padding-bottom: 1rem;
      overflow-y: hidden;
    }

    &.#{$prefix}--data-table--compact th .#{$prefix}--table-header-label {
      padding-top: rem(3px);
      padding-bottom: 0;
    }

    &.#{$prefix}--data-table--short th .#{$prefix}--table-header-label {
      padding-top: rem(8px);
      padding-bottom: 0;
    }

    &.#{$prefix}--data-table--tall th .#{$prefix}--table-header-label {
      padding-top: 1rem;
    }

    &.#{$prefix}--data-table--tall th.#{$prefix}--table-expand {
      display: flex;
      align-items: flex-start;
    }

    // With dynamic content overrides
    &.#{$prefix}--data-table--compact
      tr.#{$prefix}--parent-row
      .#{$prefix}--table-column-checkbox,
    &.#{$prefix}--data-table--short
      tr.#{$prefix}--parent-row
      .#{$prefix}--table-column-checkbox {
      align-items: flex-start;
    }
  }

  @include sticky-header($max-width: 100%);

  // -------------------
  // with boolean column
  // -------------------
  .#{$prefix}--data-table .bx--form-item.bx--checkbox-wrapper:last-of-type {
    margin: 0;
  }

  .#{$prefix}--data-table--short
    .#{$prefix}--form-item.#{$prefix}--checkbox-wrapper:last-of-type,
  .#{$prefix}--data-table--compact
    .#{$prefix}--form-item.#{$prefix}--checkbox-wrapper:last-of-type {
    margin: rem(-3px) 0;
  }
}

@include exports('data-table-v2-core') {
  @include data-table-core;
}
