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


export default function WaveBlocks({
	size,
	width,
	height,
	className,
	animationDuration = 600,
	color,
	stroke,
	fill,
	ariaLabel = "Loading"
}: Readonly<LoadingBlocksProps>){
	//https://github.com/n3r4zzurr0/svg-spinners/blob/main/svg-smil/blocks-wave.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"
		>
			<rect
				x="1"
				y="1"
				width="7.33"
				height="7.33"
				className={className}
				stroke={color ?? stroke}
				fill={color ?? fill}
			>
				<animate
					id={`rectangle1_1_${id}`}
					begin={`0;rectangle9_1_${id}.end+${dur / 3}s`}
					attributeName="x"
					dur={dur}
					values="1;4;1"
				/>
				<animate
					begin={`0;rectangle9_1_${id}.end+${dur / 3}s`}
					attributeName="y"
					dur={dur}
					values="1;4;1"
				/>
				<animate
					begin={`0;rectangle9_1_${id}.end+${dur / 3}s`}
					attributeName="width"
					dur={dur}
					values="7.33;1.33;7.33"
				/>
				<animate
					begin={`0;rectangle9_1_${id}.end+${dur / 3}s`}
					attributeName="height"
					dur={dur}
					values="7.33;1.33;7.33"
				/>
			</rect>
			<rect
				x="8.33"
				y="1"
				width="7.33"
				height="7.33"
				className={className}
				stroke={color ?? stroke}
				fill={color ?? fill}
			>
				<animate
					begin={`rectangle1_1_${id}.begin+${dur / 6}s`}
					attributeName="x"
					dur={dur}
					values="8.33;11.33;8.33"
				/>
				<animate
					begin={`rectangle1_1_${id}.begin+${dur / 6}s`}
					attributeName="y"
					dur={dur}
					values="1;4;1"
				/>
				<animate
					begin={`rectangle1_1_${id}.begin+${dur / 6}s`}
					attributeName="width"
					dur={dur}
					values="7.33;1.33;7.33"
				/>
				<animate
					begin={`rectangle1_1_${id}.begin+${dur / 6}s`}
					attributeName="height"
					dur={dur}
					values="7.33;1.33;7.33"
				/>
			</rect>
			<rect
				x="1"
				y="8.33"
				width="7.33"
				height="7.33"
				className={className}
				stroke={color ?? stroke}
				fill={color ?? fill}
			>
				<animate
					begin={`rectangle1_1_${id}.begin+${dur / 6}s`}
					attributeName="x"
					dur={dur}
					values="1;4;1"
				/>
				<animate
					begin={`rectangle1_1_${id}.begin+${dur / 6}s`}
					attributeName="y"
					dur={dur}
					values="8.33;11.33;8.33"
				/>
				<animate
					begin={`rectangle1_1_${id}.begin+${dur / 6}s`}
					attributeName="width"
					dur={dur}
					values="7.33;1.33;7.33"
				/>
				<animate
					begin={`rectangle1_1_${id}.begin+${dur / 6}s`}
					attributeName="height"
					dur={dur}
					values="7.33;1.33;7.33"
				/>
			</rect>
			<rect
				x="15.66"
				y="1"
				width="7.33"
				height="7.33"
				className={className}
				stroke={color ?? stroke}
				fill={color ?? fill}
			>
				<animate
					begin={`rectangle1_1_${id}.begin+${dur / 3}s`}
					attributeName="x"
					dur={dur}
					values="15.66;18.66;15.66"
				/>
				<animate
					begin={`rectangle1_1_${id}.begin+${dur / 3}s`}
					attributeName="y"
					dur={dur}
					values="1;4;1"
				/>
				<animate
					begin={`rectangle1_1_${id}.begin+${dur / 3}s`}
					attributeName="width"
					dur={dur}
					values="7.33;1.33;7.33"
				/>
				<animate
					begin={`rectangle1_1_${id}.begin+${dur / 3}s`}
					attributeName="height"
					dur={dur}
					values="7.33;1.33;7.33"
				/>
			</rect>
			<rect
				x="8.33"
				y="8.33"
				width="7.33"
				height="7.33"
				className={className}
				stroke={color ?? stroke}
				fill={color ?? fill}
			>
				<animate
					begin={`rectangle1_1_${id}.begin+${dur / 3}s`}
					attributeName="x"
					dur={dur}
					values="8.33;11.33;8.33"
				/>
				<animate
					begin={`rectangle1_1_${id}.begin+${dur / 3}s`}
					attributeName="y"
					dur={dur}
					values="8.33;11.33;8.33"
				/>
				<animate
					begin={`rectangle1_1_${id}.begin+${dur / 3}s`}
					attributeName="width"
					dur={dur}
					values="7.33;1.33;7.33"
				/>
				<animate
					begin={`rectangle1_1_${id}.begin+${dur / 3}s`}
					attributeName="height"
					dur={dur}
					values="7.33;1.33;7.33"
				/>
			</rect>
			<rect
				x="1"
				y="15.66"
				width="7.33"
				height="7.33"
				className={className}
				stroke={color ?? stroke}
				fill={color ?? fill}
			>
				<animate
					begin={`rectangle1_1_${id}.begin+${dur / 3}s`}
					attributeName="x"
					dur={dur}
					values="1;4;1"
				/>
				<animate
					begin={`rectangle1_1_${id}.begin+${dur / 3}s`}
					attributeName="y"
					dur={dur}
					values="15.66;18.66;15.66"
				/>
				<animate
					begin={`rectangle1_1_${id}.begin+${dur / 3}s`}
					attributeName="width"
					dur={dur}
					values="7.33;1.33;7.33"
				/>
				<animate
					begin={`rectangle1_1_${id}.begin+${dur / 3}s`}
					attributeName="height"
					dur={dur}
					values="7.33;1.33;7.33"
				/>
			</rect>
			<rect
				x="15.66"
				y="8.33"
				width="7.33"
				height="7.33"
				className={className}
				stroke={color ?? stroke}
				fill={color ?? fill}
			>
				<animate
					begin={`rectangle1_1_${id}.begin+${dur / 2}s`}
					attributeName="x"
					dur={dur}
					values="15.66;18.66;15.66"
				/>
				<animate
					begin={`rectangle1_1_${id}.begin+${dur / 2}s`}
					attributeName="y"
					dur={dur}
					values="8.33;11.33;8.33"
				/>
				<animate
					begin={`rectangle1_1_${id}.begin+${dur / 2}s`}
					attributeName="width"
					dur={dur}
					values="7.33;1.33;7.33"
				/>
				<animate
					begin={`rectangle1_1_${id}.begin+${dur / 2}s`}
					attributeName="height"
					dur={dur}
					values="7.33;1.33;7.33"
				/>
			</rect>
			<rect
				x="8.33"
				y="15.66"
				width="7.33"
				height="7.33"
				className={className}
				stroke={color ?? stroke}
				fill={color ?? fill}
			>
				<animate
					begin={`rectangle1_1_${id}.begin+${dur / 2}s`}
					attributeName="x"
					dur={dur}
					values="8.33;11.33;8.33"
				/>
				<animate
					begin={`rectangle1_1_${id}.begin+${dur / 2}s`}
					attributeName="y"
					dur={dur}
					values="15.66;18.66;15.66"
				/>
				<animate
					begin={`rectangle1_1_${id}.begin+${dur / 2}s`}
					attributeName="width"
					dur={dur}
					values="7.33;1.33;7.33"
				/>
				<animate
					begin={`rectangle1_1_${id}.begin+${dur / 2}s`}
					attributeName="height"
					dur={dur}
					values="7.33;1.33;7.33"
				/>
			</rect>
			<rect
				x="15.66"
				y="15.66"
				width="7.33"
				height="7.33"
				className={className}
				stroke={color ?? stroke}
				fill={color ?? fill}
			>
				<animate
					id={`rectangle9_1_${id}`}
					begin={`rectangle1_1_${id}.begin+${dur * 2 / 3}s`}
					attributeName="x"
					dur={dur}
					values="15.66;18.66;15.66"
				/>
				<animate
					begin={`rectangle1_1_${id}.begin+${dur * 2 / 3}s`}
					attributeName="y"
					dur={dur}
					values="15.66;18.66;15.66"
				/>
				<animate
					begin={`rectangle1_1_${id}.begin+${dur * 2 / 3}s`}
					attributeName="width"
					dur={dur}
					values="7.33;1.33;7.33"
				/>
				<animate
					begin={`rectangle1_1_${id}.begin+${dur * 2 / 3}s`}
					attributeName="height"
					dur={dur}
					values="7.33;1.33;7.33"
				/>
			</rect>
		</svg>
	);
}
