import { useCallback, createElement } from "react";
import { Button } from "@contember/ui";
import { getBaseHref, IDP_SESSION_KEY, IDP_CODE } from "./common.js";
import { useInitSignInIDP } from "../../mutations/idp/initSignInIDP.js";
const IDPInitButton = ({ provider, onError }) => {
  const initRequest = useInitSignInIDP();
  const onInitIDP = useCallback(async (provider2) => {
    const response = await initRequest({
      redirectUrl: getBaseHref(),
      identityProvider: provider2
    });
    if (!response.ok) {
      console.error(response.error);
      onError("Failed to initiate login.");
    } else {
      localStorage.setItem(IDP_SESSION_KEY, JSON.stringify(response.result.sessionData));
      localStorage.setItem(IDP_CODE, provider2);
      window.location.href = response.result.authUrl;
    }
  }, [initRequest, onError]);
  return /* @__PURE__ */ createElement(Button, {
    onClick: () => onInitIDP(provider.provider)
  }, "Login using ", provider.name ?? provider.provider);
};
export { IDPInitButton };
//# sourceMappingURL=IDPInitButton.js.map
