import { useRef } from 'react';
import type { CSSProperties, ReactNode } from 'react';
import { useAmbient } from '@/site/hooks/use-in-view';
import { cx } from '@/site/lib/cx';

type MarqueeProps = {
    children: ReactNode;
    /** Seconds for one full loop. */
    duration?: number;
    className?: string;
};

/**
 * An endless horizontal ticker (pure CSS). The content is rendered twice;
 * the copy is hidden from assistive technology. Without motion it is a
 * static, clipped row.
 */
export function Marquee({ children, duration = 48, className }: MarqueeProps) {
    const ref = useRef<HTMLDivElement>(null);

    useAmbient(ref);

    return (
        <div
            ref={ref}
            data-ambient=""
            className={cx('overflow-hidden', className)}
            style={{ '--marquee-duration': `${duration}s` } as CSSProperties}
        >
            <div className="animate-marquee flex w-max">
                <div className="flex shrink-0 items-center">{children}</div>
                <div className="flex shrink-0 items-center" aria-hidden="true">
                    {children}
                </div>
            </div>
        </div>
    );
}
