import * as e from "react";
import d from "classnames";
import { Trans as o } from "react-i18next";
import { Button as h, ButtonStyle as k } from "./accelbyte-widgets107.mjs";
import { Radio as g } from "./accelbyte-widgets538.mjs";
import { WarningIcon as E } from "./accelbyte-widgets402.mjs";
import { t as p } from "./accelbyte-widgets58.mjs";
import { AccPath as C } from "./accelbyte-widgets34.mjs";
import { Ctx as N } from "./accelbyte-widgets33.mjs";
import i from "./accelbyte-widgets182.mjs";
import { LinkAccountDialog as y } from "./accelbyte-widgets610.mjs";
import { Iam as P, IamUserAuthorizationClient as S } from "@accelbyte/sdk-iam";
class u extends e.Component {
  render() {
    const { platformName: c, userName: a, email: t } = this.props;
    return /* @__PURE__ */ e.createElement("div", { className: i.accountDetails }, /* @__PURE__ */ e.createElement("p", { className: i.accountDetailsPlatformName }, /* @__PURE__ */ e.createElement(o, { i18nKey: "LinkAccountWithOneTimeCodePage.pickProgression.accountDetails.name" }, { platformName: c }, " Account")), /* @__PURE__ */ e.createElement("p", { className: "text-2sm" }, a), /* @__PURE__ */ e.createElement("p", { className: d(i.accountDetailsEmail, !t && "italic") }, t || /* @__PURE__ */ e.createElement(o, { i18nKey: "LinkAccountWithOneTimeCodePage.pickProgression.accountDetails.noEmail" }, "no email registered")));
  }
}
class v extends e.Component {
  constructor(c) {
    super(c), this.toggleConfirmDialog = () => {
      this.setState((a) => ({ isConfirmDialogOpen: !a.isConfirmDialogOpen }));
    }, this.chooseGame = (a) => {
      this.setState((t) => {
        const { chosenGames: n } = t;
        return n.set(a, !0), { chosenGames: n };
      });
    }, this.unchooseGame = (a) => {
      this.setState((t) => {
        const { chosenGames: n } = t;
        return n.set(a, !1), { chosenGames: n };
      });
    }, this.onConfirm = async () => {
      const a = P.UsersApi(this.appState.getSdk()), { isConfirming: t, chosenGames: n } = this.state, { oneTimeLinkCode: r, onError: s, onSuccess: m } = this.props;
      try {
        if (t)
          return;
        this.setState({ isConfirming: !0 }), await a.createUserMeHeadlesLinkWithProgression({
          oneTimeLinkCode: r,
          chosenNamespaces: [...n.keys()].filter((l) => n.get(l))
        }), this.setState({ isConfirmDialogOpen: !1, isConfirming: !1 }), m();
      } catch (l) {
        this.setState({ isConfirming: !1 }), console.error(l), s(l);
      }
    }, this.onLinkWithAnotherAccount = () => {
      const { createLoginURL: a } = new S(this.appState.getSdk()), { oneTimeLinkCode: t } = this.props, n = C.LinkAccountWithOneTimeCode.makePath(void 0, { oneTimeLinkCode: t }), r = a(n, void 0, t);
      window.location.replace(r);
    }, this.renderAccountDetailsSection = () => {
      const { platformName: a, headlessAccount: t, currentAccount: n } = this.props;
      return /* @__PURE__ */ e.createElement(e.Fragment, null, /* @__PURE__ */ e.createElement("p", { className: i.sectionTitle }, /* @__PURE__ */ e.createElement(o, { i18nKey: "LinkAccountWithOneTimeCodePage.pickProgression.accountDetails.title" }, "Account Details")), /* @__PURE__ */ e.createElement("div", { className: "xs:grid-cols-2 mb-6 grid gap-6" }, /* @__PURE__ */ e.createElement(u, { platformName: a, userName: (t == null ? void 0 : t.userName) || (t == null ? void 0 : t.displayName) || "" }), /* @__PURE__ */ e.createElement(
        u,
        {
          platformName: this.appState.getBrandName(),
          userName: (n == null ? void 0 : n.userName) || (n == null ? void 0 : n.displayName) || "",
          email: (n == null ? void 0 : n.email) || ""
        }
      )), /* @__PURE__ */ e.createElement("hr", null));
    }, this.renderGamesTable = () => {
      const { chosenGames: a } = this.state, { currentAccount: t, headlessAccount: n } = this.props, r = ((n == null ? void 0 : n.linkedGames) || []).concat((t == null ? void 0 : t.linkedGames) || []);
      return /* @__PURE__ */ e.createElement("div", { className: i.progressionTable }, /* @__PURE__ */ e.createElement("table", null, /* @__PURE__ */ e.createElement("thead", null, /* @__PURE__ */ e.createElement("tr", null, /* @__PURE__ */ e.createElement("th", null, /* @__PURE__ */ e.createElement(o, { i18nKey: "LinkAccountWithOneTimeCodePage.pickProgression.table.game" }, "Game")), /* @__PURE__ */ e.createElement("th", null, /* @__PURE__ */ e.createElement(o, { i18nKey: "LinkAccountWithOneTimeCodePage.pickProgression.table.migrate" }, "Migrate to Full Account?")))), /* @__PURE__ */ e.createElement("tbody", null, [...new Set(r)].map((s) => {
        const m = a.get(s);
        return /* @__PURE__ */ e.createElement("tr", { key: s }, /* @__PURE__ */ e.createElement("td", null, s), /* @__PURE__ */ e.createElement("td", { className: "flex items-center" }, /* @__PURE__ */ e.createElement(g, { label: p("common.yes"), className: "mr-6", checked: m, onClick: () => this.chooseGame(s) }), /* @__PURE__ */ e.createElement(g, { label: p("common.no"), checked: m === !1, onClick: () => this.unchooseGame(s) })));
      }))));
    }, this.renderGamesSection = () => {
      const { platformName: a } = this.props, t = this.appState.getBrandName();
      return /* @__PURE__ */ e.createElement(e.Fragment, null, /* @__PURE__ */ e.createElement("p", { className: i.sectionTitle }, /* @__PURE__ */ e.createElement(o, { i18nKey: "LinkAccountWithOneTimeCodePage.pickProgression.games.title" }, { platformName: a }, " Games")), /* @__PURE__ */ e.createElement("p", { className: d(i.description, "!mt-1 mb-6") }, /* @__PURE__ */ e.createElement(o, { i18nKey: "LinkAccountWithOneTimeCodePage.pickProgression.games.desc" }, "You have game progression data in your ", { platformName: a }, " account for the following games. For each game, please select whether or not you'd like to migrate your progress to your ", { publisherName: t }, " account.")), /* @__PURE__ */ e.createElement("div", { className: i.warningTip }, /* @__PURE__ */ e.createElement(E, { defaultColor: !1, className: i.warningIcon }), /* @__PURE__ */ e.createElement("p", null, /* @__PURE__ */ e.createElement(o, { i18nKey: "LinkAccountWithOneTimeCodePage.pickProgression.games.warning" }, "If you choose to migrate your progress, your ", { publisherName: t }, " account progress will be replaced with your", " ", { platformName: a }, " progress for that game."))), this.renderGamesTable(), /* @__PURE__ */ e.createElement("hr", null));
    }, this.renderConfirmButton = () => {
      const { chosenGames: a } = this.state, { currentAccount: t, headlessAccount: n } = this.props, r = [...new Set(((n == null ? void 0 : n.linkedGames) || []).concat((t == null ? void 0 : t.linkedGames) || []))], s = this.appState.getBrandName(), m = a.size === r.length;
      return /* @__PURE__ */ e.createElement("div", { className: "text-center" }, /* @__PURE__ */ e.createElement(
        h,
        {
          loadingIndicator: "SPINNER",
          className: "btn btn-primary mx-auto mb-6 w-60 py-2",
          onClick: this.toggleConfirmDialog,
          disabled: !m
        },
        p("Common.confirm")
      ), /* @__PURE__ */ e.createElement("p", { className: i.description }, /* @__PURE__ */ e.createElement(o, { i18nKey: "LinkAccountWithOneTimeCodePage.pickProgression.linkAnother.question" }, "Don't want to link to this existing account?")), /* @__PURE__ */ e.createElement(h, { className: "text-2sm text-primary-60 mx-auto py-2", buttonStyle: k.NONE, onClick: this.onLinkWithAnotherAccount }, /* @__PURE__ */ e.createElement(o, { i18nKey: "LinkAccountWithOneTimeCodePage.pickProgression.linkAnother.action" }, "Link with another ", { publisherName: s }, " account")));
    }, this.state = {
      chosenGames: /* @__PURE__ */ new Map(),
      isConfirmDialogOpen: !1,
      isConfirming: !1
    }, this.appState = N.getAppState();
  }
  render() {
    const { isConfirmDialogOpen: c, isConfirming: a } = this.state;
    return /* @__PURE__ */ e.createElement(e.Fragment, null, /* @__PURE__ */ e.createElement("div", { className: i.pickProgression }, this.renderAccountDetailsSection(), this.renderGamesSection(), this.renderConfirmButton()), c && /* @__PURE__ */ e.createElement(y, { isConfirming: a, onCancel: this.toggleConfirmDialog, onConfirm: this.onConfirm }));
  }
}
export {
  v as PickProgression
};
