"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxDevRuntime = require("react/jsx-dev-runtime");
const ui = require("@contember/ui");
const react = require("react");
const reactIdentity = require("@contember/react-identity");
const confirmOtp = require("../../mutations/otp/confirmOtp.cjs");
const reactRouting = require("@contember/react-routing");
const useForm = require("../../lib/useForm.cjs");
const initialValues = {
  token: ""
};
const ConfirmOtpForm = ({ redirectOnSuccess, onSuccess }) => {
  const redirect = reactRouting.useRedirect();
  const addToast = ui.useShowToast();
  const confirmOtp$1 = confirmOtp.useConfirmOtp();
  const { refreshIdentity } = reactIdentity.useIdentityMethods();
  const { isSubmitting, onSubmit, register } = useForm.useForm(
    initialValues,
    react.useCallback(
      async (values) => {
        const response = await confirmOtp$1({ token: values.token });
        if (response.ok) {
          addToast({
            type: "success",
            message: `Two-factor authentication has been enabled`,
            dismiss: true
          });
          refreshIdentity();
          redirectOnSuccess && redirect(redirectOnSuccess);
          onSuccess && onSuccess();
        } else {
          switch (response.error.code) {
            case "INVALID_OTP_TOKEN":
              return addToast({ message: `Provided code is not valid`, type: "error" });
            case "NOT_PREPARED":
              return addToast({ message: `Two factor setup was not initialized`, type: "error" });
          }
        }
      },
      [addToast, confirmOtp$1, onSuccess, redirect, redirectOnSuccess, refreshIdentity]
    )
  );
  return /* @__PURE__ */ jsxDevRuntime.jsxDEV("form", { onSubmit, children: [
    /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.FieldContainer, { label: "Two-factor code", children: /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.TextInput, { ...register("token"), required: true }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/otp/ConfirmOtpForm.tsx",
      lineNumber: 52,
      columnNumber: 5
    }, void 0) }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/otp/ConfirmOtpForm.tsx",
      lineNumber: 51,
      columnNumber: 4
    }, void 0),
    /* @__PURE__ */ jsxDevRuntime.jsxDEV("br", {}, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/otp/ConfirmOtpForm.tsx",
      lineNumber: 54,
      columnNumber: 4
    }, void 0),
    /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.Button, { distinction: "primary", type: "submit", disabled: isSubmitting, children: "Confirm two-factor authentication" }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/otp/ConfirmOtpForm.tsx",
      lineNumber: 55,
      columnNumber: 4
    }, void 0)
  ] }, void 0, true, {
    fileName: "/__w/interface/interface/packages/admin/src/tenant/components/otp/ConfirmOtpForm.tsx",
    lineNumber: 50,
    columnNumber: 3
  }, void 0);
};
exports.ConfirmOtpForm = ConfirmOtpForm;
//# sourceMappingURL=ConfirmOtpForm.cjs.map
