import { jsxDEV, Fragment } from "react/jsx-dev-runtime";
import { useState, useMemo } from "react";
import { DisableOtpForm } from "./DisableOtpForm.js";
import { Button } from "@contember/ui";
import { PrepareOtpForm } from "./PrepareOtpForm.js";
import { ConfirmOtpForm } from "./ConfirmOtpForm.js";
import qrcode from "qrcode-generator";
import { useIdentity } from "../../../components/Identity/useIdentity.js";
const OtpManagement = () => {
  const identity = useIdentity();
  const [initializingOtp, setInitializingOtp] = useState(false);
  const [otpUri, setOtpUri] = useState();
  const person = identity.person;
  if (!person) {
    return /* @__PURE__ */ jsxDEV(Fragment, { children: "Cannot setup OTP for API key." }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/otp/OtpManagement.tsx",
      lineNumber: 17,
      columnNumber: 10
    }, void 0);
  }
  if (initializingOtp) {
    return /* @__PURE__ */ jsxDEV(Fragment, { children: /* @__PURE__ */ jsxDEV(
      PrepareOtpForm,
      {
        onPrepared: ({ otpUri: otpUri2 }) => {
          setInitializingOtp(false);
          setOtpUri(otpUri2);
        },
        isReSetup: person.otpEnabled,
        onCancel: () => setInitializingOtp(false)
      },
      void 0,
      false,
      {
        fileName: "/__w/interface/interface/packages/admin/src/tenant/components/otp/OtpManagement.tsx",
        lineNumber: 25,
        columnNumber: 5
      },
      void 0
    ) }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/otp/OtpManagement.tsx",
      lineNumber: 24,
      columnNumber: 4
    }, void 0);
  }
  if (otpUri) {
    return /* @__PURE__ */ jsxDEV(Fragment, { children: [
      /* @__PURE__ */ jsxDEV("p", { children: "Now scan the following QR code in you application:" }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/tenant/components/otp/OtpManagement.tsx",
        lineNumber: 38,
        columnNumber: 4
      }, void 0),
      /* @__PURE__ */ jsxDEV(QrCode, { data: otpUri }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/tenant/components/otp/OtpManagement.tsx",
        lineNumber: 41,
        columnNumber: 4
      }, void 0),
      /* @__PURE__ */ jsxDEV("p", { children: "After registering to you application, write down a code from the application." }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/tenant/components/otp/OtpManagement.tsx",
        lineNumber: 42,
        columnNumber: 4
      }, void 0),
      /* @__PURE__ */ jsxDEV(ConfirmOtpForm, { onSuccess: () => {
        setOtpUri(void 0);
      } }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/tenant/components/otp/OtpManagement.tsx",
        lineNumber: 45,
        columnNumber: 4
      }, void 0)
    ] }, void 0, true, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/otp/OtpManagement.tsx",
      lineNumber: 37,
      columnNumber: 10
    }, void 0);
  }
  if (person.otpEnabled) {
    return /* @__PURE__ */ jsxDEV(Fragment, { children: [
      /* @__PURE__ */ jsxDEV("p", { children: "Two factor authentication is ENABLED." }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/tenant/components/otp/OtpManagement.tsx",
        lineNumber: 53,
        columnNumber: 4
      }, void 0),
      /* @__PURE__ */ jsxDEV(DisableOtpForm, {}, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/tenant/components/otp/OtpManagement.tsx",
        lineNumber: 56,
        columnNumber: 4
      }, void 0),
      /* @__PURE__ */ jsxDEV("p", { children: "You already have two-factor authentication active. By enabling a new code, the old one will no longer work." }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/tenant/components/otp/OtpManagement.tsx",
        lineNumber: 57,
        columnNumber: 4
      }, void 0),
      /* @__PURE__ */ jsxDEV(Button, { onClick: () => setInitializingOtp(true), children: "Setup again" }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/tenant/components/otp/OtpManagement.tsx",
        lineNumber: 58,
        columnNumber: 4
      }, void 0)
    ] }, void 0, true, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/otp/OtpManagement.tsx",
      lineNumber: 52,
      columnNumber: 10
    }, void 0);
  }
  return /* @__PURE__ */ jsxDEV(Fragment, { children: [
    /* @__PURE__ */ jsxDEV("p", { children: "2-Step Verification provides stronger security for your account by requiring a second step of verification when you sign in. In addition to your password, you’ll also need a code generated by an application on your phone." }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/otp/OtpManagement.tsx",
      lineNumber: 62,
      columnNumber: 3
    }, void 0),
    /* @__PURE__ */ jsxDEV(Button, { onClick: () => setInitializingOtp(true), children: "Setup two-factor authentication" }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/otp/OtpManagement.tsx",
      lineNumber: 67,
      columnNumber: 3
    }, void 0)
  ] }, void 0, true, {
    fileName: "/__w/interface/interface/packages/admin/src/tenant/components/otp/OtpManagement.tsx",
    lineNumber: 61,
    columnNumber: 9
  }, void 0);
};
const QrCode = ({ data }) => {
  const code = useMemo(() => {
    const qr = qrcode(0, "L");
    qr.addData(data);
    qr.make();
    return qr.createDataURL(4);
  }, [data]);
  return /* @__PURE__ */ jsxDEV("img", { src: code }, void 0, false, {
    fileName: "/__w/interface/interface/packages/admin/src/tenant/components/otp/OtpManagement.tsx",
    lineNumber: 78,
    columnNumber: 9
  }, void 0);
};
export {
  OtpManagement
};
//# sourceMappingURL=OtpManagement.js.map
