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


export default function SwellingDots({
	size,
	width,
	height,
	className,
	animationDuration = 750,
	color,
	stroke,
	fill,
	ariaLabel = "Loading"
}: Readonly<LoadingDotsProps>){
	//https://github.com/n3r4zzurr0/svg-spinners/blob/main/svg-smil/3-dots-scale.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="3"
				className={className}
				stroke={color ?? stroke}
				fill={color ?? fill}
			>
				<animate
					id={`firstDot_${id}`}
					begin={`0;lastDot_${id}.end-${dur / 3}s`}
					attributeName="r"
					dur={dur}
					values="3;.2;3"
				/>
			</circle>
			<circle
				cx="12"
				cy="12"
				r="3"
				className={className}
				stroke={color ?? stroke}
				fill={color ?? fill}
			>
				<animate
					begin={`firstDot_${id}.end-${dur * 4 / 5}s`}
					attributeName="r"
					dur={dur}
					values="3;.2;3"
				/>
			</circle>
			<circle
				cx="20"
				cy="12"
				r="3"
				className={className}
				stroke={color ?? stroke}
				fill={color ?? fill}
			>
				<animate
					id={`lastDot_${id}`}
					begin={`firstDot_${id}.end-${dur * 2 / 3}s`}
					attributeName="r"
					dur={dur}
					values="3;.2;3"
				/>
			</circle>
		</svg>
	);
}
