import { Ctx as c } from "./accelbyte-widgets33.mjs";
import { NO_BUILDINFO as d } from "./accelbyte-widgets243.mjs";
import { Container as p } from "unstated";
import { DownloadControlRule as i } from "./accelbyte-widgets423.mjs";
class v extends p {
  constructor() {
    super(), this.reloadDownloadStateTree = async (t) => {
      if (!t)
        return null;
      const a = await Device.downloadManager.makeDownloadStateTree(t);
      return this.setState({
        downloadStateTree: a
      }), a;
    }, this.uninstall = async (t) => {
      const a = t.appId;
      a && Device.downloadManager.uninstall({ appId: a });
    }, this.stopAllDownload = (t) => {
      !t || t.length === 0 || t.forEach((a) => {
        Device.downloadManager.pauseInstallation(a);
      });
    }, this.clearIncompletePreparation = async (t) => {
      const a = await this.reloadDownloadStateTree(t);
      a && i.isInstallationCorrupt(a.own) && (console.info(`clear "${t}" incomplete prepare installation...`), Device.downloadManager.clearIncompletePreparation(t));
    }, this.prepareDownload = async (t, a, o, n) => {
      const r = c.getAppState().isSingleGame(), { interactivity: s } = this.state, l = !s.prepareDownloadIsRunning && n && i.canInstall(n) && !i.isBackgroundPreparation(n.own);
      r && l && (await this.setState({ interactivity: { ...this.state.interactivity, prepareDownloadIsRunning: !0 } }), console.info("Prepare download is running..."), Device.downloadManager.prepareInstall({ appId: t, appDirectory: a, manifest: o }));
    }, this.startDownload = (t, a, o) => {
      const { interactivity: n } = this.state;
      if (!(a != null && a.appId) || n.attemptingToDownload || !o) {
        console.error("DMS: 001 Cannot startDownload", a, n, !o);
        return;
      }
      this.setState({ interactivity: { ...n, attemptingToDownload: !0 } });
      const e = i.isInstalling(t.own), r = i.isBackgroundPreparation(t.own);
      if (e && !r) {
        console.error("DMS: 002 Cannot startDownload, isInstalling"), this.setState({
          interactivity: { ...n, attemptingToDownload: !1 }
        });
        return;
      }
      this.setState({
        downloadDialogCallback: this.downloadDialogCallbackFunc(a, o),
        interactivity: { ...n, attemptingToDownload: !1 }
      });
    }, this.cancelDownload = () => {
      this.setState({
        downloadDialogCallback: void 0,
        interactivity: { ...this.state.interactivity, attemptingToDownload: !1 }
      });
    }, this.startUpdate = async (t, a, o) => {
      if (!this.state.interactivity.updatingGame) {
        await this.setState((n) => ({
          interactivity: {
            ...n.interactivity,
            updatingGame: !0
          }
        }));
        try {
          await Device.downloadManager.update({ appId: t, manifest: a, diffCache: o });
        } catch (n) {
          console.error(n);
        } finally {
          await this.setState((n) => ({
            interactivity: {
              ...n.interactivity,
              updatingGame: !1
            }
          }));
        }
      }
    }, this.uiProgressLoadPermissionError = () => {
      const { progressLoadError: t } = this.state;
      return t && t.load.data === DMLoadErrorTypes.CAN_NOT_READ_FILE;
    }, this.uiAllowRemoveControl = () => {
      const { downloadStateTree: t, progressLoadError: a } = this.state;
      return t ? i.canRemoveControl(t) && !a : !1;
    }, this.uiAllowUpdate = () => {
      const {
        downloadStateTree: t,
        interactivity: { attemptingToDownload: a, startingApp: o, calculatingDownloadInfo: n }
      } = this.state;
      return t ? !!i.canUpdate(t) && !(a || o || n) : !1;
    }, this.uiAllowRepair = () => {
      const {
        downloadStateTree: t,
        interactivity: { checkingForUpdate: a, attemptingToDownload: o, startingApp: n, calculatingDownloadInfo: e }
      } = this.state;
      return t ? !!i.canRepair(t) && !(a || o || n || e) : !1;
    }, this.isDownloadButtonDisabled = (t, a) => {
      const { interactivity: o, downloadDialogCallback: n } = this.state, { attemptingToDownload: e, calculatingDownloadInfo: r } = o, s = a && i.isInstalling(a.own);
      return !((a == null ? void 0 : a.own) && i.isBackgroundPreparation(a.own)) && (s || e || t === d || r || n !== void 0);
    }, this.downloadDialogCallbackFunc = (t, a) => (o) => {
      const { interactivity: n } = this.state;
      if (!(t != null && t.appId)) {
        console.log("DMS: 004 downloadDialogCallback, !gameInfo.appId");
        return;
      }
      this.setState({ interactivity: { ...n } }), Device.downloadManager.install({ appId: t.appId, appDirectory: o, manifest: a }), this.setState({
        downloadDialogCallback: void 0,
        interactivity: { ...n, attemptingToDownload: !1 }
      });
    }, this.state = {
      downloadDialogCallback: void 0,
      interactivity: {
        checkingForUpdate: !1,
        attemptingToDownload: !1,
        prepareDownloadIsRunning: !1,
        startingApp: !1,
        calculatingDownloadInfo: !1,
        updatingGame: !1
      },
      downloadStateTree: null,
      progressLoadError: null
    };
  }
  appIsOutOfDate(t, a) {
    const { progressShortSummary: o } = t;
    return !!a && !!o && o.buildId !== a.buildId;
  }
}
export {
  v as DownloadManagerState
};
