var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/utils/task.ts
var task_exports = {};
__export(task_exports, {
  default: () => task_default
});
module.exports = __toCommonJS(task_exports);
var Tasks = class {
  constructor(actions) {
    this.actions = /* @__PURE__ */ new Map();
    this.taskQueue = [];
    this.isRunning = false;
    if (actions) {
      this.actions = actions;
    }
  }
  /**
   * 清空所有任务
   */
  clear() {
    this.actions.clear();
    this.taskQueue = [];
    this.isRunning = false;
  }
  /**
   * 清空任务执行队列
   */
  clearTaskQueue() {
    this.taskQueue = [];
    this.isRunning = false;
  }
  /**
   * 添加action
   * @param name action名称
   * @param action action函数
   */
  addAction(name, action) {
    this.actions.set(name, action);
  }
  /**
   * 添加任务
   * @param task 任务
   */
  addTask(task) {
    this.taskQueue.push(task);
    this.run();
  }
  /**
   * 执行任务
   */
  async run() {
    var _a;
    if (this.isRunning) {
      return;
    }
    if (this.taskQueue.length === 0) {
      this.isRunning = false;
      return;
    }
    this.isRunning = true;
    while (this.taskQueue.length > 0) {
      const task = (_a = this.taskQueue) == null ? void 0 : _a[0];
      if (!task) {
        this.isRunning = false;
        return;
      }
      const action = this.actions.get(task.type);
      if (action) {
        try {
          await action({ ...(task == null ? void 0 : task.actionParams) || {}, uuid: task == null ? void 0 : task.uuid });
          console.log("task_success>>>>>>>", task);
        } catch (error) {
          console.error("task_error>>>>>>>", error);
        } finally {
          const _task = this.taskQueue.shift();
          console.log("task_shift>>>>>>>", _task);
        }
      } else {
        const _task = this.taskQueue.shift();
        console.log("task_action_not_found>>>>>>>", _task);
      }
    }
    this.isRunning = false;
  }
  /**
   * 判定当前的任务是否依然存在
   */
  isTaskExist(uuid) {
    return this.taskQueue.some((task) => (task == null ? void 0 : task.uuid) === uuid);
  }
};
var task_default = Tasks;
