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

/* Widgets - Switch */
////
/// @group Widgets-Switch
/// Widgets - Switch

///
[data-switch] {
	// ─── Component CSS API ─────────────────────────────────────────────
	--osui-switch-height: #{variables.$token-scale-600}; // 24px
	--osui-switch-width: #{variables.$token-scale-1000}; // 40px
	--osui-switch-thumb-size: #{variables.$token-scale-500}; // 20px
	--osui-switch-thumb-offset-start: 2px;
	--osui-switch-thumb-offset-end: 18px;
	--osui-switch-track-color: #{variables.$token-bg-neutral-base-default};
	--osui-switch-checked-track-color: var(--color-primary);
	--osui-switch-disabled-overlay: #{variables.$token-state-disabled};
	--osui-switch-thumb-color: #{variables.$token-bg-surface-inverse};
	--osui-switch-thumb-shadow: #{variables.$token-elevation-1};
	// ───────────────────────────────────────────────────────────────────

	background-color: transparent;
	border-radius: 0;
	height: var(--osui-switch-height);
	overflow: initial;
	width: var(--osui-switch-width);

	&:empty {
		&:before {
			background-color: var(--osui-switch-track-color);
			border: 0;
			border-radius: var(--border-radius-rounded);
			height: var(--osui-switch-height);
			opacity: 1;
			transition: background-color variables.$token-transition-time-200 variables.$token-transition-curve-linear;
			width: var(--osui-switch-width);
		}

		&:after {
			background-color: var(--osui-switch-thumb-color);
			border: 0;
			bottom: 0;
			box-shadow: var(--osui-switch-thumb-shadow);
			height: var(--osui-switch-thumb-size);
			// Physical on purpose: the thumb offset is a `translateX`, which has no logical
			// form. A logical inset would flip the anchor in RTL while the transform kept
			// pushing the same way, sliding the thumb outside the track.
			left: 0;
			margin-left: 0;
			top: calc((var(--osui-switch-height) - var(--osui-switch-thumb-size)) / 2);
			transform: translateX(var(--osui-switch-thumb-offset-start)) translateZ(0);
			transition: transform variables.$token-transition-time-200 variables.$token-transition-curve-linear;
			width: var(--osui-switch-thumb-size);
		}
	}

	&:checked {
		&:before {
			background-color: var(--osui-switch-checked-track-color);
		}

		&:after {
			transform: translateX(var(--osui-switch-thumb-offset-end)) translateZ(0);
		}
	}

	&[disabled] {
		pointer-events: none;

		&:empty {
			&:before {
				background-color: var(--osui-switch-track-color);
				border: 0;
				box-shadow: inset 0 0 0 999px var(--osui-switch-disabled-overlay);
			}

			&:after {
				background-color: var(--osui-switch-thumb-color);
				border: 0;
				box-shadow: var(--osui-switch-thumb-shadow);
			}
		}

		&:checked:empty:before {
			background-color: var(--osui-switch-checked-track-color);
		}
	}
}

// Accessibility -----------------------------------------------------------------
///
.has-accessible-features {
	[data-switch] {
		border-radius: var(--border-radius-rounded);

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

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

		&:checked {
			&:before {
				background-color: var(--osui-switch-checked-track-color);
				border-color: var(--osui-switch-checked-track-color);
			}
		}

		&:focus-visible {
			--osui-focus-shadow-gap: #{variables.$token-scale-050};

			@include a11y-focus-shadow;

			&:before {
				border-color: variables.$token-semantics-primary-base;
			}
		}
	}
}

// OS HighContrast Enabled -------------------------------------------------------
///
.os-high-contrast {
	[data-switch] {
		&:empty:after {
			align-items: center;
			border: variables.$token-border-size-025 solid var(--color-text-disabled);
			content: '0';
			display: flex;
			font-family: monospace;
			font-size: variables.$token-font-size-300;
			justify-content: center;
			inset-inline-start: -1px;
			top: variables.$token-scale-075;
		}

		&:checked:after {
			content: '1';
		}
	}
}
