import { Marquee } from '@/site/components/motion/marquee';
import { useShared } from '@/site/hooks/use-shared';
import { cx } from '@/site/lib/cx';
import type { Certificate } from '@/site/types';

/** Height of every logo, in rem: logos are scaled to it, not to their width. */
const HEIGHT = 2.75;

function Logo({ logo, mono }: { logo: Certificate; mono: boolean }) {
    const src = mono && logo.srcMono ? logo.srcMono : logo.src;

    return (
        <img
            src={src}
            alt={logo.name}
            title={logo.name}
            width={logo.width}
            height={logo.height}
            loading="lazy"
            decoding="async"
            className="block w-auto max-w-none shrink-0"
            style={{ height: `${HEIGHT}rem` }}
        />
    );
}

type CertificatesProps = {
    /** Light single-colour logos for dark backgrounds, colour logos otherwise. */
    tone: 'dark' | 'light';
    /** `ticker`: an endless moving row; `grid`: all logos laid out. */
    layout?: 'ticker' | 'grid';
    label?: string;
    className?: string;
};

/**
 * The clinic's memberships and certificates as a row of logos
 * (config/site.php → certificates). Nothing is rendered while none of the
 * logo files exists.
 */
export function Certificates({
    tone,
    layout = 'ticker',
    label,
    className,
}: CertificatesProps) {
    const { site } = useShared();
    const logos = site.certificates;

    if (logos.length === 0) {
        return null;
    }

    const mono = tone === 'dark';

    return (
        <div
            className={cx(
                'flex flex-col gap-5 md:flex-row md:items-center md:gap-10',
                className,
            )}
        >
            {label ? (
                <p className="text-eyebrow text-soft shrink-0 font-semibold uppercase md:max-w-[11rem]">
                    {label}
                </p>
            ) : null}

            {layout === 'ticker' ? (
                <Marquee
                    duration={logos.length * 4}
                    className="min-w-0 grow [mask-image:linear-gradient(to_right,transparent,#000_8%,#000_92%,transparent)]"
                >
                    {logos.map((logo) => (
                        <span
                            key={logo.key}
                            className={cx(
                                'me-12 flex items-center md:me-16',
                                mono && 'opacity-75',
                            )}
                        >
                            <Logo logo={logo} mono={mono} />
                        </span>
                    ))}
                </Marquee>
            ) : (
                <ul className="flex flex-wrap items-center gap-x-10 gap-y-8">
                    {logos.map((logo) => (
                        <li key={logo.key}>
                            <Logo logo={logo} mono={mono} />
                        </li>
                    ))}
                </ul>
            )}
        </div>
    );
}
