@use 'sass:map';
@use 'mixins/mixins' as *;
@use 'mixins/var' as *;
@use 'common/var' as *;

@include b(tab) {
  @include set-component-css-var('tab', $tab);

  @include e(header) {
    padding: 0;
    position: relative;
    margin: 0 0 var(--sg-tab-header-horizontal-margin);
  }
  @include e(active-bar) {
    position: absolute;
    bottom: 0;
    left: 0;
    height: var(--sg-tab-active-bar-height);
    background-color: var(--sg-color-primary);
    z-index: 1;
    transition: width var(--sg-transition-duration)
        var(--sg-transition-function-ease-in-out-bezier),
      transform var(--sg-transition-duration)
        var(--sg-transition-function-ease-in-out-bezier);
    list-style: none;
  }
  @include e(new-tab) {
    float: right;
    border: 1px solid #d3dce6;
    height: 18px;
    width: 18px;
    line-height: 18px;
    margin: 10px;
    border-radius: 3px;
    text-align: center;
    font-size: 12px;
    color: #d3dce6;
    cursor: pointer;
    transition: all 0.15s;

    .is-icon-plus {
      height: inherit;
      width: inherit;
      transform: scale(0.8, 0.8);

      svg {
        vertical-align: middle;
      }
    }

    &:hover {
      color: var(--sg-color-primary);
    }
  }
  @include e(nav-wrap) {
    overflow: hidden;
    margin-bottom: -1px;
    position: relative;

    &::after {
      content: '';
      position: absolute;
      left: 0;
      bottom: 0;
      width: 100%;
      height: 1px;
      background-color: var(--sg-border-color-extra-light);
      z-index: var(--sg-index-normal);
    }

    @include when(scrollable) {
      padding: 0 32px;
      box-sizing: border-box;
    }
  }
  @include e(nav-scroll) {
    overflow: hidden;
  }
  @include e((nav-next, nav-prev)) {
    position: absolute;
    cursor: pointer;
    width: 32px;
    line-height: 44px;
    font-size: 12px;
    color: var(--sg-text-color-secondary);
    text-align: center;

    @include when(disabled) {
      color: var(--sg-disabled-text-color);
      cursor: not-allowed;
    }
  }
  @include e(nav-next) {
    right: 0;
  }
  @include e(nav-prev) {
    left: 0;
  }
  @include e(nav) {
    white-space: nowrap;
    position: relative;
    transition: transform var(--sg-transition-duration);
    float: left;
    z-index: calc(var(--sg-index-normal) + 1);

    @include when(stretch) {
      min-width: 100%;
      display: flex;
      & > * {
        flex: 1;
        text-align: center;
      }
    }
  }
  @include e(item) {
    padding: 0 var(--sg-tab-item-x-padding);
    height: 40px;
    box-sizing: border-box;
    line-height: 40px;
    display: inline-block;
    list-style: none;
    font-size: 14px;
    font-weight: 500;
    color: var(--sg-text-color-primary);
    position: relative;
    user-select: none;

    &:focus,
    &:focus:active {
      outline: none;
    }

    & .is-icon-close {
      border-radius: 50%;
      text-align: center;
      transition: all var(--sg-transition-duration)
        var(--sg-transition-function-ease-in-out-bezier);
      margin-left: 8px;
      &:before {
        transform: scale(0.9);
        display: inline-block;
      }

      &:hover {
        background-color: var(--sg-text-color-placeholder);
        color: $color-white;
      }

      svg {
        margin-top: 1px;
      }
    }

    @include when(active) {
      color: var(--sg-color-primary);
    }

    &:hover {
      color: var(--sg-color-primary);
      cursor: pointer;
    }

    @include when(disabled) {
      color: var(--sg-disabled-text-color);
      cursor: default;
    }
  }
  @include e(content) {
    overflow: hidden;
    position: relative;
  }
  // 卡片风格
  @include m(card) {
    > .#{$namespace}-tab__header {
      border-bottom: 1px solid var(--sg-border-color-base);
    }
    > .#{$namespace}-tab__header .#{$namespace}-tab__nav-wrap::after {
      content: none;
    }
    > .#{$namespace}-tab__header .#{$namespace}-tab__nav {
      border: 1px solid var(--sg-border-color-base);
      border-bottom: none;
      border-radius: map.get($border-radius, 'base') map.get($border-radius, 'base') 0 0;
      box-sizing: border-box;
    }
    > .#{$namespace}-tab__header .#{$namespace}-tab__item .is-icon-close {
      position: relative;
      font-size: 12px;
      width: 14px;
      height: 14px;
      vertical-align: middle;
      line-height: 15px;
      overflow: hidden;
      top: -1px;
      right: -2px;
      transform-origin: 100% 50%;
    }
    > .#{$namespace}-tab__header .#{$namespace}-tab__item {
      border-bottom: 1px solid transparent;
      border-left: 1px solid var(--sg-border-color-base);
      transition: color var(--sg-transition-duration) var(--sg-transition-function-ease-in-out-bezier),
        padding var(--sg-transition-duration) var(--sg-transition-function-ease-in-out-bezier);
      &:first-child {
        border-left: none;
      }

      &.is-active {
        border-bottom-color: $color-white;
      }
    }
  }
  // 边框卡片风格
  @include m(border-card) {
    background: $color-white;
    border: 1px solid var(--sg-border-color-base);
    box-shadow: var(--sg-box-shadow-input);

    > .#{$namespace}-tab__content {
      padding: 16px;
    }
    > .#{$namespace}-tab__header {
      background-color: var(--sg-bg-color);
      border-bottom: 1px solid var(--sg-border-color-base);
      margin: 0;
    }
    > .#{$namespace}-tab__header .#{$namespace}-tab__nav-wrap::after {
      content: none;
    }
    > .#{$namespace}-tab__header .#{$namespace}-tab__item {
      transition: all var(--sg-transition-duration) var(--sg-transition-function-ease-in-out-bezier);
      border: 1px solid transparent;
      margin-top: -1px;
      color: var(--sg-text-color-secondary);

      &:first-child {
        margin-left: -1px;
      }

      & + .#{$namespace}-tab__item {
        margin-left: -1px;
      }

      &.is-active {
        color: var(--sg-color-primary);
        background-color: $color-white;
        border-right-color: var(--sg-border-color-base);
        border-left-color: var(--sg-border-color-base);
      }
      &:not(.is-disabled):hover {
        color: var(--sg-color-primary);
      }
      &.is-disabled {
        color: var(--sg-disabled-text-color);
      }
    }

    > .#{$namespace}-tab__header
      .is-scrollable
      .#{$namespace}-tab__item:first-child {
      margin-left: 0;
    }
  }
  // 按钮风格
  @include m(button) {
    > .#{$namespace}-tab__header .#{$namespace}-tab__nav-wrap::after {
      content: none;
    }
    > .#{$namespace}-tab__header .#{$namespace}-tab__item {
      border: 1px solid var(--sg-border-color-base);
      transition: all var(--sg-transition-duration) var(--sg-transition-function-ease-in-out-bezier);

      &:first-child {
        border-top-left-radius: var(--sg-border-radius-base);
        border-bottom-left-radius: var(--sg-border-radius-base);
      }

      &:last-child {
        border-top-right-radius: var(--sg-border-radius-base);
        border-bottom-right-radius: var(--sg-border-radius-base);
      }

      &:not(:last-child) {
        margin-right: -1px;
      }

      &.is-active {
        color: $color-white;
        background-color: var(--sg-color-primary);
        border-color: var(--sg-color-primary);
      }
      &.is-disabled {
        color: var(--sg-disabled-text-color);
      }
    }
  }
  // 圆角风格
  @include m(button-round) {
    > .#{$namespace}-tab__header .#{$namespace}-tab__nav-wrap::after {
      content: none;
    }

    > .#{$namespace}-tab__header .#{$namespace}-tab__nav {
      border: 1px solid var(--sg-border-color-base);
      border-radius: var(--sg-border-radius-round);
    }

    > .#{$namespace}-tab__header .#{$namespace}-tab__item {
      transition: all var(--sg-transition-duration) var(--sg-transition-function-ease-in-out-bezier);

      &.is-active {
        color: $color-white;
        background-color: var(--sg-color-primary);
        border-radius: var(--sg-border-radius-round);
      }

      &.is-disabled {
        color: var(--sg-disabled-text-color);
      }
    }
  }
  @include m((top, bottom)) {
    .#{$namespace}-tab__item.is-top:nth-child(2),
    .#{$namespace}-tab__item.is-bottom:nth-child(2) {
      padding-left: 0;
    }
    .#{$namespace}-tab__item.is-top:last-child,
    .#{$namespace}-tab__item.is-bottom:last-child {
      padding-right: 0;
    }

    &.#{$namespace}-tab--border-card,
    &.#{$namespace}-tab--card,
    &.#{$namespace}-tab--button,
    &.#{$namespace}-tab--button-round,
    .#{$namespace}-tab--left,
    .#{$namespace}-tab--right {
      > .#{$namespace}-tab__header {
        .#{$namespace}-tab__item:nth-child(2) {
          padding-left: var(--sg-tab-item-x-padding);
        }
        .#{$namespace}-tab__item:last-child {
          padding-right: var(--sg-tab-item-x-padding);
        }
      }
    }
  }
  @include m(bottom) {
    .#{$namespace}-tab__header.is-bottom {
      margin-bottom: 0;
      margin-top: var(--sg-tab-header-horizontal-margin);
    }

    &.#{$namespace}-tab--card {
      .#{$namespace}-tab__header.is-bottom {
        border-bottom: 0;
        border-top: 1px solid var(--sg-border-color-base);
      }

      .#{$namespace}-tab__nav-wrap.is-bottom {
        margin-top: -1px;
      }

      .#{$namespace}-tab__item.is-bottom {
        border-top: 1px solid transparent;
        border-bottom: 1px solid var(--sg-border-color-base);

        &.is-active {
          border-top-color: $color-white;
        }
      }
    }

    &.#{$namespace}-tab--border-card {
      .#{$namespace}-tab__header.is-bottom {
        border-bottom: 0;
        border-top: 1px solid var(--sg-border-color-base);
      }
      .#{$namespace}-tab__nav-wrap.is-bottom {
        margin-top: -1px;
        margin-bottom: 0;
      }
      .#{$namespace}-tab__item.is-bottom:not(.is-active) {
        border: 1px solid transparent;
      }
      .#{$namespace}-tab__item.is-bottom {
        margin: 0 -1px -1px -1px;
      }
    }
  }
  @include m((left, right)) {
    overflow: hidden;

    .#{$namespace}-tab__header.is-left,
    .#{$namespace}-tab__header.is-right,
    .#{$namespace}-tab__nav-wrap.is-left,
    .#{$namespace}-tab__nav-wrap.is-right,
    .#{$namespace}-tab__nav-scroll {
      height: 100%;
    }

    .#{$namespace}-tab__active-bar.is-left,
    .#{$namespace}-tab__active-bar.is-right {
      top: 0;
      bottom: auto;
      width: 2px;
      height: auto;
    }

    .#{$namespace}-tab__nav-wrap.is-left,
    .#{$namespace}-tab__nav-wrap.is-right {
      margin-bottom: 0;

      > .#{$namespace}-tab__nav-prev,
      > .#{$namespace}-tab__nav-next {
        height: 30px;
        line-height: 30px;
        width: 100%;
        text-align: center;
        cursor: pointer;

        i {
          transform: rotateZ(90deg);
        }
      }
      > .#{$namespace}-tab__nav-prev {
        left: auto;
        top: 0;
      }
      > .#{$namespace}-tab__nav-next {
        right: auto;
        bottom: 0;
      }

      &.is-scrollable {
        padding: 30px 0;
      }

      &::after {
        height: 100%;
        width: var(--sg-tab-active-bar-height);
        bottom: auto;
        top: 0;
      }
    }

    .#{$namespace}-tab__nav.is-left,
    .#{$namespace}-tab__nav.is-right {
      float: none;
    }
    .#{$namespace}-tab__item.is-left,
    .#{$namespace}-tab__item.is-right {
      display: block;
    }
  }
  @include m(left) {
    .#{$namespace}-tab__header.is-left {
      float: left;
      margin-bottom: 0;
      margin-right: var(--sg-tab-header-vertical-margin);
    }
    .#{$namespace}-tab__nav-wrap.is-left {
      margin-right: -1px;
      &::after {
        left: auto;
        right: 0;
      }
    }
    .#{$namespace}-tab__active-bar.is-left {
      right: 0;
      left: auto;
    }
    .#{$namespace}-tab__item.is-left {
      text-align: right;
    }

    &.#{$namespace}-tab--card {
      .#{$namespace}-tab__active-bar.is-left {
        display: none;
      }
      .#{$namespace}-tab__item.is-left {
        border-left: none;
        border-right: 1px solid var(--sg-border-color-base);
        border-bottom: none;
        border-top: 1px solid var(--sg-border-color-base);
        text-align: left;
      }
      .#{$namespace}-tab__item.is-left:first-child {
        border-right: 1px solid var(--sg-border-color-base);
        border-top: none;
      }
      .#{$namespace}-tab__item.is-left.is-active {
        border: 1px solid var(--sg-border-color-base);
        border-right-color: #fff;
        border-left: none;
        border-bottom: none;

        &:first-child {
          border-top: none;
        }
        &:last-child {
          border-bottom: none;
        }
      }

      .#{$namespace}-tab__nav {
        border-radius: map.get($border-radius, 'base') 0 0 map.get($border-radius, 'base');
        border-bottom: 1px solid var(--sg-border-color-base);
        border-right: none;
      }

      .#{$namespace}-tab__new-tab {
        float: none;
      }
    }

    &.#{$namespace}-tab--border-card {
      .#{$namespace}-tab__header.is-left {
        border-right: 1px solid #dfe4ed;
      }
      .#{$namespace}-tab__item.is-left {
        border: 1px solid transparent;
        margin: -1px 0 -1px -1px;

        &.is-active {
          border-color: transparent;
          border-top-color: rgb(209, 219, 229);
          border-bottom-color: rgb(209, 219, 229);
        }
      }
    }

    &.#{$namespace}-tab--button {
      .#{$namespace}-tab__item.is-left {
        margin-right: 0;

        &:not(:first-child) {
          margin-top: -1px;
        }
      }
    }
  }
  @include m(right) {
    .#{$namespace}-tab__header.is-right {
      float: right;
      margin-bottom: 0;
      margin-left: var(--sg-tab-header-vertical-margin);
    }

    .#{$namespace}-tab__nav-wrap.is-right {
      margin-left: -1px;
      &::after {
        left: 0;
        right: auto;
      }
    }

    .#{$namespace}-tab__active-bar.is-right {
      left: 0;
    }

    &.#{$namespace}-tab--card {
      .#{$namespace}-tab__active-bar.is-right {
        display: none;
      }
      .#{$namespace}-tab__item.is-right {
        border-bottom: none;
        border-top: 1px solid var(--sg-border-color-base);
      }
      .#{$namespace}-tab__item.is-right:first-child {
        border-left: 1px solid var(--sg-border-color-base);
        border-top: none;
      }
      .#{$namespace}-tab__item.is-right.is-active {
        border: 1px solid var(--sg-border-color-base);
        border-left-color: #fff;
        border-right: none;
        border-bottom: none;

        &:first-child {
          border-top: none;
        }
        &:last-child {
          border-bottom: none;
        }
      }

      .#{$namespace}-tab__nav {
        border-radius: 0 map.get($border-radius, 'base') map.get($border-radius, 'base') 0;
        border-bottom: 1px solid var(--sg-border-color-base);
        border-left: none;
      }
    }
    &.#{$namespace}-tab--border-card {
      .#{$namespace}-tab__header.is-right {
        border-left: 1px solid #dfe4ed;
      }
      .#{$namespace}-tab__item.is-right {
        border: 1px solid transparent;
        margin: -1px -1px -1px 0;

        &.is-active {
          border-color: transparent;
          border-top-color: rgb(209, 219, 229);
          border-bottom-color: rgb(209, 219, 229);
        }
      }
    }

    &.#{$namespace}-tab--button {
      .#{$namespace}-tab__item.is-right {
        margin-right: 0;

        &:not(:first-child) {
          margin-top: -1px;
        }
      }
    }
  }
}

.slideInRight-transition,
.slideInLeft-transition {
  display: inline-block;
}
.slideInRight-enter {
  animation: slideInRight-enter var(--sg-transition-duration);
}
.slideInRight-leave {
  position: absolute;
  left: 0;
  right: 0;
  animation: slideInRight-leave var(--sg-transition-duration);
}
.slideInLeft-enter {
  animation: slideInLeft-enter var(--sg-transition-duration);
}
.slideInLeft-leave {
  position: absolute;
  left: 0;
  right: 0;
  animation: slideInLeft-leave var(--sg-transition-duration);
}

@keyframes slideInRight-enter {
  0% {
    opacity: 0;
    transform-origin: 0 0;
    transform: translateX(100%);
  }

  to {
    opacity: 1;
    transform-origin: 0 0;
    transform: translateX(0);
  }
}
@keyframes slideInRight-leave {
  0% {
    transform-origin: 0 0;
    transform: translateX(0);
    opacity: 1;
  }

  100% {
    transform-origin: 0 0;
    transform: translateX(100%);
    opacity: 0;
  }
}
@keyframes slideInLeft-enter {
  0% {
    opacity: 0;
    transform-origin: 0 0;
    transform: translateX(-100%);
  }

  to {
    opacity: 1;
    transform-origin: 0 0;
    transform: translateX(0);
  }
}
@keyframes slideInLeft-leave {
  0% {
    transform-origin: 0 0;
    transform: translateX(0);
    opacity: 1;
  }

  100% {
    transform-origin: 0 0;
    transform: translateX(-100%);
    opacity: 0;
  }
}
