import { useT } from '@/site/i18n';
import { cx } from '@/site/lib/cx';
import type { Crumb } from '@/site/types';
import { ChevronRight } from './icons';
import { SiteLink } from './link';

/**
 * Visible breadcrumb trail. It shows the same path as the BreadcrumbList
 * structured data built on the server (App\Seo\SchemaGraph).
 */
export function Breadcrumbs({
    items,
    className,
}: {
    items: Crumb[];
    className?: string;
}) {
    const t = useT();

    if (items.length < 2) {
        return null;
    }

    return (
        <nav aria-label={t.common.breadcrumb} className={className}>
            <ol className="text-soft flex flex-wrap items-center gap-x-2 gap-y-1 text-sm">
                {items.map((item, index) => {
                    const last = index === items.length - 1;

                    return (
                        <li key={item.url} className="flex items-center gap-2">
                            {last ? (
                                <span
                                    aria-current="page"
                                    className={cx('text-fg')}
                                >
                                    {item.name}
                                </span>
                            ) : (
                                <>
                                    <SiteLink
                                        href={item.url}
                                        className="link-underline hover:text-fg transition-colors"
                                    >
                                        {item.name}
                                    </SiteLink>
                                    <ChevronRight className="size-3.5 opacity-50 rtl:-scale-x-100" />
                                </>
                            )}
                        </li>
                    );
                })}
            </ol>
        </nav>
    );
}
