import { jsx, jsxs, Fragment } from "react/jsx-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__ */ jsx("form", { onSubmit, children: /* @__PURE__ */ jsxs(Stack, { gap: "large", children: [
    /* @__PURE__ */ jsx(FieldContainer, { label: "Label for an identification in two-factor app", children: /* @__PURE__ */ jsx(TextInput, { ...register("label") }) }),
    isReSetup && /* @__PURE__ */ jsx(Fragment, { children: /* @__PURE__ */ jsx(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.' }) }),
    /* @__PURE__ */ jsxs(Stack, { horizontal: true, style: { alignSelf: "flex-end" }, children: [
      /* @__PURE__ */ jsx(Button, { type: "submit", distinction: "seamless", onClick: onCancel, children: "Cancel" }),
      /* @__PURE__ */ jsx(Button, { intent: isReSetup ? "danger" : void 0, distinction: "primary", type: "submit", disabled: isSubmitting, children: "Continue" })
    ] })
  ] }) });
};
export {
  PrepareOtpForm
};
//# sourceMappingURL=PrepareOtpForm.js.map
