import m, { useState as c, useEffect as z } from "react";
import { ReclaimPanel as J } from "./accelbyte-widgets512.mjs";
import { Ctx as Q } from "./accelbyte-widgets33.mjs";
import X from "axios";
import { ClaimErrorPanel as Y } from "./accelbyte-widgets513.mjs";
import { ClaimSuccessPanel as Z } from "./accelbyte-widgets514.mjs";
import { LoadingPanel as U } from "./accelbyte-widgets515.mjs";
import { WrongAccountPanel as $ } from "./accelbyte-widgets516.mjs";
import { TwitchDropValidateStatus as w, TwitchErrorCode as O } from "./accelbyte-widgets317.mjs";
import { LinkedPlatformId as ee } from "./accelbyte-widgets239.mjs";
const d = "AB-Twitch_drops-claim-game_id", f = "AB-Twitch_drops-claim-game_namespace", p = "AB-Twitch_drops-claim-game_client_id", C = "AB-Twitch_drops-claim-game_name", te = "twitch-drop-thumbnail";
function Se({ displayName: D, validateStatus: S, retryStatus: L, onLink: y, className: g }) {
  const [r, n] = c("HOME"), [M, u] = c(!0), [k, P] = c(!1), [A, v] = c(""), [R, H] = c(""), [T, K] = c(""), [_, V] = c(""), [N, G] = c([]), I = Q.getAppState();
  z(() => {
    const t = sessionStorage.getItem(d), a = sessionStorage.getItem(f), o = sessionStorage.getItem(p), i = sessionStorage.getItem(C);
    t && a && i && o && (v(t), H(a), V(i), K(o), sessionStorage.removeItem(d), sessionStorage.removeItem(f), sessionStorage.removeItem(C), sessionStorage.removeItem(p), S && n("VALIDATE")), r === "VALIDATE" && (S === w.SUCCESS ? n("CLAIM") : S === w.FAILED && n("INCORRECT_TWITCH_ACCOUNT")), L && !k && S === w.SUCCESS && h(R, A, T, _, !0);
  }, [r, S, N, L]);
  const x = async () => {
    await I.linkAccountManager.unLinkAccount(ee.Enum.twitch), window.location.reload();
  }, h = async (t, a = A, o, i, l) => {
    const e = I.getUser();
    if (e != null && e.userId)
      try {
        sessionStorage.setItem(d, a), sessionStorage.setItem(f, t), sessionStorage.setItem(p, o), sessionStorage.setItem(C, i), l && P(!0);
        const s = await I.linkTwitchManager.claim(t, a);
        s.length === 0 && (u(!1), n("HOME")), s.length > 0 && (await F(s, t), u(!0), n("SUCCESS"));
      } catch (s) {
        u(!0), l ? n("ERROR") : B(s, a, t, o, i);
      }
  }, B = async (t, a, o, i, l) => {
    var e, s;
    if (t instanceof X.AxiosError) {
      const E = (s = (e = t == null ? void 0 : t.response) == null ? void 0 : e.data) == null ? void 0 : s.errorCode;
      if (E === O.INVALID_PLATFORM_TOKEN && r !== "CLAIM")
        sessionStorage.setItem(d, a), sessionStorage.setItem(f, o), sessionStorage.setItem(p, i), sessionStorage.setItem(C, l), y(!0, o, i);
      else if ([O.ERROR_USER_ID_NOT_LINKED, O.ERROR_USER_ID_NOT_LINKED_ALTERNATIVE].includes(E))
        try {
          await I.linkTwitchManager.createUserPlatform(o), await h(o, a, i, l);
        } catch {
          n("ERROR");
        }
      else
        n("ERROR");
    } else
      n("ERROR");
  }, F = async (t, a) => {
    const o = t.filter((e) => !!e.itemSku).map((e) => e.itemSku ? I.linkTwitchManager.getDropItemData(e.itemSku, a) : {}), l = (await Promise.all(o)).map((e) => {
      var E;
      let s = null;
      return e.images && (s = ((E = e.images.find((q) => q.as === te)) == null ? void 0 : E.imageUrl) || e.images[0].imageUrl), {
        name: e.name || "",
        imageUrl: s
      };
    }).sort((e, s) => e.name < s.name ? -1 : e.name > s.name ? 1 : 0);
    G(l);
  }, W = () => {
    sessionStorage.setItem(d, A), sessionStorage.setItem(f, R), sessionStorage.setItem(p, T), sessionStorage.setItem(C, _), y(!0, R, T);
  }, b = () => {
    n("HOME");
  };
  return /* @__PURE__ */ m.createElement(m.Fragment, null, r === "HOME" && /* @__PURE__ */ m.createElement(
    J,
    {
      displayName: D ?? "",
      onUnlink: x,
      onClaim: h,
      className: g,
      hasDropEntitlements: M
    }
  ), r === "VALIDATE" && /* @__PURE__ */ m.createElement(U, { message: "Validating user account...", className: g }), r === "CLAIM" && /* @__PURE__ */ m.createElement(U, { message: "Claiming your drops...", className: g }), r === "SUCCESS" && /* @__PURE__ */ m.createElement(
    Z,
    {
      namespace: R,
      gameName: _,
      itemList: N,
      className: g
    }
  ), r === "ERROR" && /* @__PURE__ */ m.createElement(Y, { onRetry: b, className: g }), r === "INCORRECT_TWITCH_ACCOUNT" && /* @__PURE__ */ m.createElement($, { displayName: D ?? "", onRelogin: W, className: g }));
}
export {
  Se as TwitchDropReclaimPage
};
