import { useShowToast, Button } from "@contember/ui";
import { useContext, useCallback } from "react";
import { jsxDEV } from "react/jsx-dev-runtime";
import { useDisableOtp } from "../../mutations/otp/disableOtp.js";
import { useRedirect } from "../../../routing/binding/useRedirect.js";
import { IdentityRefreshContext } from "../../../components/Identity/IdentityProvider.js";
import { useForm } from "../../lib/useForm.js";
var _jsxFileName = "/__w/interface/interface/packages/admin/src/tenant/components/otp/DisableOtpForm.tsx";
const initialValues = {};
const DisableOtpForm = ({
  redirectOnSuccess
}) => {
  const redirect = useRedirect();
  const addToast = useShowToast();
  const disableOtp = useDisableOtp();
  const refreshIdentity = useContext(IdentityRefreshContext);
  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__ */ jsxDEV("form", {
    onSubmit,
    children: /* @__PURE__ */ jsxDEV(Button, {
      intent: "danger",
      type: "submit",
      disabled: isSubmitting,
      children: "Disable two-factor"
    }, void 0, false, {
      fileName: _jsxFileName,
      lineNumber: 51,
      columnNumber: 4
    }, void 0)
  }, void 0, false, {
    fileName: _jsxFileName,
    lineNumber: 50,
    columnNumber: 3
  }, void 0);
};
export {
  DisableOtpForm
};
//# sourceMappingURL=DisableOtpForm.js.map
