import {
  Route,
  RouterProvider,
  createBrowserRouter,
  createRoutesFromElements,
} from "react-router-dom";
import { AuthApi } from "./api/AuthApi";
import { ServerApi } from "./api/ServerApi";
import { LoginRoute } from "./routes/LoginRoute";
import { NotFoundRoute } from "./routes/NotFoundRoute";
import { HomeRoute } from "./routes/HomeRoute";
import { BaseAuthenticatedPage } from "./widgets/BaseAuthenticatedPage";
import { PendingDevicesRoute } from "./routes/PendingDevicesRoute";

export function App() {
  if (!AuthApi.SignedIn && !ServerApi.Config.auth_disabled)
    return <LoginRoute />;

  const router = createBrowserRouter(
    createRoutesFromElements(
      <Route path="*" element={<BaseAuthenticatedPage />}>
        <Route path="" element={<HomeRoute />} />
        <Route path="pending_devices" element={<PendingDevicesRoute />} />
        <Route path="*" element={<NotFoundRoute />} />
      </Route>
    )
  );

  return <RouterProvider router={router} />;
}
