import { CommonError as u } from "./accelbyte-widgets124.mjs";
import { EmailIcon as f } from "./accelbyte-widgets168.mjs";
import { t as r } from "./accelbyte-widgets58.mjs";
import { Ctx as h } from "./accelbyte-widgets33.mjs";
import { AlertType as m } from "./accelbyte-widgets128.mjs";
import a from "react";
import { Trans as c } from "react-i18next";
import { FactorSectionComponent as p } from "./accelbyte-widgets565.mjs";
import { EmailAuthModal as b } from "./accelbyte-widgets567.mjs";
import { Iam as d } from "@accelbyte/sdk-iam";
const l = "email";
class L extends a.Component {
  constructor(o) {
    super(o), this.requestEmailCode = async (e = !1) => {
      const { onError: t } = this.props;
      this.setState({ isLoading: !0 });
      try {
        await d.UsersV4Api(this.webSdk).createUserMeMfaEmailCode(), this.setState({ isEnableModalOpen: !0 }), e && this.setState({
          modalAlert: {
            type: m.success,
            element: /* @__PURE__ */ a.createElement(c, { i18nKey: "AccountPasswordSecurity.2fa.emailModal.sendCodeSuccess" }, "A verification code has been sent to your email")
          }
        });
      } catch (s) {
        if (console.error(s), e) {
          this.setState({
            modalAlert: {
              type: m.danger,
              element: /* @__PURE__ */ a.createElement(u, { error: s })
            }
          });
          return;
        }
        t(s);
      } finally {
        this.setState({ isLoading: !1 });
      }
    }, this.enableEmailMethod = async (e, t) => {
      const { onSuccess: s, onUpdated: n } = this.props;
      this.setState({ isLoading: !0 });
      try {
        await d.UsersV4Api(this.webSdk).postUserMeMfaEmailEnable({ code: e }), s(), n(), this.setState({ isEnableModalOpen: !1 });
      } catch (i) {
        console.error(i), this.setState({
          modalAlert: {
            type: m.danger,
            element: /* @__PURE__ */ a.createElement(u, { error: i })
          }
        }), t && t();
      } finally {
        this.setState({ isLoading: !1 });
      }
    }, this.setEmailDefault = async () => {
      const { onError: e, onSuccess: t, onUpdated: s, closeConfirmDialog: n } = this.props;
      try {
        await d.UsersV4Api(this.webSdk).postUserMeMfaFactor({ factor: l }), t(), n(), s();
      } catch (i) {
        console.error(i), e(i);
      }
    }, this.disableEmailFactor = async () => {
      const { onError: e, onSuccess: t, onUpdated: s, closeConfirmDialog: n } = this.props;
      try {
        await d.UsersV4Api(this.webSdk).createUserMeMfaEmailDisable(), t(), n(), s();
      } catch (i) {
        console.error(i), e(i);
      }
    }, this.closeModal = () => {
      this.setState({ isEnableModalOpen: !1, modalAlert: null });
    }, this.isEmailMethodEnabled = () => {
      const { enabledFactors: e } = this.props;
      return e ? e.enabled.includes(l) : !1;
    }, this.onClickDefault = () => {
      const { setConfirmDialogData: e, closeConfirmDialog: t } = this.props;
      e({
        title: r("AccountPasswordSecurity.2fa.confirmation.label"),
        message: /* @__PURE__ */ a.createElement(c, { i18nKey: "AccountPasswordSecurity.2fa.backupcode.default.message" }, "Are you sure want to set ", /* @__PURE__ */ a.createElement("span", { className: "font-semibold" }, "Backup Codes"), " as default two-factor authentication method?"),
        cancelBtnLabel: r("AccountPasswordSecurity.2fa.cancelBtn"),
        confirmBtnLabel: r("AccountPasswordSecurity.2fa.yesBtn"),
        isOpen: !0,
        onCancel: t,
        onConfirm: () => this.setEmailDefault()
      });
    }, this.onClickDisable = () => {
      const { setConfirmDialogData: e, closeConfirmDialog: t } = this.props;
      e({
        title: r("AccountPasswordSecurity.2fa.disable.backupcode"),
        message: /* @__PURE__ */ a.createElement(c, { i18nKey: "AccountPasswordSecurity.2fa.disable.email.message" }, "Are you sure want to disable ", /* @__PURE__ */ a.createElement("span", { className: "font-semibold" }, "Email Authentication"), "?"),
        cancelBtnLabel: r("AccountPasswordSecurity.2fa.cancelBtn"),
        confirmBtnLabel: r("AccountPasswordSecurity.2fa.disableBtn"),
        isOpen: !0,
        onCancel: t,
        onConfirm: () => this.disableEmailFactor()
      });
    }, this.state = {
      isEnableModalOpen: !1,
      isLoading: !1,
      modalAlert: null
    }, this.webSdk = h.getAppState().getSdk();
  }
  render() {
    const { enabledFactors: o } = this.props, { isEnableModalOpen: e, modalAlert: t, isLoading: s } = this.state;
    return /* @__PURE__ */ a.createElement(a.Fragment, null, /* @__PURE__ */ a.createElement(
      p,
      {
        icon: /* @__PURE__ */ a.createElement(f, { className: "iconMethod" }),
        title: r("AccountPasswordSecurity.2fa.email"),
        description: /* @__PURE__ */ a.createElement(c, { i18nKey: "AccountPasswordSecurity.2fa.emailDesc" }, "Get a verification code sent to your email each time you log in."),
        enabledFactors: o,
        checkBoxLabel: r("AccountPasswordSecurity.2fa.backupCodesCheckbox"),
        onClickCheckbox: o && o.default !== l ? this.onClickDefault : void 0,
        isCheckboxDisabled: !!o && o.default === l && o.enabled.length === 1,
        isFactorEnabled: this.isEmailMethodEnabled(),
        onEnable: () => this.requestEmailCode(!1),
        onDisable: this.onClickDisable,
        FactorValue: l,
        isPreviewMode: this.props.isPreviewMode
      }
    ), /* @__PURE__ */ a.createElement(
      b,
      {
        isOpen: e,
        onClose: this.closeModal,
        onResendCode: () => this.requestEmailCode(!0),
        closeAlert: () => this.setState({ modalAlert: null }),
        alert: t,
        enableEmail: this.enableEmailMethod,
        isLoading: s
      }
    ));
  }
}
export {
  L as EmailAuthSection
};
