@use "styleboost/utilities/properties";
@use "styleboost/utilities/functions";
@use "styleboost/utilities/mixins";
@use "styleboost/variables";
@use "sass:string";

@layer defaults {

	$core: functions.themePropertyVar('color',          'core',          'menu');
	$core-text: functions.themePropertyVar('color',     'core-text',     'menu');
	$bg: functions.themePropertyVar('color',            'bg',            'menu');
	$bg-text: functions.themePropertyVar('color',       'bg-text',       'menu');
	$bg-rgb: functions.themePropertyVar('color',        'bg-rgb',        'menu');
	$accent: functions.themePropertyVar('color',        'accent',        'menu');
	$accent-text: functions.themePropertyVar('color',   'accent-text',   'menu');
	$emphasis: functions.themePropertyVar('color',      'emphasis',      'menu');
	$emphasis-text: functions.themePropertyVar('color', 'emphasis-text', 'menu');
	$emphasis-rgb: functions.themePropertyVar('color',  'emphasis-rgb',  'menu');

	:root {
		@include mixins.print-quoted-string-property('--menu-arrow-content', variables.$menu-arrow-content);
		@include mixins.print-quoted-string-property('--menu-arrow-font', variables.$menu-arrow-font);

		// Make menus transparent by default
		--color-c-menu-bg: transparent;

		// Default padding
		--size-c-menu-spacer-x: 0.5em;
		--size-c-menu-spacer-y: 0.75em;
		--size-c-menu-indent-left: 0.5em;
	}

	// The menu wrapper
	.menu {
		--color-p-menu-bg: #{properties.backgroundColor('menu')};
		--color-p-menu-bg-text: #{properties.textColor('menu')};

		--color-p-menu-bg-hover: #{properties.flashyHoverBackgroundColor('menu')};
		--color-p-menu-bg-hover-text: #{properties.flashyHoverBackgroundColor('menu', 'text')};

		--color-p-menu-bg-focus: #{properties.flashyFocusBackgroundColor('menu')};
		--color-p-menu-bg-focus-text: #{properties.flashyFocusBackgroundColor('menu', 'text')};

		--color-p-menu-bg-active: #{properties.flashyActiveBackgroundColor('menu')};
		--color-p-menu-bg-active-text: #{properties.flashyActiveBackgroundColor('menu', 'text')};

		--color-p-menu-bg-sub: #{properties.decoratedBackgroundColor('menu')};
		--color-p-menu-bg-sub-text: #{properties.decoratedBackgroundColor('menu', 'text')};

		--color-s-menu-bg: var(--color-p-menu-bg);
		--color-s-menu-bg-text: var(--color-p-menu-bg-text);

		// The actual menu list (ul)
		.menu-list {
			list-style: none;
			line-height: 1.25;
			margin: 0;
			padding: 0;
			min-height: 0;
			overflow: clip;
		}

		.menu-list-animator {
			display: grid;
			grid-template-rows: 1fr;
			min-height: 0;
			transition: grid-template-rows var(--duration-animation);
			overflow: clip;

			.menu-list {
				transition: margin var(--duration-animation);
			}
		}

		// The menu item (li)
		.menu-item {
			margin-bottom: 0;

			// Sub menus
			.menu-list {
				border-left: 1px solid rgba(#{$emphasis-rgb}, 0.4);
				margin: 0.5rem 1rem;

				--color-s-menu-bg: var(--color-p-menu-bg-sub);
				--color-s-menu-bg-text: var(--color-p-menu-bg-sub-text);

				.menu-link {
					padding-left: calc(var(--size-c-menu-indent-left) + var(--size-c-menu-spacer-x))
				}
			}

			// For when you want to let sub-menus slide in
			&:has(> .menu-list-animator) {
				&:has(input[type="radio"]:not(:checked), input[type="checkbox"]:not(:checked)) {
					.menu-list-animator:not(:focus-within) {
						grid-template-rows: 0fr;
					}

					.menu-list:not(:focus-within) {
						margin: 0;
					}
				}
			}

			// For when you do not want to let sub-menus slide in
			&:not(:has(> .menu-list-animator)) {
				&:has(input[type="radio"]:not(:checked), input[type="checkbox"]:not(:checked)) {
					.menu-list:not(:focus-within) {
						height: 0;
						margin: 0;
					}
				}
			}
		}

		// The menu "link" (display value inside an li)
		.menu-link {
			cursor: pointer;
			display: flex;
			align-items: center;
			padding: var(--size-c-menu-spacer-y) var(--size-c-menu-spacer-x);

			background-color: var(--color-s-menu-bg);
			color: var(--color-s-menu-bg-text);
			text-decoration: none;

			// Hide the inputs used for CSS-only click menus
			input[type="radio"],
			input[type="checkbox"] {
				display: none;
			}

			&:has(input[type="radio"]:checked),
			&:has(input[type="checkbox"]:checked) {
				> .menu-arrow::after {
					transform: rotate(90deg);
				}
			}

			&:has(.is-active),
			&:has(.active-link),
			&:active,
			&.is-active,
			&.active-link {
				--color-s-menu-bg: var(--color-p-menu-bg-active);
				--color-s-menu-bg-text: var(--color-p-menu-bg-active-text);
			}

			.menu-icon,
			.menu-bullet {
				margin-right: .5em;
				min-width: 2rem;
			}
		}

		.menu-arrow {
			display: flex;
			align-items: stretch;
			position: relative;
			overflow: hidden;
			flex-shrink: 0;
			margin-left: 5px;

			// This and the margin-left will make the arrow align to the right
			flex-grow: 1;

			&::after {
				transition: transform;
				transition-duration: 0.3s;
				margin-left: auto;
				content: var(--menu-arrow-content);
				font-family: var(--menu-arrow-font);
			}
		}
	}
}

// Putting these states here because they should have higher precedence
@layer components {
	.menu .menu-link {
		&:focus {
			--color-s-menu-bg: var(--color-p-menu-bg-focus);
			--color-s-menu-bg-text: var(--color-p-menu-bg-focus-text);
		}

		&:hover {
			--color-s-menu-bg: var(--color-p-menu-bg-hover);
			--color-s-menu-bg-text: var(--color-p-menu-bg-hover-text);
		}
	}
}