import { Env as g } from "./accelbyte-widgets108.mjs";
import { initialUserFetcher as M } from "./accelbyte-widgets117.mjs";
import { AppBuilderUtils as n } from "./accelbyte-widgets118.mjs";
import { ConfigKeys as c } from "./accelbyte-widgets78.mjs";
import { ConfigError as O, ConfigErrorCode as G } from "./accelbyte-widgets119.mjs";
import { AppState as _ } from "./accelbyte-widgets79.mjs";
import { ErrorHelper as v } from "./accelbyte-widgets120.mjs";
const e = class {
  constructor(a, t) {
    this.appStateImpl = new _({
      sdkOptions: a || {
        baseURL: g.IS_DEV_MODE ? `${window.location.origin}/api` : g.BASE_URL,
        clientId: g.CLIENT_ID,
        redirectURI: g.REDIRECT_URL,
        namespace: g.NAMESPACE,
        cache: !1
      },
      basePath: t
    });
  }
  static getAppState(a, t) {
    return e.instance(a, t).appStateImpl;
  }
  static getUserManager() {
    return e.instance().appStateImpl.getUserManager();
  }
  static getLegalManager() {
    return e.instance().appStateImpl.getLegalManager();
  }
  static getWalletManager() {
    return e.instance().appStateImpl.getWalletManager();
  }
  static getGameState() {
    return e.instance().appStateImpl.getGameState();
  }
  static async registerUserFetch() {
    await M(e.instance().appStateImpl);
  }
  static async fetchConfigs(a, t) {
    var h, w, m, C, y, E;
    let s = !1, o = 0, f, i = {}, d = !1, l;
    for (; !s && o < 3; ) {
      try {
        l = void 0, await e.instance().appStateImpl.fetchDiscovery(a, t == null ? void 0 : t.discovery), await e.instance().appStateImpl.fetchConfigs(a, t == null ? void 0 : t.configs), await e.instance().appStateImpl.linkTwitchManager.fetchConfigs(t == null ? void 0 : t.twitchDropConfigs);
      } catch (S) {
        l = S;
      } finally {
        i = e.getAppState().getConfig() || {}, f = e.getAppState().getFeatureFlags(), d = e.getAppState().linkTwitchManager.state.isConfigsFetched, s = f !== void 0 && Object.keys(i).length > 0 && d, o += 1;
      }
      !s && o < 3 && await P(o * 5e3);
    }
    if (!s)
      throw v.isAxiosNetworkError(l) || v.isAxiosServerError(l) ? l : new O(
        "Configuration errors were detected, application will not start. Please fix the configuration then try again.",
        a,
        G.INVALID_CONFIGS
      );
    const A = i[c.PAGE_CONFIG].data.isLocalStyleEnabled, I = e.getAppState().getGlobalPageConfigFromDiscovery();
    let r;
    if (A)
      r = n.convertConfigToCss(i[c.PAGE_CONFIG]);
    else if (I) {
      r = n.convertConfigToCss(I);
      const S = n.bgOverlayToCss([
        { id: "main", ...(m = (w = (h = i[c.PAGE_CONFIG]) == null ? void 0 : h.data) == null ? void 0 : w.mainPage) == null ? void 0 : m.backgroundOverlay }
      ]), u = n.bgOverlayToCss([
        { id: "login", ...(E = (y = (C = i[c.PAGE_CONFIG]) == null ? void 0 : C.data) == null ? void 0 : y.loginPage) == null ? void 0 : E.backgroundOverlay }
      ]);
      r = r + " " + S + " " + u;
    }
    const F = n.bgOverlayToCss([
      { id: "twitchDrop", ...e.getAppState().linkTwitchManager.getBackgroundOverlay() }
    ]);
    r = r + " " + F, e.getAppState().isSingleGame() ? n.injectCss(r) : n.injectCss("");
  }
  static instance(a, t) {
    return e._instance || (e._instance = new e(a, t)), e._instance;
  }
};
let p = e;
p.getSettingsState = () => e.instance().appStateImpl.getSettingsState();
p.getInputValidationManager = () => e.instance().appStateImpl.getInputValidationManager();
p.getGdprManager = () => e.instance().appStateImpl.getGdprManager();
p.getFeatureFlagManager = () => e.instance().appStateImpl.featureFlagsManager;
p.hasFeature = (a) => e.instance().appStateImpl.featureFlagsManager.hasFeature(a);
function P(a) {
  return new Promise((t) => {
    setTimeout(() => t(void 0), a);
  });
}
export {
  p as Ctx
};
