import NextLink from 'next/link';
import { makeLinkComponent } from '@keystar/ui/link';
import { jsx } from 'react/jsx-runtime';

const UniversalNextLink = makeLinkComponent((_ref, ref) => {
  let {
    href,
    onClick,
    rel,
    ...props
  } = _ref;
  const shouldUseNext = href[0] === '/' && href[1] !== '/';
  return shouldUseNext ? /*#__PURE__*/jsx(NextLink, {
    href: href,
    ref: ref,
    ...props
  }) : /*#__PURE__*/jsx("a", {
    ref: ref,
    href: href,
    rel: rel || 'noreferrer noopener',
    onClick: event => {
      if (href === '' || href === '#') {
        event.preventDefault();
      }
      if (typeof onClick === 'function') {
        onClick(event);
      }
    },
    ...props
  });
});

export { UniversalNextLink as U };
