@import './../theme/vars.scss';
@import './../mixins/icon.scss';

$treePrefixCls: #{$vender-prefix}-tree;

// $tree-default-open-icon: '\e900'; // caretdown
// $tree-showline-open-icon: '\e905'; // minus-square-o
// $tree-showline-close-icon: '\e904'; // plus-square-o
// $tree-doc-icon: '\e902'; // file-leaf
// $tree-loading-icon: '\e903'; // loading_1
$tree-default-open-icon: var(--af-tree-default-open-icon); // caretdown
$tree-showline-open-icon: var(--af-tree-showline-open-icon); // minus-square-o
$tree-showline-close-icon: var(--af-tree-showline-close-icon); // plus-square-o
$tree-doc-icon: var(--af-tree-doc-icon); // file-leaf
$tree-loading-icon: var(--af-tree-loading-icon); // loading_1

// $treeColor: $primary-color;
$treeColor: var(--primary-color);

@mixin treeSwitcherIcon($type: $tree-default-open-icon) {
  &::after {
    @include iconfont-size-under-12px(7px);
    @include iconfont-font($type);

    display: inline-block;
    font-weight: bold;
    transition: transform 0.3s;
  }
}

@mixin treeShowLineIcon($type) {
  &::after {
    @include iconfont-size-under-12px(12px);
    @include iconfont-font($type);

    display: inline-block;
    font-weight: normal;
    vertical-align: baseline;
    transition: transform 0.3s;
  }
}

.#{$treePrefixCls} {
  padding: 5px;
  margin: 0;
  font-size: $font-size-base;

  &-treenode {
    &-loading {
      .#{$treePrefixCls}-node-content-wrapper {
        position: relative;
      }
    }
  }

  ol,
  ul {
    padding: 0;
    margin: 0;
    list-style: none;
  }

  li {
    padding: 4px 0;
    margin: 0;
    white-space: nowrap;
    list-style: none;
    outline: 0;

    &:last-child {
      padding: 0;
    }

    span[draggable],
    span[draggable='true'] {
      margin-top: -2px;
      border-top: 2px transparent solid;
      border-bottom: 2px transparent solid;
      user-select: none;

      /* Required to make elements draggable in old WebKit */
      -khtml-user-drag: element;
      -webkit-user-drag: element;
    }

    &.drag-over {
      > span[draggable] {
        color: white;
        background-color: $treeColor;
        opacity: 0.8;
      }
    }

    &.drag-over-gap-top {
      > span[draggable] {
        border-top-color: $treeColor;
      }
    }

    &.drag-over-gap-bottom {
      > span[draggable] {
        border-bottom-color: $treeColor;
      }
    }

    &.tree-node-matched {
      > span {
        font-weight: bold;
        color: $highlight-treenode-color;
      }
    }

    ul {
      padding: 0 0 0 18px;
      margin: 0;
    }

    .#{$treePrefixCls}-node-content-wrapper {
      display: inline-block;
      height: var(--af-tree-node-height, 24px);
      padding: 0 5px; // 3px 5px
      margin: 0;
      // line-height: 24px;
      // line-height: var(--af-tree-node-height, 24px);
      color: $text-color;
      text-decoration: none;
      // vertical-align: top;
      cursor: pointer;
      border-radius: var(--af-tree-node-border-radius, 2px);
      transition: all 0.3s ease;

      &:hover {
        background-color: $treenode-itemhover-color;
      }

      &.#{$treePrefixCls}-node-selected {
        background-color: $treenode-selected-color;
      }
    }

    span {
      &.#{$treePrefixCls}-checkbox {
        margin: 0 4px 0 2px;
        vertical-align: middle;
      }

      &.#{$treePrefixCls}-switcher,
      &.#{$treePrefixCls}-iconele {
        display: inline-block;
        width: 24px;
        height: 24px;
        margin: 0;
        line-height: 24px;
        text-align: center;
        // vertical-align: middle;
        cursor: pointer;
        border: 0 none;
        outline: none;
      }

      &.#{$treePrefixCls}-icon-loading {
        position: absolute;
        top: 1px;
        left: 0;
        background: white;
        transform: translateX(-100%);
        transition: all 0.3s;

        &::after {
          @include iconfont-font($tree-loading-icon);

          display: inline-block;
          color: $treeColor;
          animation: loadingCircle 1s infinite linear;
        }
      }

      &.#{$treePrefixCls}-switcher {
        &.#{$treePrefixCls}-switcher-noop {
          vertical-align: bottom;
          cursor: default;
        }

        &.#{$treePrefixCls}-switcher-open {
          @include treeSwitcherIcon();
        }

        &.#{$treePrefixCls}-switcher-close {
          @include treeSwitcherIcon();

          &::after {
            transform: rotate(270deg) scale(0.59);
          }
        }
      }
    }

    &:last-child > span {
      &.#{$treePrefixCls}-switcher,
      &.#{$treePrefixCls}-iconele {
        &::before {
          display: none;
        }
      }
    }
  }

  > li {
    &:first-child {
      padding-top: 7px;
    }

    &:last-child {
      padding-bottom: 7px;
    }
  }

  &-child-tree {
    display: none;

    &-open {
      display: block;
    }
  }

  li.#{$treePrefixCls}-treenode-disabled {
    > span:not(.#{$treePrefixCls}-switcher),
    > .#{$treePrefixCls}-node-content-wrapper,
    > .#{$treePrefixCls}-node-content-wrapper span {
      color: $treenode-disabled-color;
      cursor: not-allowed;
    }

    > .#{$treePrefixCls}-node-content-wrapper:hover {
      background: transparent;
    }
  }

  &-icon-open {
    margin-right: 2px;
    vertical-align: top;
  }

  &-icon-close {
    margin-right: 2px;
    vertical-align: top;
  }
  // Tree with line
  &.#{$treePrefixCls}-show-line {
    li {
      position: relative;

      span {
        &.#{$treePrefixCls}-switcher {
          color: $text-color;
          background: $component-background;

          &.#{$treePrefixCls}-switcher-noop {
            @include treeShowLineIcon($tree-doc-icon);
          }

          &.#{$treePrefixCls}-switcher-open {
            @include treeShowLineIcon($tree-showline-open-icon);
          }

          &.#{$treePrefixCls}-switcher-close {
            @include treeShowLineIcon($tree-showline-close-icon);
          }
        }
      }
    }

    li:not(:last-child)::before {
      position: absolute;
      left: 12px;
      width: 1px;
      height: 100%;
      margin: 18px 0;
      border-left: 1px solid $border-color-base;
      content: ' ';
    }
  }

  // Tree with line
  &.#{$treePrefixCls}-dashline {
    li {
      position: relative;

      &::before,
      &::after {
        position: absolute;
        content: '';
      }

      &::before {
        top: -6px;
        left: -6px;
        width: 1px;
        height: 100%;
        border-left: 1px dashed #c5c5c5;
      }

      &::after {
        top: 14px;
        left: -5px;
        width: 10px;
        height: 10px;
        border-top: 1px dashed #c5c5c5;
      }

      &:last-child {
        &::before {
          height: 19px;
        }

        &::after {
          top: 12px;
        }
      }

      span {
        &.#{$treePrefixCls}-switcher {
          color: $text-color;
          background: $component-background;

          &.#{$treePrefixCls}-switcher-noop {
            @include treeShowLineIcon($tree-doc-icon);
          }

          &.#{$treePrefixCls}-switcher-open {
            @include treeShowLineIcon($tree-showline-open-icon);
          }

          &.#{$treePrefixCls}-switcher-close {
            @include treeShowLineIcon($tree-showline-close-icon);
          }
        }
      }
    }

    > li {
      &::before,
      &::after {
        border: 0;
      }
    }
  }

  // tight tree
  &.#{$treePrefixCls}-tight {
    li {
      ul {
        padding-left: 32px;
      }

      .amos-tree-node-content-wrapper {
        padding: 2px 5px;
      }
    }
  }

  .amos-tree-title {
    display: inline-block;
  }
}

// -- checkbox
$treeCheckboxPrefixCls: #{$vender-prefix}-tree-checkbox;
$checkboxInnerPrefixCls: #{$treeCheckboxPrefixCls}-inner;

// 一般状态
.#{$treeCheckboxPrefixCls} {
  position: relative;
  top: -0.09em;
  display: inline-block;
  line-height: 1;
  white-space: nowrap;
  vertical-align: middle;
  cursor: pointer;
  outline: none;

  &:hover &-inner {
    border-color: $treeColor;
  }

  &-checked::after {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    border: 1px solid $treeColor;
    border-radius: $border-radius-sm;
    content: '';
    visibility: hidden;
  }

  &:not(&-disabled)::after,
  .#{$treeCheckboxPrefixCls}-wrapper:hover &::after {
    visibility: visible;
  }

  &-inner {
    position: relative;
    top: 0;
    left: 0;
    display: block;
    width: #{$tree-checkbox-size}px;
    height: #{$tree-checkbox-size}px;

    &::before,
    &::after {
      position: absolute;
      border-radius: 2px;
      content: '';
      box-sizing: border-box;
    }

    &::before {
      top: 50%;
      left: 0;
      width: 16px;
      height: 16px;
      margin-top: -8px;
      font-size: 16px;
      background-color: white;
      border: $border-width-base $border-style-base $border-color-base;
      border-radius: $border-radius-sm;
    }

    &::after {
      top: 50%;
      left: 5px;
      display: none;
      width: 6px;
      height: 10px;
      margin-top: -6px;
      border: 2px solid $primary-color-deep;
      border-top: 0;
      border-left: 0;
      transform: rotate(45deg);
    }
  }
}

// 半选状态
.#{$treeCheckboxPrefixCls}-indeterminate {
  .#{$checkboxInnerPrefixCls} {
    &::after {
      left: 3px;
      display: block;
      width: 10px;
      height: 10px;
      margin-top: -5px;
      background-color: $primary-color-deep;
      border: 0;
      transform: none;
    }

    &::before {
      border-color: $treeColor;
    }
  }

  &.#{$treeCheckboxPrefixCls}-disabled {
    .#{$checkboxInnerPrefixCls}::after {
      border-color: $treenode-disabled-color;
    }
  }
}

// 选中状态
.#{$treeCheckboxPrefixCls}-checked {
  .#{$checkboxInnerPrefixCls}::after {
    display: block;
  }

  .#{$checkboxInnerPrefixCls}::before {
    border-color: $treeColor;
  }
}

.#{$treeCheckboxPrefixCls}-checked,
.#{$treeCheckboxPrefixCls}-indeterminate {
  .#{$checkboxInnerPrefixCls} {
    background-color: $treeColor;
    border-color: $treeColor;
  }
}

.#{$treeCheckboxPrefixCls}-disabled {
  cursor: not-allowed;

  &.#{$treeCheckboxPrefixCls}-checked {
    .#{$checkboxInnerPrefixCls}::after {
      border-color: $treenode-disabled-color;
    }

    .#{$checkboxInnerPrefixCls}::before {
      border-color: $border-color-base;
    }
  }

  .#{$treeCheckboxPrefixCls}-input {
    cursor: not-allowed;
  }

  .#{$checkboxInnerPrefixCls} {
    &::before {
      background-color: $tree-input-disable-bg;
      border-color: $border-color-base;
    }

    &::after {
      border-color: $tree-input-disable-bg;
    }
  }

  & + span {
    color: $treenode-disabled-color;
    cursor: not-allowed;
  }
}

.#{$treeCheckboxPrefixCls}-wrapper {
  display: inline-block;
  line-height: unset;
  cursor: pointer;

  & + & {
    margin-left: 8px;
  }
}

.#{$treeCheckboxPrefixCls}-wrapper + span,
.#{$treeCheckboxPrefixCls} + span {
  padding-right: 8px;
  padding-left: 8px;
}

.#{$treeCheckboxPrefixCls}-group {
  display: inline-block;

  &-item {
    display: inline-block;
    margin-right: 8px;

    &:last-child {
      margin-right: 0;
    }
  }

  &-item + &-item {
    margin-left: 0;
  }
}

// menu tree
.#{$treePrefixCls}-menu {
  position: relative;

  // Stretch selector width
  > li,
  .#{$treePrefixCls}-child-tree > li {
    span {
      &.#{$treePrefixCls}-switcher {
        position: relative;
        z-index: 1;

        &.#{$treePrefixCls}-switcher-noop {
          pointer-events: none;
        }
      }

      &.#{$treePrefixCls}-checkbox {
        position: relative;
        z-index: 1;
      }

      &.#{$treePrefixCls}-node-content-wrapper {
        border-radius: 0;
        user-select: none;

        &:hover {
          background: transparent;

          &::before {
            background: $primary-1;
          }
        }

        &.#{$treePrefixCls}-node-selected {
          color: white;
          background: transparent;
        }

        &::before {
          position: absolute;
          right: 0;
          left: 0;
          height: var(--af-tree-node-height, 24px);
          content: '';
          transition: all 0.3s;
        }

        > span {
          position: relative;
          z-index: 1;
        }
      }
    }

    &.#{$treePrefixCls}-treenode-selected {
      color: white;

      > span {
        &.#{$treePrefixCls}-checkbox {
          .#{$treePrefixCls}-checkbox-inner {
            border-color: $treeColor;
          }

          &.#{$treePrefixCls}-checkbox-checked {
            &::after {
              border-color: white;
            }

            .#{$treePrefixCls}-checkbox-inner {
              background: white;

              &::after {
                border-color: $treeColor;
              }
            }
          }
        }

        &.#{$treePrefixCls}-node-content-wrapper {
          &::before {
            background: $treeColor;
          }
        }

        // switcher
        &.#{$treePrefixCls}-switcher {
          color: white;
        }
      }
    }
  }

  &.#{$treePrefixCls}-tight {
    li {
      padding: 2px 0;
    }
  }

  // dashline
  &.#{$treePrefixCls}-show-line,
  &.#{$treePrefixCls}-dashline {
    li span.#{$treePrefixCls}-switcher {
      background: transparent;
    }
  }
}
