import { SiteLink } from '@/site/components/ui/link';
import { useShared } from '@/site/hooks/use-shared';
import { cx } from '@/site/lib/cx';

/**
 * Brand mark linking to the home page: the logo file (public/assets/brand/,
 * or the one uploaded in Admin → Site settings) in the version drawn for
 * the background it sits on. Without any file the name is set as a
 * wordmark: first word in the display face, the rest in small spaced capitals.
 */
export function Logo({
    className,
    onDark = true,
    priority = false,
}: {
    className?: string;
    onDark?: boolean;
    /** The header's logo: often the largest element of the first paint (LCP). */
    priority?: boolean;
}) {
    const { site, nav } = useShared();
    const image = onDark
        ? (site.logoDark ?? site.logo)
        : (site.logo ?? site.logoDark);
    const [first, ...rest] = site.name.split(' ');

    return (
        <SiteLink
            href={nav.pages.home.url}
            aria-label={`${site.name} — ${nav.pages.home.label}`}
            className={cx('inline-flex items-baseline gap-2.5', className)}
        >
            {image ? (
                // The pixel size reserves the box before the file arrives (no layout shift).
                <img
                    src={image.src}
                    alt={site.name}
                    width={image.width ?? undefined}
                    height={image.height ?? 40}
                    fetchPriority={priority ? 'high' : undefined}
                    className="h-10 w-auto self-center"
                />
            ) : (
                <>
                    <span className="font-display text-[1.75rem] leading-none tracking-tight">
                        {first}
                    </span>
                    {/*
                      A real space between the two words: without it the
                      visible text reads "CLVCLINICS" to assistive technology
                      and no longer matches the link's label. (Flex layout
                      ignores it, the gap stays as designed.)
                    */}{' '}
                    {rest.length > 0 ? (
                        // Upper-cased here, not with CSS: on Turkish pages CSS
                        // would produce the dotted "İ" (CLİNİCS).
                        <span className="text-soft text-[0.625rem] font-semibold tracking-[0.34em]">
                            {rest.join(' ').toUpperCase()}
                        </span>
                    ) : null}
                </>
            )}
        </SiteLink>
    );
}
