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

// Variant selectors use :where() to retain single-class specificity.
.select {
	display: block;
	background-color: $components-color-background;
	border: none;
	color: $components-color-foreground;
	cursor: var(--wpds-cursor-control);
	font-family: inherit;
	text-align: start;
	user-select: none;
	width: 100%;
	font-size: $mobile-text-min-font-size;

	&[data-focus-visible] {
		outline: none; // Handled by InputBase.
	}

	@media (min-width: $break-small) {
		font-size: $font-size-medium;

		&:where(.is-small) {
			font-size: $font-size-x-small;
		}
	}
}

// Keep the trigger's end padding aligned with SelectControlChevronDown (18px).
// The 6px correction aligns the item's checkmark with that chevron.
$sizes: (default, 40px, 12px), (small, 24px, 8px);

@each $size, $height, $padding in $sizes {
	$variant: "";
	@if $size != default {
		$variant: ":where(.is-#{$size})";
	}

	.select#{$variant} {
		padding-inline-start: $padding;
		padding-inline-end: $padding + 18px;

		min-height: $height;
	}

	.item#{$variant} {
		padding-inline-start: $padding;
		padding-inline-end: $padding - 6px;
	}
}

@keyframes slide-down {
	from {
		transform: translateY(-4px);
	}
	to {
		transform: translateY(0);
	}
}

@keyframes fade-in {
	from {
		opacity: 0;
	}
	to {
		opacity: 1;
	}
}

.popover {
	display: flex;
	flex-direction: column;
	background-color: $components-color-background;
	border-radius: $radius-small;
	border: 1px solid $components-color-foreground;
	box-shadow: $elevation-medium;
	/* z-index(".components-popover") */
	z-index: 1000000;
	max-height: min(var(--popover-available-height, 400px), 400px);
	overflow: auto;
	overscroll-behavior: contain;
	// The smallest size without overflowing the container.
	min-width: min-content;

	&[data-open] {
		@media not (prefers-reduced-motion) {
			animation-name: slide-down, fade-in;
			animation-duration: var(--wpds-motion-duration-md), var(--wpds-motion-duration-sm);
			animation-timing-function: var(--wpds-motion-easing-expressive), linear;
			will-change: transform, opacity;
		}
	}

	&[data-focus-visible] {
		// The outline will be on the trigger, rather than the popover.
		outline: none;
	}
}

.item {
	cursor: default;
	display: flex;
	align-items: center;
	justify-content: space-between;
	font-size: $font-size-medium;
	line-height: 28px;
	padding-block: 8px;
	scroll-margin: 4px;
	user-select: none;

	&[data-active-item] {
		// Keep highlight colors paired when options have custom inline colors.
		@include composite-item.highlight(!important);

		.item-hint {
			color: inherit;
		}
	}
}

.selected-hint-wrapper {
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}

.selected-hint {
	color: var(--wpds-color-foreground-content-neutral-weak);
	margin-inline-start: 8px;
}

.hint-wrapper {
	display: flex;
	justify-content: space-between;
	align-items: center;
	flex-wrap: wrap;
	flex: 1;
	column-gap: 16px;
}

.item-hint {
	color: var(--wpds-color-foreground-content-neutral-weak);
	text-align: initial;
	line-height: 1.4;
	padding-inline-end: 4px;
	margin-block: 4px;
}

.item-check {
	display: flex;
	align-items: center;
	margin-inline-start: 8px;
	fill: currentColor;
	// Align the 24px checkmark with the item's 28px line height: (28 - 24) / 2.
	align-self: start;
	margin-block-start: 2px;
	// Collapse the space for unselected items without hints. The icon uses em units.
	font-size: 0;

	.hint-wrapper ~ &,
	&:not(:empty) {
		font-size: 24px;
	}
}
