@use 'mixins/mixins' as *;
@use 'mixins/var' as *;
@use 'mixins/utils' as *;
@use 'common/var' as *;
@use 'common/transition';

@mixin menu-item {
  display: flex;
  align-items: center;
  height: getCssVar('menu-item-height');
  line-height: getCssVar('menu-item-height');
  font-size: getCssVar('menu-item-font-size');
  color: getCssVar('menu-text-color');
  padding: 0 getCssVar('menu-base-level-padding');
  list-style: none;
  cursor: pointer;
  position: relative;
  transition: border-color getCssVar('transition-duration'),
    background-color getCssVar('transition-duration'),
    color getCssVar('transition-duration');
  box-sizing: border-box;
  white-space: nowrap;

  * {
    vertical-align: bottom;
  }

  i {
    color: inherit;
  }

  &:hover,
  &:focus {
    outline: none;
  }

  &:hover {
    background-color: getCssVar('menu-hover-bg-color');
  }

  @include when(disabled) {
    opacity: 0.25;
    cursor: not-allowed;
    background: none !important;
  }
}

:root {
  // They are defined on :root so they can be inherited by sub-menu instead of overwritten
  @include set-component-css-var('menu', $menu);
}

@include b(menu) {
  border-right: solid 1px getCssVar('menu-border-color');
  list-style: none;
  position: relative;
  margin: 0;
  padding-left: 0;
  background-color: getCssVar('menu-bg-color');
  box-sizing: border-box;

  @include m(vertical) {
    &:not(.#{$namespace}-menu--collapse):not(.#{$namespace}-menu--popup-container) {
      & .#{$namespace}-menu-item,
      & .#{$namespace}-sub-menu__title,
      & .#{$namespace}-menu-item-group__title {
        padding-left: calc(
          #{getCssVar('menu-base-level-padding')} + #{getCssVar('menu-level')} *
            #{getCssVar('menu-level-padding')}
        );
      }
    }
  }

  // TODO兼容此刻的写法
  @each $size in (large, default, small) {
    &--#{$size} {
      .#{$namespace}-menu-item {
        height: getCssVar('menu-item-height');
        line-height: getCssVar('menu-item-height');
      }
      .#{$namespace}-sub-menu__title {
        height: getCssVar('menu-item-height');
        line-height: getCssVar('menu-item-height');
      }
    }
  }

  @include m(collapse) {
    width: calc(
      #{getCssVar('menu-icon-width')} + #{getCssVar('menu-base-level-padding')} *
        2
    );

    > .#{$namespace}-menu-item,
    > .#{$namespace}-sub-menu > .#{$namespace}-sub-menu__title {
      [class^='#{$namespace}-icon'] {
        margin: 0;
        vertical-align: middle;
        width: getCssVar('menu-icon-width');
        text-align: center;
      }

      .#{$namespace}-sub-menu__icon-arrow {
        display: none;
      }

      > span {
        height: 0;
        width: 0;
        overflow: hidden;
        visibility: hidden;
        display: inline-block;
      }
    }

    > .#{$namespace}-menu-item.is-active i {
      color: inherit;
    }

    .#{$namespace}-menu .#{$namespace}-sub-menu {
      min-width: 200px;
    }

    .#{$namespace}-sub-menu {
      position: relative;

      & .#{$namespace}-menu {
        position: absolute;
        margin-left: 5px;
        top: 0;
        left: 100%;
        z-index: 10;
        border: 1px solid getCssVar('border-color-light');
        border-radius: getCssVar('border-radius-small');
        box-shadow: getCssVar('box-shadow-light');
      }
      &.is-opened {
        > .#{$namespace}-sub-menu__title .#{$namespace}-sub-menu__icon-arrow {
          transform: getCssVar('menu-icon-transform-closed');
        }
      }

      &.is-active .#{$namespace}-sub-menu__title {
        color: getCssVar('menu-active-color');
      }
    }
  }
  @include m(popup) {
    z-index: 100;
    min-width: 200px;
    border: none;
    padding: 5px 0;
    border-radius: getCssVar('border-radius-small');
    box-shadow: getCssVar('box-shadow-light');
  }

  .#{$namespace}-icon {
    flex-shrink: 0;
  }
}

@include b(menu-item) {
  @include menu-item;

  & [class^='#{$namespace}-icon'] {
    margin-right: 5px;
    width: getCssVar('menu-icon-width');
    text-align: center;
    font-size: 18px;
    vertical-align: middle;
  }
  @include when(active) {
    color: getCssVar('menu-active-color');
    i {
      color: inherit;
    }
  }
  .#{$namespace}-menu-tooltip__trigger {
    position: absolute;
    left: 0;
    top: 0;
    height: 100%;
    width: 100%;
    display: inline-flex;
    align-items: center;
    box-sizing: border-box;
    padding: 0 getCssVar('menu-base-level-padding');
  }
}

@include b(sub-menu) {
  list-style: none;
  margin: 0;
  padding-left: 0;

  @include e(title) {
    @include menu-item;

    &:hover {
      background-color: getCssVar('menu-hover-bg-color');
    }
  }
  & .#{$namespace}-menu {
    border: none;
  }
  & .#{$namespace}-menu-item {
    height: getCssVar('menu-sub-item-height');
    line-height: getCssVar('menu-sub-item-height');
    min-width: 200px;
  }
  @include e(hide-arrow) {
    .#{$namespace}-sub-menu__icon-arrow {
      display: none !important;
    }
  }
  @include when(active) {
    .#{$namespace}-sub-menu__title {
      border-bottom-color: getCssVar('menu-active-color');
    }
  }
  @include when(opened) {
    > .#{$namespace}-sub-menu__title .#{$namespace}-sub-menu__icon-arrow {
      transform: getCssVar('menu-icon-transform-open');
    }
  }
  @include when(disabled) {
    .#{$namespace}-sub-menu__title,
    .#{$namespace}-menu-item {
      opacity: 0.25;
      cursor: not-allowed;
      background: none !important;
    }
  }
  .#{$namespace}-icon {
    vertical-align: middle;
    margin-right: 5px;
    width: getCssVar('menu-icon-width');
    text-align: center;
    font-size: 18px;

    &.#{$namespace}-sub-menu__icon-more {
      margin-right: 0 !important;
    }
  }
  .#{$namespace}-sub-menu__icon-arrow {
    position: absolute;
    top: 50%;
    right: getCssVar('menu-base-level-padding');
    margin-top: -7px;
    transform: getCssVar('menu-icon-transform-closed');
    transition: transform getCssVar('transition-duration');
    font-size: 12px;
    margin-right: 0;
    width: inherit;
  }
}

@include b(menu-item-group) {
  > ul {
    padding: 0;
  }
  @include e(title) {
    padding: 7px 0 7px getCssVar('menu-base-level-padding');
    line-height: normal;
    font-size: 12px;
    color: getCssVar('text-color', 'secondary');
  }
}

.horizontal-collapse-transition
  .#{$namespace}-sub-menu__title
  .#{$namespace}-sub-menu__icon-arrow {
  transition: getCssVar('transition-duration-fast');
  opacity: 0;
}
