import { useContext } from 'react';
import { Route, BrowserRouter as Router, Routes } from "react-router-dom";
import GlobalStateContext from './Contexts/GlobalStateContext';
import DefaultLayout from './Layouts/DefaultLayout';
import Login from './Pages/Login';
import { RouteLink } from './Routes/Routes';

function App() { 
  const context = useContext(GlobalStateContext); 
  if (!context) throw new Error('App must be used within a GlobalStateProvider'); 
  const { isAuthenticate } = context; 

  return ( 
    <Router> 
      <Routes> 
        <Route path="/login" element={<Login />} /> 
        <Route path="/*" element={isAuthenticate === true ? (<DefaultLayout><Routes>{RouteLink.map(({ path, Component }, index) => (<Route key={index} path={path} element={<Component />} />))}</Routes></DefaultLayout>) : (<Login />)} /> 
        <Route path="*" element={<Login />} /> 
      </Routes> 
    </Router> 
  ); 
} 

export default App;
