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


export default function CirclePulsingDots({
	size,
	width,
	height,
	className,
	rotationAnimationDuration = 6000,
	growingAnimationDuration = 600,
	color,
	stroke,
	fill,
	ariaLabel = "Loading"
}: Readonly<CirclePulsingDotsProps>){
	//https://github.com/n3r4zzurr0/svg-spinners/blob/main/svg-smil/12-dots-scale-rotate.svg
	const id = useId();
	const reducedMotion = usePrefersReducedMotion();
	const rotationAnimationDur = reducedMotion ? rotationAnimationDuration / 100 : rotationAnimationDuration / 1000;
	const growingAnimationDur = reducedMotion ? growingAnimationDuration / 100 : growingAnimationDuration / 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"
		>
			<g>
				<circle
					cx="12"
					cy="3"
					r="1"
					className={className}
					stroke={color ?? stroke}
					fill={color ?? fill}
				>
					<animate
						id={`circle1_${id}`}
						begin={`0;circle3_${id}.end-${growingAnimationDur * 5 / 6}s`}
						attributeName="r"
						calcMode="spline"
						dur={growingAnimationDur}
						values="1;2;1"
						keySplines=".27,.42,.37,.99;.53,0,.61,.73"
					/>
				</circle>
				<circle
					cx="16.50"
					cy="4.21"
					r="1"
					className={className}
					stroke={color ?? stroke}
					fill={color ?? fill}
				>
					<animate
						id={`circle2_${id}`}
						begin={`circle1_${id}.begin+${growingAnimationDur / 6}s`}
						attributeName="r"
						calcMode="spline"
						dur={growingAnimationDur}
						values="1;2;1"
						keySplines=".27,.42,.37,.99;.53,0,.61,.73"
					/>
				</circle>
				<circle
					cx="7.50"
					cy="4.21"
					r="1"
					className={className}
					stroke={color ?? stroke}
					fill={color ?? fill}
				>
					<animate
						id={`circle3_${id}`}
						begin={`circle5_${id}.begin+${growingAnimationDur / 6}s`}
						attributeName="r"
						calcMode="spline"
						dur={growingAnimationDur}
						values="1;2;1"
						keySplines=".27,.42,.37,.99;.53,0,.61,.73"
					/>
				</circle>
				<circle
					cx="19.79"
					cy="7.50"
					r="1"
					className={className}
					stroke={color ?? stroke}
					fill={color ?? fill}
				>
					<animate
						id={`circle4_${id}`}
						begin={`circle2_${id}.begin+${growingAnimationDur / 6}s`}
						attributeName="r"
						calcMode="spline"
						dur={growingAnimationDur}
						values="1;2;1"
						keySplines=".27,.42,.37,.99;.53,0,.61,.73"
					/>
				</circle>
				<circle
					cx="4.21"
					cy="7.50"
					r="1"
					className={className}
					stroke={color ?? stroke}
					fill={color ?? fill}
				>
					<animate
						id={`circle5_${id}`}
						begin={`circle7_${id}.begin+${growingAnimationDur / 6}s`}
						attributeName="r"
						calcMode="spline"
						dur={growingAnimationDur}
						values="1;2;1"
						keySplines=".27,.42,.37,.99;.53,0,.61,.73"
					/>
				</circle>
				<circle
					cx="21.00"
					cy="12.00"
					r="1"
					className={className}
					stroke={color ?? stroke}
					fill={color ?? fill}
				>
					<animate
						id={`circle6_${id}`}
						begin={`circle4_${id}.begin+${growingAnimationDur / 6}s`}
						attributeName="r"
						calcMode="spline"
						dur={growingAnimationDur}
						values="1;2;1"
						keySplines=".27,.42,.37,.99;.53,0,.61,.73"
					/>
				</circle>
				<circle
					cx="3.00"
					cy="12.00"
					r="1"
					className={className}
					stroke={color ?? stroke}
					fill={color ?? fill}
				>
					<animate
						id={`circle7_${id}`}
						begin={`circle9_${id}.begin+${growingAnimationDur / 6}s`}
						attributeName="r"
						calcMode="spline"
						dur={growingAnimationDur}
						values="1;2;1"
						keySplines=".27,.42,.37,.99;.53,0,.61,.73"
					/>
				</circle>
				<circle
					cx="19.79"
					cy="16.50"
					r="1"
					className={className}
					stroke={color ?? stroke}
					fill={color ?? fill}
				>
					<animate
						id={`circle8_${id}`}
						begin={`circle6_${id}.begin+${growingAnimationDur / 6}s`}
						attributeName="r"
						calcMode="spline"
						dur={growingAnimationDur}
						values="1;2;1"
						keySplines=".27,.42,.37,.99;.53,0,.61,.73"
					/>
				</circle>
				<circle
					cx="4.21"
					cy="16.50"
					r="1"
					className={className}
					stroke={color ?? stroke}
					fill={color ?? fill}
				>
					<animate
						id={`circle9_${id}`}
						begin={`circle11_${id}.begin+${growingAnimationDur / 6}s`}
						attributeName="r"
						calcMode="spline"
						dur={growingAnimationDur}
						values="1;2;1"
						keySplines=".27,.42,.37,.99;.53,0,.61,.73"
					/>
				</circle>
				<circle
					cx="16.50"
					cy="19.79"
					r="1"
					className={className}
					stroke={color ?? stroke}
					fill={color ?? fill}
				>
					<animate
						id={`circle10_${id}`}
						begin={`circle8_${id}.begin+${growingAnimationDur / 6}s`}
						attributeName="r"
						calcMode="spline"
						dur={growingAnimationDur}
						values="1;2;1"
						keySplines=".27,.42,.37,.99;.53,0,.61,.73"
					/>
				</circle>
				<circle
					cx="7.50"
					cy="19.79"
					r="1"
					className={className}
					stroke={color ?? stroke}
					fill={color ?? fill}
				>
					<animate
						id={`circle11_${id}`}
						begin={`circle12_${id}.begin+${growingAnimationDur / 6}s`}
						attributeName="r"
						calcMode="spline"
						dur={growingAnimationDur}
						values="1;2;1"
						keySplines=".27,.42,.37,.99;.53,0,.61,.73"
					/>
				</circle>
				<circle
					cx="12"
					cy="21"
					r="1"
					className={className}
					stroke={color ?? stroke}
					fill={color ?? fill}
				>
					<animate
						id={`circle12_${id}`}
						begin={`circle10_${id}.begin+${growingAnimationDur / 6}s`}
						attributeName="r"
						calcMode="spline"
						dur={growingAnimationDur}
						values="1;2;1"
						keySplines=".27,.42,.37,.99;.53,0,.61,.73"
					/>
				</circle>
				<animateTransform
					attributeName="transform"
					type="rotate"
					dur={rotationAnimationDur}
					values="360 12 12;0 12 12"
					repeatCount="indefinite"
				/>
			</g>
		</svg>
	);
}
