@use '../../../00-abstract/_mixins.scss' as *;
@use '../../../tokens/variables';

/* Patterns - Interaction - DropdownServerSide */
////
/// @group Patterns-DropdownServerSide
/// Patterns - Interaction - DropdownServerSide

// Balloon margin value related with selectedValuesWrapper
$balloonMarginToSelectValuesWrapper: 2px;
$balloonMobileTopMargin: 5vh;

///
.osui-dropdown-serverside {
	// ─── Component CSS API ─────────────────────────────────────────────
	--osui-dropdown-ss-arrow-color: #{variables.$token-icon-subtlest};
	--osui-dropdown-ss-arrow-size: #{variables.$token-scale-400};
	--osui-dropdown-ss-background: var(--color-background-input);
	--osui-dropdown-ss-balloon-max-height: 300px;
	--osui-dropdown-ss-border-color: var(--color-border-input);
	--osui-dropdown-ss-border-radius: var(--border-radius-xs, #{variables.$token-shape-soft-xs});
	--osui-dropdown-ss-color: var(--color-text);
	--osui-dropdown-ss-disabled-background: var(--color-background-input-disabled);
	--osui-dropdown-ss-disabled-color: var(--color-text-disabled);
	--osui-dropdown-ss-focus-border-color: var(--color-border-primary);
	--osui-dropdown-ss-focus-ring-color: #{variables.$token-border-focus-default};
	--osui-dropdown-ss-hover-border-color: var(--color-border-input-hover);
	--osui-dropdown-ss-min-width: 170px;
	--osui-dropdown-ss-prompt-color: var(--color-text-subtlest);
	--osui-dropdown-ss-scroll-bar-width: 5px;
	// ───────────────────────────────────────────────────────────────────

	// Shared FloatingPosition contract — read by name from TS
	// (Utils/FloatingPosition/Enum.ts `CssCustomProperties.Offset`), so it keeps
	// the generic `--osui-floating-offset` name and is NOT part of this pattern's API.
	--osui-floating-offset: #{variables.$token-scale-100};
	position: relative;

	.osui-balloon {
		// Match the widget Dropdown list container: soft radius, overlay elevation, neutral border
		--osui-balloon-shadow: #{variables.$token-elevation-3};
		--osui-dropdown-ss-popup-border-radius: var(--border-radius-lg, #{variables.$token-shape-soft-lg});
		// Popup tier (lg) on portaled balloon — see detached-root rules at file end.
		--osui-balloon-border-radius: var(--osui-dropdown-ss-popup-border-radius);

		border: variables.$token-border-size-025 solid var(--color-border);
		display: flex;
		flex-direction: column;
		flex: 1;
		height: auto;
		min-width: var(--osui-dropdown-ss-min-width);
		overflow: hidden;
		z-index: var(--layer-global-elevated);

		// Service Studio Preview
		& {
			-servicestudio-left: initial;
			-servicestudio-margin-top: 4px;
			-servicestudio-max-height: 320px;
			-servicestudio-opacity: 1;
			-servicestudio-position: relative !important;
			-servicestudio-top: 0;
			-servicestudio-width: 100%;
		}
	}

	// "Input" section - SelectedValues container
	&__selected-values {
		align-items: center;
		display: flex;
		flex: 1;
		height: inherit;
		margin-inline-end: variables.$token-scale-400;
		overflow: hidden;

		&:after {
			align-items: center;
			color: var(--osui-dropdown-ss-arrow-color);
			display: flex;
			height: var(--osui-dropdown-ss-arrow-size);
			justify-content: center;
			line-height: 1;
			pointer-events: none;
			position: absolute;
			inset-inline-end: variables.$token-scale-400;
			top: 50%;
			transform: translateY(-50%);
			transition: transform variables.$token-transition-time-200 variables.$token-transition-curve-expressive;
			width: var(--osui-dropdown-ss-arrow-size);
		}

		& > * {
			&::selection {
				background-color: transparent;
			}

			&:first-child {
				overflow: hidden;
				text-overflow: ellipsis;
				white-space: nowrap;
				width: 100%;
			}
		}

		&-wrapper {
			align-items: center;
			background-color: var(--osui-dropdown-ss-background);
			border-radius: var(--osui-dropdown-ss-border-radius);
			border: variables.$token-border-size-025 solid var(--osui-dropdown-ss-border-color);
			color: var(--osui-dropdown-ss-color);
			cursor: pointer;
			display: flex;
			font-size: variables.$token-font-size-350;
			height: variables.$token-scale-1000;
			padding-block: variables.$token-scale-0;
			padding-inline: variables.$token-scale-400;
			position: relative;
			transition: border variables.$token-transition-time-300 variables.$token-transition-curve-expressive;
			width: 100%;

			// Hover — border darkens (matches Figma border/input/hover)
			&:hover {
				border-color: var(--osui-dropdown-ss-hover-border-color);
			}

			// Service Studio Preview
			& {
				-servicestudio-height: fit-content;
				-servicestudio-min-height: 40px;
			}
		}

		[data-expression] {
			white-space: nowrap;
		}
	}

	// Balloon section
	&__balloon {
		--osui-dropdown-ss-popup-border-radius: var(--border-radius-lg, #{variables.$token-shape-soft-lg});

		// Search Wrapper
		&-search-wrapper {
			background-color: var(--osui-dropdown-ss-background);
			padding: variables.$token-scale-0;
			position: relative;
			display: flex;
			align-items: center;
		}

		// Search placeholder
		&-search {
			flex: 1;

			&:before {
				align-items: center;
				color: var(--color-text-disabled);
				display: flex;
				height: 100%;
				inset-inline-start: variables.$token-scale-400;
				position: absolute;
				top: 0;
			}

			&:empty + .osui-dropdown-serverside__balloon-search-icon {
				display: none;
			}

			input,
			.form-control[data-input] {
				background-color: transparent;
				border-radius: variables.$token-border-size-0;
				border: none;
				color: inherit;
				font-size: variables.$token-font-size-350;
				height: variables.$token-scale-1000;
				padding-block: variables.$token-scale-0;
				padding-inline-end: variables.$token-scale-200;
				padding-inline-start: variables.$token-scale-400;
				width: 100%;

				&:focus,
				&:focus-visible {
					box-shadow: none;
					outline: none;
				}
			}
		}

		// Search Icon placeholder
		&-search-icon {
			align-items: center;
			display: flex;
			height: 100%;
			padding-block: variables.$token-scale-0;
			padding-inline: variables.$token-scale-400;
		}

		// Options placeholder
		&-content {
			border-block-start: variables.$token-border-size-025 solid var(--color-border-input);
			flex: 1;
			height: auto;
			max-height: var(--osui-dropdown-ss-balloon-max-height);
			overflow-x: hidden;
			overflow-y: auto;
			padding: variables.$token-scale-0;

			&::-webkit-scrollbar {
				width: var(--osui-dropdown-ss-scroll-bar-width);
			}

			&::-webkit-scrollbar-track {
				background: var(--color-border);
			}

			&::-webkit-scrollbar-thumb {
				background-color: var(--color-text-disabled);
			}

			// Avoid having data-list above other elements in this context, such as a StickyTop SelectAll Option container.
			& > *:not([data-list]) {
				z-index: var(--layer-local-tier-2);
			}
		}

		// If Search input do not exist, this modifier will be added!
		&--has-not-search {
			.osui-dropdown-serverside__balloon-content {
				border-block-start: none;
			}
		}

		// Footer placeholder
		&-footer {
			border-block-start: variables.$token-border-size-025 solid var(--color-border-input);
			padding-block: variables.$token-scale-200;
			padding-inline: variables.$token-scale-400;
		}
	}

	// Balloon IsOpen
	&:has(.osui-dropdown-serverside--is-opened) {
		.osui-dropdown-serverside {
			// "Input" section wrapper - SelectedValues container wrapper
			&__selected-values-wrapper {
				border-color: var(--osui-dropdown-ss-focus-border-color);
				box-shadow: 0 0 0 variables.$token-border-size-050 var(--osui-dropdown-ss-focus-ring-color);
			}

			// "Input" section - SelectedValues container
			&__selected-values {
				&:after {
					transform: translateY(-50%) rotate(180deg);
					transform-origin: center;
				}
			}
		}
	}

	// When it's disabled
	&--is-disabled {
		--osui-dropdown-ss-background: var(--color-background-input-disabled);
		--osui-dropdown-ss-border-color: var(--color-border);
		--osui-dropdown-ss-color: var(--color-text-disabled);

		.osui-dropdown-serverside__selected-values {
			&:after {
				border-color: var(--color-text-disabled);
			}

			&-wrapper {
				background-color: var(--osui-dropdown-ss-background);
				border-color: var(--osui-dropdown-ss-border-color);
				color: var(--osui-dropdown-ss-color);
				pointer-events: none;
			}
		}
	}

	// When it's not valid
	&--not-valid {
		.osui-dropdown-serverside__selected-values-wrapper {
			/* !important should be here in order to grant more specific selectors be overriding border color at this context. */
			border-color: variables.$token-border-danger-default !important;
		}

		& + .osui-dropdown-serverside-error-message {
			color: variables.$token-semantics-danger-base;
			font-size: variables.$token-font-size-300;
			margin-inline-start: variables.$token-scale-0;
			margin-block-start: variables.$token-scale-075;
		}
	}

	// Prompt (empty) state — muted text by default, darkens to text-default on hover (matches Figma).
	// Empty is detected by the absence of a selected option item under the root.
	&:not(.osui-dropdown-serverside--is-disabled):not(:has(.osui-dropdown-serverside-item--is-selected)) {
		.osui-dropdown-serverside__text {
			color: var(--osui-dropdown-ss-prompt-color);
		}

		.osui-dropdown-serverside__selected-values-wrapper:hover .osui-dropdown-serverside__text {
			color: var(--osui-dropdown-ss-color);
		}
	}
}

// Inside Form
.form {
	.osui-dropdown-serverside__balloon-search-wrapper input[data-input] {
		margin-block-end: 0;
	}

	.osui-dropdown-serverside__balloon .wcag-hide-text {
		position: absolute;
		margin: -1px;
	}
}

// Inside Tabs
.windows .osui-tabs {
	// subtracts the size of the border so it doesn't get cut off inside the tabs, in windows
	.osui-dropdown-serverside__selected-values-wrapper {
		width: calc(100% - 2 * variables.$token-border-size-025);
	}
}

// Accessibility -----------------------------------------------------------------
///
.has-accessible-features {
	.osui-dropdown-serverside {
		&__selected-values {
			&:hover,
			&:after,
			&-wrapper,
			&-footer {
				border-color: var(--color-border-input);
			}
		}

		&__balloon {
			border-color: var(--color-border-input);

			&-search-wrapper {
				&:focus-within:has(:focus-visible) {
					@include a11y-focus-shadow($isInner: true);
				}
			}

			&-search {
				&:before {
					color: var(--color-border-input);
				}
			}

			&-content {
				--osui-outline-size: calc(var(--osui-focus-inner-shadow-gap) + var(--osui-focus-shadow-width));

				&::-webkit-scrollbar-thumb {
					background-color: var(--color-border-input);
				}

				&:focus-visible {
					// Due the possibility of having a focus inside the balloon, we need to remove the outline from the balloon itself.
					box-shadow: none;

					// Common outline styles
					&:before,
					&:after,
					& > *:before,
					& > *:after {
						background-color: var(--osui-focus-shadow-color);
						content: '';
						display: block;
					}

					// This is the outline that will be applied to the content of the balloon vertically.
					&:before,
					&:after {
						height: var(--osui-outline-size);
						/* This negative margin will avoid having sticky element on pushing down the elements */
						margin-block-start: calc(-1 * var(--osui-outline-size));
						position: sticky;
						width: 100%;
						z-index: var(--layer-local-tier-3);
					}

					&:before {
						top: 0;
					}

					&:after {
						bottom: 0;
					}

					// This is the outline that will be applied to the content of the balloon horizontally.
					& > *:before,
					& > *:after {
						height: 100%;
						position: absolute;
						top: 0;
						width: var(--osui-outline-size);
						z-index: var(--layer-local-tier-2);
					}

					& > *:before {
						inset-inline-start: 0;
					}
					& > *:after {
						inset-inline-end: 0;
					}
				}
			}
		}
	}
}

// OS HighContrast Enabled -------------------------------------------------------
///
.os-high-contrast {
	.has-accessible-features .osui-dropdown-serverside__balloon-content:focus-visible {
		--osui-outline-size: 0;

		box-shadow: inset 0 0 0 variables.$token-border-size-075 var(--color-focus-outer);
	}
}

// Responsive --------------------------------------------------------------------
///
.tablet,
.phone {
	.osui-dropdown-serverside {
		&__selected-values-wrapper {
			height: variables.$token-scale-1200;
		}

		&__balloon-search {
			input,
			.form-control[data-input] {
				font-size: variables.$token-font-size-400;
				height: variables.$token-scale-1200;
			}
		}
	}
}

///
.phone {
	&.android {
		&.landscape {
			.osui-dropdown-serverside__balloon-container {
				max-height: 90vh;
			}
		}

		.osui-dropdown-serverside__balloon-container {
			max-height: calc(90vh - $balloonMobileTopMargin - var(--header-size));
		}
	}

	&.ios {
		.osui-dropdown-serverside__search-input--is-focused {
			.osui-dropdown-serverside__balloon-container {
				// 100vw because ios split in half the vertical height when keyboard is open!
				max-height: calc(100vw - $balloonMobileTopMargin - var(--header-size));
			}
		}
	}

	&.portrait {
		.osui-dropdown-serverside {
			&--at-content {
				.osui-dropdown-serverside__balloon-container {
					--ballon-top-margin-value: calc(var(--header-size) + var(--header-size-content) + 5vw);
				}
			}
		}
	}

	&.landscape {
		.osui-dropdown-serverside {
			// Balloon section
			&__balloon {
				// Content Container
				&-container {
					max-height: 50vh;
				}

				// If do not exist a input search inside balloon
				&--has-not-search {
					.osui-dropdown-serverside__balloon-container {
						max-height: 90vh;
					}
				}
			}

			&--at-content {
				.osui-dropdown-serverside__balloon-container {
					--ballon-top-margin-value: 10px;
				}
			}
		}
	}

	.osui-dropdown-serverside {
		// Balloon section
		&__balloon {
			align-items: center;
			background-color: variables.$token-backdrop;
			border: none;
			border-radius: #{variables.$token-border-radius-0};
			display: flex;
			inset-inline-start: 0;
			max-width: 100vw;
			opacity: 0;
			overflow: hidden;
			top: 0;
			transition: opacity variables.$token-transition-time-300 variables.$token-transition-curve-expressive;
			width: 100vw;
			z-index: var(--layer-global-instant-interaction);

			// Service Studio Preview
			& {
				-servicestudio-background-color: variables.$token-bg-surface-default;
				-servicestudio-left: initial;
				-servicestudio-opacity: 1;
				-servicestudio-position: relative;
				-servicestudio-top: 2px;
			}

			&-container {
				background: variables.$token-bg-surface-default;
				border-radius: var(
					--osui-dropdown-ss-popup-border-radius,
					var(--border-radius-lg, #{variables.$token-shape-soft-lg})
				);
				box-shadow: variables.$token-elevation-4;
				display: flex;
				flex-direction: column;
				height: auto;
				margin-block-start: $balloonMobileTopMargin;
				// 90vh - maxHeight in order to create a margin top/bottom with 5vh
				max-height: calc(90vh - $balloonMobileTopMargin - var(--header-size));
				overflow: hidden;
				width: 85vw;

				// Service Studio Preview
				& {
					-servicestudio-box-shadow: none;
					-servicestudio-margin-top: initial;
					-servicestudio-opacity: 1;
					-servicestudio-width: 100%;
				}
			}

			&-content {
				height: var(--osui-dropdown-ss-balloon-max-height);
				max-height: unset;
			}

			// If do not exist a input search inside balloon
			&--has-not-search {
				.osui-dropdown-serverside__balloon-container {
					margin-block-start: $balloonMobileTopMargin;
					max-height: calc(var(--viewport-height, 100vh) - $balloonMobileTopMargin - var(--header-size));
				}
			}
		}

		&--is-opened {
			&.osui-dropdown-serverside__balloon {
				min-height: 100vh;
				opacity: 1;
			}
		}

		// When moved to content container context
		&--at-content {
			align-items: flex-start;
			justify-content: center;
			max-height: 0;

			&.osui-dropdown-serverside--is-opened {
				max-height: 60vh;

				.osui-dropdown-serverside__balloon-container {
					transform: translateY(0);
				}
			}

			.osui-dropdown-serverside__balloon-container {
				margin-block-start: var(--ballon-top-margin-value);
				position: relative;
				transform: translateY(calc(0.5 * var(--ballon-top-margin-value)));
				transition: all variables.$token-transition-time-300 variables.$token-transition-curve-expressive;
			}

			.osui-dropdown-serverside__balloon-content {
				max-height: calc(100vh - (2 * var(--ballon-top-margin-value)));

				/* width */
				&::-webkit-scrollbar {
					width: 3px;
				}

				/* Track */
				&::-webkit-scrollbar-track {
					background-color: variables.$token-bg-neutral-subtle-default;
				}

				/* Handle */
				&::-webkit-scrollbar-thumb {
					background-color: var(--color-text-disabled);
				}

				/* Handle on hover */
				&::-webkit-scrollbar-thumb:hover {
					background-color: var(--color-border-input-press);
				}
			}
		}
	}
}

// Portaled popup roots — detached from `.osui-dropdown-serverside` (Balloon feature).
// CSS API must live on these selectors, not the pattern root. See ADR-0010.
.osui-dropdown-serverside__balloon.osui-balloon {
	--osui-dropdown-ss-popup-border-radius: var(--border-radius-lg, #{variables.$token-shape-soft-lg});
	--osui-balloon-border-radius: var(--osui-dropdown-ss-popup-border-radius);
}

.osui-dropdown-serverside__balloon-container {
	--osui-dropdown-ss-popup-border-radius: var(--border-radius-lg, #{variables.$token-shape-soft-lg});
	border-radius: var(--osui-dropdown-ss-popup-border-radius);
}
