@import '@financial-times/math';
@import '@financial-times/o-brand/main';
@import '@financial-times/o-loading/main';
@import '@financial-times/o-private-foundation/main';

@import 'src/scss/variables';
@import 'src/scss/brand';

/// Output all oAutocomplete features
/// @param {Map} $opts [()] - A map of options to configure the output
/// @access public
/// @example scss
///		@include oAutocomplete($opts: (
///			// your opts here
///		))
@mixin oAutocomplete($opts: ()) {
	@include oPrivateFoundation();
	// content of primary mixin
	.o-autocomplete {
		position: relative;
		display: inline-grid;
	}

	.o-autocomplete__listbox-container {
		@include oPrivateTypographySans($scale: 0);
		grid-row: 1;
		grid-column: 1;
	}

	.o-autocomplete__visually-hidden {
		@include oPrivateNormaliseVisuallyHidden;
	}

	.o-autocomplete__clear {
		@include oPrivateIconsContent(
			'cross',
			_oAutocompleteGet('search-close'),
			$size: $_o-autocomplete-icon-size
		);
		border: 0;
		padding: 0;
		appearance: none;

		grid-row: 1;
		grid-column: 1;
		align-self: center;
		justify-self: end;

		$margin-size: calc(
			#{$_o-autocomplete-input-space} - #{$_o-autocomplete-icon-size-excess-whitespace}
		);
		margin-left: $margin-size;
		margin-right: $margin-size;

		z-index: 1;
	}

	// The .o-autocomplete is needed for this to have a bigger weight than
	// .o-forms-input--text input which sets the padding to 8px
	.o-autocomplete .o-autocomplete__input {
		box-sizing: border-box;
		// This is to make the input stop before the clear button.
		// Without this, the input's text would flow behind the clear button.
		padding-right: calc(
			#{$_o-autocomplete-icon-size} + #{$_o-autocomplete-input-space * 2}
		);
	}

	.o-autocomplete__input::-ms-clear {
		display: none;
	}

	.o-autocomplete__menu {
		@include oPrivateVisualEffectsShadowContent('high');
		top: oPrivateSpacingByName('s1');
		background-color: oPrivateFoundationGet('o3-color-palette-white');
		border-top: 0;
		color: oPrivateFoundationGet('o3-color-palette-black');
		margin: 0;
		max-height: 200px; // Make this configurable as it decides how many rows to show
		overflow-x: hidden;
		padding-left: oPrivateSpacingByName('s2');
		padding-right: oPrivateSpacingByName('s2');
		padding-top: oPrivateSpacingByName('s4');
		padding-bottom: oPrivateSpacingByName('s4');
		width: 100%;
	}

	.o-autocomplete__menu--visible {
		display: block;
	}

	.o-autocomplete__menu--loading {
		display: none;
	}

	.o-autocomplete__menu--hidden {
		display: none;
	}

	.o-autocomplete__menu--overlay {
		@include oPrivateVisualEffectsShadowContent('high');
		box-sizing: border-box;
	}

	.o-autocomplete__menu--inline {
		position: relative;
	}

	.o-autocomplete__option {
		border-bottom: 1px solid #e3e3e3;
		border-width: 1px 0;
		cursor: pointer;
		display: block;
		position: relative;
		padding-left: oPrivateSpacingByName('s1');
		padding-right: oPrivateSpacingByName('s1');
		padding-top: oPrivateSpacingByName('s2');
		padding-bottom: oPrivateSpacingByName('s2');
	}

	.o-autocomplete__option > * {
		pointer-events: none;
	}

	.o-autocomplete__option:first-of-type {
		border-top-width: 0;
	}

	.o-autocomplete__option:last-of-type {
		border-bottom-width: 0;
	}

	.o-autocomplete__option--focused,
	.o-autocomplete__option:hover {
		background-color: oPrivateFoundationGet('o3-color-palette-teal');
		border-color: oPrivateFoundationGet('o3-color-palette-teal');
		color: oPrivateFoundationGet('o3-color-palette-white');
		outline: none;
	}

	.o-autocomplete__option--no-results {
		background-color: oPrivateColorsMix(
			'o3-color-palette-white',
			'o3-color-palette-black',
			98
		); // #fafafa
		color: oPrivateColorsGetTextColor(
			oPrivateColorsMix('o3-color-palette-white', 'o3-color-palette-black', 98),
			$minimum-contrast: 'aa-normal'
		);
		cursor: not-allowed;
	}

	.o-autocomplete__option--highlight {
		font-weight: bolder;
	}

	.o-autocomplete__menu-loading {
		@include oLoadingContent(
			$opts: (
				size: 'mini',
				theme: 'dark',
			)
		);
	}

	// `.o-autocomplete__menu-loading` is declared twice intentionally.
	// `oLoadingContent` outputs css at the root using `@at-root`
	// and creates a new rule set. So custom styles that follow
	// `oLoadingContent` do not come after the mixin output.
	// https://github.com/Financial-Times/o-loading/blob/082a9c5e35f1fa61b3adce755b1df5193b72e0c8/src/scss/_mixins.scss#L79
	.o-autocomplete__menu-loading {
		grid-row: 2;
	}
	.o-autocomplete__menu-loading-container {
		@include oPrivateVisualEffectsShadowContent('high');
		display: flex;
		justify-content: center;
		align-items: center;
		padding: oPrivateSpacingByName('s2');
		box-sizing: border-box;
		min-height: 40px;
		background-color: oPrivateFoundationGet('o3-color-palette-white');
	}

	.o-autocomplete__menu-loading-container,
	.o-autocomplete__menu--overlay {
		position: absolute;
		top: calc(100% + #{oPrivateSpacingByName('s1')});
		left: 0;
		right: 0;
		z-index: 100;
	}
}
