import { Route, Routes } from "react-router-dom";

import routes from "@/constants/routes";
import LayoutWrapper from "@greatness/components/src/LayoutWrapper";

import PrivateRoute from "./components/PrivateRoute";

export default function AppRouter() {
  return (
    <Routes>
      {routes.public.map((route) => (
        <Route key={route.path} path={route.path} element={<route.Component />} />
      ))}
      <Route path="/" element={<LayoutWrapper />}>
        {routes.private.map(({ path, Component, subroutes, redirectTo }) => (
          <Route
            key={path}
            path={path}
            element={<PrivateRoute Component={Component} redirectTo={redirectTo} />}
          >
            {subroutes?.map((subroute) => (
              <Route
                key={subroute.path}
                path={subroute.path}
                element={
                  <PrivateRoute
                    Component={subroute.Component}
                    redirectTo={subroute.redirectTo}
                  />
                }
              />
            )) || null}
          </Route>
        ))}
      </Route>
    </Routes>
  );
}
