/* ===========================================================
   Repetir Boutique — app shell + hash router + mount
   Routes:  #/  → Boutique   #/commande → Checkout   #/commandes → Orders
   window.RepApp
   =========================================================== */
(function () {
  const { useState, useEffect, useRef } = React;
  const { StoreProvider, useStore } = window.RepetirStore;

  function useHashRoute() {
    const [hash, setHash] = useState(window.location.hash || "#/");
    useEffect(() => {
      const onHash = () => { setHash(window.location.hash || "#/"); window.scrollTo({ top: 0 }); };
      window.addEventListener("hashchange", onHash);
      return () => window.removeEventListener("hashchange", onHash);
    }, []);
    return hash;
  }

  function Shell() {
    const route = useHashRoute();
    const [filter, setFilter] = useState("Tout");
    const [cartOpen, setCartOpen] = useState(false);
    const shopRef = useRef(null);
    const rewardsRef = useRef(null);
    const NAV = ["Vêtements", "Figurines & 3D", "Déguisements", "Papeterie"];

    const isShop = route === "#/" || route === "" || route === "#";
    const isCheckout = route.indexOf("#/commande") === 0 && route !== "#/commandes";
    const isOrders = route === "#/commandes";

    const pickCat = (cat) => { setFilter(cat); setTimeout(() => shopRef.current && shopRef.current.scrollIntoView({ behavior: "smooth" }), 30); };

    return React.createElement(React.Fragment, null,
      React.createElement("div", { className: "construction-bar", role: "status" },
        React.createElement("span", { className: "construction-bar__icon" }, "🚧"),
        React.createElement("span", null, "Site de la boutique ", React.createElement("strong", null, "en construction"), " — fonctionnalités en cours de mise en place.")
      ),
      React.createElement(window.RepHeader, {
        variant: isShop ? "shop" : "back",
        activeCat: NAV.includes(filter) ? filter : null,
        onPickCat: pickCat,
        onOpenCart: () => setCartOpen(true),
      }),

      isShop && React.createElement(window.RepBoutique, { filter, setFilter, shopRef, rewardsRef }),
      isCheckout && React.createElement(window.RepCheckout, null),
      isOrders && React.createElement(window.RepOrders, null),

      isShop && React.createElement(window.RepFooter, null),
      isShop && React.createElement(window.RepCartDrawer, { open: cartOpen, onClose: () => setCartOpen(false) }),
      React.createElement(window.RepToast, null)
    );
  }

  function App() {
    return React.createElement(StoreProvider, null, React.createElement(Shell, null));
  }

  window.RepApp = App;

  const root = ReactDOM.createRoot(document.getElementById("root"));
  root.render(React.createElement(App, null));
})();
