@use "sass:math";
@use "@wordpress/base-styles/mixins" as *;
@use "@wordpress/base-styles/variables" as *;

$toggle-width: $grid-unit-40;
$toggle-height: $grid-unit-20;
$toggle-thumb-width: $grid-unit-20 + $grid-unit-05;
$toggle-border-width: 1px;
$toggle-label-size: $grid-unit-10;
// How far the thumb, and the labels with it, travel between the two states.
$toggle-travel: $toggle-width - $toggle-thumb-width;
// Centers each label in the part of the track the thumb leaves exposed.
$toggle-label-offset: math.div($toggle-travel - $toggle-label-size, 2);
// Where the brand panel starts when the toggle is off. Its inline-end edge sits
// under the middle of the thumb.
$toggle-panel-offset: math.div($toggle-thumb-width, 2) - $toggle-width;

$transition-duration: 0.2s;

.components-form-toggle {
	position: relative;
	isolation: isolate;
	display: inline-block;
	height: $toggle-height;

	// Unchecked state.
	.components-form-toggle__track {
		content: "";
		display: inline-block;
		box-sizing: border-box;
		vertical-align: top;
		background-color: var(--wpds-color-background-track-neutral);
		border: $toggle-border-width solid var(--wpds-color-stroke-interactive-neutral);
		width: $toggle-width;
		height: $toggle-height;
		border-radius: math.div($toggle-height, 2);

		@media not (prefers-reduced-motion) {
			transition: $transition-duration border-color ease;
		}
	}

	// Clips the brand panel and the labels as they slide in and out. It covers
	// the track border, so the panel's rounded edge meets the page rather than
	// the neutral track.
	.components-form-toggle__labels {
		position: absolute;
		top: 0;
		inset-inline-start: 0;
		width: $toggle-width;
		height: $toggle-height;
		overflow: hidden;
		border-radius: math.div($toggle-height, 2);
		pointer-events: none;

		// The brand half of the track. It slides with the thumb rather than
		// changing color, and the thumb always covers the edge where it meets
		// the neutral half.
		&::before {
			content: "";
			position: absolute;
			top: 0;
			bottom: 0;
			inset-inline-start: $toggle-panel-offset;
			width: $toggle-width;
			background-color: var(--wpds-color-background-interactive-brand-strong);

			@media not (prefers-reduced-motion) {
				transition: $transition-duration inset-inline-start ease;
			}

			@media ( forced-colors: active ) {
				background-color: Highlight;
			}
		}
	}

	// The thumb covers the track border, so it spans the full toggle height.
	.components-form-toggle__thumb {
		display: block;
		position: absolute;
		box-sizing: border-box;
		top: 0;
		inset-inline-start: 0;
		width: $toggle-thumb-width;
		height: $toggle-height;
		border: $toggle-border-width solid var(--wpds-color-stroke-interactive-neutral);
		border-radius: math.div($toggle-height, 2);
		background-color: var(--wpds-color-background-thumb-neutral);
		box-shadow: $elevation-x-small;

		@media not (prefers-reduced-motion) {
			transition:
				$transition-duration inset-inline-start ease,
				$transition-duration border-color ease;
		}

		@media ( forced-colors: active ) {
			background-color: Canvas;
			border-color: CanvasText;
		}
	}

	// The labels slide along with the thumb: the label for the current state
	// sits in the exposed part of the track, and the other waits just outside
	// the track's edge, where it is clipped.
	svg.components-form-toggle__label {
		position: absolute;
		top: 0;
		bottom: 0;
		margin: auto 0;
		width: $toggle-label-size;
		height: $toggle-label-size;
		// Override the browser's SVG default so the labels follow forced system colors.
		forced-color-adjust: auto;

		@media not (prefers-reduced-motion) {
			transition:
				$transition-duration inset-inline-start ease,
				$transition-duration inset-inline-end ease;
		}
	}

	svg.components-form-toggle__on-label {
		inset-inline-start: $toggle-label-offset + $toggle-border-width - $toggle-travel;
		color: var(--wpds-color-foreground-interactive-brand-strong);

		@media ( forced-colors: active ) {
			color: HighlightText;
		}
	}

	svg.components-form-toggle__off-label {
		inset-inline-end: $toggle-label-offset + $toggle-border-width;
		color: var(--wpds-color-foreground-interactive-neutral-weak);

		@media ( forced-colors: active ) {
			color: CanvasText;
		}
	}

	// The hover and checked states follow the input rather than the `checked`
	// prop, so they also work when the toggle is uncontrolled. `:where()` keeps
	// them at a lower specificity than the disabled state.
	:where(.components-form-toggle__input:not(:checked, :disabled):hover) {
		~ .components-form-toggle__track,
		~ .components-form-toggle__thumb {
			border-color: var(--wpds-color-stroke-interactive-neutral-active);
		}
	}

	.components-form-toggle__input:focus + .components-form-toggle__track {
		@include outset-ring__focus();
	}

	// Checked state.
	:where(.components-form-toggle__input:checked) {
		~ .components-form-toggle__track {
			border-color: var(--wpds-color-background-interactive-brand-strong);

			@media ( forced-colors: active ) {
				border-color: Highlight;
			}
		}

		~ .components-form-toggle__labels::before {
			inset-inline-start: $toggle-panel-offset + $toggle-travel;
		}

		~ .components-form-toggle__thumb {
			background-color: var(--wpds-color-background-thumb-brand-strong);
			border-color: var(--wpds-color-stroke-interactive-brand);
			inset-inline-start: $toggle-travel;

			@media ( forced-colors: active ) {
				border-color: Highlight;
			}
		}

		~ .components-form-toggle__labels svg.components-form-toggle__on-label {
			inset-inline-start: $toggle-label-offset + $toggle-border-width;
		}

		~ .components-form-toggle__labels svg.components-form-toggle__off-label {
			inset-inline-end: $toggle-label-offset + $toggle-border-width - $toggle-travel;
		}
	}

	// Disabled state:
	&.is-disabled,
	[inert] & {
		.components-form-toggle__track {
			background-color: var(--wpds-color-background-track-neutral-disabled);
			border-color: var(--wpds-color-stroke-interactive-neutral-disabled);

			@media ( forced-colors: active ) {
				border-color: GrayText;
			}
		}

		// Disabled toggles use the same track whether on or off.
		.components-form-toggle__labels::before {
			display: none;
		}

		.components-form-toggle__thumb {
			background-color: var(--wpds-color-background-thumb-neutral-disabled);
			border-color: var(--wpds-color-stroke-interactive-neutral-disabled);
			box-shadow: none;

			@media ( forced-colors: active ) {
				border-color: GrayText;
			}
		}

		svg.components-form-toggle__label {
			color: var(--wpds-color-foreground-interactive-neutral-disabled);

			@media ( forced-colors: active ) {
				color: GrayText;
			}
		}
	}
}

// This needs specificity to override inherited checkbox styles.
.components-form-toggle input.components-form-toggle__input[type="checkbox"] {
	position: absolute;
	top: 0;
	left: 0;
	width: 100%;
	height: 100%;
	opacity: 0;
	margin: 0;
	padding: 0;
	// Sit above the visual track and thumb so the invisible input receives pointer events.
	z-index: 1;

	// This overrides a border style that is inherited from parent checkbox styles.
	border: none;
	&:checked {
		background: none;
	}

	// Don't show custom checkbox checkmark.
	&::before {
		content: "";
	}

	&:not(:disabled, [aria-disabled="true"]) {
		cursor: var(--wpds-cursor-control);
	}
}
