//
// Copyright IBM Corp. 2020
//
// This source code is licensed under the Apache-2.0 license found in the
// LICENSE file in the root directory of this source tree.
//

@use '../button/tokens' as button-tokens;
@use '../../config' as *;
@use '../../layer' as *;
@use '../../motion' as *;
@use '../../spacing' as *;
@use '../../theme' as *;
@use '../../type' as *;
@use '../../utilities/box-shadow' as *;
@use '../../utilities/focus-outline' as *;
@use '../../utilities/z-index' as *;
@use '../../utilities/convert';
@use '../../utilities/component-reset';

/// Menu styles
/// @access public
/// @group menu
@mixin menu {
  // Menu

  .#{$prefix}--menu {
    @include component-reset.reset;
    @include box-shadow;

    position: fixed;
    z-index: z('modal');
    padding: $spacing-02 0;
    background-color: $layer;
    max-inline-size: 18rem;
    min-inline-size: 10rem;
    opacity: 0;
    overflow-y: auto;
    visibility: hidden;
  }

  .#{$prefix}--menu--border {
    outline: 1px solid $border-subtle;
  }

  .#{$prefix}--menu--background-token__background {
    background-color: $background;
  }

  .#{$prefix}--menu--with-icons {
    min-inline-size: 12rem;
  }

  .#{$prefix}--menu--open {
    visibility: visible;

    &:focus {
      @include focus-outline('border');
    }
  }

  .#{$prefix}--menu:not(.#{$prefix}--menu--open) .#{$prefix}--menu--open {
    visibility: hidden;
  }

  .#{$prefix}--menu--shown {
    overflow: visible;
    opacity: 1;
  }

  // MenuItem

  .#{$prefix}--menu-item {
    @include type-style('body-short-01');

    display: grid;
    align-items: center;
    block-size: 2rem;
    color: $text-secondary;
    column-gap: $spacing-03;
    cursor: pointer;
    grid-template-columns: 1fr max-content;
    padding-inline: $spacing-05;
    transition: background-color $duration-fast-01 motion(standard, productive);

    &:focus {
      @include focus-outline('outline');
    }
  }

  .#{$prefix}--menu-item:hover {
    background-color: $layer-hover;
    color: $text-primary;
  }

  $supported-sizes: (
    'xs': 1.5rem,
    'sm': 2rem,
    'md': 2.5rem,
    'lg': 3rem,
  );

  @each $size, $value in $supported-sizes {
    .#{$prefix}--menu--#{$size} .#{$prefix}--menu-item {
      block-size: $value;
    }
  }

  .#{$prefix}--menu-item__label {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .#{$prefix}--menu-item__shortcut {
    display: flex;
  }

  .#{$prefix}--menu-item-group > ul,
  .#{$prefix}--menu-item-radio-group > ul {
    @include component-reset.reset;
  }

  .#{$prefix}--menu-item__icon,
  .#{$prefix}--menu-item__selection-icon {
    display: none;
  }

  .#{$prefix}--menu--with-icons > .#{$prefix}--menu-item,
  .#{$prefix}--menu--with-icons
    > .#{$prefix}--menu-item-group
    > ul
    > .#{$prefix}--menu-item,
  .#{$prefix}--menu--with-icons
    > .#{$prefix}--menu-item-radio-group
    > ul
    > .#{$prefix}--menu-item,
  .#{$prefix}--menu--with-selectable-items > .#{$prefix}--menu-item,
  .#{$prefix}--menu--with-selectable-items
    > .#{$prefix}--menu-item-group
    > ul
    > .#{$prefix}--menu-item,
  .#{$prefix}--menu--with-selectable-items
    > .#{$prefix}--menu-item-radio-group
    > ul
    > .#{$prefix}--menu-item {
    grid-template-columns: 1rem 1fr max-content;
  }

  .#{$prefix}--menu--with-icons
    > .#{$prefix}--menu-item
    > .#{$prefix}--menu-item__icon,
  .#{$prefix}--menu--with-icons
    > .#{$prefix}--menu-item-group
    > ul
    > .#{$prefix}--menu-item
    > .#{$prefix}--menu-item__icon,
  .#{$prefix}--menu--with-icons
    > .#{$prefix}--menu-item-radio-group
    > ul
    > .#{$prefix}--menu-item
    > .#{$prefix}--menu-item__icon,
  .#{$prefix}--menu--with-selectable-items
    > .#{$prefix}--menu-item
    > .#{$prefix}--menu-item__selection-icon,
  .#{$prefix}--menu--with-selectable-items
    > .#{$prefix}--menu-item-group
    > ul
    > .#{$prefix}--menu-item
    > .#{$prefix}--menu-item__selection-icon,
  .#{$prefix}--menu--with-selectable-items
    > .#{$prefix}--menu-item-radio-group
    > ul
    > .#{$prefix}--menu-item
    > .#{$prefix}--menu-item__selection-icon {
    display: flex;
  }

  .#{$prefix}--menu--with-icons.#{$prefix}--menu--with-selectable-items
    > .#{$prefix}--menu-item,
  .#{$prefix}--menu--with-icons.#{$prefix}--menu--with-selectable-items
    > .#{$prefix}--menu-item-group
    > ul
    > .#{$prefix}--menu-item,
  .#{$prefix}--menu--with-icons.#{$prefix}--menu--with-selectable-items
    > .#{$prefix}--menu-item-radio-group
    > ul
    > .#{$prefix}--menu-item {
    grid-template-columns: 1rem 1rem 1fr max-content;
  }

  .#{$prefix}--menu-item--disabled {
    color: $text-disabled;
    cursor: not-allowed;
  }

  .#{$prefix}--menu-item--disabled:hover,
  .#{$prefix}--menu-item--disabled.#{$prefix}--menu-item--danger:hover {
    background-color: $layer;
    color: $text-disabled;
  }

  .#{$prefix}--menu-item--danger:focus,
  .#{$prefix}--menu-item--danger:hover {
    background-color: button-tokens.$button-danger-primary;
    color: $text-on-color;
  }

  // MenuItemDivider

  .#{$prefix}--menu-item-divider {
    display: block;
    background-color: $border-subtle;
    block-size: convert.to-rem(1px);
    inline-size: 100%;
    margin-block: $spacing-02;
  }
}
