import { useState as o, useCallback as i, useRef as B, useEffect as K } from "react";
import { u as d, F as A, W as G } from "./FetchConfigsUtils-476a602b.mjs";
import { u as m } from "./usePendingOrders-53ca2538.mjs";
import { Platform as P, OrderStatus as H } from "@accelbyte/sdk-platform";
var f = /* @__PURE__ */ ((_) => (_.WORKER_READY = "WORKER_READY", _.WORKER_TO_MAIN = "WORKER_TO_MAIN", _.WORKER_DISPATCH_MSG_RENDERER = "WORKER_DISPATCH_MSG_RENDERER", _.WORKER_MSG_RENDERER = "WORKER_MSG_RENDERER", _.WORKER_TASKS_STATUS = "WORKER_TASKS_STATUS", _.WORKER_DISPATCH_MSG = "WORKER_DISPATCH_MSG", _.WORKER_MSG = "WORKER_MSG", _.WORKER_DISPATCH_TASK = "WORKER_DISPATCH_TASK", _.WORKER_TASK = "WORKER_TASK", _.ON_MOUNTED = "ON_MOUNTED", _.ON_MAXIMIZE_WINDOW = "ON_MAXIMIZE_WINDOW", _.ON_UNMAXIMIZE_WINDOW = "ON_UNMAXIMIZE_WINDOW", _.ON_MINIMIZE_WINDOW = "ON_MINIMIZE_WINDOW", _.ON_CLOSE_WINDOW = "ON_CLOSE_WINDOW", _.GET_BROWSER_WINDOW = "GET_BROWSER_WINDOW", _.REFRESH_BROWSER_WINDOW = "REFRESH_BROWSER_WINDOW", _.OPEN_EXTERNAL = "OPEN_EXTERNAL", _.OPEN_URL = "OPEN_URL", _.GET_USER_PLATFORM = "GET_USER_PLATFORM", _.UPDATE_LAUNCHER_NAME = "UPDATE_LAUNCHER_NAME", _.DESKTOP_DB_GET = "DESKTOP_DB_GET", _.DESKTOP_DB_SET = "DESKTOP_DB_SET", _.DESKTOP_DB_CLEAR = "DESKTOP_DB_CLEAR", _.DESKTOP_DB_DELETE = "DESKTOP_DB_DELETE", _.OPEN_DIR_DIALOG = "OPEN_DIR_DIALOG", _.ON_SELECT_DIR = "ON_SELECT_DIR", _.OS_SETTINGS = "OS_SETTINGS", _.IS_ONLINE = "IS_ONLINE", _.START_CHECK_CONNECTION = "START_CHECK_CONNECTION", _.DESTROY_CHECK_CONNECTION = "DESTROY_CHECK_CONNECTION", _.DM_RESUME_INSTALLATION = "DM_RESUME_INSTALLATION", _.DM_PAUSE_INSTALLATION = "DM_PAUSE_INSTALLATION", _.DM_INSTALL = "DM_INSTALL", _.DM_UNINSTALL = "DM_UNINSTALL", _.DM_LOAD_MANIFEST = "DM_LOAD_MANIFEST", _.DM_PROGRESS = "DM_PROGRESS", _.DM_PREPARE = "DM_PREPARE", _.DM_GET_SPEED_LIMIT = "DM_GET_SPEED_LIMIT", _.DM_SET_SPEED_LIMIT = "DM_SET_SPEED_LIMIT", _.DM_OPEN_DIRECTORY = "DM_OPEN_DIRECTORY", _.DM_GET_DISK_SPACE = "DM_GET_DISK_SPACE", _.DM_RECEIVE_DISK_SPACE = "DM_RECEIVE_DISK_SPACE", _.DM_GET_DEFAULT_INSTALLATION_DIR = "DM_GET_DEFAULT_INSTALLATION_DIR", _.DM_EXECUTE = "DM_EXECUTE", _.DM_CLOSE_GAME = "DM_CLOSE_GAME", _.DM_SUBSCRIBE_GAME_STATUS = "DM_SUBSCRIBE_GAME_STATUS", _.DM_UNSUBSCRIBE_GAME_STATUS = "DM_UNSUBSCRIBE_GAME_STATUS", _.DM_MAKE_DOWNLOAD_STATE_TREE = "DM_MAKE_DOWNLOAD_STATE_TREE", _.DM_GET_PROGRESS_ERROR = "DM_GET_PROGRESS_ERROR", _.DM_REPAIR = "DM_REPAIR", _.DM_VERIFY = "DM_VERIFY", _.DM_UPDATE = "DM_UPDATE", _.DM_COMMAND_MANAGER_CONSUME = "DM_COMMAND_MANAGER_CONSUME", _.DM_COMMAND_MANAGER_ADD_LISTENER = "DM_COMMAND_MANAGER_ADD_LISTENER", _.DM_COMMAND_MANAGER_REMOVE_LISTENER = "DM_COMMAND_MANAGER_REMOVE_LISTENER", _.DM_SUBSCRIBE_ACTIVITY = "DM_SUBSCRIBE_ACTIVITY", _.DM_SUBSCRIBE_INSTALLATION_STATUS = "DM_SUBSCRIBE_INSTALLATION_STATUS", _.DM_SUBSCRIBE_VERIFICATION_STATUS = "DM_SUBSCRIBE_VERIFICATION_STATUS", _.DM_UNSUBSCRIBE_ACTIVITY = "DM_UNSUBSCRIBE_ACTIVITY", _.DM_UNSUBSCRIBE_INSTALLATION_STATUS = "DM_UNSUBSCRIBE_INSTALLATION_STATUS", _.DM_UNSUBSCRIBE_VERIFICATION_STATUS = "DM_UNSUBSCRIBE_VERIFICATION_STATUS", _.DM_GET_SPEED = "DM_GET_SPEED", _.DM_SEND_AVAILABLE_GAME_TITLE = "DM_SEND_AVAILABLE_GAME_TITLE", _.DM_UPDATE_TRAY_STATUS = "DM_UPDATE_TRAY_STATUS", _.DM_GET_IN_PROGRESS_DOWNLOAD_STATUS = "DM_GET_IN_PROGRESS_DOWNLOAD_STATUS", _.DM_CLEAR_INCOMPLETE_PREPARATION = "DM_CLEAR_INCOMPLETE_PREPARATION", _.GAME_PLAY = "GAME_PLAY", _.SHOW_LAUNCHER_TERMINATION_MODAL = "SHOW_LAUNCHER_TERMINATION_MODAL", _.HIDE_LAUNCHER_TERMINATION_MODAL = "HIDE_LAUNCHER_TERMINATION_MODAL", _.TERMINATE_LAUNCHER = "TERMINATE_LAUNCHER", _.UPD_UPDATE_INIT = "UPD_UPDATE_INIT", _.UPD_UPDATE_INFO = "UPD_UPDATE_INFO", _.UPD_PROGRESS = "UPD_PROGRESS", _.UPD_STATUS = "UPD_STATUS", _.UPD_CHECK_UPDATE = "UPD_CHECK_UPDATE", _.UPD_DOWNLOAD_UPDATE = "UPD_DOWNLOAD_UPDATE", _.UPD_QUIT_AND_INSTALL = "UPD_QUIT_AND_INSTALL", _.UPD_SET_WINDOW_SETTING = "UPD_SET_WINDOW_SETTING", _.UPD_IS_SKIP_UPDATE = "UPD_IS_SKIP_UPDATE", _.DEBUG = "DEBUG", _.LAUNCHER_ENABLE_DEV_MODE = "LAUNCHER_ENABLE_DEV_MODE", _))(f || {});
const w = { useOrdersList: F, useOrderDetail: V };
function F() {
  const [_] = d(), [N, S] = o({
    ordersList: null,
    ordersListFetchStatus: A.IDLE,
    ordersListError: null
  }), M = i(
    async ({
      userId: r,
      page: t,
      pageSize: R,
      status: I
    }) => {
      let E = null, T = null;
      try {
        return S((D) => ({
          ...D,
          ordersListError: null,
          ordersListFetchStatus: A.FETCHING
        })), E = await P.OrderApi(_).getOrders_ByUserId(r, {
          status: I,
          offset: Y(t),
          limit: W(R)
        }), { data: E };
      } catch (D) {
        return T = G.getErrorType(D), { error: T };
      } finally {
        S({
          ordersList: E,
          ordersListError: T,
          ordersListFetchStatus: A.IDLE
        });
      }
    },
    [_]
  );
  return {
    state: N,
    mutations: {
      fetchOrdersList: M
    }
  };
}
function V() {
  const [_] = d(), [N, S] = o({
    order: null,
    orderedItem: null,
    isOrderExpired: !1,
    orderFetchStatus: A.IDLE,
    orderError: null
  }), {
    mutations: { fetchPendingOrders: M }
  } = m(), r = B(-1);
  K(() => () => {
    window.clearTimeout(r.current);
  }, []);
  const t = i(
    async ({
      userId: R,
      orderNo: I
    }) => {
      let E = null, T = null, D = null;
      try {
        S((L) => ({ ...L, orderError: null, orderFetchStatus: A.FETCHING })), E = await P.OrderApi(_).getOrder_ByUserId_ByOrderNo(R, I), T = (await P.ItemApi(_).getItemsLocaleByIds({ itemIds: E.itemId }))[0];
        const e = await M({ itemId: E.itemId, userId: R });
        if (e.error)
          throw e.error;
        if (E && E.status === H.enum.INIT && E.expireTime) {
          const L = new Date().getTime();
          let U = new Date(E.expireTime).getTime() - L;
          U < 0 && (U = 0), r.current = window.setTimeout(() => {
            S((u) => ({ ...u, isOrderExpired: !0 }));
          }, U);
        }
        return { data: { order: E, orderedItem: T } };
      } catch (O) {
        return D = G.getErrorType(O), { error: D };
      } finally {
        S((O) => ({
          ...O,
          order: E,
          orderedItem: T,
          orderError: D,
          orderFetchStatus: A.IDLE
        }));
      }
    },
    [_]
  );
  return {
    state: N,
    mutations: {
      fetchOrder: t
    }
  };
}
function W(_ = 10) {
  return _;
}
function Y(_) {
  return _ ? (_ - 1) * W() : 0;
}
export {
  f as I,
  w as O
};
