/* Common Works — app router + mount */
const { useState: useStateA, useEffect: useEffectA } = React;

const ROUTES = { home: "home", what: "what", careers: "careers", terms: "terms", privacy: "privacy" };

function parseHash() {
  const h = (window.location.hash || "").replace(/^#\/?/, "").split("/")[0];
  return ROUTES[h] || "home";
}

function App() {
  const [route, setRoute] = useStateA(parseHash());

  const go = (r) => {
    window.location.hash = "#/" + r;
  };
  window.__go = go;

  useEffectA(() => {
    const onHash = () => {
      setRoute(parseHash());
      window.scrollTo({ top: 0, behavior: "auto" });
    };
    window.addEventListener("hashchange", onHash);
    return () => window.removeEventListener("hashchange", onHash);
  }, []);

  let Page;
  if (route === "what") Page = <WhatWeDo />;
  else if (route === "careers") Page = <Careers />;
  else if (route === "terms") Page = <Legal doc={window.CW.legal.terms} />;
  else if (route === "privacy") Page = <Legal doc={window.CW.legal.privacy} />;
  else Page = <Home />;

  return (
    <React.Fragment>
      <TopNav route={route} go={go} />
      {Page}
      <Footer go={go} />
    </React.Fragment>
  );
}

ReactDOM.createRoot(document.getElementById("root")).render(<App />);
