import { jsx } from "react/jsx-runtime";
import { useShowToast, Button } from "@contember/ui";
import { useCallback } from "react";
import { useIdentityMethods } from "@contember/react-identity";
import { useDisableOtp } from "../../mutations/otp/disableOtp.js";
import { useRedirect } from "@contember/react-routing";
import { useForm } from "../../lib/useForm.js";
const initialValues = {};
const DisableOtpForm = ({ redirectOnSuccess }) => {
  const redirect = useRedirect();
  const addToast = useShowToast();
  const disableOtp = useDisableOtp();
  const { refreshIdentity } = useIdentityMethods();
  const { isSubmitting, onSubmit } = useForm(
    initialValues,
    useCallback(
      async () => {
        if (!confirm("Are you sure?")) {
          return;
        }
        const response = await disableOtp({});
        if (response.ok) {
          addToast({
            type: "success",
            message: `Two-factor authentication has been disabled`,
            dismiss: true
          });
          refreshIdentity();
          redirectOnSuccess && redirect(redirectOnSuccess);
        } else {
          switch (response.error.code) {
            case "OTP_NOT_ACTIVE":
              return addToast({ message: `Two factor is not active`, type: "error" });
          }
        }
      },
      [addToast, disableOtp, redirect, redirectOnSuccess, refreshIdentity]
    )
  );
  return /* @__PURE__ */ jsx("form", { onSubmit, children: /* @__PURE__ */ jsx(Button, { intent: "danger", type: "submit", disabled: isSubmitting, children: "Disable two-factor" }) });
};
export {
  DisableOtpForm
};
//# sourceMappingURL=DisableOtpForm.js.map
