import { Ctx as c } from "./accelbyte-widgets33.mjs";
import { STORE_IMAGE_KEY as g } from "./accelbyte-widgets185.mjs";
import { DownloadControlRule as w } from "./accelbyte-widgets423.mjs";
import { UrlUtils as h } from "./accelbyte-widgets90.mjs";
import y from "filesize";
import { MediumHelper as A } from "./accelbyte-widgets392.mjs";
import { LauncherHelper as v } from "./accelbyte-widgets424.mjs";
import { YoutubeVideoMediumCodec as D } from "./accelbyte-widgets396.mjs";
const l = class {
  static determineBackgroundImage(e, t) {
    return e ?? t;
  }
  static formatFileSize(e) {
    return y(e, { standard: "jedec", base: 2 });
  }
  static formatTimeEstimation(e) {
    let t = "sec";
    return e ? (e > 60 && (e = e / 60, t = "min", e > 60 && (e = e / 60, t = "hrs")), e = Math.floor(e), `Est ${e} ${t}`) : "calculating";
  }
  static makeFormattedInstallationStatus({ installationStatus: e }) {
    if (!e)
      return null;
    const { summary: t, speed: a } = e;
    return !t || t.data.all === 0 ? null : {
      original: {
        summary: t,
        speed: a
      },
      presented: {
        written: this.formatFileSize(t.data.written),
        all: this.formatFileSize(t.data.all),
        speed: typeof a == "number" ? `${this.formatFileSize(a)}/second` : null,
        timeEstimation: a != null && a > 0 ? String(this.formatTimeEstimation((t.data.all - t.data.written) / a)) : null
      }
    };
  }
  static makeFormattedPercentage({ progress: e, total: t }) {
    const a = t ? e / t : 0;
    return `${Math.round(a * 100)}%`;
  }
  static makeFormattedSpeed(e = 0) {
    return `${l.formatFileSize(e)}/second`;
  }
  static getDiskSpace(e) {
    Device.downloadManager.getDiskSpace(e);
  }
  static receiveAvailableDiskSpace(e) {
    Device.downloadManager.receiveDiskSpace((t) => {
      e(t);
    });
  }
};
let i = l;
i.loadGameInfo = async () => {
  var a;
  const e = c.getAppState().isSingleGame(), t = (a = c.getAppState().getUser()) == null ? void 0 : a.userId;
  t && (e ? await l.loadSingleGameInfo(t) : await l.loadOwnGameInfo(t));
};
i.loadOwnGameInfo = async (e) => {
  const t = c.getAppState().state.appHistory.location.search, a = c.getGameState(), r = await a.fetchGameEntitlements(e);
  if (r) {
    const n = r.map((s) => s.appId).filter(Boolean);
    await a.fetchGamesInfo(n);
    let o = a.state.activeGameId;
    if (o)
      n.includes(o) || (o = n[0]);
    else {
      const s = v.getAppIdFromQueryParameter(t) || "";
      n.includes(s) && (o = s);
    }
    a.setActiveGame(o || n[0]);
  }
};
i.loadSingleGameInfo = async (e) => {
  var n;
  const t = c.getGameState(), a = c.getAppState().downloadManagerState, r = c.getAppState().getGameId();
  if (r && a) {
    t.setActiveGame(r);
    const [o] = await Promise.all([
      t.fetchGameEntitlements(e),
      a.clearIncompletePreparation(r)
    ]), s = o && o.find((d) => d.appId === r), [m, u] = await Promise.all([
      t.fetchGamesInfo([r]),
      s && t.fetchBuildManifest(r, l.getUserPlatform())
    ]);
    if (u) {
      const { updatedBuildManifestRecord: d } = t.state, p = d && d[r], I = m && m[r], S = l.getOsPathSeparator(), G = `${((n = c.getSettingsState()) == null ? void 0 : n.getSettings().installDir) || ""}${S}${r}`, f = await a.reloadDownloadStateTree(r);
      if (f && p && a.appIsOutOfDate(f.own, p) && w.isBackgroundPreparation(f.own) && I) {
        console.info("Uninstalling old version prepare installation"), await a.uninstall(I);
        return;
      }
      a.prepareDownload(r, G, u, f);
    }
  }
};
i.getActiveItemId = () => {
  var a;
  const { activeGameId: e, gameInfoRecord: t } = c.getGameState().state;
  if (!(!e || !t))
    return (a = t[e]) == null ? void 0 : a.itemId;
};
i.getGameTitle = (e) => e.replace(/[-[\]{}()*\\^$|]/g, "");
i.getGameBackgroundImage = (e) => {
  if (!e)
    return "";
  const t = l.getGameImageByKey(e, g.GAME_BANNER);
  return Array.isArray(t) && t.length > 0 ? t[0].imageUrl : "";
};
i.getGameIconImage = (e) => {
  const t = l.getGameImageByKey(e, g.GAME_ICON);
  if (Array.isArray(t) && t.length > 0)
    return t[0].imageUrl;
};
i.getGameScreenshotImages = (e) => l.getGameImageByKey(e, g.SCREENSHOT);
i.getGameImageByKey = (e, t) => {
  const { images: a } = e;
  if (a && Array.isArray(a) && a.length) {
    const r = a.filter(
      (n) => n.as === t && h.transformToValidURLOrUndefined(n.imageUrl)
    );
    if (t !== g.SCREENSHOT)
      return r;
    {
      const n = [];
      return r.forEach((o) => {
        const [s, m] = A.fromRaw(o);
        !m && s && !D.safeParse(s).success && n.push(o);
      }), n;
    }
  }
};
i.updateGameDesktopDBByGameId = async ({
  dbKeys: e,
  persistDB: t,
  gameId: a,
  updatedData: r
}) => {
  const n = t.get(e);
  let o;
  n ? o = [...n.filter((m) => m.appId !== a), r].filter((m) => m !== void 0) : o = [r], await t.set(e, o);
};
i.getUserPlatform = () => Device.downloadManager.getUserPlatform();
i.getOsPathSeparator = () => l.getUserPlatform().platform === "win32" ? "\\" : "/";
export {
  i as GameHelper
};
