// Tiny hash router. Routes:
//   #/           → home
//   #/c/<slug>   → category
//   #/p/<sku>    → product
//   #/contact    → contact
// Anything else falls back to home.

(function () {
  function parse() {
    const h = window.location.hash || '#/';
    const m = h.match(/^#\/(contact|c|p)?\/?(.*)$/);
    if (!m) return { page: 'home', param: null };
    const [, kind, rest] = m;
    if (!kind) return { page: 'home', param: null };
    if (kind === 'c') return { page: 'category', param: rest || null };
    if (kind === 'p') return { page: 'product', param: rest || null };
    if (kind === 'contact') return { page: 'contact', param: null };
    return { page: 'home', param: null };
  }

  window.useRoute = function useRoute() {
    const [route, setRoute] = React.useState(parse);
    React.useEffect(() => {
      const onHash = () => { setRoute(parse()); window.scrollTo({ top: 0, behavior: 'instant' }); };
      window.addEventListener('hashchange', onHash);
      return () => window.removeEventListener('hashchange', onHash);
    }, []);
    return route;
  };
})();
