import type { LoadingDotsProps } from "$/types/LoadingTypes";
import { usePrefersReducedMotion } from "$/util/AccessibilityUtil";
import { useId } from "react";


export default function CyclingDots({
	size,
	width,
	height,
	className,
	animationDuration = 500,
	color,
	stroke,
	fill,
	ariaLabel = "Loading"
}: Readonly<LoadingDotsProps>){
	//https://github.com/n3r4zzurr0/svg-spinners/blob/main/svg-smil/3-dots-move.svg
	const id = useId();
	const reducedMotion = usePrefersReducedMotion();
	const dur = reducedMotion ? animationDuration / 100 : animationDuration / 1000;


	return (
		<svg
			width={size ?? width}
			height={size ?? height}
			role="status"
			aria-live="polite"
			aria-label={ariaLabel}
			viewBox="0 0 24 24"
			xmlns="http://www.w3.org/2000/svg"
		>
			<circle
				cx="4"
				cy="12"
				r="0"
				className={className}
				stroke={color ?? stroke}
				fill={color ?? fill}
			>
				<animate
					begin="0;spinner_z0Or.end"
					attributeName="r"
					calcMode="spline"
					dur={dur}
					keySplines=".36,.6,.31,1"
					values="0;3"
					fill="freeze"
				/>
				<animate
					begin="spinner_OLMs.end"
					attributeName="cx"
					calcMode="spline"
					dur={dur}
					keySplines=".36,.6,.31,1"
					values="4;12"
					fill="freeze"
				/>
				<animate
					begin="spinner_UHR2.end"
					attributeName="cx"
					calcMode="spline"
					dur={dur}
					keySplines=".36,.6,.31,1"
					values="12;20"
					fill="freeze"
				/>
				<animate
					id={`firstDotFourthAnimate_${id}`}
					begin="spinner_Aguh.end"
					attributeName="r"
					calcMode="spline"
					dur={dur}
					keySplines=".36,.6,.31,1"
					values="3;0"
					fill="freeze"
				/>
				<animate
					id="spinner_z0Or"
					begin={`firstDotFourthAnimate_${id}.end`}
					attributeName="cx"
					dur="0.001s"
					values="20;4"
					fill="freeze"
				/>
			</circle>
			<circle
				cx="4"
				cy="12"
				r="3"
				className={className}
				stroke={color ?? stroke}
				fill={color ?? fill}
			>
				<animate
					begin="0;spinner_z0Or.end"
					attributeName="cx"
					calcMode="spline"
					dur={dur}
					keySplines=".36,.6,.31,1"
					values="4;12"
					fill="freeze"
				/>
				<animate
					begin="spinner_OLMs.end"
					attributeName="cx"
					calcMode="spline"
					dur={dur}
					keySplines=".36,.6,.31,1"
					values="12;20"
					fill="freeze"
				/>
				<animate
					id={`secondDotThirdAnimate_${id}`}
					begin="spinner_UHR2.end"
					attributeName="r"
					calcMode="spline"
					dur={dur}
					keySplines=".36,.6,.31,1"
					values="3;0"
					fill="freeze"
				/>
				<animate
					id="spinner_Aguh"
					begin={`secondDotThirdAnimate_${id}.end`}
					attributeName="cx"
					dur="0.001s"
					values="20;4"
					fill="freeze"
				/>
				<animate
					begin="spinner_Aguh.end"
					attributeName="r"
					calcMode="spline"
					dur={dur}
					keySplines=".36,.6,.31,1"
					values="0;3"
					fill="freeze"
				/>
			</circle>
			<circle
				cx="12"
				cy="12"
				r="3"
				className={className}
				stroke={color ?? stroke}
				fill={color ?? fill}
			>
				<animate
					begin="0;spinner_z0Or.end"
					attributeName="cx"
					calcMode="spline"
					dur={dur}
					keySplines=".36,.6,.31,1"
					values="12;20"
					fill="freeze"
				/>
				<animate
					id={`thirdDotSecondAnimate_${id}`}
					begin="spinner_OLMs.end"
					attributeName="r"
					calcMode="spline"
					dur={dur}
					keySplines=".36,.6,.31,1"
					values="3;0"
					fill="freeze"
				/>
				<animate
					id="spinner_UHR2"
					begin={`thirdDotSecondAnimate_${id}.end`}
					attributeName="cx"
					dur="0.001s"
					values="20;4"
					fill="freeze"
				/>
				<animate
					begin="spinner_UHR2.end"
					attributeName="r"
					calcMode="spline"
					dur={dur}
					keySplines=".36,.6,.31,1"
					values="0;3"
					fill="freeze"
				/>
				<animate
					begin="spinner_Aguh.end"
					attributeName="cx"
					calcMode="spline"
					dur={dur}
					keySplines=".36,.6,.31,1"
					values="4;12"
					fill="freeze"
				/>
			</circle>
			<circle
				cx="20"
				cy="12"
				r="3"
				className={className}
				stroke={color ?? stroke}
				fill={color ?? fill}
			>
				<animate
					id={`fourthDotFirstAnimate_${id}`}
					begin="0;spinner_z0Or.end"
					attributeName="r"
					calcMode="spline"
					dur={dur}
					keySplines=".36,.6,.31,1"
					values="3;0"
					fill="freeze"
				/>
				<animate
					id="spinner_OLMs"
					begin={`fourthDotFirstAnimate_${id}.end`}
					attributeName="cx"
					dur="0.001s"
					values="20;4"
					fill="freeze"
				/>
				<animate
					begin="spinner_OLMs.end"
					attributeName="r"
					calcMode="spline"
					dur={dur}
					keySplines=".36,.6,.31,1"
					values="0;3"
					fill="freeze"
				/>
				<animate
					begin="spinner_UHR2.end"
					attributeName="cx"
					calcMode="spline"
					dur={dur}
					keySplines=".36,.6,.31,1"
					values="4;12"
					fill="freeze"
				/>
				<animate
					begin="spinner_Aguh.end"
					attributeName="cx"
					calcMode="spline"
					dur={dur}
					keySplines=".36,.6,.31,1"
					values="12;20"
					fill="freeze"
				/>
			</circle>
		</svg>
	);
}
