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


export default function CenterGrowingBars({
	size,
	width,
	height,
	className,
	animationDuration = 600,
	color,
	stroke,
	fill,
	ariaLabel = "Loading"
}: Readonly<LoadingBarsProps>){
	//https://github.com/n3r4zzurr0/svg-spinners/blob/main/svg-smil/bars-scale-middle.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="6"
				width="2.8"
				height="12"
				className={className}
				stroke={color ?? stroke}
				fill={color ?? fill}
			>
				<animate
					begin={`rectangle3_${id}.begin+${dur * 2 / 3}s`}
					attributeName="y"
					calcMode="spline"
					dur={dur}
					values="6;1;6"
					keySplines=".14,.73,.34,1;.65,.26,.82,.45"
				/>
				<animate
					begin={`rectangle3_${id}.begin+${dur * 2 / 3}s`}
					attributeName="height"
					calcMode="spline"
					dur={dur}
					values="12;22;12"
					keySplines=".14,.73,.34,1;.65,.26,.82,.45"
				/>
			</rect>
			<rect
				x="5.8"
				y="6"
				width="2.8"
				height="12"
				className={className}
				stroke={color ?? stroke}
				fill={color ?? fill}
			>
				<animate
					begin={`rectangle3_${id}.begin+${dur / 3}s`}
					attributeName="y"
					calcMode="spline"
					dur={dur}
					values="6;1;6"
					keySplines=".14,.73,.34,1;.65,.26,.82,.45"
				/>
				<animate
					begin={`rectangle3_${id}.begin+${dur / 3}s`}
					attributeName="height"
					calcMode="spline"
					dur={dur}
					values="12;22;12"
					keySplines=".14,.73,.34,1;.65,.26,.82,.45"
				/>
			</rect>
			<rect
				x="10.6"
				y="6"
				width="2.8"
				height="12"
				className={className}
				stroke={color ?? stroke}
				fill={color ?? fill}
			>
				<animate
					id={`rectangle3_${id}`}
					begin={`0;rectangle5_${id}.end-${dur / 6}s`}
					attributeName="y"
					calcMode="spline"
					dur={dur}
					values="6;1;6"
					keySplines=".14,.73,.34,1;.65,.26,.82,.45"
				/>
				<animate
					begin={`0;rectangle5_${id}.end-${dur / 6}s`}
					attributeName="height"
					calcMode="spline"
					dur={dur}
					values="12;22;12"
					keySplines=".14,.73,.34,1;.65,.26,.82,.45"
				/>
			</rect>
			<rect
				x="15.4"
				y="6"
				width="2.8"
				height="12"
				className={className}
				stroke={color ?? stroke}
				fill={color ?? fill}
			>
				<animate
					begin={`rectangle3_${id}.begin+${dur / 3}s`}
					attributeName="y"
					calcMode="spline"
					dur={dur}
					values="6;1;6"
					keySplines=".14,.73,.34,1;.65,.26,.82,.45"
				/>
				<animate
					begin={`rectangle3_${id}.begin+${dur / 3}s`}
					attributeName="height"
					calcMode="spline"
					dur={dur}
					values="12;22;12"
					keySplines=".14,.73,.34,1;.65,.26,.82,.45"
				/>
			</rect>
			<rect
				x="20.2"
				y="6"
				width="2.8"
				height="12"
				className={className}
				stroke={color ?? stroke}
				fill={color ?? fill}
			>
				<animate
					id={`rectangle5_${id}`}
					begin={`rectangle3_${id}.begin+${dur * 2 / 3}s`}
					attributeName="y"
					calcMode="spline"
					dur={dur}
					values="6;1;6"
					keySplines=".14,.73,.34,1;.65,.26,.82,.45"
				/>
				<animate
					begin={`rectangle3_${id}.begin+${dur * 2 / 3}s`}
					attributeName="height"
					calcMode="spline"
					dur={dur}
					values="12;22;12"
					keySplines=".14,.73,.34,1;.65,.26,.82,.45"
				/>
			</rect>
		</svg>
	);
}
