@use 'src/styles/abstracts' as *;

$border-color: $cuddle-50;
$box-shadow: 0 3px 9px rgba(0, 0, 0, 0.1);
$border-radius: 0.2rem;

:root {
  --cell-height: 28px;
  --group-margin: 4px;
  --cell-font-size: 0.875rem;
}

.Table {
  position: relative;
  display: flex;
  width: max-content;
  min-width: 100%;
  min-height: 100%;
  box-sizing: border-box;
  font-weight: $font-500;
  &__container {
    height: 100%;
    width: 100%;
    box-sizing: border-box;
    background: #fff;
    &--small {
      --cell-height: 22px;
      --group-margin: 2px;
      --cell-font-size: 0.875rem;
      @include monospaceFontFamily(14);
    }
    &--medium {
      --cell-height: 28px;
      --group-margin: 4px;
      --cell-font-size: 1rem;
      @include monospaceFontFamily();
    }

    &--large {
      --cell-height: 32px;
      --group-margin: 6px;
      --cell-font-size: 0.875rem;
    }
  }
  &__groupsColumn__cell {
    span,
    div {
      display: inline-block;
      width: 120px;
      min-width: 120px;
      justify-content: center;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      margin-right: 0.5rem;
      padding-right: 0.5rem;
      border-right: 0.0625rem solid #ccc;
      &:last-child {
        margin-right: 0;
        padding-right: 0;
        border-right: unset;
      }
    }
  }
}
.Table__pane {
  display: flex;
  width: stretch;
  width: -webkit-fill-available;
  width: -moz-available;
  min-height: 100%;
  box-sizing: border-box;
  z-index: 0;
}
.Table__status_indicator {
  width: 8px;
  height: 8px;
  padding: 0;
  background-color: unset !important;
  margin-right: 3px;
  &::before {
    width: 4px;
    height: 4px;
    top: 2px;
    left: 0;
  }
}

.Table__action__icon {
  color: $primary-color;
}

.Table__pane--left,
.Table__pane--right,
.Table__pane--selection {
  position: -webkit-sticky;
  position: sticky;
  width: auto;
  z-index: 1;
}

.Table__pane--left {
  left: 0;
  box-shadow: 1px 0 3px 0 $cuddle;
  &.withSelectionColumn {
    left: var(--left-position);
  }
}

.Table__pane--selection {
  left: 0;
  background: #fff;
  z-index: 2;
  &.withShadow {
    box-shadow: 1px 0 3px 0 $cuddle;
  }
}

.Table__pane--right {
  right: 0;
  box-shadow: -1px 0 3px 0 $cuddle;
}

.Table__pane--middle {
  position: relative;

  .Table__column {
    position: absolute;
  }
}

.Table__column {
  display: flex;
  flex-direction: column;
  background-color: #fff;
  z-index: 0;
  width: stretch;
  width: -webkit-fill-available;
  width: -moz-available;
  max-width: 500px;
  .Table__column__defaultSelectIcon {
    border: 1.5px solid $pico-80;
    border-radius: $border-radius-xs;
    width: 14px;
    height: 14px;
  }
  .Table__column__selectedSelectIcon {
    border-radius: $border-radius-xs;
    width: 14px;
    height: 14px;
    background: $primary-color;
    color: $white;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .Table__column__partiallySelectedSelectIcon {
    border-radius: $border-radius-xs;
    width: 14px;
    height: 14px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .configCheckbox {
    margin: 0 $space-xxs 0 toRem(1.5px);
  }
  &--groups {
    max-width: unset !important;
  }

  &:last-of-type {
    flex: 1;
    .Table__cell,
    .Table__group__config__cell {
      border-right: none;
    }
  }

  &.Table__column--actions {
    .Table__cell {
      padding: 0;
      justify-content: center;
    }
  }
  &.Table__column--selection {
    padding: 0;
    .Table__cell--header {
      padding: 0;
    }
    .Table__cell {
      padding: 0;
      border-right: 1px solid $border-color;
    }
    .Table__cell--header > *:first-child {
      margin-right: 0;
    }
  }
  &__selectCheckbox {
    width: 32px;
    height: var(--cell-height);
    border-radius: unset;
    padding: 0.5rem;
  }
  &__headerCheckbox {
    margin-left: 1rem;
  }
}

.Table__action {
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  width: 23px;
  height: 23px;
  margin: 5px 0 5px 5px;
  border-radius: $border-radius-circle;
  transition: background-color ease 110ms;
  user-select: none;

  &:hover {
    background-color: $grey-lighter;
  }

  &:active {
    background-color: $grey-light-xx;
    color: $grey-darker;
  }
}

.Table__action__popup {
  background-color: #fff;
  border: 1px solid $primary-lighter;
  border-radius: $border-radius;
  box-shadow: $box-shadow;
}

.Table__action__popup__body {
  min-width: 180px;
  padding: 0.5rem;

  &__panelTitle {
    text-transform: uppercase;
    margin: 0.5rem 0;
  }
}

.Table__action__anchor {
  cursor: pointer;
}

.Table__action__popup__item {
  padding: $space-xxs $space-xs !important;
  text-transform: capitalize;
  cursor: pointer;
  display: flex;
  align-items: center;
  color: $primary-color;
  transition: background-color 110ms ease;
  user-select: none;

  &:last-of-type {
    border-bottom: none;
  }

  &:hover {
    background-color: $grey-bg;
  }

  &:active {
    background-color: $primary-lighter;
    color: $primary-darker;
  }
  &_icon {
    width: 0.875rem;
    display: flex;
    margin-right: 1rem;
  }
}

.Table__cell {
  display: flex;
  align-items: center;
  font-size: var(--cell-font-size);
  padding: 0 $space-unit;
  border-bottom: 1px solid $border-color;
  border-right: 1px solid $border-color;
  text-align: left;
  box-sizing: border-box;
  background-color: #fff;
  white-space: nowrap;
  text-overflow: ellipsis;
  height: var(--cell-height);
  color: $pico-100;
  position: relative;
  &.isNumeric {
    justify-content: flex-end;
  }
  &.placeholder {
    border-right: unset;
  }

  &.colorIndicator {
    &::before {
      content: '';
      position: absolute;
      height: calc(var(--cell-height) - 4px);
      width: 0.375rem;
      border-radius: 0.375rem;
      background: var(--color-indicator);
      top: 2px;
      left: 8px;
    }
  }

  &.Table__group__header__cell {
    position: relative;
    background-color: $cuddle-10;
    border-color: $border-color;
    z-index: 1;
    transition: background-color 0.2s ease-out;

    &.expanded {
      position: -webkit-sticky;
      position: sticky;
      background-color: $cuddle-20;
      top: calc(2 * var(--cell-height));
    }

    &.expandable:hover {
      background-color: $cuddle-30;
    }
  }

  &.Table__group__config__column__cell {
    padding-left: 2.1875rem;

    .Table__cell__rowMeta {
      display: flex;
      align-items: flex-start;
      height: 100%;
      max-width: 25vw;
      overflow: auto;
      box-sizing: border-box;
      margin: 0;

      .Table__group__config__meta {
        max-height: 100%;
      }
    }
  }
  &.inactive {
    background-color: $cuddle-10;
    color: $pico-50;
    .Table__cell__value {
      color: $pico-50;
    }
  }

  &.selected {
    background-color: $primary-color-10;
  }

  &.hovered {
    background-color: $cuddle-20;
  }

  &.focused {
    background-color: $cuddle-50;
  }

  & > * {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  &.actions {
    div {
      width: 100%;
      height: 100%;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
}

.Table__cell--header {
  position: -webkit-sticky;
  position: sticky;
  top: var(--cell-height);
  flex: none;
  background-color: $cuddle-20;
  padding: $space-xxs $space-unit;
  border-color: $border-color;
  z-index: 3;
  overflow: visible;
  font-family: 'Inter', sans-serif;

  & > *:first-child {
    flex: 1;
    margin-right: 0.25rem;
    &.Table__column__selectCheckbox {
      flex: unset;
      margin-left: calc(20px + var(--group-margin));
      margin-right: toRem(14px);
    }
  }
}

.Table__column__resizeHandler {
  position: absolute;
  right: -3px;
  top: 0;
  width: 6px;
  height: 100%;
  background-color: $primary-color;
  z-index: 10;
  cursor: col-resize;
  opacity: 0;
  transition: opacity 110ms ease;

  &.leftResize {
    right: unset;
    left: -1px;
  }

  &.isResizing,
  &:hover {
    opacity: 1;
  }
}

.Table__cell--topHeader {
  top: 0;
  letter-spacing: 0;
  background-color: $cuddle-20;
  border-top: 0.0625rem solid $border-color;
}

.Table__header__action__container {
  display: flex;
}

.Table__GroupedColumnHeader {
  color: $pico-50;
  span {
    font-weight: $font-700;
  }
}

.Table__header__action {
  display: flex;
  align-items: center;
  justify-content: center;
  box-sizing: border-box;
  width: 25px;
  height: 21px;
  margin-left: $space-xxs;
  background-color: $primary-bg;
  border: 1px solid $primary-lighter;
  border-radius: $border-radius;
  color: $primary-color;
  cursor: pointer;
  opacity: 1;
  user-select: none;
  transition: border-color 110ms ease;

  &:hover,
  &.active {
    background-color: $primary-color-5;
  }

  &.disabled {
    opacity: 0.65;
    cursor: default;
    pointer-events: none;
    background-color: $grey-bg;
    border-color: $grey-light-xx;
    color: $grey;
  }
}

.Table__group {
  margin-top: var(--group-margin);
  border-top: 1px solid $border-color;
  position: relative;
  &.colorIndicator {
    &::before {
      content: '';
      position: absolute;
      height: calc(100% + 1px);
      width: toRem(6px);
      border-radius: toRem(6px) 0 0 toRem(6px);
      background: var(--color-indicator);
      top: -1px;
      left: -1px;
      z-index: 2;
    }
    .expanded {
      background: var(--extended-group-background-color);
      &:hover {
        background: var(--extended-group-background-color);
      }
    }
  }
  .Table__cell:last-of-type {
    border-bottom: 1px solid $border-color;
  }
}

.Table--grouped {
  .Table__pane {
    padding-bottom: var(--group-margin);
    &:first-of-type {
      .Table__column {
        &:first-of-type {
          .Table__group {
            border-left: 1px solid $border-color;
            margin-left: var(--group-margin);
            border-top-left-radius: $border-radius;
            border-bottom-left-radius: $border-radius;

            .Table__group__config__cell {
              border-top-left-radius: $border-radius;
              padding: 0 0.6rem;
            }

            .Table__cell:last-of-type {
              border-bottom-left-radius: $border-radius;
            }
          }
        }
      }
    }
    &:last-of-type {
      .Table__column {
        &:last-of-type {
          .Table__group {
            margin-right: var(--group-margin);
            border-right: 1px solid $border-color;
            border-top-right-radius: $border-radius;
            border-bottom-right-radius: $border-radius;

            .Table__cell:last-of-type {
              border-bottom-right-radius: $border-radius;
            }
          }
        }
      }
    }
  }
}

.Table__cell__rowMeta {
  display: flex;
  align-items: center;
  margin-right: $space-sm;
  flex: none;
}

.Table__cell__value {
  display: flex;
  flex: 1;
  color: $pico;
  .Badge {
    margin-right: $space-xxxs;
  }
  &.hasColorIndicator {
    padding-left: 0.5rem;
    .Table__column__selectCheckbox {
      margin-left: 0.5rem;
    }
  }
  &.isNumeric {
    justify-content: flex-end;
  }
}

.Popover__body {
  position: fixed;
  background-color: #fff;
  outline: 0;
  overflow-y: auto;
  z-index: 999;
}

.Table__group__config {
  display: flex;
  flex: 1;
  align-items: center;
  cursor: pointer;
  &__chart {
    display: flex;
    width: 1.25rem;
    height: 1.25rem;
    align-items: center;
    justify-content: center;
    margin-left: 0.5em;
    border: $border-grey;
    border-radius: $border-radius;
    font-size: 0.6rem;
  }
  &__stroke {
    display: block;
    background-color: #fff;
    width: 2.5rem;
    height: 1rem;
    margin-left: 0.5rem;
    border: 1px solid;
    border-radius: $border-radius;
    padding: 0 0.3rem;

    & > line {
      stroke: #333;
      stroke-width: 2;
    }
  }
  &__itemsCount {
    flex: 1;
    text-align: right;
    margin-left: 0.5rem;
  }
}
