import { e as U, f as m, F as o, u as j, b as x, W as c, E as O } from "./FetchConfigsUtils-476a602b.mjs";
import { useCallback as u } from "react";
import { IamUserAuthorizationClient as P, Iam as d, IamOAuthClient as F, TokenResponseV3 as W } from "@accelbyte/sdk-iam";
import { Basic as y } from "@accelbyte/sdk-basic";
const _ = {
  location: {
    state: {},
    key: "",
    pathname: "",
    hash: "",
    search: ""
  },
  push: () => null,
  replace: () => null,
  block: () => ({
    state: "unblocked",
    reset: void 0,
    proceed: void 0,
    location: void 0
  })
}, z = U(_);
function B() {
  const [t, f] = m(z), s = u((h) => {
    f(h);
  }, []), E = u((h) => {
    f({ location: h });
  }, []);
  return {
    state: t,
    mutations: {
      initHistory: s,
      setLocation: E
    }
  };
}
const S = {
  user: null,
  userFetchError: null,
  userUpdateError: null,
  userFetchStatus: o.IDLE,
  userProfile: null,
  userProfileError: null,
  userProfileFetchStatus: o.IDLE,
  mfaData: null,
  authenticatorKey: null,
  authenticatorKeyFetchStatus: o.IDLE,
  isLinkingAccountRequired2FA: !1
}, $ = U(S);
function Z() {
  const [t] = j(), [f, s] = m($), { state: E } = B(), { state: h } = x(), D = u(() => {
    t && s({ mfaData: P.getMfaDataFromStorage() });
  }, [t]), A = u(() => {
    s(S);
  }, []), g = u(async () => {
    let e = null, r = null;
    try {
      return s({ userFetchError: null, userFetchStatus: o.FETCHING }), e = await d.UsersApi(t).getUsersMe(), { data: e };
    } catch (a) {
      return r = c.getErrorType(a), { error: r };
    } finally {
      s({ user: e, userFetchError: r, userFetchStatus: o.IDLE });
    }
  }, [t]), T = u(
    async (e = !1) => {
      var n, l;
      let r = null, a = null;
      try {
        s({ userProfileFetchStatus: o.FETCHING, userProfileError: null });
        const i = await I(t);
        if (i.data && (r = i.data), i.error) {
          if (!e)
            throw i.error;
          if (e && i.error && O.isAxiosError(i.error) && Number((l = (n = i.error.response) == null ? void 0 : n.data) == null ? void 0 : l.errorCode) === 11440) {
            const p = await w(t);
            if (p.error)
              throw p.error;
            r = p.data;
          }
        }
        return { data: r };
      } catch (i) {
        return a = c.getErrorType(i), { error: a };
      } finally {
        s({ userProfileError: a, userProfile: r, userProfileFetchStatus: o.IDLE });
      }
    },
    [t]
  ), I = async (e) => {
    try {
      return { data: await y.UserProfileApi(e).getUsersMeProfiles() };
    } catch (r) {
      return { error: c.getErrorType(r) };
    }
  }, w = async (e) => {
    try {
      return { data: await y.UserProfileApi(e).createUserMeProfile({}) };
    } catch (r) {
      return { error: c.getErrorType(r) };
    }
  }, k = u(
    async (e) => {
      let r = null, a = null;
      if (Object.keys(e).length !== 0)
        try {
          return s({
            userFetchStatus: o.UPDATING,
            userUpdateError: null
          }), r = await d.UsersApi(t).updateUserMe(e), s({ user: r, userFetchStatus: o.IDLE }), { data: r };
        } catch (n) {
          return a = c.getErrorType(n), s({ userUpdateError: a, userFetchStatus: o.IDLE }), { error: a };
        }
    },
    [t]
  ), L = u(
    async (e) => {
      let r = null, a = null;
      if (Object.keys(e).length !== 0)
        try {
          return s({
            userProfileFetchStatus: o.UPDATING,
            userProfileError: null
          }), r = await y.UserProfileApi(t).updateUserMeProfile(e), { data: r };
        } catch (n) {
          return a = c.getErrorType(n), { error: a };
        } finally {
          s({ userProfileError: a, userProfile: r, userProfileFetchStatus: o.IDLE });
        }
    },
    [t]
  ), M = u(
    async (e, r) => {
      let a = null, n;
      try {
        return s({
          userFetchStatus: o.UPDATING,
          userUpdateError: null
        }), await d.UsersV4Api(t).updateUserMeEmail({ code: e, emailAddress: r }), n = r, { data: null };
      } catch (l) {
        return a = c.getErrorType(l), { error: a };
      } finally {
        s((l) => ({
          ...l,
          userUpdateError: a,
          user: l.user ? {
            ...l.user,
            emailAddress: n || l.user.emailAddress
          } : l.user,
          userFetchStatus: o.IDLE
        }));
      }
    },
    [t]
  ), R = u(
    async (e, r, a) => {
      let n = null;
      try {
        return s({ userUpdateError: null, userFetchStatus: o.UPDATING }), await d.UsersApi(t).updateUserMePassword({
          oldPassword: e,
          newPassword: r,
          languageTag: a
        }), { data: null };
      } catch (l) {
        return n = c.getErrorType(l), { error: n };
      } finally {
        s({ userUpdateError: n, userFetchStatus: o.IDLE });
      }
    },
    [t]
  ), N = u(
    async (e) => {
      let r = null, a = null;
      try {
        return s({ userProfileError: null, userProfileFetchStatus: o.UPDATING }), r = await y.UserProfileApi(t).updateUserMeProfile({ avatarUrl: e }), { data: r };
      } catch (n) {
        return a = c.getErrorType(n), { error: n };
      } finally {
        s({ userProfile: r, userProfileError: a, userProfileFetchStatus: o.IDLE });
      }
    },
    [t]
  ), C = u(
    (e) => {
      s({ mfaData: e || P.getMfaDataFromStorage() });
    },
    [t]
  ), G = u(() => {
    s({ mfaData: void 0 });
  }, []), b = u(async (e) => {
    s({ mfaData: e, isLinkingAccountRequired2FA: !0 }), E.replace(h.home.link);
  }, []), v = u(() => {
    s({ isLinkingAccountRequired2FA: !1 });
  }, []), K = u(
    async (e) => {
      if (f.mfaData)
        try {
          return { data: (await new F(t).verify2FA({ ...e, mfaToken: f.mfaData.mfaToken })).data };
        } catch (r) {
          return { error: c.getErrorType(r) };
        }
    },
    [t, f.mfaData, f.isLinkingAccountRequired2FA]
  ), q = u(async () => {
    await new F(t).logout();
  }, [t]), H = u(async () => {
    const e = await new P(t).refreshToken();
    if (!e)
      return A(), null;
    const r = W.safeParse(e);
    return r.success ? r.data : null;
  }, [t]), V = u(async () => {
    let e = null;
    try {
      return s({ authenticatorKeyFetchStatus: o.CREATING }), e = await d.UsersV4Api(t).createUserMeMfaAuthenticatorKey(), { data: e };
    } catch (r) {
      return { error: r };
    } finally {
      s({ authenticatorKey: e, authenticatorKeyFetchStatus: o.IDLE });
    }
  }, [t]);
  return {
    state: f,
    mutations: {
      resetState: A,
      fetchCurrentUserInformation: g,
      fetchUserProfile: T,
      updateUser: k,
      updateUserProfile: L,
      updateEmail: M,
      updatePassword: R,
      uploadUserProfileImage: N,
      logout: q,
      updateMFAData: C,
      removeMFAData: G,
      verify2FA: K,
      linkAccountRequired2FA: b,
      setDefaultMfaData: D,
      refreshToken: H,
      generateAuthenticatorSecretKey: V,
      clearLinkAccountRequired2FA: v
    }
  };
}
export {
  B as a,
  Z as u
};
