// router.jsx — tiny hash router for saraslot
// Loaded after tweaks-panel.jsx, before components.jsx.

const ROUTES = {
  HOME: "/",
  SHOP: "/new",
  HISTORY: "/history",
  PROMO: "/promo",
  CHAT: "/chat",
  SETTINGS: "/settings",
};

const KNOWN_ROUTES = Object.values(ROUTES);

function parseRoute(hash) {
  if (!hash) return ROUTES.HOME;
  const path = hash.replace(/^#/, "").trim() || "/";
  return KNOWN_ROUTES.includes(path) ? path : ROUTES.HOME;
}

function useHashRoute() {
  const [route, setRoute] = React.useState(() => parseRoute(window.location.hash));

  React.useEffect(() => {
    const onChange = () => setRoute(parseRoute(window.location.hash));
    window.addEventListener("hashchange", onChange);
    return () => window.removeEventListener("hashchange", onChange);
  }, []);

  const navigate = React.useCallback((path) => {
    const next = KNOWN_ROUTES.includes(path) ? path : ROUTES.HOME;
    if (("#" + next) !== window.location.hash) {
      window.location.hash = next;
    } else {
      setRoute(next);
    }
  }, []);

  return [route, navigate];
}

window.ROUTES = ROUTES;
window.parseRoute = parseRoute;
window.useHashRoute = useHashRoute;
