// Router.jsx — Simple hash-based router for 503.health multi-page site
const useRoute = () => {
  const [path, setPath] = React.useState(window.location.pathname);

  React.useEffect(() => {
    const onPop = () => setPath(window.location.pathname);
    window.addEventListener('popstate', onPop);
    return () => window.removeEventListener('popstate', onPop);
  }, []);

  const navigate = (to) => {
    window.history.pushState({}, '', to);
    setPath(to);
    window.scrollTo(0, 0);
  };

  return [path, navigate];
};

// Intercept all internal link clicks
const useInterceptLinks = (navigate) => {
  React.useEffect(() => {
    const handler = (e) => {
      const link = e.target.closest('a');
      if (!link) return;
      const href = link.getAttribute('href');
      if (!href) return;
      // Only intercept internal links
      if (href.startsWith('/') && !href.startsWith('//')) {
        e.preventDefault();
        navigate(href);
      }
    };
    document.addEventListener('click', handler);
    return () => document.removeEventListener('click', handler);
  }, [navigate]);
};

window.useRoute = useRoute;
window.useInterceptLinks = useInterceptLinks;
