import { jsxDEV, Fragment } from "react/jsx-dev-runtime";
import { useShowToast, Stack, FieldContainer, TextInput, Message, Button } from "@contember/ui";
import { useCallback } from "react";
import { usePrepareOtp } from "../../mutations/otp/prepareOtp.js";
import { useForm } from "../../lib/useForm.js";
const initialValues = {
  label: "Contember Admin"
};
const PrepareOtpForm = ({ onPrepared, isReSetup, onCancel }) => {
  const addToast = useShowToast();
  const prepareOtp = usePrepareOtp();
  const { isSubmitting, onSubmit, register } = useForm(
    initialValues,
    useCallback(
      async (values) => {
        const response = await prepareOtp({ label: values.label });
        if (response.ok) {
          onPrepared(response.result);
        } else {
          switch (response.error.code) {
            case "OTP_NOT_ACTIVE":
              return addToast({ message: `Two factor is not active`, type: "error", dismiss: true });
          }
        }
      },
      [addToast, prepareOtp, onPrepared]
    )
  );
  return /* @__PURE__ */ jsxDEV("form", { onSubmit, children: /* @__PURE__ */ jsxDEV(Stack, { gap: "large", children: [
    /* @__PURE__ */ jsxDEV(FieldContainer, { label: "Label for an identification in two-factor app", children: /* @__PURE__ */ jsxDEV(TextInput, { ...register("label") }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/otp/PrepareOtpForm.tsx",
      lineNumber: 40,
      columnNumber: 6
    }, void 0) }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/otp/PrepareOtpForm.tsx",
      lineNumber: 39,
      columnNumber: 5
    }, void 0),
    isReSetup && /* @__PURE__ */ jsxDEV(Fragment, { children: /* @__PURE__ */ jsxDEV(Message, { important: true, padding: true, intent: "warn", children: 'You already have two-factor authentication active. By clicking "Continue", the old one will no longer work.' }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/otp/PrepareOtpForm.tsx",
      lineNumber: 44,
      columnNumber: 6
    }, void 0) }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/otp/PrepareOtpForm.tsx",
      lineNumber: 43,
      columnNumber: 19
    }, void 0),
    /* @__PURE__ */ jsxDEV(Stack, { horizontal: true, style: { alignSelf: "flex-end" }, children: [
      /* @__PURE__ */ jsxDEV(Button, { type: "submit", distinction: "seamless", onClick: onCancel, children: "Cancel" }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/tenant/components/otp/PrepareOtpForm.tsx",
        lineNumber: 50,
        columnNumber: 6
      }, void 0),
      /* @__PURE__ */ jsxDEV(Button, { intent: isReSetup ? "danger" : void 0, distinction: "primary", type: "submit", disabled: isSubmitting, children: "Continue" }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/tenant/components/otp/PrepareOtpForm.tsx",
        lineNumber: 54,
        columnNumber: 6
      }, void 0)
    ] }, void 0, true, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/otp/PrepareOtpForm.tsx",
      lineNumber: 49,
      columnNumber: 5
    }, void 0)
  ] }, void 0, true, {
    fileName: "/__w/interface/interface/packages/admin/src/tenant/components/otp/PrepareOtpForm.tsx",
    lineNumber: 38,
    columnNumber: 4
  }, void 0) }, void 0, false, {
    fileName: "/__w/interface/interface/packages/admin/src/tenant/components/otp/PrepareOtpForm.tsx",
    lineNumber: 37,
    columnNumber: 3
  }, void 0);
};
export {
  PrepareOtpForm
};
//# sourceMappingURL=PrepareOtpForm.js.map
