/**
 * @author : 최정우
 * @since : 2022.09.20
 * @dscription : React를 활용한 Client단 구현 대상인 Application의 시작점(Index) Component 입니다.
 */
import React from "react";

//Application의 Route 정보를 관리하는 Component
import AllApp, {AdminApp, GovernmentApp, AgencyApp, GuardianApp} from "./AppRoute.jsx";

//Test Layout
import Header from "../layout/Header.jsx";
import Menu from "../layout/Menu.jsx";
import Login from "./login/Login.jsx";
import { useLocation, useNavigate } from "react-router";
import Weather from "./main/Weather.jsx";

function App() {
  const location = useLocation();
  const navigate = useNavigate();
  const [isLogin, setIsLogin] = React.useState(true);

  const getLogin = () => {
    setIsLogin(true);
    navigate("/Main");
  };


  const menuItems = GovernmentApp.menuItems;  //AdminApp, GovernmentApp, AllApp, AgencyApp, GuardianApp 
  const AppRoute = GovernmentApp.AppRoute;


  const { title } = menuItems.find(
    (item) =>
      item.path === location.pathname ||
      location.pathname.startsWith(item.prefix) ||
      item.childrens?.some((child) => location.pathname.startsWith(child.path))
  ) ?? { title: '' };

  return (
    <div id="App">
      {isLogin ? (
        <div id="layout">
          <Header title={title} />
          <Menu items={menuItems}/>
          <div id="pages">
            <AppRoute />
          </div>
        </div>
      ) : (
        <Login getLogin={getLogin} />
      )}
    </div>
  );
}

export default App;
