/*! © 2026 Yahoo, Inc. UDS v0.0.0-development */
"use client";
import { cx, getStyles } from "../../styles/styler.js";
import { IconSlot } from "../IconSlot.js";
import { Box } from "../Box.js";
import { FormLabel } from "../FormLabel.js";
import { SpringMotionConfig } from "./SpringMotionConfig.js";
import { forwardRef, useCallback, useEffect, useId, useMemo, useRef, useState } from "react";
import { jsx, jsxs } from "react/jsx-runtime";
import { m, useReducedMotion } from "motion/react";
//#region src/components/client/Switch.tsx
const SIZE_MAP = {
	md: { isOnX: "1.25rem" },
	sm: { isOnX: "0.75rem" }
};
const MotionBox = m.create(Box);
const toggleTransition = {
	layoutVariant: "bouncy",
	layoutSpeed: "4"
};
/**
* **⚙️️ An switch.
*
* @componentType Client component
*
* @description
* A switch (also called a toggle) is a binary on/off input control. It is clickable or touchable on mobile devices allowing users to pick between two clearly opposite choices.
*
* @see
* Check out the {@link https://uds.build/docs/components/switch Switch Docs} for more info
*
* @usage
* - Forms: switch
*
* @example
* ```tsx
* 'use client';
* import { Switch } from "@yahoo/uds";
*
* <Switch label="Name" required />
*```
*
* @related [Checkbox](https://uds.build/docs/components/checkbox).
**/
const Switch = forwardRef(function Switch({ id, isOn: isOnProp, defaultIsOn, onChange, label, labelPosition = "start", size = "md", onIcon, offIcon, reduceMotion: forceReduceMotion, disabled, required, onFocus, onBlur, className, style, slotProps, ...switchProps }, parentRef) {
	const generatedId = useId();
	const uid = id ?? `uds-input-${generatedId}`;
	const rootSlotProps = slotProps?.root ?? {};
	const switchSlotProps = slotProps?.switch ?? {};
	const handleSlotProps = slotProps?.handle ?? {};
	const labelSlotProps = slotProps?.label ?? {};
	const innerRef = useRef(null);
	/**
	* State
	*/
	const isControlled = isOnProp !== void 0;
	const [isOn, setIsOn] = useState(isControlled ? isOnProp : defaultIsOn);
	const [isHovered, setIsHovered] = useState(false);
	/**
	* Derived values
	*/
	const showHoverEffect = useMemo(() => {
		return !disabled && isHovered;
	}, [disabled, isHovered]);
	const { isOnX } = SIZE_MAP[size];
	const motionInitialAndAnimate = useMemo(() => ({
		initial: {
			scale: 1,
			x: isOn ? isOnX : 0
		},
		animate: {
			scale: showHoverEffect ? 1.05 : 1,
			x: isOn ? isOnX : 0
		}
	}), [
		showHoverEffect,
		isOn,
		isOnX
	]);
	/**
	* Handlers
	*/
	const handleChange = useCallback((e) => {
		onChange?.(e);
		if (!isControlled) setIsOn(e.target.checked);
	}, [isControlled, onChange]);
	const handleFocus = useCallback((e) => {
		onFocus?.(e);
	}, [onFocus]);
	const handleBlur = useCallback((e) => {
		onBlur?.(e);
	}, [onBlur]);
	const handleHoverStart = useCallback(() => {
		setIsHovered(true);
	}, []);
	const handleHoverEnd = useCallback(() => {
		setIsHovered(false);
	}, []);
	const prefersReducedMotion = useReducedMotion();
	const reduceMotion = forceReduceMotion ? "always" : "user";
	const cssAnimationDuration = prefersReducedMotion || forceReduceMotion ? "duration-0" : "duration-120";
	/**
	* Effects
	*/
	useEffect(() => {
		if (isControlled) setIsOn(isOnProp);
	}, [isOnProp, isControlled]);
	const classNames = {
		root: getStyles({
			switchSizeRoot: size,
			switchVariantRoot: "default",
			switchVariantActiveRoot: isOn ? "on" : "off",
			display: "flex",
			flexDirection: labelPosition === "start" ? "row" : "row-reverse",
			alignItems: "center",
			className: cx("group", {
				"cursor-pointer": !disabled,
				"cursor-default": disabled,
				"opacity-50": disabled
			}, className)
		}),
		switch: getStyles({
			switchVariantSwitch: "default",
			switchSizeSwitch: size,
			switchVariantActiveSwitch: isOn ? "on" : "off",
			borderRadius: "full",
			position: "relative",
			alignItems: "center",
			className: cx("uds-ring", "uds-ring-within", cssAnimationDuration, "transition-[background-color,box-shadow]")
		}),
		handle: getStyles({
			switchSizeHandle: size,
			className: cx("relative", "pointer-events-none", "uds-border-radius-full", "overflow-hidden")
		}),
		handleCircle: getStyles({
			switchVariantActiveHandle: isOn ? "on" : "off",
			switchVariantHandle: "default",
			className: cx("absolute", "top-0", "left-0", "right-0", "bottom-0", "opacity-95 group-hover:opacity-100", cssAnimationDuration, "transition-[background-color,box-shadow,opacity]")
		}),
		handleIcon: getStyles({
			switchSizeHandleIcon: size,
			switchVariantHandleIcon: "default",
			switchVariantActiveHandleIcon: isOn ? "on" : "off",
			className: cx("absolute", "opacity-0", "top-1/2", "left-1/2", "transform", "translate-x-[-50%]", "translate-y-[-50%]", cssAnimationDuration, "transition-opacity")
		}),
		htmlCheckbox: cx("cursor-[inherit]", "absolute", "opacity-0", "top-1/2", "left-1/2", "w-[calc(100%+2px)]", "h-[calc(100%+2px)]", "transform", "translate-x-[-50%]", "translate-y-[-50%]"),
		label: cx(labelPosition === "start" ? "text-start" : "text-end")
	};
	const RootElement = label ? m.label : m.div;
	return /* @__PURE__ */ jsx(SpringMotionConfig, {
		reducedMotion: reduceMotion,
		...toggleTransition,
		children: /* @__PURE__ */ jsxs(RootElement, {
			className: classNames.root,
			"data-testid": "container",
			style,
			onHoverStart: handleHoverStart,
			onHoverEnd: handleHoverEnd,
			tabIndex: -1,
			...rootSlotProps,
			children: [/* @__PURE__ */ jsxs(MotionBox, {
				className: classNames.switch,
				...switchSlotProps,
				children: [/* @__PURE__ */ jsx("input", {
					type: "checkbox",
					tabIndex: 0,
					...switchProps,
					ref: (ref) => {
						innerRef.current = ref;
						if (typeof parentRef === "function") parentRef(ref);
						else if (parentRef !== null) parentRef.current = ref;
					},
					onFocus: handleFocus,
					onBlur: handleBlur,
					id: uid,
					disabled,
					required,
					checked: isControlled ? isOn : void 0,
					defaultChecked: isControlled ? void 0 : defaultIsOn,
					onChange: handleChange,
					className: classNames.htmlCheckbox
				}), /* @__PURE__ */ jsxs(MotionBox, {
					className: classNames.handle,
					...motionInitialAndAnimate,
					...handleSlotProps,
					children: [
						/* @__PURE__ */ jsx(Box, { className: classNames.handleCircle }),
						onIcon && /* @__PURE__ */ jsx(IconSlot, {
							icon: onIcon,
							iconProps: {
								variant: "fill",
								size: "sm"
							},
							"data-testid": "on-icon",
							className: cx(classNames.handleIcon, isOn ? "opacity-100" : "opacity-0")
						}),
						offIcon && /* @__PURE__ */ jsx(IconSlot, {
							icon: offIcon,
							iconProps: {
								variant: "fill",
								size: "sm"
							},
							"data-testid": "off-icon",
							className: cx(classNames.handleIcon, !isOn ? "opacity-100" : "opacity-0")
						})
					]
				})]
			}), label && /* @__PURE__ */ jsx(FormLabel, {
				as: "div",
				variant: "inherit",
				color: "inherit",
				required,
				label,
				className: classNames.label,
				...labelSlotProps
			})]
		})
	});
});
Switch.displayName = "Switch";
//#endregion
export { Switch };
