/* global React */
/* EgoYüzme · App — hash router */

const { useState, useEffect } = React;

const ROUTES = {
  "#/": "HomePage",
  "": "HomePage",
  "#": "HomePage",
  "#/hakkimizda": "AboutPage",
  "#/programlar": "ProgramsPage",
  "#/neden-biz": "WhyUsPage",
  "#/lisansli-sporcu": "LicensedPage",
  "#/subeler": "BranchesPage",
  "#/sss": "FaqPage",
  "#/iletisim": "ContactPage",
};

function App() {
  const [route, setRoute] = useState(window.location.hash || "#/");
  const [toast, setToast] = useState(null);

  useEffect(() => {
    const handler = () => {
      const newRoute = window.location.hash || "#/";
      setRoute(newRoute);
      // Scroll to top on route change
      window.scrollTo({ top: 0, behavior: "instant" });
    };
    window.addEventListener("hashchange", handler);
    return () => window.removeEventListener("hashchange", handler);
  }, []);

  // Init Lucide icons whenever the DOM updates (mostly for header phone icons etc)
  useEffect(() => {
    if (window.lucide) {
      try { window.lucide.createIcons(); } catch (e) {}
    }
  });

  const flash = (msg) => { setToast(msg); setTimeout(() => setToast(null), 2400); };

  const PageComponent = (() => {
    const key = ROUTES[route] ? route : "#/";
    const name = ROUTES[key];
    return window[name] || window.HomePage;
  })();

  return (
    <>
      <Header route={route} />
      <PageComponent />
      <Footer />
      <WhatsAppFAB onClick={() => { flash("WhatsApp açılıyor…"); setTimeout(() => window.open("https://wa.me/905551234567", "_blank"), 300); }} />
      {toast ? <div className="toast">{toast}</div> : null}
    </>
  );
}

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