import Link from 'next/link'
import { type ComponentProps, type PropsWithChildren, forwardRef } from 'react'

export type Props = ComponentProps<typeof Link>

const HybridLink = forwardRef<HTMLAnchorElement, Props>(function HybridLink(
  { href, children, ...rest }: PropsWithChildren<Props>,
  ref,
) {
  if (href && (typeof href !== 'string' || href.startsWith('/')))
    return (
      <Link href={href} {...rest} ref={ref}>
        {children}
      </Link>
    )
  else
    return (
      <a href={href} {...rest} ref={ref}>
        {children}
      </a>
    )
})

export default HybridLink
