import React, { memo, useContext } from "react";
// import { AuthContext } from "Context/Auth";
import { NotFound } from "./Routes";
import PublicRoute from "./PublicRoutes";
import AdminRoute from "./AdminRoutes";
import { AuthContext } from "Context/Auth";

const PrivateRoute = ({ path, element, access }) => {
  const Auth = useContext(AuthContext);

  if (Auth?.state?.isAuthenticated) {
    switch (true) {
      case Auth?.state?.role === "admin" && access === "admin":
        return <AdminRoute path={path}>{element}</AdminRoute>;

      default:
        return <PublicRoute path={"*"} element={<NotFound />} />;
    }
  }
  if (!Auth?.state?.isAuthenticated) {
    return <PublicRoute path={"*"} element={<NotFound />} />;
  }
};

export default memo(PrivateRoute);
