@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;
  }

  @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) {
  list-style: none;
  position: relative;
  margin: 0;
  padding-left: 0;
  background-color: getCssVar('menu-bg-color');
  box-sizing: border-box;

  @include m(vertical) {
    width: 100%;
    .#{$namespace}-menu-item {
      height: 36px;
      line-height: 36px;
      margin-bottom: 8px;
      padding: 8px;
      &:hover {
        background-color: getCssVar('menu-hover-bg-color');
      }
      @include when(active) {
        background-color: getCssVar('bg-color-active');
      }
    }
    .#{$namespace}-menu--fold {
      height: 36px;
      line-height: 36px;
      margin-bottom: 8px;
      padding: 0 8px 0px 36px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      font-size: 14px;
      .#{$namespace}-icon {
        font-size: 14px;
      }
    }
    .#{$namespace}-menu--fold-collapse {
      padding: 8px;
      justify-content: center;
    }
    .#{$namespace}-sub-menu {
      .#{$namespace}-menu-item {
        padding: 0 8px 0px 36px;
      }
      .#{$namespace}-sub-menu__title {
        height: 36px;
        line-height: 36px;
        margin-bottom: 8px;
        padding: 8px;
      }
      .#{$namespace}-sub-menu {
        .#{$namespace}-sub-menu__title {
          padding: 0 8px 0px 36px;
        }
      }
    }
    .#{$namespace}-sub-menu__icon-arrow {
      position: absolute;
      top: 50%;
      right: 8px;
      margin-top: -6px;
      transition: transform getCssVar('transition-duration');
      font-size: 12px;
      margin-right: 0;
    }
    &:not(.#{$namespace}-menu--collapse):not(.#{$namespace}-menu--popup-container) {
      & .#{$namespace}-menu-item,
      & .#{$namespace}-sub-menu__title,
      & .#{$namespace}-menu-item-group__title {
        white-space: nowrap;
      }
    }
  }

  @include m(horizontal) {
    display: flex;
    flex-wrap: nowrap;
    border-bottom: solid 1px getCssVar('menu-border-color');
    border-right: none;

    & > .#{$namespace}-menu-item {
      display: inline-flex;
      justify-content: center;
      align-items: center;

      height: 100%;
      margin: 0;
      border-bottom: 4px solid transparent;
      color: getCssVar('menu-text-color');

      a,
      a:hover {
        color: inherit;
      }
    }
    & > .#{$namespace}-sub-menu {
      &:focus,
      &:hover {
        outline: none;
      }

      &:hover {
        .#{$namespace}-sub-menu__title {
          color: getCssVar('menu-hover-text-color');
        }
      }

      &.is-active {
        .#{$namespace}-sub-menu__title {
          border-bottom: 4px solid getCssVar('menu-active-color');
          color: getCssVar('menu-active-color');
        }
      }

      & .#{$namespace}-sub-menu__title {
        height: 100%;
        border-bottom: 4px solid transparent;
        color: getCssVar('menu-text-color');

        &:hover {
          background-color: getCssVar('bg-color', 'overlay');
        }
      }
    }
    & .#{$namespace}-menu {
      & .#{$namespace}-menu-item,
      & .#{$namespace}-sub-menu__title {
        background-color: getCssVar('menu-bg-color');
        display: flex;
        align-items: center;
        height: getCssVar('menu-horizontal-sub-item-height');
        padding: 0 16px;
        color: getCssVar('menu-text-color');
      }

      & .#{$namespace}-sub-menu__title {
        padding-right: 40px;
      }

      & .#{$namespace}-menu-item.is-active,
      & .#{$namespace}-sub-menu.is-active > .#{$namespace}-sub-menu__title {
        color: getCssVar('menu-active-color');
      }
    }
    & > .#{$namespace}-menu-item.is-active {
      border-bottom: 4px solid getCssVar('menu-active-color');
      color: getCssVar('menu-active-color') !important;
    }
  }
  @include m(collapse) {
    width: 36px;

    > .#{$namespace}-menu-item,
    > .#{$namespace}-sub-menu > .#{$namespace}-sub-menu__title,
    > .#{$namespace}-menu-item-group
      > ul
      > .#{$namespace}-sub-menu
      > .#{$namespace}-sub-menu__title {
      [class^='#{$namespace}-icon'] {
        margin: 0;
        vertical-align: middle;
        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;
    }
  }
  @include m(popup) {
    z-index: 100;
    border: none;
    padding: 8px 0;
    border-radius: getCssVar('border-radius-base');
    box-shadow: getCssVar('box-shadow-regular');
  }

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

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

  & [class^='#{$namespace}-icon'] {
    margin-right: 8px;
    text-align: center;
    font-size: 20px;
    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 8px;
  }
}

@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');
  }
  @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(disabled) {
    .#{$namespace}-sub-menu__title,
    .#{$namespace}-menu-item {
      opacity: 0.25;
      cursor: not-allowed;
      background: none !important;
    }
  }
  .#{$namespace}-icon {
    vertical-align: middle;
    margin-right: 8px;
    text-align: center;
    font-size: 20px;

    &.#{$namespace}-sub-menu__icon-more {
      margin-right: 0 !important;
    }
  }
  .#{$namespace}-sub-menu__icon-arrow {
    margin-left: 4px;
    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: 8px 8px 8px 36px;
    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;
}

.#{$namespace}-menu-popper {
  border: none !important;
  @include b(menu) {
    @include m(horizontal) {
      border-bottom: none;
      & .#{$namespace}-menu-item:not(.is-disabled):hover,
      & .#{$namespace}-menu-item:not(.is-disabled):focus {
        outline: none;
        color: getCssVar('text-color-primary');
        background-color: getCssVar('menu-hover-bg-color');
      }
    }
    @include m(vertical) {
      .#{$namespace}-menu-item {
        margin-bottom: 0;
        padding: 8px 12px;
      }
    }
  }

  .#{$namespace}-menu--popup-title {
    height: 32px;
    line-height: 32px;
    color: getCssVar('text-color-secondary');
    font-size: 12px;
    padding: 0 12px;
    i,
    .qd-icon {
      display: none;
    }
  }
}
