import * as e from "react";
import S from "classnames";
import { Trans as a } from "react-i18next";
import { IamUserAuthorizationClient as C, Iam as y, IamHelper as T } from "@accelbyte/sdk-iam";
import { t as m } from "./accelbyte-widgets58.mjs";
import { Alert as P } from "./accelbyte-widgets123.mjs";
import { Button as p, ButtonStyle as x } from "./accelbyte-widgets107.mjs";
import { CommonError as O } from "./accelbyte-widgets124.mjs";
import { LoginIcon as b } from "./accelbyte-widgets303.mjs";
import { ProfileImage as v } from "./accelbyte-widgets305.mjs";
import { AccPath as W } from "./accelbyte-widgets34.mjs";
import { ErrorHelper as w } from "./accelbyte-widgets120.mjs";
import { Ctx as h } from "./accelbyte-widgets33.mjs";
import { FeatureFlags as R } from "./accelbyte-widgets57.mjs";
import { UserHelper as g } from "./accelbyte-widgets277.mjs";
import { AlertType as d } from "./accelbyte-widgets128.mjs";
import r from "./accelbyte-widgets182.mjs";
import { PickProgression as U } from "./accelbyte-widgets525.mjs";
import { SuccessMessage as I } from "./accelbyte-widgets523.mjs";
import { EXPIRED_ONE_TIME_ERROR_CODE as k, ONE_TIME_CODE_LENGTH as M } from "./accelbyte-widgets353.mjs";
import { RestartMessage as F } from "./accelbyte-widgets524.mjs";
import { CommonTitle as H } from "./accelbyte-widgets357.mjs";
class ae extends e.Component {
  constructor(l) {
    super(l), this.dismissAlert = () => {
      this.setState({ alert: null });
    }, this.onClickLogin = () => {
      const { oneTimeLinkCode: t } = this.props, i = W.LinkAccountWithOneTimeCode.makePath(void 0, { oneTimeLinkCode: t }), s = new C(this.appState.getSdk()).createLoginURL(i, void 0, t);
      window.location.replace(s);
    }, this.setErrorAlert = (t) => {
      const { oneTimeLinkCode: i, onHideTitle: s } = this.props, n = w.extractServiceErrorCode(t);
      n === k && (s(), this.setState({ step: "shouldRestart" })), this.setState({
        alert: {
          type: d.danger,
          element: (() => {
            switch (n) {
              case k:
                return i.length === M ? m("LinkAccountWithOneTimeCodePage.error.expired") : m("LinkAccountWithOneTimeCodePage.error.invalid");
              default:
                return /* @__PURE__ */ e.createElement(O, { error: t });
            }
          })()
        }
      });
    }, this.onLinkToExisting = async () => {
      const t = y.UsersApi(this.appState.getSdk()), { oneTimeLinkCode: i, onHideTitle: s } = this.props, { isLinking: n } = this.state, c = this.appState.getBrandName();
      try {
        if (n)
          return;
        this.setState({ isLinking: !0 });
        const o = await t.getUsersMeHeadlessLinkConflict({ oneTimeLinkCode: i }), { platformAlreadyLinked: E, platformLinkConflict: f, platformId: u, currentAccount: L, headlessAccount: A } = o, N = g.getPlatformName(u);
        if (E || f) {
          this.setState({
            isLinking: !1,
            alert: {
              type: d.danger,
              element: /* @__PURE__ */ e.createElement(a, { i18nKey: "LinkAccountWithOneTimeCodePage.selectLinkMode.linkToExisting.conflict.message" }, "Unable to link this ", { publisherName: c }, " account. Because it has linked to other ", { platformName: N }, " account previously. Please use another ", { publisherName: c }, " account.")
            }
          });
          return;
        }
        s(), this.setState({
          step: "pickProgression",
          progressionData: { currentAccount: L, headlessAccount: A, platformId: u }
        });
      } catch (o) {
        console.error(o), this.setState({ isLinking: !1 }), this.setErrorAlert(o);
      }
    }, this.onLinkWithProgressionSuccess = () => {
      this.setState({ alert: null, step: "success" });
    }, this.renderNoSession = () => {
      const t = this.appState.getBrandName();
      return /* @__PURE__ */ e.createElement(e.Fragment, null, /* @__PURE__ */ e.createElement("div", { className: r.noSession }, /* @__PURE__ */ e.createElement(b, { className: r.loginIcon }), /* @__PURE__ */ e.createElement("p", { className: r.description }, /* @__PURE__ */ e.createElement(a, { i18nKey: "LinkAccountWithOneTimeCodePage.selectLinkMode.linkToExisting.noSession.message" }, "There is no logged session, please login to your ", { publisherName: t }, " account to continue the account linking process."))), /* @__PURE__ */ e.createElement(p, { className: "btn btn-primary mt-6 w-full py-2 px-0", onClick: this.onClickLogin }, m("Navigation.links.login")));
    }, this.renderWithSession = () => {
      const { userProfile: t, user: i } = this.appState.getUserManager().state, { isLinking: s } = this.state, n = this.appState.getBrandName();
      return i ? /* @__PURE__ */ e.createElement(e.Fragment, null, /* @__PURE__ */ e.createElement("div", { className: r.withSession }, h.hasFeature(R.isAvatarVisible) && /* @__PURE__ */ e.createElement(v, { imageLink: (t == null ? void 0 : t.avatarUrl) || "", className: r.withSessionAvatar }), /* @__PURE__ */ e.createElement("div", { className: "text-2sm ml-4" }, /* @__PURE__ */ e.createElement("p", { className: r.withSessionUserName }, i.userName || i.displayName), /* @__PURE__ */ e.createElement("p", { className: r.withSessionEmail }, i.emailAddress))), /* @__PURE__ */ e.createElement("div", { className: "mt-6 text-center" }, /* @__PURE__ */ e.createElement(p, { className: "btn btn-primary mb-6 w-full py-2 px-0", onClick: this.onLinkToExisting, isLoading: s }, /* @__PURE__ */ e.createElement(a, { i18nKey: "LinkAccountWithOneTimeCodePage.selectLinkMode.tab.linkToExisting" }, "Link to Existing Account")), /* @__PURE__ */ e.createElement("p", { className: r.description }, /* @__PURE__ */ e.createElement(a, { i18nKey: "LinkAccountWithOneTimeCodePage.pickProgression.linkAnother.question" }, "Don't want to link to this existing account?")), /* @__PURE__ */ e.createElement(p, { className: "text-2sm text-primary-60 mx-auto py-2", buttonStyle: x.NONE, onClick: this.onClickLogin }, /* @__PURE__ */ e.createElement(a, { i18nKey: "LinkAccountWithOneTimeCodePage.pickProgression.linkAnother.action" }, "Link with another ", { publisherName: n }, " account")))) : null;
    }, this.renderLinkAccountStep = () => {
      const { user: t } = this.appState.getUserManager().state;
      return t && !T.currentUserIsHeadlessAccount(t) ? this.renderWithSession() : this.renderNoSession();
    }, this.state = {
      alert: null,
      isLinking: !1,
      step: "linkAccount",
      progressionData: null
    }, this.appState = h.getAppState();
  }
  render() {
    const { oneTimeLinkCode: l, onRestart: t } = this.props, { alert: i, step: s, progressionData: n } = this.state, c = this.appState.getBrandName(), o = g.getPlatformName((n == null ? void 0 : n.platformId) || "");
    return /* @__PURE__ */ e.createElement(e.Fragment, null, s === "pickProgression" && /* @__PURE__ */ e.createElement(e.Fragment, null, /* @__PURE__ */ e.createElement("div", { className: "mb-5 text-center" }, /* @__PURE__ */ e.createElement("p", { className: r.title }, /* @__PURE__ */ e.createElement(a, { i18nKey: "LinkAccountWithOneTimeCodePage.pickProgression.title" }, "Choose Game Progression")), /* @__PURE__ */ e.createElement("p", { className: r.description }, /* @__PURE__ */ e.createElement(a, { i18nKey: "LinkAccountWithOneTimeCodePage.pickProgression.desc" }, "Select the game progression you'd like to migrate from your ", { platformName: o }, " account to your ", { publisherName: c }, " ", "account."))), /* @__PURE__ */ e.createElement("hr", null)), s === "shouldRestart" && /* @__PURE__ */ e.createElement(H, null), !!i && /* @__PURE__ */ e.createElement(
      P,
      {
        type: i.type,
        onDismiss: this.dismissAlert,
        className: S("mb-6 text-left", s === "shouldRestart" && "mt-5")
      },
      i.element
    ), s === "linkAccount" && this.renderLinkAccountStep(), s === "pickProgression" && n && /* @__PURE__ */ e.createElement(
      U,
      {
        platformName: o,
        oneTimeLinkCode: l,
        currentAccount: n == null ? void 0 : n.currentAccount,
        headlessAccount: n == null ? void 0 : n.headlessAccount,
        onSuccess: this.onLinkWithProgressionSuccess,
        onError: this.setErrorAlert
      }
    ), s === "success" && /* @__PURE__ */ e.createElement(I, { isUpgradeToFull: !1, platformName: o }), s === "shouldRestart" && /* @__PURE__ */ e.createElement(F, { onRestart: t }));
  }
}
export {
  ae as LinkToExistingAccount
};
