@use "../../styles/abstract/base" as base;
@use "../../styles/abstract/variables" as v;
@use "../../styles/abstract/mixins" as m;
@use "../../styles/abstract/functions" as f;
@use "../../styles/abstract/theme" as t;
@use "../../styles/abstract/direction" as d;

$prefix: base.$prefix;

$component: "#{$prefix}-menu";

@mixin mirrored {
[dir='rtl'] &,
&.#{$component}--rtl {
@content;
}
}

.#{$component} {
--#{base.$prefix}-density: 0;
@include m.typography("body-medium");
@include m.shape("extra-small");

position: fixed;
z-index: f.get-z-index("menu");
min-width: 112px;
max-width: 280px;
padding: 8px 0;
background-color: t.color("surface-container");
color: t.color("on-surface");
@include m.elevation(2);
overflow: hidden;
box-sizing: border-box;

display: flex;
flex-direction: column;
opacity: 0;
transform-origin: top center;
transform: scaleY(0);
pointer-events: none;
visibility: hidden;

transition:
transform v.motion("duration-medium1") v.motion("easing-emphasized"),
opacity v.motion("duration-medium1") v.motion("easing-emphasized"),
visibility v.motion("duration-medium1");

&[popover] {
inset: auto;
margin: 0;
border: 0;
}

&--visible {
opacity: 1;
transform: scaleY(1);
pointer-events: auto;
visibility: visible;
}

&--position-top {
transform-origin: bottom center;
}

&--submenu {
position: absolute;
z-index: f.get-z-index("menu") + 1;
}

&__group {
display: block;
list-style: none;

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

+ .#{$component}__group {
margin-top: 8px;
}
}

&__list {
margin: 0;
padding: 0;
list-style: none;
overflow-y: auto;
flex: 1 1 auto;
min-height: 0;
@include m.scrollbar;
width: 100%;
}

&__item {
@include m.typography("label-large");
@include m.flex-row;

position: relative;
min-height: 48px;
padding: 12px;
padding-inline-end: 40px;
cursor: pointer;
user-select: none;
color: t.color("on-surface");
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
width: 100%;
box-sizing: border-box;

&:hover {
@include m.state-layer(t.color("on-surface"), "hover");
}

&:focus {
outline: none;
}

&:focus-visible {
outline: 3px solid t.color("secondary");
outline-offset: -3px;
}

&:focus-visible,
&--active {
@include m.state-layer(t.color("on-surface"), "focus");
}

&--keyboard-active {
outline: 3px solid t.color("secondary");
outline-offset: -3px;
}

&:active {
@include m.state-layer(t.color("on-surface"), "pressed");
}

&--submenu {
padding-right: 48px;

&::after {
content: "";
position: absolute;
right: 12px;
top: 50%;
transform: translateY(-50%);
width: 24px;
height: 24px;
mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M10 17l5-5-5-5v10z'/%3E%3C/svg%3E")
center / contain no-repeat;
-webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M10 17l5-5-5-5v10z'/%3E%3C/svg%3E")
center / contain no-repeat;
background-color: currentColor;
opacity: 0.87;
}

&[aria-expanded="true"] {
@include m.state-layer(t.color("on-surface"), "hover");

&::after {
opacity: 1;
}
}
}

&--disabled {
cursor: default;
color: t.alpha("on-surface", 0.38);

.#{$component}__item-icon,
.#{$component}__item-supporting,
.#{$component}__item-shortcut {
color: inherit;
}

&:hover,
&:active {
&::before {
opacity: 0;
}
}
}

&--selected {
background-color: t.color("secondary-container");
color: t.color("on-secondary-container");

&:hover {
@include m.state-layer(t.color("on-secondary-container"), "hover");
}

&:focus-visible,
&.#{$component}__item--active {
@include m.state-layer(t.color("on-secondary-container"), "focus");
}

&:active {
@include m.state-layer(t.color("on-secondary-container"), "pressed");
}

.#{$component}__item-icon,
.#{$component}__item-shortcut {
color: inherit;
}

&::after {
content: "";
position: absolute;
left: 12px;
right: 12px;
margin-inline-start: auto;
top: 50%;
transform: translateY(-50%);
width: 18px;
height: 18px;
mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpolyline points='20 6 9 17 4 12' stroke='white' stroke-width='2' fill='none'/%3E%3C/svg%3E")
center / contain no-repeat;
-webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpolyline points='20 6 9 17 4 12' stroke='white' stroke-width='2' fill='none'/%3E%3C/svg%3E")
center / contain no-repeat;
background-color: currentColor;
}

&.#{$component}__item--disabled {
background-color: t.alpha("secondary-container", 0.38);
color: t.alpha("on-secondary-container", 0.38);

&:hover,
&:active {
&::before {
opacity: 0;
}
}
}
}

&-content {
display: flex;
align-items: center;
width: 100%;
overflow: hidden;
}

&-icon {
margin-inline-end: 12px;
display: flex;
align-items: center;
justify-content: center;
flex-shrink: 0;
color: t.color("on-surface-variant");

svg {
width: 24px;
height: 24px;
}
}

&-text {
flex: 1;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}

&-label {
display: flex;
flex-direction: column;
justify-content: center;
flex: 1;
min-width: 0;
}

&-supporting {
@include m.typography("body-medium");
color: t.color("on-surface-variant");
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}

&-shortcut {
margin-left: 12px;
color: t.color("on-surface-variant");
flex-shrink: 0;
}
}

&__divider {
height: 1px;
flex-shrink: 0;
margin: 8px 0;
background-color: t.color("outline-variant");
}

&:focus-visible {
outline: 3px solid t.color("secondary");
outline-offset: -3px;
}

&--vertical {
--#{$prefix}-menu-container: #{t.color("surface-container-low")};
--#{$prefix}-menu-label: #{t.color("on-surface")};
--#{$prefix}-menu-supporting: #{t.color("on-surface-variant")};
--#{$prefix}-menu-selected-container: #{t.color("tertiary-container")};
--#{$prefix}-menu-selected-label: #{t.color("on-tertiary-container")};
--#{$prefix}-menu-selected-disabled-container: color-mix(
    in srgb,
    var(--#{$prefix}-menu-selected-container) 38%,
    transparent
);

border-radius: v.shape("large");
background-color: var(--#{$prefix}-menu-container);
color: var(--#{$prefix}-menu-label);
padding: 4px;

&.#{$component}--vibrant {
--#{$prefix}-menu-container: #{t.color("tertiary-container")};
--#{$prefix}-menu-label: #{t.color("on-tertiary-container")};
--#{$prefix}-menu-supporting: #{t.color("on-tertiary-container")};
--#{$prefix}-menu-selected-container: #{t.color("tertiary")};
--#{$prefix}-menu-selected-label: #{t.color("on-tertiary")};
--#{$prefix}-menu-selected-disabled-container: var(--#{$prefix}-menu-selected-container);
}

.#{$component}__list {
display: flex;
flex-direction: column;
gap: 2px;
}

.#{$component}__item {
@include m.typography("body-large");
min-height: 44px;
padding: 8px 16px;
padding-inline-end: 16px;
color: var(--#{$prefix}-menu-label);
border-radius: v.shape("extra-small");
transition:
border-radius v.motion("spring-fast-spatial-duration")
v.motion("spring-fast-spatial-easing"),
background-color v.motion("duration-short2")
v.motion("easing-standard");

&--submenu {
padding-inline-end: 48px;
}

&:hover,
&:focus-visible,
&--active,
&:active {
border-radius: v.shape("medium");
}

&:first-child {
@include d.corners($start-start: v.shape("medium"), $start-end: v.shape("medium"), $arm: false);
}

&:last-child {
@include d.corners($end-start: v.shape("medium"), $end-end: v.shape("medium"), $arm: false);
}

&--selected {
background-color: var(--#{$prefix}-menu-selected-container);
color: var(--#{$prefix}-menu-selected-label);
border-radius: v.shape("medium");

&:hover {
@include m.state-layer(var(--#{$prefix}-menu-selected-label), "hover");
}

&:focus-visible,
&.#{$component}__item--active {
@include m.state-layer(var(--#{$prefix}-menu-selected-label), "focus");
}

&:active {
@include m.state-layer(var(--#{$prefix}-menu-selected-label), "pressed");
}

&.#{$component}__item--disabled {
background-color: var(--#{$prefix}-menu-selected-disabled-container);
color: color-mix(in srgb, var(--#{$prefix}-menu-selected-label) 38%, transparent);

&:hover,
&:active {
&::before {
opacity: 0;
}
}
}
}

&--selected:not(.#{$component}__item--submenu)::after {
content: none;
}

&--disabled {
color: color-mix(in srgb, var(--#{$prefix}-menu-label) 38%, transparent);

.#{$component}__item-icon,
.#{$component}__item-supporting,
.#{$component}__item-shortcut {
color: inherit;
}
}

&-icon {
color: var(--#{$prefix}-menu-supporting);
margin-inline-end: 12px;

svg {
width: 20px;
height: 20px;
}
}

&-shortcut {
@include m.typography("label-small");
color: var(--#{$prefix}-menu-supporting);
}

&-label {
display: flex;
flex-direction: column;
justify-content: center;
flex: 1;
min-width: 0;
}

&-supporting {
@include m.typography("body-medium");
color: var(--#{$prefix}-menu-supporting);
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}

&--selected {
.#{$component}__item-icon,
.#{$component}__item-shortcut,
.#{$component}__item-supporting {
color: inherit;
}
}
}

.#{$component}__divider {
margin: 6px 0;
}
}

&--vertical:has(.#{$component}__group) {
background-color: transparent;
box-shadow: none;
padding: 0;

overflow: visible;

.#{$component}__list {
overflow: visible;
gap: 2px;
}

.#{$component}__group {
background-color: var(--#{$prefix}-menu-container);
padding: 4px;
@include m.elevation(2);

border-radius: v.shape("small");

&:first-child {
@include d.corners($start-start: v.shape("large"), $start-end: v.shape("large"), $arm: false);
}

&:last-child {
@include d.corners($end-start: v.shape("large"), $end-end: v.shape("large"), $arm: false);
}

+ .#{$component}__group {
margin-top: 0;
}

> ul {
display: flex;
flex-direction: column;
gap: 2px;
}
}
}

&--vertical {
transition:
transform v.motion("duration-medium1") v.motion("easing-emphasized"),
opacity v.motion("duration-medium1") v.motion("easing-emphasized"),
visibility v.motion("duration-medium1"),
border-radius v.motion("spring-fast-spatial-duration")
v.motion("spring-fast-spatial-easing");

&.#{$component}--active {
border-radius: 24px;
}

&.#{$component}--inactive {
border-radius: v.shape("small");
}
}

&--dense {
padding: 4px 0;

.#{$component}__item {
@include m.typography("body-medium");
min-height: 32px;
padding: 4px 12px;
padding-right: 32px;

&--submenu {
padding-right: 36px;
}

&-icon svg {
width: 16px;
height: 16px;
}
}

.#{$component}__divider {
margin: 4px 0;
}
}

@include m.reduced-motion {
transition: opacity v.motion("duration-short2") linear, visibility v.motion("duration-short2");

&,
&--visible {
transform: none;
}

.#{$component}__item {
transition: none;
}
}

@include m.high-contrast {
&,
&[popover] {
border: 1px solid CurrentColor;
}

.#{$component}__group {
border: 1px solid CurrentColor;
}

.#{$component}__divider {
background-color: CurrentColor;
}

.#{$component}__item--selected {
background-color: Highlight;
color: HighlightText;
}

.#{$component}__item--disabled {
opacity: 1;
color: GrayText;
}
}

@include mirrored {
transform-origin: top right;
}

[dir='rtl'] &--position-top,
&.#{$component}--rtl.#{$component}--position-top {
transform-origin: bottom right;
}

@include mirrored {
.#{$component}__item--submenu {
padding-right: 16px;
padding-left: 48px;

&::after {
right: auto;
left: 16px;
transform: translateY(-50%) rotate(180deg);
}
}
}
}
