"use client";
/*! © 2026 Yahoo, Inc. UDS v0.0.0-development */
import { cx, getStyles } from "../../styles/styler.js";
import { Box } from "../Box.js";
import { FormLabel } from "../FormLabel.js";
import { Icon } from "../Icon.js";
import { SpringMotionConfig } from "./SpringMotionConfig.js";
import React, { forwardRef, useCallback, useEffect, useId, useMemo, useRef, useState } from "react";
import { jsx, jsxs } from "react/jsx-runtime";
import { Check, Minus } from "@yahoo/uds-icons";
import { m, useReducedMotion } from "motion/react";

//#region src/components/client/Checkbox.tsx
const VARIANTS = ["primary", "secondary"];
const SIZES = ["sm", "md"];
const VARIANT_ERROR_MAP = {
	primary: "alert",
	secondary: "alert-secondary"
};
const isIndeterminate = (checked) => checked === "indeterminate";
const getIcon = (checked) => {
	if (isIndeterminate(checked)) return Minus;
	return Check;
};
const MotionBox = m.create(Box);
const hoverTransition = {
	layoutVariant: "smooth",
	layoutSpeed: "3"
};
const pressTransition = {
	layoutVariant: "veryBouncy",
	layoutSpeed: "4"
};
/**
* **⚙️️ An checkbox.
*
* @componentType Client component
*
* @description
* A checkbox component allows users to select one or multiple options from a set. It represents a binary state, typically as checked or unchecked, and optionally includes a third "indeterminate" state to indicate partial selection.
*
* @see
* Check out the {@link https://uds.build/docs/components/checkbox Checkbox Docs} for more info
*
* @usage
* - Forms: checkbox
*
* @example
* ```tsx
* 'use client';
* import { Checkbox } from "@yahoo/uds";
*
* <Checkbox label="Name" required />
*```
*
* @related [Radio](https://uds.build/docs/components/radio), [Switch](https://uds.build/docs/components/switch), [Input](https://uds.build/docs/components/input).
**/
const Checkbox = forwardRef(function Checkbox({ id, checked: checkedProp, defaultChecked, onChange, label, labelPosition = "start", variant: variantProp = "primary", size = "md", reduceMotion: forceReduceMotion, disabled, required, hasError, className, layerClassNames, ...checkboxProps }, parentRef) {
	const generatedId = useId();
	const uid = id ?? `uds-input-${generatedId}`;
	const innerRef = useRef(null);
	/**
	* State
	*/
	const isControlled = checkedProp !== void 0;
	const [checked, setChecked] = useState(isControlled ? checkedProp : defaultChecked);
	const [isHovered, setIsHovered] = useState(false);
	const [isPressed, setIsPressed] = useState(false);
	/**
	* Derived values
	*/
	const variant = useMemo(() => hasError ? VARIANT_ERROR_MAP[variantProp] : variantProp, [hasError, variantProp]);
	const showHoverEffect = !disabled && isHovered;
	const showPressedEffect = !disabled && isPressed;
	const scale = useMemo(() => {
		if (showPressedEffect) return .9;
		if (showHoverEffect) return 1.1;
		return 1;
	}, [showPressedEffect, showHoverEffect]);
	const checkOpacity = useMemo(() => {
		if (!checked) return "opacity-0";
		if (showPressedEffect) return "opacity-55";
		return "opacity-100";
	}, [checked, showPressedEffect]);
	const motionAnimate = useMemo(() => ({ scale }), [scale]);
	const motionTransition = useMemo(() => isPressed ? pressTransition : hoverTransition, [isPressed]);
	/**
	* Handlers
	*/
	const handleChange = useCallback((e) => {
		onChange?.(e);
		if (!isControlled) setChecked(e.target.checked);
	}, [isControlled, onChange]);
	const handleHoverStart = useCallback(() => {
		setIsHovered(true);
	}, []);
	const handleHoverEnd = useCallback(() => {
		setIsHovered(false);
	}, []);
	const handleTapStart = useCallback(() => {
		setIsPressed(true);
	}, []);
	const handleTapEnd = useCallback(() => {
		setIsPressed(false);
	}, []);
	/**
	* Helpers
	*/
	const setDOMIndeterminate = (checked_) => {
		if (!innerRef.current) return;
		innerRef.current.indeterminate = isIndeterminate(checked_);
	};
	const prefersReducedMotion = useReducedMotion();
	const reduceMotion = forceReduceMotion ? "always" : "user";
	const cssAnimationDuration = prefersReducedMotion || forceReduceMotion ? "duration-0" : "duration-120";
	/**
	* Effects
	*/
	useEffect(() => {
		if (isControlled) setChecked(checkedProp);
	}, [checkedProp, isControlled]);
	useEffect(() => {
		setDOMIndeterminate(checked);
	}, [checked]);
	const valueState = isIndeterminate(checked) ? "indeterminate" : checked ? "checked" : "unchecked";
	const classNames = {
		root: getStyles({
			checkboxSizeRoot: size,
			checkboxVariantRoot: variant,
			checkboxVariantValueRoot: valueState,
			display: "flex",
			flexDirection: labelPosition === "start" ? "row" : "row-reverse",
			alignItems: "center",
			className: cx({
				"cursor-pointer": !disabled,
				"cursor-default": disabled,
				"opacity-50": disabled
			}, className, layerClassNames?.root)
		}),
		checkbox: getStyles({
			checkboxSizeCheckbox: size,
			checkboxVariantCheckbox: variant,
			checkboxVariantValueCheckbox: valueState,
			className: cx("uds-ring", "uds-ring-within", cssAnimationDuration, "transition-[background-color,border-color,box-shadow]", layerClassNames?.checkbox)
		}),
		check: getStyles({
			checkboxVariantCheckboxIcon: variant,
			checkboxVariantValueCheckboxIcon: valueState,
			className: cx("pointer-events-none", checkOpacity, 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", layerClassNames?.label)
	};
	const RootElement = label ? m.label : m.div;
	return /* @__PURE__ */ jsx(SpringMotionConfig, {
		reducedMotion: reduceMotion,
		...motionTransition,
		children: /* @__PURE__ */ jsxs(RootElement, {
			className: classNames.root,
			"data-testid": "container",
			onHoverStart: handleHoverStart,
			onHoverEnd: handleHoverEnd,
			onTapStart: handleTapStart,
			onTap: handleTapEnd,
			onTapCancel: handleTapEnd,
			tabIndex: -1,
			children: [/* @__PURE__ */ jsxs(MotionBox, {
				position: "relative",
				className: classNames.checkbox,
				alignItems: "center",
				justifyContent: "center",
				flex: "none",
				animate: motionAnimate,
				children: [/* @__PURE__ */ jsx("input", {
					type: "checkbox",
					tabIndex: 0,
					...checkboxProps,
					ref: (ref) => {
						innerRef.current = ref;
						setDOMIndeterminate(checked);
						if (typeof parentRef === "function") parentRef(ref);
						else if (parentRef !== null) parentRef.current = ref;
					},
					id: uid,
					disabled,
					required,
					"aria-invalid": hasError,
					checked: isControlled ? isIndeterminate(checked) ? false : checked : void 0,
					defaultChecked: isControlled ? void 0 : defaultChecked,
					onChange: handleChange,
					className: classNames.htmlCheckbox
				}), /* @__PURE__ */ jsx(Icon, {
					name: getIcon(checked),
					size: "sm",
					className: classNames.check
				})]
			}), label && /* @__PURE__ */ jsx(FormLabel, {
				as: "div",
				variant: "inherit",
				color: "inherit",
				required,
				label,
				className: classNames.label
			})]
		})
	});
});
Checkbox.displayName = "Checkbox";

//#endregion
export { Checkbox, SIZES, VARIANTS };