import { LinkedPlatformId as n } from "./accelbyte-widgets239.mjs";
import { Iam as i } from "@accelbyte/sdk-iam";
import { Container as y } from "unstated";
import { AppName as k } from "./accelbyte-widgets78.mjs";
import { FetchConfigsUtils as w } from "./accelbyte-widgets241.mjs";
import { Env as L } from "./accelbyte-widgets108.mjs";
import { Ctx as r } from "./accelbyte-widgets33.mjs";
import { Platform as m } from "@accelbyte/sdk-platform";
const u = {
  linkedTwitchAccount: null,
  isFetchingLinkedTwitchAccount: !1,
  isFetchingLoginURL: !1,
  isFetchingToken: !1,
  configs: {},
  isFetchingConfigs: !1,
  isConfigsFetched: !1
};
class R extends y {
  constructor(p) {
    super(), this.sdk = p, this.fetchLinkedTwitchAccount = async (e) => {
      this.setState({ isFetchingLinkedTwitchAccount: !0 });
      try {
        const a = (await i.UsersApi(this.sdk).getPlatforms_ByUserId(e)).data.find((s) => s.platformId === n.enum.twitch);
        return this.setState({ linkedTwitchAccount: a }), a;
      } catch (t) {
        throw console.error(t), t;
      } finally {
        this.setState({ isFetchingLinkedTwitchAccount: !1 });
      }
    }, this.getTwitchURL = async (e, t, a) => {
      if (this.state.isFetchingLoginURL)
        return null;
      this.setState({ isFetchingLoginURL: !0 });
      try {
        return (await i.UsersApi(this.sdk, { namespace: a }).getWebLinkMeUsers_ByPlatformId(n.enum.twitch, {
          redirectUri: e,
          clientId: t
        })).third_party_url;
      } catch (s) {
        return console.error(s), null;
      } finally {
        this.setState({ isFetchingLoginURL: !1 });
      }
    }, this.getTwitchPlatformToken = async (e) => {
      this.setState({ isFetchingToken: !0 });
      try {
        return await i.OAuth20Api(this.sdk).getPlatformTokenOauth_ByUserId_ByPlatformId(e, n.enum.twitch);
      } catch (t) {
        throw console.error(t), t;
      } finally {
        this.setState({ isFetchingToken: !1 });
      }
    }, this.claim = async (e, t) => await m.IapApi(this.sdk, { namespace: e }).updateUserMeIapTwitchSync({ gameId: t }), this.createUserPlatform = async (e) => {
      await i.UsersApi(this.sdk).createUserMePlatformJustice_ByTargetNamespace(e);
    }, this.fetchConfigs = async (e) => {
      try {
        this.setState({ isFetchingConfigs: !0 });
        const t = await w.fetchConfigs({
          sdk: r.getAppState().getSdk(),
          appName: k.TWITCHDROP,
          discoveryData: r.getAppState().state.discovery,
          widgetOptions: e,
          useConfigService: !!L.CONF_SERVER
        });
        t && (this.setState({ configs: t }), this.setState({ isConfigsFetched: !0 }));
      } catch (t) {
        console.error(t);
      } finally {
        this.setState({ isFetchingConfigs: !1 });
      }
    }, this.getBackgroundURL = (e = this.state.configs) => {
      var t;
      return (t = e.backgroundConfig) == null ? void 0 : t.data.background;
    }, this.getBackgroundOverlay = (e = this.state.configs) => {
      var t, a;
      return (a = (t = e.backgroundConfig) == null ? void 0 : t.data) == null ? void 0 : a.backgroundOverlay;
    }, this.getCompanyLogo = (e = this.state.configs) => {
      var s, o, c, h, g, d, l, f;
      const a = ((s = e.logoVariant) == null ? void 0 : s.data.header) ?? ((h = (c = (o = r.getAppState().state.discovery) == null ? void 0 : o.companyLogo) == null ? void 0 : c.data) == null ? void 0 : h.defaultIndex);
      return ((f = (l = (d = (g = r.getAppState().state.discovery) == null ? void 0 : g.companyLogo) == null ? void 0 : d.data) == null ? void 0 : l.images[a]) == null ? void 0 : f.url) || "";
    }, this.getGameOptions = (e = this.state.configs) => {
      var a;
      return (((a = e.gameList) == null ? void 0 : a.data.games) || []).map((s) => ({ value: s.gameId, label: s.gameName, namespace: s.namespace, clientId: s.clientId }));
    }, this.getGamesData = (e = this.state.configs) => {
      var a;
      return (((a = e.gameList) == null ? void 0 : a.data.games) || []).map((s) => ({ value: s.gameId, label: s.gameName, namespace: s.namespace, clientId: s.clientId }));
    }, this.getDropItemData = async (e, t) => {
      try {
        return await m.ItemApi(this.sdk, { namespace: t }).getItemsBySku({ sku: e });
      } catch {
        return {};
      }
    }, this.getStreamInfoData = (e = this.state.configs) => {
      var t;
      return (t = e.streamerInfo) == null ? void 0 : t.data;
    }, this.state = u;
  }
  clear() {
    this.setState(u);
  }
}
export {
  R as LinkTwitchAppManager
};
