// Hash-based router. Pages: home, conditions, specialty/:id, doctors, doctor/:id, locations
function useRoute() {
  const [hash, setHash] = React.useState(typeof window !== 'undefined' ? window.location.hash : '');
  React.useEffect(() => {
    const h = () => { setHash(window.location.hash); window.scrollTo({ top: 0, behavior: 'instant' }); };
    window.addEventListener('hashchange', h);
    return () => window.removeEventListener('hashchange', h);
  }, []);
  // Parse: #/page or #/page/arg
  const clean = (hash || '').replace(/^#\/?/, '').replace(/^#/, '');
  const [page = '', arg = ''] = clean.split('/');
  return { page: page || 'home', arg, raw: hash };
}

function navigate(path) {
  // path like 'home', 'specialty/psychiatry'
  window.location.hash = '#/' + path;
}

function Link({ to, children, style, className, onClick }) {
  const handle = (e) => {
    e.preventDefault();
    if (onClick) onClick(e);
    navigate(to);
  };
  return (
    <a href={'#/' + to} onClick={handle} style={style} className={className}>{children}</a>
  );
}

Object.assign(window, { useRoute, navigate, Link });
