import { Link } from '@inertiajs/react';
import type { ComponentProps } from 'react';

type SiteLinkProps = Omit<ComponentProps<typeof Link>, 'href'> & {
    href: string;
};

/**
 * Internal link of the public site: a real <a href> (crawlable) that
 * navigates through Inertia and prefetches the target on hover, so pages
 * open almost instantly.
 *
 * Hover prefetching makes Inertia set the link's own onMouseEnter and
 * onMouseLeave, silently dropping the caller's (the mega menu's areas did
 * not switch the panel on hover, the face map's points did not light up).
 * The caller's two handlers are therefore attached as their pointer
 * counterparts, which Inertia leaves alone; they fire just before the
 * mouse events, and for pen and touch as well.
 *
 * Use a plain <a> for anything outside the public site (admin panel,
 * language switch, external URLs, files).
 */
export function SiteLink({
    href,
    prefetch = 'hover',
    onMouseEnter,
    onMouseLeave,
    ...props
}: SiteLinkProps) {
    return (
        <Link
            href={href}
            prefetch={prefetch}
            onPointerEnter={onMouseEnter}
            onPointerLeave={onMouseLeave}
            {...props}
        />
    );
}
