import AppRouter from "./router";
import { useSelector } from "react-redux";
import { useMatch, useNavigate, useSearchParams } from "react-router-dom";
import { useEffect } from "react";
import { RootState } from "./redux/store";
import { withCookies, ReactCookieProps } from "react-cookie";

const App: React.FC<ReactCookieProps> = ({ cookies }) => {
  const navigate = useNavigate(); 
  const isPanel = useMatch("/auth/login");
  const isCookiePresent = !!cookies?.get("authToken");
  console.log("cookies present:", isCookiePresent);
  const [searchParams] = useSearchParams();

  const isAuthenticated = useSelector(
    (state: RootState) => state.authReducer.isAuthenticated
  );

  useEffect(() => {
    if (isPanel && isCookiePresent) {
      navigate("/panel/dashboard");
    }
  }, [isPanel, isAuthenticated, searchParams]);

  return <AppRouter />;
};

export default withCookies(App);
