import { BackupCodeIcon as k } from "./accelbyte-widgets167.mjs";
import { t as s } from "./accelbyte-widgets58.mjs";
import { Ctx as h } from "./accelbyte-widgets33.mjs";
import { AlertType as d } from "./accelbyte-widgets128.mjs";
import { Iam as l } from "@accelbyte/sdk-iam";
import t from "react";
import { Trans as n } from "react-i18next";
import { BackupCodesModal as b } from "./accelbyte-widgets566.mjs";
import { FactorSectionComponent as f } from "./accelbyte-widgets565.mjs";
const u = "backupCode";
class D extends t.Component {
  constructor(o) {
    super(o), this.onClickOpenBackupCodes = () => {
      this.setState({ isBackupCodesModalOpen: !0 }), this.getBackupCodes();
    }, this.onCloseBackupCodes = () => {
      this.setState({ isBackupCodesModalOpen: !1 });
    }, this.enableBackupCodes = async () => {
      const { onSuccess: e, onUpdated: a } = this.props;
      this.setState({ isBackupCodesModalOpen: !0, isGenerating: !0 });
      try {
        await l.UsersV4Api(this.webSdk).createUserMeMfaBackupCodeEnable(), e(), this.setState({ isBackupCodesModalOpen: !0 }), a(), this.getBackupCodes(), this.setState({
          alert: {
            type: d.success,
            element: /* @__PURE__ */ t.createElement(n, { i18nKey: "AccountPasswordSecurity.2fa.updated" }, "Two-factor authentication has been updated.")
          }
        });
      } catch (c) {
        console.error(c), this.setState({
          backupCode: null,
          backupCodeError: {
            message: s("AccountPasswordSecurity.2fa.backupCode.error.generate"),
            retryLabel: s("AccountPasswordSecurity.2fa.backupCode.generateNewCode"),
            onRetry: this.enableBackupCodes
          }
        });
      } finally {
        this.setState({ isGenerating: !1 });
      }
    }, this.getBackupCodes = async () => {
      this.setState({ backupCodeError: null, isFetching: !0 });
      try {
        const e = await l.UsersV4Api(this.webSdk).getUsersMeMfaBackupCode();
        this.setState({ backupCode: e });
      } catch (e) {
        console.error(e), this.setState({
          backupCodeError: {
            message: s("AccountPasswordSecurity.2fa.backupCode.error.fetch"),
            retryLabel: s("AccountPasswordSecurity.2fa.backupCode.tryAgain"),
            onRetry: this.getBackupCodes
          }
        });
      } finally {
        this.setState({ isFetching: !1 });
      }
    }, this.generateCodes = async () => {
      this.setState({ backupCodeError: null, isGenerating: !0 });
      try {
        const e = await l.UsersV4Api(this.webSdk).createUserMeMfaBackupCode();
        this.setState({ backupCode: e }), this.setState({
          alert: {
            type: d.success,
            element: /* @__PURE__ */ t.createElement(n, { i18nKey: "AccountPasswordSecurity.2fa.backupCode.updated" }, "Backup codes updated")
          }
        });
      } catch (e) {
        console.error(e), this.setState({
          backupCodeError: {
            message: s("AccountPasswordSecurity.2fa.backupCode.error.generate"),
            retryLabel: s("AccountPasswordSecurity.2fa.backupCode.generateNewCode"),
            onRetry: this.generateCodes
          }
        });
      } finally {
        this.setState({ isGenerating: !1 });
      }
    }, this.disableBackupCodesFactor = async () => {
      const { onError: e, onSuccess: a, onUpdated: c, closeConfirmDialog: i } = this.props;
      try {
        await l.UsersV4Api(this.webSdk).deleteUserMeMfaBackupCodeDisable(), a(), i(), c();
      } catch (r) {
        console.error(r), e(r);
      }
    }, this.setBackupcodeDefault = async () => {
      const { onError: e, onSuccess: a, onUpdated: c, closeConfirmDialog: i } = this.props;
      try {
        await l.UsersV4Api(this.webSdk).postUserMeMfaFactor({ factor: u }), a(), i(), c();
      } catch (r) {
        console.error(r), e(r);
      }
    }, this.onClickDisable = () => {
      const { setConfirmDialogData: e, closeConfirmDialog: a } = this.props;
      e({
        title: s("AccountPasswordSecurity.2fa.disable.backupcode"),
        message: /* @__PURE__ */ t.createElement(n, { i18nKey: "AccountPasswordSecurity.2fa.disable.backupcode.message" }, "Are you sure want to disable ", /* @__PURE__ */ t.createElement("span", { className: "font-semibold" }, "Backup Codes"), "?"),
        cancelBtnLabel: s("AccountPasswordSecurity.2fa.cancelBtn"),
        confirmBtnLabel: s("AccountPasswordSecurity.2fa.disableBtn"),
        isOpen: !0,
        onCancel: a,
        onConfirm: () => this.disableBackupCodesFactor()
      });
    }, this.onClickDefault = () => {
      const { setConfirmDialogData: e, closeConfirmDialog: a } = this.props;
      e({
        title: s("AccountPasswordSecurity.2fa.confirmation.label"),
        message: /* @__PURE__ */ t.createElement(n, { i18nKey: "AccountPasswordSecurity.2fa.backupcode.default.message" }, "Are you sure want to set ", /* @__PURE__ */ t.createElement("span", { className: "font-semibold" }, "Backup Codes"), " as default two-factor authentication method?"),
        cancelBtnLabel: s("AccountPasswordSecurity.2fa.cancelBtn"),
        confirmBtnLabel: s("AccountPasswordSecurity.2fa.yesBtn"),
        isOpen: !0,
        onCancel: a,
        onConfirm: () => this.setBackupcodeDefault()
      });
    }, this.isBackupCodesEnabled = () => {
      const { enabledFactors: e } = this.props;
      return e ? e.enabled.includes(u) : !1;
    }, this.state = {
      isBackupCodesModalOpen: !1,
      backupCode: null,
      isFetching: !1,
      isGenerating: !1,
      alert: null,
      backupCodeError: null
    }, this.webSdk = h.getAppState().getSdk();
  }
  render() {
    const { enabledFactors: o } = this.props, { isBackupCodesModalOpen: e, backupCode: a, isFetching: c, isGenerating: i, alert: r, backupCodeError: p } = this.state;
    return /* @__PURE__ */ t.createElement(t.Fragment, null, /* @__PURE__ */ t.createElement(
      f,
      {
        icon: /* @__PURE__ */ t.createElement(k, { className: "iconMethod" }),
        title: /* @__PURE__ */ t.createElement(n, { i18nKey: "AccountPasswordSecurity.2fa.backupCodes" }, "Backup Codes"),
        description: /* @__PURE__ */ t.createElement(t.Fragment, null, /* @__PURE__ */ t.createElement(n, { i18nKey: "AccountPasswordSecurity.2fa.backupCodesDesc" }, "These one-time passcodes allow you to sign in when away from your phone."), this.isBackupCodesEnabled() && /* @__PURE__ */ t.createElement(n, { i18nKey: "AccountPasswordSecurity.2fa.clickMore" }, "To manage your backup codes,", /* @__PURE__ */ t.createElement("span", { onClick: this.onClickOpenBackupCodes, className: "ml-2 cursor-pointer text-blue-50" }, "Click here."))),
        enabledFactors: o,
        checkBoxLabel: s("AccountPasswordSecurity.2fa.backupCodesCheckbox"),
        onClickCheckbox: o && o.default !== u ? this.onClickDefault : void 0,
        isCheckboxDisabled: !!o && o.default === u && o.enabled.length === 1,
        isFactorEnabled: this.isBackupCodesEnabled(),
        onEnable: this.enableBackupCodes,
        onDisable: this.onClickDisable,
        FactorValue: u,
        isFirstSection: !0,
        isPreviewMode: this.props.isPreviewMode
      }
    ), /* @__PURE__ */ t.createElement(
      b,
      {
        isOpen: e,
        onClose: this.onCloseBackupCodes,
        backupCode: a,
        isFetching: c,
        isGenerating: i,
        alert: r,
        backupCodeError: p,
        setAlert: (C) => this.setState({ alert: C }),
        generateCodes: this.generateCodes
      }
    ));
  }
}
export {
  D as BackupCodesSectionComponent
};
