"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const common = require("./common.cjs");
const react = require("react");
const useIDPStateStore = require("./useIDPStateStore.cjs");
const initSignInIDP = require("../../mutations/idp/initSignInIDP.cjs");
const useRedirectToBacklink = require("../person/useRedirectToBacklink.cjs");
const useInitIDPRedirect = ({ onError }) => {
  const initRequest = initSignInIDP.useInitSignInIDP();
  const saveBacklink = useRedirectToBacklink.useSaveBacklink();
  const { set: saveIdpState } = useIDPStateStore.useIDPStateStore();
  return react.useCallback(async ({ provider }) => {
    const fail = (error = "Failed to initiate login.") => {
      onError?.(error);
      return { ok: false, error };
    };
    try {
      const response = await initRequest({
        redirectUrl: common.getBaseHref(),
        identityProvider: provider
      });
      if (!response.ok) {
        console.error(response.error);
        return fail();
      } else {
        saveIdpState({ provider, sessionData: response.result.sessionData });
        saveBacklink();
        window.location.href = response.result.authUrl;
        return { ok: true };
      }
    } catch (e) {
      console.error(e);
      return fail();
    }
  }, [initRequest, onError, saveBacklink, saveIdpState]);
};
exports.useInitIDPRedirect = useInitIDPRedirect;
//# sourceMappingURL=useInitIDPRedirect.cjs.map
