import { t as d, getCurrentLanguage as D, I18N_DATE_LOCALES as h } from "./accelbyte-widgets58.mjs";
import { OsSettingsKey as y } from "./accelbyte-widgets148.mjs";
import { Ctx as l } from "./accelbyte-widgets33.mjs";
import { MediaType as S } from "./accelbyte-widgets149.mjs";
import { DBKeys as E } from "./accelbyte-widgets150.mjs";
import { ErrorPath as I } from "./accelbyte-widgets69.mjs";
import { marked as A } from "./accelbyte-widgets151.mjs";
import { format as P } from "date-fns";
import { isValidElement as T } from "react";
import { EDITOR_PREVIEWER_ID as w } from "./accelbyte-widgets78.mjs";
import { CodeChallenge as v, LoginErrorExpired as b, LoginErrorCancelled as k } from "@accelbyte/sdk-iam";
const c = class {
  static makeHtml(e) {
    switch (e.type) {
      case S.enum["text/markdown"]:
        return A(e.content);
    }
    return null;
  }
  static waitForImageLoad(e) {
    return new Promise((t, r) => {
      if (e) {
        const n = new Image();
        n.onload = () => t(), n.onerror = (a) => r(a), n.src = e;
      } else
        r();
    });
  }
  static makeTitleWithAppName(e) {
    if (!c.isInEditorPreview())
      return e ? `${e} - ${this.getAppName()}` : this.getAppName();
  }
  static formatDate(e, t) {
    if (!e)
      return "";
    const r = D(), n = e instanceof Date ? e : new Date(e);
    return t.type === "dynamic" ? C(n, r, t.format) : P(n, t.format, { locale: h[r] });
  }
};
let o = c;
o.separatorVisibility = (e, t) => e.some((r) => r) ? "" : t;
o.getAppDirectory = (e) => {
  var n;
  const t = (n = l.getAppState().getPersistDB().getDownloadState(e)) == null ? void 0 : n.appDirectory;
  let r = t;
  if (!t) {
    const { installDir: a } = Device.desktopStorage.get(E.SETTINGS) ?? {};
    r = a || Device.osSettings.get(y.APPDIR);
  }
  return r;
};
o.playGame = () => {
  const { activeGameId: e, buildManifestRecord: t } = l.getGameState().state, r = e;
  if (r) {
    const n = t && t[e];
    if (!n)
      return;
    const { defaultLaunchProfile: a, launchArguments: s } = n;
    if (!a)
      return;
    const { defaultEntryPoint: i, defaultClientId: p, defaultRedirectURI: u } = a, f = l.getAppState().getPersistDB().getAccessToken(), g = c.getAppDirectory(r);
    f && i && p && u && g && Device.downloadManager.execute({
      appId: r,
      appDirectory: g,
      entryPoint: i,
      launchArguments: s || "",
      accessToken: f,
      clientId: p,
      redirectURI: u,
      codeChallenge: v.generateChallenge().challenge
    });
  }
};
o.repairGame = async (e) => {
  console.log("repairGame invoked. TODO", e);
};
o.verifyGame = async (e) => {
  console.log("verifyGame invoked. TODO", e);
};
o.checkUpdateGame = async (e, t, r, n, a, s, i) => {
  console.log("checkUpdateGame invoked. TODO", i);
};
o.getDropDownList = (e, t) => {
  const n = [
    t && {
      label: d("detailApp.options.uninstall"),
      onClick: () => e.setState({ showUninstallDialog: !0 })
    }
  ].filter(Boolean);
  return n.length === 0 ? null : n;
};
o.getSearchParamsObject = (e) => {
  try {
    const t = e.location.search;
    return new URLSearchParams(t);
  } catch {
    throw new m();
  }
};
o.redirectToErrorPage = (e, t) => t instanceof b ? (l.getAppState().logout(), e.replace(I.ErrorSessionExpired.makePath())) : t instanceof k ? e.replace(`${e.location.pathname}`) : e.replace(`${e.location.pathname}`);
o.reactNodeToString = (e) => {
  let t = "";
  return typeof e == "string" ? t = e : typeof e == "number" ? t = e.toString() : e instanceof Array ? e.forEach((r) => {
    t += c.reactNodeToString(r);
  }) : T(e) && (t += c.reactNodeToString(e.props.children)), t;
};
o.debounce = (e, t = 500) => {
  let r;
  return (...a) => {
    clearTimeout(r), r = setTimeout(() => e(...a), t);
  };
};
o.isInEditorPreview = () => !!document.getElementById(w);
o.getAppName = () => {
  const e = l.getAppState().getBrandName();
  return d("Common.appName", { publisherName: e });
};
class m extends Error {
  constructor(t) {
    super(t), Object.setPrototypeOf(this, m.prototype);
  }
}
function C(e, t, r) {
  if (r === "LL")
    return Intl.DateTimeFormat(t, {
      day: "2-digit",
      month: "long",
      year: "numeric"
    }).format(e);
  const n = Intl.DateTimeFormat(t, {
    dateStyle: "full",
    timeStyle: "short"
  }).formatToParts(e);
  let a, s = "";
  for (const i of n) {
    if (i.type === "literal" && a === "year") {
      s += " ";
      continue;
    }
    s += i.value, a = i.type;
  }
  return s;
}
export {
  o as FeUtils
};
