import { LinkedAccountsErrorAlert as N } from "./accelbyte-widgets270.mjs";
import { Alert as k } from "./accelbyte-widgets123.mjs";
import { Button as g, ButtonType as U, ButtonStyle as O } from "./accelbyte-widgets107.mjs";
import { PlusSignIcon as v } from "./accelbyte-widgets271.mjs";
import { Loading as I } from "./accelbyte-widgets136.mjs";
import { gaSendErrorEvent as P } from "./accelbyte-widgets145.mjs";
import { IpcChannels as C } from "./accelbyte-widgets148.mjs";
import "./accelbyte-widgets108.mjs";
import { Ctx as d } from "./accelbyte-widgets33.mjs";
import { ExperimentalFeatureFlags as T, FeatureFlags as M } from "./accelbyte-widgets57.mjs";
import { AlertType as A } from "./accelbyte-widgets128.mjs";
import { LinkedPlatformId as s, LinkingProgressStatus as y, LinkedAccountGroup as x } from "./accelbyte-widgets239.mjs";
import { ErrorHelper as w } from "./accelbyte-widgets120.mjs";
import { UrlUtils as _ } from "./accelbyte-widgets90.mjs";
import K from "classnames";
import { DesktopChecker as L } from "@accelbyte/sdk";
import e from "react";
import { Trans as p } from "react-i18next";
import { AccountMainSection as W, AccountMainSectionHeader as B, AccountMainSectionBody as F } from "./accelbyte-widgets122.mjs";
import { LinkAccountConfirmDialog as H } from "./accelbyte-widgets272.mjs";
import { LinkAccountDialog as R } from "./accelbyte-widgets273.mjs";
import "./accelbyte-widgets274.mjs";
import { getThirdPartyPlatformIcon as G } from "./accelbyte-widgets275.mjs";
import { UnlinkAccountConfirmDialog as q } from "./accelbyte-widgets276.mjs";
import { mockUserLinkedPlatform as Y } from "./accelbyte-widgets209.mjs";
import { Link as j } from "react-router-dom";
import { TwitchPath as V } from "./accelbyte-widgets56.mjs";
import l from "js-cookie";
import { UserHelper as z } from "./accelbyte-widgets277.mjs";
import { Dialog as X, DialogBody as J, DialogFooter as Q } from "./accelbyte-widgets235.mjs";
import { t as h } from "./accelbyte-widgets58.mjs";
class Oe extends e.Component {
  constructor(n) {
    var m;
    super(n), this.getUserDistinctLinkedPlatform = async () => {
      this.userId && await this.linkedAccountsManager.getUserDistinctLinkedPlatform(this.userId);
    }, this.refreshUI = () => {
      this.setState({});
    }, this.getOculusWebCode = () => L.isDesktopApp() ? l.get("oculusweb_code") : window.location.hash.substring(1), this.removeOculusWebCookie = () => {
      l.remove("platform_id"), l.remove("state"), l.remove("oculusweb_code");
    }, this.linkIfGivenParameter = async (t) => {
      const c = t ? new URL(t).search : window.location.search, i = _.silentSearchParamsBuilder(c).get("requestId"), { appHistory: r } = d.getAppState().state, a = l.get("platform_id") === s.enum.oculusweb && !!l.get("state");
      if (this.setState({ isOpenLinkAccountDialog: !1, selectedPlatformToLink: null }), a && this.getOculusWebCode()) {
        this.setState({ isOpenOculuswebLinkConfirmationDialog: !0 });
        return;
      }
      if (i && this.userId) {
        this.clearAlertStatus();
        try {
          const o = await this.linkedAccountsManager.requestStatus(i);
          o && o.status === y.enum.SUCCESS && (this.setState({ accountLinkingSuccessful: !0 }), this.appState.updateUserCustomAttribute(this.userId, o.payload.platformId));
        } catch (o) {
          this.setState({ accountLinkError: o });
        }
      }
      await this.getUserDistinctLinkedPlatform(), r.replace(r.location.pathname);
    }, this.clearAlertStatus = () => {
      this.linkedAccountsManager.setState({ error: null, accountConflictInformation: null }), this.setState({
        accountUnlinkingSuccessful: !1,
        accountLinkingSuccessful: !1,
        accountUnlinkError: null,
        accountLinkError: null
      });
    }, this.openLinkAccountDialog = () => {
      this.setState({ isOpenLinkAccountDialog: !0 });
    }, this.openConfirmLinkDialog = (t) => {
      this.setState({ selectedPlatformToLink: t });
    }, this.openConfirmUnlinkDialog = (t) => {
      this.setState({ selectedUnlinkAblePlatformName: t });
    }, this.onConfirmUnlinkAccount = async (t) => {
      this.clearAlertStatus();
      try {
        await this.linkedAccountsManager.unLinkAccount(t), this.setState({ accountUnlinkingSuccessful: !0 });
      } catch (c) {
        this.setState({ accountLinkError: c });
      } finally {
        this.setState({ selectedUnlinkAblePlatformName: null }), await this.getUserDistinctLinkedPlatform();
      }
    }, this.onConfirmOculusWebLinking = async () => {
      const t = this.getOculusWebCode(), c = l.get("platform_id") || "", i = { state: l.get("state") || "", code: t }, { appHistory: r } = d.getAppState().state;
      if (t && this.userId) {
        this.clearAlertStatus();
        try {
          const a = await this.linkedAccountsManager.linkThirdPartyAccountByWeblink(c, i);
          a && a.status === y.enum.SUCCESS && (this.setState({ accountLinkingSuccessful: !0 }), this.appState.updateUserCustomAttribute(this.userId, a.payload.platformId));
        } catch (a) {
          this.setState({ accountLinkError: a });
        } finally {
          this.removeOculusWebCookie(), this.setState({ isOpenOculuswebLinkConfirmationDialog: !1 });
        }
      }
      await this.getUserDistinctLinkedPlatform(), window.location.hash && r.replace(r.location.pathname);
    }, this.renderListPlatform = (t) => {
      const { isPreviewMode: c } = this.props, { platformName: i, details: r } = t, a = r[0].displayName || "", { isLinking: o } = this.linkedAccountsManager.state, f = this.appState.featureFlagsManager.hasFeature(T.isUnlinkAccountEnabled);
      return i ? /* @__PURE__ */ e.createElement("tr", { key: i }, /* @__PURE__ */ e.createElement("td", { className: "text-left font-medium capitalize" }, /* @__PURE__ */ e.createElement("span", { className: "platform flex items-center" }, G(i))), /* @__PURE__ */ e.createElement("td", { className: "text-left" }, !a && /* @__PURE__ */ e.createElement(p, { i18nKey: "LinkedAccountPage.connected" }, "Connected"), !!a && /* @__PURE__ */ e.createElement(p, { i18nKey: "LinkedAccountPage.connectedTo" }, "Connected to ", { displayName: a })), /* @__PURE__ */ e.createElement("td", { className: "text-right" }, f !== !1 && /* @__PURE__ */ e.createElement(
        "span",
        {
          className: K("action", o && "text-[color:rgb(var(--od-rgb-text),0.65)]"),
          onClick: c ? void 0 : () => {
            o || this.openConfirmUnlinkDialog(i);
          }
        },
        /* @__PURE__ */ e.createElement(p, { i18nKey: "LinkedAccountsPage.disconnect" }, "Unlink")
      ))) : null;
    }, this.renderAlerts = () => {
      const { accountUnlinkingSuccessful: t, accountLinkingSuccessful: c, accountLinkError: i, accountUnlinkError: r } = this.state, { error: a, accountConflictInformation: o } = this.linkedAccountsManager.state;
      return c ? /* @__PURE__ */ e.createElement(k, { className: "mb-6", type: A.success, onDismiss: () => this.clearAlertStatus() }, /* @__PURE__ */ e.createElement(p, { i18nKey: "LinkedAccountPage.linkSuccess" }, "Cool! Your account is now linked.")) : t ? /* @__PURE__ */ e.createElement(k, { className: "mb-6", type: A.info, onDismiss: () => this.clearAlertStatus() }, /* @__PURE__ */ e.createElement(p, { i18nKey: "LinkedAccountPage.unlinkSuccess" }, "Your account has been unlinked.")) : a || i || r || o ? (i && P({
        eventCategory: "LinkAccountError",
        eventLabel: w.errorToString(i)
      }), r && P({
        eventCategory: "UnlinkAccountError",
        eventLabel: w.errorToString(r)
      }), /* @__PURE__ */ e.createElement(
        N,
        {
          error: a || i || r,
          accountConflictInformation: o,
          onDismiss: this.clearAlertStatus
        }
      )) : null;
    }, this.linkedAccountsManager = d.getAppState().linkAccountManager, this.linkedAccountsManager.subscribe(this.refreshUI), this.appState = d.getAppState(), this.userId = (m = d.getAppState().getUser()) == null ? void 0 : m.userId, this.state = {
      isOpenLinkAccountDialog: !1,
      isOpenOculuswebLinkConfirmationDialog: !1,
      selectedUnlinkAblePlatformName: null,
      selectedPlatformToLink: null,
      accountUnlinkingSuccessful: !1,
      accountLinkingSuccessful: !1,
      accountUnlinkError: null,
      accountLinkError: null,
      platformNamespace: ""
    };
  }
  componentDidMount() {
    var m;
    if ((m = this.props.isPreviewMode) != null && m.isOpenModalLinked)
      return this.setState({
        isOpenLinkAccountDialog: !0
      });
    if (this.props.isPreviewMode)
      return;
    const n = l.get("platform_id") === s.enum.oculusweb && !!l.get("state");
    this.linkIfGivenParameter(), L.isDesktopApp() && Device.receive(C.OPEN_URL, (t) => {
      n && l.set("oculusweb_code", new URL(t).hash.substring(1) || ""), this.linkIfGivenParameter(t);
    });
  }
  componentDidUpdate(n) {
    var m, t;
    ((m = n.isPreviewMode) == null ? void 0 : m.isOpenModalLinked) !== ((t = this.props.isPreviewMode) == null ? void 0 : t.isOpenModalLinked) && this.setState({
      isOpenLinkAccountDialog: !this.state.isOpenLinkAccountDialog
    });
  }
  componentWillUnmount() {
    L.isDesktopApp() && Device.remove(C.OPEN_URL);
  }
  renderTwitchDropAlert() {
    const { linkedPlatform: n } = this.linkedAccountsManager.state;
    return (n == null ? void 0 : n.find((t) => t.platformName === s.Enum.twitch)) && d.hasFeature(M.isTwitchDropEnabled) ? /* @__PURE__ */ e.createElement(k, { className: "LinkedAccountsAlert twitch-alert mb-6", type: A.info }, /* @__PURE__ */ e.createElement(p, { i18nKey: "LinkedAccountPage.problemClaimingTwitchDrops" }, "Have problem with claiming Twitch Drops?", " ", /* @__PURE__ */ e.createElement(j, { to: V.twitchDropPage.withSearchParams({ step: "reclaim" }) }, "Click here to re-claim your Drops"))) : null;
  }
  render() {
    var E, b;
    const { isPreviewMode: n } = this.props, { isOpenLinkAccountDialog: m, selectedUnlinkAblePlatformName: t, selectedPlatformToLink: c, isOpenOculuswebLinkConfirmationDialog: i } = this.state, { linkedPlatform: r, isFetching: a, isLinking: o } = this.linkedAccountsManager.state, f = n ? Y : r, D = location.pathname.includes("/player-portal/accounts/linked-accounts-modal"), { appHistory: S } = d.getAppState().state;
    return /* @__PURE__ */ e.createElement(W, { className: "linkedAccountsPage" }, /* @__PURE__ */ e.createElement(B, null, /* @__PURE__ */ e.createElement(p, { i18nKey: "LinkedAccountsPage.title" }, "Linked Accounts")), /* @__PURE__ */ e.createElement(F, null, this.renderAlerts(), this.renderTwitchDropAlert(), a && /* @__PURE__ */ e.createElement("div", { className: "flex" }, /* @__PURE__ */ e.createElement(I, { className: "justify-center" })), f && /* @__PURE__ */ e.createElement("table", { className: "w-full" }, /* @__PURE__ */ e.createElement("thead", null, /* @__PURE__ */ e.createElement("tr", null, /* @__PURE__ */ e.createElement("th", { className: "text-left" }, /* @__PURE__ */ e.createElement(p, { i18nKey: "LinkedAccountsPage.paymentAccounts.platform" }, "Platform")), /* @__PURE__ */ e.createElement("th", { className: "text-left" }, /* @__PURE__ */ e.createElement(p, { i18nKey: "LinkedAccountsPage.paymentAccounts.status" }, "Status")), /* @__PURE__ */ e.createElement("th", null))), /* @__PURE__ */ e.createElement("tbody", { className: "border-gray-80 border-b border-solid" }, f.length >= 0 && f.map((u) => this.renderListPlatform(u)), f && f.length === 0 && /* @__PURE__ */ e.createElement("tr", null, /* @__PURE__ */ e.createElement("td", { colSpan: 3, className: "text-center" }, /* @__PURE__ */ e.createElement(p, { i18nKey: "LinkedAccountPage.noLinkedAccount" }, "You have no linked accounts."))))), /* @__PURE__ */ e.createElement(
      g,
      {
        loadingIndicator: "SPINNER",
        disabled: o || !f,
        className: "btn btn-primary connectButtonPlatform",
        isLoading: o,
        onClick: n ? void 0 : this.openLinkAccountDialog
      },
      /* @__PURE__ */ e.createElement(p, { i18nKey: "LinkedAccountPage.btn.LinkAccount" }, "Link Account"),
      /* @__PURE__ */ e.createElement("span", { className: "ml-2" }, /* @__PURE__ */ e.createElement(v, null))
    ), (m || D) && f && /* @__PURE__ */ e.createElement(
      R,
      {
        openConfirmLinkToPlatformDialog: n != null && n.isOpenModalLinked ? () => null : this.openConfirmLinkDialog,
        onClose: n != null && n.isOpenModalLinked ? () => null : () => this.setState({ isOpenLinkAccountDialog: !1 }),
        linkedPlatform: new Set(
          f.map((u) => {
            switch (!0) {
              case u.platformName === s.enum.steam:
                return s.enum.steamopenid;
              case u.platformName === x.enum.psn:
                return s.enum.ps4web;
              case u.platformName === s.enum.live:
              case u.platformName === s.enum.xblweb:
                return s.enum.xblweb;
              case u.platformName === s.enum.facebook:
                return s.enum.facebook;
              case u.platformName === s.enum.apple:
                return s.enum.apple;
              default:
                return u.platformName;
            }
          }).filter((u) => !!u)
        ),
        isPreviewMode: n == null ? void 0 : n.isOpenModalLinked
      }
    ), t && /* @__PURE__ */ e.createElement(
      q,
      {
        platformId: t,
        onCancel: () => this.setState({ selectedUnlinkAblePlatformName: null }),
        onConfirm: n ? void 0 : () => this.onConfirmUnlinkAccount(t)
      }
    ), c && /* @__PURE__ */ e.createElement(
      H,
      {
        platformConfig: c,
        onCancel: () => {
          this.setState({ selectedPlatformToLink: null }), this.removeOculusWebCookie();
        }
      }
    ), /* @__PURE__ */ e.createElement(
      X,
      {
        title: h("LinkedAccountsPage.linkConfirmation", { platformName: z.getPlatformName(l.get("platform_id") || "") }),
        isOpen: i,
        className: "w-4/12 min-w-[640px] max-w-[720px]"
      },
      /* @__PURE__ */ e.createElement(J, null, /* @__PURE__ */ e.createElement("p", { className: "dialogPlatformBodyText" }, h("LinkedAccountsPage.linkConfirmationSubtitle", {
        displayName: (E = d.getAppState().getUser()) == null ? void 0 : E.displayName,
        emailAddress: (b = d.getAppState().getUser()) == null ? void 0 : b.emailAddress
      }))),
      /* @__PURE__ */ e.createElement(Q, null, /* @__PURE__ */ e.createElement("div", { className: "flex justify-center" }, /* @__PURE__ */ e.createElement(
        g,
        {
          className: "btn btn-ghost mr-2 uppercase",
          buttonType: U.TEXT,
          buttonStyle: O.NONE,
          disabled: o,
          onClick: async () => {
            this.removeOculusWebCookie(), this.setState({ isOpenOculuswebLinkConfirmationDialog: !1 }), await this.getUserDistinctLinkedPlatform(), window.location.hash && S.replace(S.location.pathname);
          }
        },
        h("Common.cancel").toUpperCase()
      ), /* @__PURE__ */ e.createElement(
        g,
        {
          className: "btn btn-primary uppercase",
          isLoading: o,
          onClick: () => {
            this.onConfirmOculusWebLinking();
          }
        },
        h("Common.confirm").toUpperCase()
      )))
    )));
  }
}
export {
  Oe as default
};
