import { useState, useEffect } from "react";
import { OpenApiRouter } from "../config/router.context.mjs";
import { AuthContext } from "./auth.context.mjs";
const AuthGuard = ({ type, redirectTo, children }) => {
  const { isAuthenticated, routes, loadingState } = AuthContext.useAuth();
  const { replace } = OpenApiRouter.useRouter();
  const [hasMounted, setHasMounted] = useState(false);
  useEffect(() => {
    setHasMounted(true);
  }, []);
  if (!hasMounted) {
    return loadingState;
  }
  if (type === "private" && !isAuthenticated) {
    replace(redirectTo || routes?.unauthenticated || "/");
    return null;
  }
  if (type === "public-only" && isAuthenticated) {
    replace(redirectTo || routes?.authenticated || "/");
    return null;
  }
  return children;
};
export {
  AuthGuard
};
