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


export default function SpinningBinary({
	size,
	width,
	height,
	className,
	animationDuration = 2000,
	color,
	stroke,
	fill,
	ariaLabel = "Loading"
}: Readonly<LoadingVariousProps>){
	//https://github.com/n3r4zzurr0/svg-spinners/blob/main/svg-smil/gooey-balls-2.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"
		>
			<defs>
				<filter
					id={`spinningBinaryFilter_${id}`}
				>
					<feGaussianBlur
						in="SourceGraphic"
						stdDeviation="1"
						result="y"
					/>
					<feColorMatrix
						in="y"
						mode="matrix"
						values="1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 18 -7"
						result="z"
					/>
					<feBlend
						in="SourceGraphic"
						in2="z"
					/>
				</filter>
			</defs>
			<g
				filter={`url(#spinningBinaryFilter_${id})`}
			>
				<circle
					cx="5"
					cy="12"
					r="4"
					className={className}
					stroke={color ?? stroke}
					fill={color ?? fill}
				>
					<animate
						attributeName="cx"
						calcMode="spline"
						dur={dur}
						values="5;8;5"
						keySplines=".36,.62,.43,.99;.79,0,.58,.57"
						repeatCount="indefinite"
					/>
				</circle>
				<circle
					cx="19"
					cy="12"
					r="4"
					className={className}
					stroke={color ?? stroke}
					fill={color ?? fill}
				>
					<animate
						attributeName="cx"
						calcMode="spline"
						dur={dur}
						values="19;16;19"
						keySplines=".36,.62,.43,.99;.79,0,.58,.57"
						repeatCount="indefinite"
					/>
				</circle>
				<animateTransform
					attributeName="transform"
					type="rotate"
					dur={dur * 3 / 8}
					values="0 12 12;360 12 12"
					repeatCount="indefinite"
				/>
			</g>
		</svg>
	);
}
