import { RedirectLoginOptions, useAuth0 } from "@auth0/auth0-react";
// import { useAuth } from "hooks";
import queryString from "query-string";
import { useEffect } from "react";
import { useLocation } from "react-router";
// import Loading from "./Loading";
import LoadingScreen from "app/LoadingScreen";

// interface Props {
//   prevPath?: string;
// }

export default function Login() {
  // const { prevPath } = props;
  const location = useLocation();
  const { loginWithRedirect } = useAuth0();

  // const setRouteBeforeLogin = useCallback((): Promise<string> => {
  //   return new Promise(function(resolve) {
  //     localStorage.setItem("routeBeforeLogin", prevPath ? prevPath : "/");
  //     return resolve("success");
  //   });
  // }, [prevPath]);

  useEffect(() => {
    const options: RedirectLoginOptions = {
      authorizationParams: {}  // Initialize here
    };
    
    if (location && location.search) {
      let parsed = queryString.parse(location.search);

      if (parsed.signup === "true") {
        // if (options.authorizationParams) options.authorizationParams.screen_hint = "signup"; // 'signup' tells Auth0 to show the signup tab
        options.authorizationParams!.screen_hint = "signup"; // 'signup' tells Auth0 to show the signup tab
        options.authorizationParams!.prompt = "login";
      }

      if (parsed.email && parsed.email !== "") {
        // if (options.authorizationParams) options.authorizationParams.login_hint = parsed.email.toString(); // prefill email
        options.authorizationParams!.login_hint = parsed.email.toString(); // prefill email
      }
    }

    // setRouteBeforeLogin().finally(() => {
    console.log(options.authorizationParams)
    loginWithRedirect(options);
    // });
  }, [location, loginWithRedirect]);

  // return <LoadingScreen fullViewport={true} />;
  return <LoadingScreen message="login redirect" />;
}
