@use "@wordpress/base-styles/variables" as *;
@use "@wordpress/base-styles/mixins" as *;
@use "@wordpress/base-styles/z-index" as *;
@use "../utils/theme-variables" as *;
@use "../utils/composite-item";

.menu {
	position: relative;
	z-index: z-index(".components-popover");

	/* Only visible in Windows High Contrast mode */
	outline: 2px solid transparent !important;
}

.menu-surface {
	display: grid;
	grid-template-columns: minmax(0, max-content) 1fr;
	grid-template-rows: auto;

	box-sizing: border-box;
	min-width: 160px;
	max-width: 320px;
	max-height: var(--popover-available-height);

	padding: $grid-unit-05;

	overscroll-behavior: contain;
	overflow: auto;

	background-color: $components-color-background;
	border-radius: $radius-medium;
	box-shadow: 0 0 0 $border-width $components-color-gray-300, $elevation-medium;

	&:where(.is-toolbar) {
		box-shadow: 0 0 0 $border-width $components-color-foreground;
	}
}

.menu-motion-root {
	// Keep the slide distance in sync with ../popover/utils.ts.
	@media not ( prefers-reduced-motion ) {
		transition-property: transform, opacity;
		transition-duration:
			var(--wpds-motion-duration-md),
			var(--wpds-motion-duration-sm);
		transition-timing-function:
			var(--wpds-motion-easing-expressive),
			linear;
		will-change: transform, opacity;

		&:not(:has(> .menu-surface[data-submenu])) {
			/* Regardless of the side, fade in and out. */
			opacity: 0;
			&:has(> .menu-surface[data-enter]) {
				opacity: 1;
			}

			/* Slide in the direction the menu is opening. */
			&:has(> .menu-surface[data-side="bottom"]) {
				transform: translateY(-4px);
			}
			&:has(> .menu-surface[data-side="top"]) {
				transform: translateY(4px);
			}
			&:has(> .menu-surface[data-side="left"]) {
				transform: translateX(4px);
			}
			&:has(> .menu-surface[data-side="right"]) {
				transform: translateX(-4px);
			}
			&:has(> .menu-surface[data-enter][data-side="bottom"]),
			&:has(> .menu-surface[data-enter][data-side="top"]) {
				transform: translateY(0);
			}
			&:has(> .menu-surface[data-enter][data-side="left"]),
			&:has(> .menu-surface[data-enter][data-side="right"]) {
				transform: translateX(0);
			}
		}
	}
}

.item {
	all: unset;
	cursor: var(--wpds-cursor-control);

	position: relative;
	min-height: $grid-unit-40;
	box-sizing: border-box;

	/* Occupy the width of all grid columns (ie. full width) */
	grid-column: 1 / -1;

	display: grid;
	grid-template-columns: minmax(0, max-content) 1fr;
	align-items: center;

	@supports ( grid-template-columns: subgrid ) {
		/*
		 * Define a grid layout which inherits the same columns configuration
		 * from the parent layout (ie. subgrid). This allows the menu
		 * to synchronize the indentation of all its items.
		 */
		grid-template-columns: subgrid;
	}

	font-size: $default-font-size;
	font-family: inherit;
	font-weight: 400;
	line-height: 20px;

	color: var(--wpds-color-foreground-interactive-neutral);
	border-radius: $radius-small;

	padding-block: $grid-unit-05;
	padding-inline: $grid-unit-15;

	/*
	 * Make sure that, when an item is scrolled into view (eg. while using the
	 * keyboard to move focus), the whole item comes into view
	 */
	scroll-margin: $grid-unit-05;

	user-select: none;
	outline: none;

	&[aria-disabled="true"] {
		color: var(--wpds-color-foreground-interactive-neutral-disabled);

		&:focus-visible {
			@include outset-ring__focus;
			outline-offset: 0;
		}
	}

	&:is(:focus, [data-focus]):not([aria-disabled="true"]) {
		@include composite-item.highlight;
	}

	&[aria-expanded="true"]:not(:focus, [data-focus], [aria-disabled="true"]) {
		background-color: var(--wpds-color-background-interactive-neutral-weak-active);

		@media ( forced-colors: active ) {
			outline: var(--wpds-border-width-xs) solid GrayText;
			outline-offset: 0;
		}
	}

	svg {
		fill: currentColor;
	}
}

.item-prefix-wrapper {
	/* Always occupy the first column, even when auto-collapsing */
	grid-column: 1;

	/*
	 * Even when the item is not checked, occupy the same screen space to avoid
	 * the space collapside when no items are checked.
	 */
	.checkbox-item > &,
	.radio-item > & {
		/* Same width as the check icons */
		min-width: $grid-unit-30;
	}

	.checkbox-item > &,
	.radio-item > &,
	&:not(:empty) {
		margin-inline-end: $grid-unit-10;
	}

	display: flex;
	align-items: center;
	justify-content: center;

	color: var(--wpds-color-foreground-content-neutral-weak);

	/* Inherit the active, open-parent, or disabled foreground. */
	[data-active-item] > &,
	[aria-expanded="true"] > &,
	[aria-disabled="true"] > & {
		color: inherit;
	}
}

.item-content-wrapper {
	/*
	 * Always occupy the second column, since the first column
	 * is taken by the prefix wrapper (when displayed).
	 */
	grid-column: 2;

	display: flex;
	align-items: center;
	justify-content: space-between;
	gap: $grid-unit-15;

	pointer-events: none;
}

.item-children-wrapper {
	flex: 1;

	display: inline-flex;
	flex-direction: column;
	gap: $grid-unit-05;
}

.item-suffix-wrapper {
	flex: 0 1 fit-content;
	min-width: 0;
	width: fit-content;

	display: flex;
	align-items: center;
	justify-content: center;
	gap: $grid-unit-15;

	color: var(--wpds-color-foreground-content-neutral-weak);

	/* Inherit the active, open-parent, or disabled foreground. */
	[data-active-item] *:not(.menu) &,
	[aria-expanded="true"] *:not(.menu) &,
	[aria-disabled="true"] *:not(.menu) & {
		color: inherit;
	}
}

.group {
	/* Ignore this element when calculating the layout. Useful for subgrid */
	display: contents;
}

.group-label {
	/* Occupy the width of all grid columns (ie. full width) */
	grid-column: 1 / -1;

	padding-block-start: $grid-unit-15;
	padding-block-end: $grid-unit-10;
	padding-inline: $grid-unit-15;
}

.separator {
	/* Occupy the width of all grid columns (ie. full width) */
	grid-column: 1 / -1;

	border: none;
	height: $border-width;
	background-color: $components-color-gray-200;
	/* Align with menu items' content */
	margin-block: $grid-unit-10;
	margin-inline: $grid-unit-15;

	/* Only visible in Windows High Contrast mode */
	outline: 2px solid transparent;

	&:where(.is-toolbar) {
		background-color: $components-color-foreground;
	}
}

.submenu-chevron-icon {
	width: 6px;
	transform: scaleX(1);

	&:where(.is-rtl) {
		transform: scaleX(-1);
	}
}

.item-label {
	font-size: $default-font-size;
	line-height: 20px;
	color: inherit;
}

.item-help-text {
	font-size: $helptext-font-size;
	line-height: 16px;
	color: var(--wpds-color-foreground-content-neutral-weak);
	overflow-wrap: anywhere;

	[data-active-item] *:not(.menu) &,
	[aria-expanded="true"] *:not(.menu) &,
	[aria-disabled="true"] *:not(.menu) & {
		color: inherit;
	}
}
