@use 'sass:map';

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

@mixin menu-item {
  position: relative;
  display: flex;
  align-items: flex-start;
  padding: 8px;
  margin-top: 4px;

  font-size: 14px;
  font-weight: 600;
  line-height: 18px;

  color: getCssVar('color-neutral-9');

  border-radius: 8px;

  list-style: none;
  cursor: pointer;

  transition: border-color getCssVar('transition-duration'),
    background-color getCssVar('transition-duration'),
    color getCssVar('transition-duration');

  * {
    vertical-align: bottom;
  }

  i {
    color: inherit;
  }

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

  &:hover:not(.is-disabled) {
    color: getCssVar('color-primary');
    background-color: getCssVar('color-primary-1');

    i {
      color: inherit;
    }
  }

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

  background-color: transparent;
  box-sizing: border-box;

  @include m(vertical) {
    padding: 8px;
    width: var(#{getCssVarName('menu-width')}, 192px);

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

  @include m(horizontal) {
    display: flex;
    flex-wrap: nowrap;
    padding: 0;

    @include b(sub-menu) {
      @include b(menu) {
        padding: 8px;
      }

      .#{$namespace}-sub-menu__title {
        margin-top: 0;
        margin-left: 1px;
      }
    }

    @include b(menu-item) {
      margin-top: 0;
      margin-left: 1px;
      white-space: nowrap;
    }

    .#{$namespace}-sub-menu__hide-arrow .#{$namespace}-sub-menu__title {
      padding-right: 8px;
    }
  }

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

    > * {
      overflow: hidden;
      width: 0;
      opacity: 0;
    }

    // > .#{$namespace}-menu-item,
    // > .#{$namespace}-sub-menu > .#{$namespace}-sub-menu__title {
    //   [class^='#{$namespace}-icon'] {
    //     margin: 0;
    //     vertical-align: middle;
    //     width: 24px;
    //     text-align: center;
    //     overflow: hidden;
    //   }

    //   .#{$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: 192px;
    border: none;
    padding: 8px;
    background-color: getCssVar('color-white');
    border-radius: getCssVar('border-radius-md');
    box-shadow: getCssVar('box-shadow-lv2');
  }

  .#{$namespace}-menu--popup-right-start {
    margin-left: 8px;
  }

  .#{$namespace}-menu--popup-left-start {
    margin-right: 8px;
  }

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

  @media #{map.get($breakpoints, 'md')} {
    @include m(collapse) {
      width: 24px;
      pointer-events: none;

      > .#{$namespace}-sub-menu,
      > .#{$namespace}-menu-item,
      > .#{$namespace}-menu-item.is-disabled {
        overflow: hidden;
        width: 0;
        opacity: 0;
      }
    }
  }
}

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

  & [class^='#{$namespace}-icon'] {
    margin-right: 8px;
    width: 16px;
    height: 18px;
    text-align: center;
    font-size: 16px;
    vertical-align: middle;
  }
  @include when(active) {
    color: getCssVar('color-neutral-9');
    background-color: getCssVar('color-neutral-3');

    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;

    padding-right: 24px;

    &:hover {
      background-color: getCssVar('color-primary-1');
    }
  }
  & .#{$namespace}-menu {
    padding: 0;
    border: none;
    width: auto;
  }

  @include e(hide-arrow) {
    .#{$namespace}-sub-menu__icon-arrow {
      display: none !important;
    }
  }
  @include when(active) {
    .#{$namespace}-sub-menu__title {
      border-bottom-color: getCssVar('color-neutral-9');
    }
  }
  @include when(opened) {
    > .#{$namespace}-sub-menu__title .#{$namespace}-sub-menu__icon-arrow {
      transform: rotateZ(180deg);
    }

    > .#{$namespace}-sub-menu__title:not(:hover)
      .#{$namespace}-sub-menu__icon-arrow {
      color: getCssVar('color-neutral-9');
    }
  }
  @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;
    width: 16px;
    text-align: center;
    font-size: 16px;

    &.#{$namespace}-sub-menu__icon-more {
      margin: 1px;
    }
  }
  .#{$namespace}-sub-menu__icon-arrow {
    position: absolute;
    top: 50%;
    right: 8px;
    width: inherit;
    margin-top: -7px;
    margin-right: 0;

    font-size: 12px;
    color: getCssVar('color-neutral-6');

    transform: none;
    transition: transform getCssVar('transition-duration');
  }
}

@include b(menu-item-group) {
  > ul {
    padding: 0;
  }
  @include e(title) {
    padding: 16px 8px 8px 0;
    line-height: 16px;
    font-size: 12px;
    font-weight: 600;
    color: getCssVar('color-neutral-7');
  }
}

.horizontal-collapse-transition {
  > * {
    display: none;
  }

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

@include b(menu-toggle) {
  position: absolute;
  top: 16px;
  right: -14px;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;

  border-radius: 14px;
  background-color: getCssVar('color-white');

  box-shadow: getCssVar('box-shadow-lv2');

  z-index: 1;

  &:hover {
    cursor: pointer;
  }

  i {
    color: getCssVar('color-primary-3');
    font-size: 12px;
  }
}

@media #{map.get($breakpoints-spec, 'sm-and-down')} {
  @include b(menu-toggle) {
    display: none;
  }
}
