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


export default function FadingDots({
	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-fade.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"
				opacity="1"
				className={className}
				stroke={color ?? stroke}
				fill={color ?? fill}
			>
				<animate
					id={`firstFadingDots_${id}`}
					begin={`0;lastFadingDots_${id}.end-${dur / 3}s`}
					attributeName="opacity"
					dur={dur}
					values="1;.2"
					fill="freeze"
				/>
			</circle>
			<circle
				cx="12"
				cy="12"
				r="3"
				opacity=".4"
				className={className}
				stroke={color ?? stroke}
				fill={color ?? fill}
			>
				<animate
					id={`secondFadingDots_${id}`}
					begin={`firstFadingDots_${id}.begin+${dur / 5}s`}
					attributeName="opacity"
					dur={dur}
					values="1;.2"
					fill="freeze"
				/>
			</circle>
			<circle
				cx="20"
				cy="12"
				r="3"
				opacity=".3"
				className={className}
				stroke={color ?? stroke}
				fill={color ?? fill}
			>
				<animate
					id={`lastFadingDots_${id}`}
					begin={`secondFadingDots_${id}.begin+${dur / 5}s`}
					attributeName="opacity"
					dur={dur}
					values="1;.2"
					fill="freeze"
				/>
			</circle>
		</svg>
	);
}
