@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/main';

/// @access public
/// @param {Map} $opts [('elements': ('checkbox', 'date', 'password', 'radio-round', 'radio-box', 'select', 'textarea', 'text', 'toggle'), 'features': ('disabled', 'inline', 'inverse', 'error-summary', 'right', 'small', 'state', 'suffix'))] The o-form features to include styles for (see the README for a full list).
/// @example
///		@include oForms($opts: (
///			'elements': ('text', 'checkbox'),
///			'features': ('inline')
///		));
/// @output Styles for form elements.
@mixin oForms(
	$opts: (
		'elements': (
			'checkbox',
			'date',
			'file',
			'password',
			'pseudo-radio-link',
			'radio-round',
			'radio-box',
			'select',
			'textarea',
			'text',
			'toggle',
		),
		'features': (
			'disabled',
			'inline',
			'inverse',
			// @deprecated, see 'themes' property
			'error-summary',
			'right',
			'small',
			'state',
			'suffix',
		),
		'themes': (
			'inverse',
			'white',
			'professional',
			'professional-inverse',
			'ft-live',
		),
	)
) {
	@include oPrivateFoundation();

	$elements: map-get($opts, 'elements');
	$features: map-get($opts, 'features');
	$themes: if(map-has-key($opts, 'themes'), map-get($opts, 'themes'), ());

	$disabled: index($features, 'disabled');
	$inline: index($features, 'inline');
	// For use on a dark background.
	$inverse-theme: index($features, 'inverse') or index($themes, 'inverse');
	// For use on a white background (as oppose to another background, e.g. paper for the core brand)
	$white-theme: index($themes, 'white');

	$error-summary: index($features, 'error-summary');
	$right: index($features, 'right');
	$small: index($features, 'small');
	$state: index($features, 'state');
	$suffix: index($features, 'suffix');
	$radio-round: index($elements, 'radio-round');
	$radio-box: index($elements, 'radio-box');
	$file: index($elements, 'file');

	@include _oFormsBase();

	@if $themes {
		@each $theme in $themes {
			$default-text-color: _oFormsGet('default-text', $theme);
			@if $default-text-color and
				oPrivateColorsColorBrightness($default-text-color) >
				65%
			{
				.o-forms-field--#{$theme} .o-forms-title {
					.o-forms-title__main:after,
					.o-forms-title__prompt {
						color: rgba(_oFormsGet('default-text', $theme), 0.7);
					}
				}
				.o-forms-field--#{$theme} input {
					@include oPrivateNormaliseFocusApply() {
						@include oPrivateNormaliseFocusContentInverse();
					}
				}
			}
		}
	}

	@if index($elements, 'pseudo-radio-link') {
		@include _oFormsPseudoRadioLink();
	}

	@if index($elements, 'checkbox') {
		@include _oFormsCheckbox($disabled, $right, $themes);
	}

	@if index($elements, 'date') {
		@include _oFormsDate($disabled);
	}

	@if $file {
		@include _oFormsFileInput();
	}

	@if $radio-round {
		@include _oFormsRadioRound($disabled, $themes);
	}

	@if $radio-box {
		@include _oFormsRadioBox($disabled, $themes);
	}

	@if index($elements, 'select') {
		@include _oFormsSelect($disabled, $small, $suffix);
	}

	@if index($elements, 'textarea') {
		@include _oFormsTextArea($disabled);
	}

	@if index($elements, 'text') or index($elements, 'password') {
		@include _oFormsTextInput(
			$disabled,
			$small,
			$suffix,
			$include-password-selector: index($elements, 'password')
		);
	}

	@if index($elements, 'toggle') {
		@include _oFormsToggle($disabled);
	}

	@if $inline {
		@include _oFormsInline();
	}

	@if $error-summary {
		@include _oFormsErrorSummary();
	}

	@if $inverse-theme {
		@include _oFormsInverse($error-summary, $radio-box, $file);
	}

	@if $white-theme {
		.o-forms-field--white {
			--o-forms-toggle-handle-color: #{oPrivateFoundationGet(
					'o3-color-palette-white'
				)};
		}
	}

	@if $state {
		@include _oFormsState();
	}
}

@if $o-forms-is-silent == false {
	@include oForms();

	$o-forms-is-silent: true !global;
}
