/* Ported from apps/web/src/App.tsx (route surface for this prototype only). */
(() => {
  const T = window.T;
  const { Navigate, useLocation } = T.Router;

  T.ROUTE_PATTERNS = ['/user/home/:view', '/user/lists/:listId', '/user/lists'];

  /* `/user/home` → default view, PRESERVING the query string (deep links —
   * a bare Navigate would drop `search`). Mirrors HomeIndexRedirect. */
  function HomeIndexRedirect() {
    const { search } = useLocation();
    return <Navigate to={{ pathname: '/user/home/signals', search }} replace />;
  }

  function App() {
    const { pathname } = useLocation();

    /* The self-serve onboarding prototype lives on its own page (start.html,
     * isolated CSS/theme). serve -s rewrites unknown paths here, so forward the
     * app's /start URLs to it — /start/email → /start.html#/email. */
    if (pathname === '/start' || pathname.startsWith('/start/')) {
      const step = pathname.replace(/^\/start\/?/, '');
      window.location.replace(`/start.html${window.location.search}#/${step || 'email'}`);
      return null;
    }

    let page = null;
    if (T.Router.matchPath('/user/home/:view', pathname)) {
      page = <T.UserHome />;
    } else if (T.Router.matchPath('/user/lists/:listId', pathname)) {
      page = <T.UserList />;
    } else if (pathname === '/user/lists') {
      page = <T.UserLists />;
    } else if (pathname === '/user/home') {
      return <HomeIndexRedirect />;
    } else {
      return <Navigate to="/user/home/signals" replace />;
    }

    return (
      <React.Fragment>
        <T.MainLayout>{page}</T.MainLayout>
        <T.Toaster />
      </React.Fragment>
    );
  }

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