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


export default function BouncingDots({
	size,
	width,
	height,
	className,
	animationDuration = 600,
	color,
	stroke,
	fill,
	ariaLabel = "Loading"
}: Readonly<LoadingDotsProps>){
	//https://github.com/n3r4zzurr0/svg-spinners/blob/main/svg-smil/3-dots-bounce.svg?short_path=50864c0
	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="3"
				className={className}
				stroke={color ?? stroke}
				fill={color ?? fill}
			>
				<animate
					id={`firstBouncingDots_${id}`}
					begin={`0;lastBouncingDots_${id}.end+${dur / 2}s`}
					attributeName="cy"
					calcMode="spline"
					dur={dur}
					values="12;6;12"
					keySplines=".33,.66,.66,1;.33,0,.66,.33"
				/>
			</circle>
			<circle
				cx="12"
				cy="12"
				r="3"
				className={className}
				stroke={color ?? stroke}
				fill={color ?? fill}
			>
				<animate
					id={`secondBouncingDots_${id}`}
					begin={`firstBouncingDots_${id}.begin+${dur / 5}s`}
					attributeName="cy"
					calcMode="spline"
					dur={dur}
					values="12;6;12"
					keySplines=".33,.66,.66,1;.33,0,.66,.33"
				/>
			</circle>
			<circle
				cx="20"
				cy="12"
				r="3"
				className={className}
				stroke={color ?? stroke}
				fill={color ?? fill}
			>
				<animate
					id={`lastBouncingDots_${id}`}
					begin={`secondBouncingDots_${id}.begin+${dur / 5}s`}
					attributeName="cy"
					calcMode="spline"
					dur={dur}
					values="12;6;12"
					keySplines=".33,.66,.66,1;.33,0,.66,.33"
				/>
			</circle>
		</svg>
	);
}
