/* Patterns - Interaction - DatePicker */
////
/// @group Patterns-DatePicker
/// Patterns - Interaction - DatePicker

/// Import Flatpickr library styles
@use 'provider/flatpickr';
@use "../../../tokens/variables";

///
.osui-datepicker {
	// ─── Component CSS API ─────────────────────────────────────────────
	--osui-datepicker-disabled-background: var(--color-background-input-disabled);
	--osui-datepicker-disabled-border-color: var(--color-border);
	--osui-datepicker-disabled-color: var(--color-text-disabled);
	--osui-datepicker-input-border-radius: var(--border-radius-xs, #{variables.$token-shape-soft-xs});
	// Input chrome — control tier (xs). Popup uses xl via `.osui-datepicker-calendar`.
	--osui-input-border-radius: var(--osui-datepicker-input-border-radius);
	// ───────────────────────────────────────────────────────────────────

	input {
		// Hack used to disable dateTime input pseudoElements (browser native placeholders)
		&[type='date'] {
			&::-webkit-inner-spin-button {
				display: none;
				-moz-appearance: none;
			}

			&:not(.flatpickr-mobile) {
				&::-webkit-calendar-picker-indicator,
				&::-webkit-datetime-edit-year-field,
				&::-webkit-datetime-edit-month-field,
				&::-webkit-datetime-edit-day-field,
				&::-webkit-datetime-edit-fields-wrapper {
					display: none;
					-moz-appearance: none;
				}
			}
		}

		// The flatpickr mobile input is the visible field on touch devices — type=date
		// for Date values, type=datetime-local when time is enabled. Since Chrome for
		// Android M154 it renders with the desktop multi-field layout, where a tap can
		// land on an inner edit field without opening the picker. Make the text
		// click-through and stretch the (invisible) picker indicator over the whole
		// input so any tap opens the native calendar.
		&.flatpickr-mobile {
			position: relative;

			&::-webkit-datetime-edit {
				pointer-events: none;
			}

			&::-webkit-calendar-picker-indicator {
				height: auto;
				inset: 0;
				opacity: 0;
				position: absolute;
				width: auto;
				z-index: 1;
			}
		}

		// Disable states for Datepicker
		&.flatpickr-input[disabled] + input {
			background-color: var(--osui-datepicker-disabled-background);
			border: variables.$token-border-size-025 solid var(--osui-datepicker-disabled-border-color);
			color: var(--osui-datepicker-disabled-color);
			pointer-events: none;
		}

		// Hide the platform input which is set as hidden by the library and we're change it into the expected type, however we do not want it visible since library will add a clone to better deal with the selected dates.
		// We cannot use the provider class since the provider class will not be taken into consideration on the input widget react lifecycle
		&:first-of-type:not(.flatpickr-calendar.inline input) {
			display: none;

			// Make the platform input visible in Service Studio
			& {
				-servicestudio-display: inline-flex !important;
			}
		}
	}

	// Service Studio Preview Container Styles
	&-calendar-ss-preview {
		display: none;

		// Service Studio Preview
		& {
			-servicestudio-background-color: variables.$token-bg-surface-default;
			-servicestudio-border-radius: variables.$token-border-radius-100;
			-servicestudio-border: variables.$token-border-size-025 solid var(--color-border-input);
			-servicestudio-display: inline-block;
			-servicestudio-margin-top: 2px;
			-servicestudio-max-width: 320px;
			-servicestudio-overflow: hidden;
			-servicestudio-position: relative;
			-servicestudio-width: 100%;
			-servicestudio-z-index: var(--layer-local-tier-1);

			&:before,
			&:after {
				-servicestudio-color: var(--color-primary);
				-servicestudio-font-family: var(--osui-icon-font-family);
				-servicestudio-font-size: clamp(20px, 1.7vw, 20px);
				-servicestudio-position: absolute;
				-servicestudio-height: 34px;
				-servicestudio-padding-top: 3%;
				-servicestudio-top: 0;
			}

			&:before {
				-servicestudio-content: var(--osui-icon-arrow-left);
				-servicestudio-padding-right: 16%;
				-servicestudio-right: 0;
			}

			&:after {
				-servicestudio-content: var(--osui-icon-arrow-right);
				-servicestudio-padding-right: 8%;
				-servicestudio-right: 0;
			}

			.osui-datepicker-calendar-ss-preview--time,
			.osui-datepicker-calendar-ss-preview--today {
				-servicestudio-display: none;
			}

			&.time12h,
			&.time24h {
				.osui-datepicker-calendar-ss-preview--time {
					-servicestudio-text-align: center;
					-servicestudio-display: block;
					-servicestudio-font-size: clamp(10px, 1.7vw, 14px);
					-servicestudio-margin-bottom: variables.$token-scale-200;
					-servicestudio-position: relative;
				}
			}

			&.time12h .osui-datepicker-calendar-ss-preview--time::before {
				-servicestudio-background-color: var(--color-primary);
				-servicestudio-border-radius: variables.$token-border-radius-100;
				-servicestudio-color: variables.$token-primitives-base-white;
				-servicestudio-content: 'am';
				-servicestudio-font-size: clamp(10px, 1.7vw, 14px);
				-servicestudio-font-weight: variables.$token-font-weight-semi-bold;
				-servicestudio-height: variables.$token-scale-400;
				-servicestudio-line-height: variables.$token-scale-400;
				-servicestudio-position: absolute;
				-servicestudio-right: 2%;
				-servicestudio-top: 50%;
				-servicestudio-transform: translateY(-50%) translateX(-50%);
				-servicestudio-vertical-align: middle;
				-servicestudio-width: 15%;
			}

			// If has today btn
			&.has-today-btn {
				.osui-datepicker-calendar-ss-preview--today {
					display: block;
					text-align: center;
					font-size: clamp(10px, 1.7vw, 14px);
				}
			}

			& + .placeholder-ss-preview {
				-servicestudio-height: 0;
				-servicestudio-opacity: 0;
				-servicestudio-overflow: hidden;
				-servicestudio-width: 100%;
			}
		}
	}

	.not-valid + .input,
	.not-valid + .flatpickr-mobile {
		border-color: variables.$token-semantics-danger-base;
	}

	.flatpickr-mobile ~ span.validation-message {
		bottom: -40px;
	}
}

///
.form {
	// Service Studio Preview
	.osui-datepicker-calendar-ss-preview {
		-servicestudio-margin-top: -22px;
	}
}
