import { Alert as u } from "./accelbyte-widgets123.mjs";
import { Button as p, ButtonType as f, ButtonStyle as g } from "./accelbyte-widgets107.mjs";
import { CommonError as h } from "./accelbyte-widgets124.mjs";
import { Dialog as y, DialogBody as E, DialogFooter as k } from "./accelbyte-widgets235.mjs";
import { WarningIcon as P } from "./accelbyte-widgets402.mjs";
import { Ctx as c } from "./accelbyte-widgets33.mjs";
import { AlertType as L } from "./accelbyte-widgets128.mjs";
import { LauncherHelper as A } from "./accelbyte-widgets424.mjs";
import { UrlUtils as N } from "./accelbyte-widgets90.mjs";
import { UserHelper as T } from "./accelbyte-widgets277.mjs";
import w from "classnames";
import { DesktopChecker as C } from "@accelbyte/sdk";
import e from "react";
import { Trans as i } from "react-i18next";
import m from "./accelbyte-widgets468.mjs";
import { Iam as I } from "@accelbyte/sdk-iam";
import { LinkedPlatformId as U } from "./accelbyte-widgets239.mjs";
import d from "js-cookie";
class G extends e.Component {
  constructor(o) {
    super(o), this.redirectToThirdPartyLogin = (t) => {
      if (C.isDesktopApp()) {
        A.openExternalBrowser(t);
        return;
      }
      window.location.href = t;
    }, this.loginPlatform = async (t) => {
      var l, r;
      const s = await N.getRedirectUrl(
        c.getAppState().sdkOptions.redirectURI,
        (r = (l = c.getAppState().state.appHistory) == null ? void 0 : l.location) == null ? void 0 : r.pathname
      ), a = c.getAppState().sdkOptions.clientId;
      try {
        this.setState({
          fetchThirdPartyLoginUrlError: null,
          isFetching: !0
        });
        const n = await I.UsersApi(this.sdk).getWebLinkMeUsers_ByPlatformId(t.PlatformId, {
          clientId: a,
          redirectUri: s
        });
        this.setState({ isFetched: !0 }), t.PlatformId === U.enum.oculusweb && (d.set("state", n.state, { expires: 1 }), d.set("platform_id", t.PlatformId, { expires: 1 })), this.redirectToThirdPartyLogin(n.third_party_url);
      } catch (n) {
        console.error(n), this.setState({ fetchThirdPartyLoginUrlError: n });
      } finally {
        this.setState({ isFetching: !1 });
      }
    }, this.state = {
      fetchThirdPartyLoginUrlError: null,
      isFetching: !1,
      isFetched: !1
    }, this.sdk = c.getAppState().getSdk();
  }
  renderError() {
    const { fetchThirdPartyLoginUrlError: o } = this.state;
    if (o)
      return /* @__PURE__ */ e.createElement(u, { className: "my-6", type: L.danger }, /* @__PURE__ */ e.createElement(h, { error: o }));
  }
  render() {
    const { onCancel: o, platformConfig: t } = this.props, { isFetching: s, isFetched: a } = this.state, { PlatformId: l } = t, r = T.getPlatformName(l);
    return /* @__PURE__ */ e.createElement(
      y,
      {
        className: m.linkAccountConfirmDialog,
        title: /* @__PURE__ */ e.createElement(i, { i18nKey: "LinkedAccountPage.confirmLinkDialog.title" }, "Linking to your ", /* @__PURE__ */ e.createElement("span", { className: "capitalize" }, { platformName: r }), " Account"),
        isOpen: !0
      },
      /* @__PURE__ */ e.createElement(E, null, this.renderError(), /* @__PURE__ */ e.createElement("p", { className: "dialogPlatformBodyText" }, a ? /* @__PURE__ */ e.createElement(i, { i18nKey: "LinkedAccountPage.confirmLinkDialog.redirecting" }, "Redirecting to the ", /* @__PURE__ */ e.createElement("span", { className: "capitalize" }, { platformName: r }), " login page. Please open your browser to finish the process.") : /* @__PURE__ */ e.createElement(i, { i18nKey: "LinkedAccountPage.confirmLinkDialog.linkConfirm" }, "Link to ", /* @__PURE__ */ e.createElement("span", { className: "capitalize" }, { platformName: r }), " Account will allow you to share your profile, game progression, and earned game content with your current account.", /* @__PURE__ */ e.createElement("div", { className: w("my-4 flex", m.textWarning) }, /* @__PURE__ */ e.createElement("div", { className: "mr-3" }, /* @__PURE__ */ e.createElement(P, { defaultColor: !1, className: m.warningIcon })), /* @__PURE__ */ e.createElement("div", null, "Once you have linked your ", /* @__PURE__ */ e.createElement("span", { className: "capitalize" }, { platformName: r }), " to your current account, you will not be able to link a different ", /* @__PURE__ */ e.createElement("span", null, { platformName: r }), " account to it")), "You will be redirected to the ", { platformName: r }, " login page to finish the link account process."))),
      /* @__PURE__ */ e.createElement(k, null, /* @__PURE__ */ e.createElement("div", { className: "flex justify-end" }, /* @__PURE__ */ e.createElement(p, { className: "btn btn-ghost mr-2 uppercase", buttonType: f.TEXT, buttonStyle: g.NONE, onClick: o }, /* @__PURE__ */ e.createElement(i, { i18nKey: "LinkedAccountPage.linkAccount.modal.action.cancel" }, "Cancel")), !a && /* @__PURE__ */ e.createElement(
        p,
        {
          className: "btn btn-primary uppercase",
          isLoading: s,
          onClick: () => {
            this.loginPlatform(t);
          }
        },
        /* @__PURE__ */ e.createElement(i, { i18nKey: "LinkedAccountPage.linkAccount.modal.action.continue" }, "Continue")
      )))
    );
  }
}
export {
  G as LinkAccountConfirmDialog
};
