import { getBaseHref } from "./common.js";
import { useCallback } from "react";
import { useIDPStateStore } from "./useIDPStateStore.js";
import { useInitSignInIDP } from "../../mutations/idp/initSignInIDP.js";
import { useSaveBacklink } from "../person/useRedirectToBacklink.js";
const useInitIDPRedirect = ({ onError }) => {
  const initRequest = useInitSignInIDP();
  const saveBacklink = useSaveBacklink();
  const { set: saveIdpState } = useIDPStateStore();
  return useCallback(async ({ provider }) => {
    const response = await initRequest({
      redirectUrl: getBaseHref(),
      identityProvider: provider
    });
    if (!response.ok) {
      console.error(response.error);
      onError("Failed to initiate login.");
    } else {
      saveIdpState({ provider, sessionData: response.result.sessionData });
      saveBacklink();
      window.location.href = response.result.authUrl;
    }
  }, [initRequest, onError, saveBacklink, saveIdpState]);
};
export {
  useInitIDPRedirect
};
//# sourceMappingURL=useInitIDPRedirect.js.map
