/* Tiny path router (History API) - clean URLs, no library.
   Routes are real paths: "/", "/learn/module-1", "/checkout", "/success",
   "/signup", "/admin", "/privacy|terms|refund". In-page scroll anchors on the
   landing page (#roadmap, #top) still work natively. Deep links work because
   Vercel rewrites any unknown path to index.html (see vercel.json). */
const FIRST_MODULE = "module-1";

function parsePath() {
  const parts = (window.location.pathname || "/").split("/").filter(Boolean); // "/learn/module-1" -> ["learn","module-1"]
  const first = parts[0] || "";
  // "pricing" is an alias for the offer page - the price/plans live on checkout.
  if (first === "checkout" || first === "pricing") return { name: "checkout" };
  if (first === "success") return { name: "success" };
  if (first === "signup" || first === "login" || first === "account") return { name: "signup" };
  if (first === "admin") return { name: "admin" };
  if (first === "privacy" || first === "terms" || first === "refund") return { name: "legal", page: first };
  if (first !== "learn") return { name: "landing" };
  return { name: "lesson", moduleId: parts[1] || FIRST_MODULE };
}

// Programmatic navigation: pushState + notify the router (no full page reload).
function navigate(to, opts) {
  opts = opts || {};
  if (opts.replace) window.history.replaceState({}, "", to);
  else window.history.pushState({}, "", to);
  window.dispatchEvent(new Event("io-navigate"));
  if (!opts.noScroll) window.scrollTo(0, 0);
}

// Intercept clicks on internal path links (<a href="/...">) so they navigate via
// the History API instead of reloading the page. Fragment links (#roadmap),
// external/mailto links, and modified/new-tab clicks pass through untouched.
if (typeof window !== "undefined" && !window.__ioRouterClick) {
  window.__ioRouterClick = true;
  document.addEventListener("click", function (e) {
    if (e.defaultPrevented || e.button !== 0 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) return;
    const a = e.target.closest && e.target.closest("a");
    if (!a) return;
    const href = a.getAttribute("href");
    if (!href || href[0] !== "/" || href[1] === "/") return; // only internal absolute paths
    if (a.target && a.target !== "_self") return;
    e.preventDefault();
    if (href !== window.location.pathname + window.location.search) navigate(href);
  });
}

function useHashRoute() { // name kept for callers; now path-based
  const [route, setRoute] = useState(parsePath);
  useEffect(() => {
    // Normalize bare "/learn" -> "/learn/module-1".
    if (window.location.pathname === "/learn" || window.location.pathname === "/learn/") {
      navigate("/learn/" + FIRST_MODULE, { replace: true, noScroll: true });
    }
    const onChange = () => setRoute(parsePath());
    window.addEventListener("popstate", onChange);
    window.addEventListener("io-navigate", onChange);
    return () => {
      window.removeEventListener("popstate", onChange);
      window.removeEventListener("io-navigate", onChange);
    };
  }, []);
  return route;
}

Object.assign(window, { parsePath, useHashRoute, navigate });
