"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxRuntime = require("react/jsx-runtime");
const ui = require("@contember/ui");
const react = require("react");
const prepareOtp = require("../../mutations/otp/prepareOtp.cjs");
const useForm = require("../../lib/useForm.cjs");
const initialValues = {
  label: "Contember Admin"
};
const PrepareOtpForm = ({ onPrepared, isReSetup, onCancel }) => {
  const addToast = ui.useShowToast();
  const prepareOtp$1 = prepareOtp.usePrepareOtp();
  const { isSubmitting, onSubmit, register } = useForm.useForm(
    initialValues,
    react.useCallback(
      async (values) => {
        const response = await prepareOtp$1({ 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$1, onPrepared]
    )
  );
  return /* @__PURE__ */ jsxRuntime.jsx("form", { onSubmit, children: /* @__PURE__ */ jsxRuntime.jsxs(ui.Stack, { gap: "large", children: [
    /* @__PURE__ */ jsxRuntime.jsx(ui.FieldContainer, { label: "Label for an identification in two-factor app", children: /* @__PURE__ */ jsxRuntime.jsx(ui.TextInput, { ...register("label") }) }),
    isReSetup && /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.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__ */ jsxRuntime.jsxs(ui.Stack, { horizontal: true, style: { alignSelf: "flex-end" }, children: [
      /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { type: "submit", distinction: "seamless", onClick: onCancel, children: "Cancel" }),
      /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { intent: isReSetup ? "danger" : void 0, distinction: "primary", type: "submit", disabled: isSubmitting, children: "Continue" })
    ] })
  ] }) });
};
exports.PrepareOtpForm = PrepareOtpForm;
//# sourceMappingURL=PrepareOtpForm.cjs.map
