import { useUserIdentity, navigation, useCurrentPage, } from "@saasquatch/component-boilerplate";
import { useEffect } from "@saasquatch/universal-hooks";
export function usePortalProtectedRoute({ requireEmailVerification, redirectTo, redirectToUnverified, }) {
  var _a;
  const userIdent = useUserIdentity();
  const authenticated = !!(userIdent === null || userIdent === void 0 ? void 0 : userIdent.jwt);
  const emailVerified = (_a = userIdent === null || userIdent === void 0 ? void 0 : userIdent.managedIdentity) === null || _a === void 0 ? void 0 : _a.emailVerified;
  const { pathname, search } = useCurrentPage();
  const nextPageParam = new URLSearchParams();
  nextPageParam.append("nextPage", `${pathname}${search}`);
  useEffect(() => {
    if (!authenticated) {
      return navigation.push({
        pathname: redirectTo,
        search: "?" + nextPageParam.toString(),
      });
    }
    if (requireEmailVerification && !emailVerified) {
      return navigation.push({
        pathname: redirectToUnverified || redirectTo,
        search: "?" + nextPageParam.toString(),
      });
    }
  }, []);
  return !!userIdent;
}
