var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/solution/BookingTicket/utils/scan/index.ts
var scan_exports = {};
__export(scan_exports, {
  default: () => Scan
});
module.exports = __toCommonJS(scan_exports);
var import_watch = __toESM(require("../../../../utils/watch"));
var import_task = __toESM(require("../../../../utils/task"));
var Scan = class {
  constructor(solution, watchKey) {
    this.listenerConfig = /* @__PURE__ */ new Map();
    this.solution = solution;
    this.watchKey = watchKey;
  }
  /**
   * 初始化外设扫码结果监听
   */
  initPeripheralsListener() {
    var _a, _b, _c, _d, _e;
    (_e = (_d = (_c = (_b = (_a = this.solution) == null ? void 0 : _a.window) == null ? void 0 : _b.interaction) == null ? void 0 : _c.utils) == null ? void 0 : _d.mountFunction) == null ? void 0 : _e.call(
      _d,
      "global",
      "peripheralsResult",
      (strVal) => {
        try {
          const result = JSON.parse(strVal);
          console.log("nativeScannerResult>>>>>>>", result);
          this.handleScan(result);
        } catch (err) {
          console.error(err);
        }
      }
    );
  }
  /**
   * 处理扫码事件
   * @param eventKey 事件key
   * @param value 扫码结果
   */
  handleScan(result) {
    const lastEnableEventKey = import_watch.default.getLastEnableEventKey(this.watchKey);
    if (lastEnableEventKey) {
      const listenerConfig = this.listenerConfig.get(lastEnableEventKey);
      if (listenerConfig) {
        listenerConfig.task.addTask({
          uuid: this.generateUuid(),
          type: lastEnableEventKey,
          actionParams: {
            scanResult: result
          }
        });
      }
    }
  }
  /**
   * 生成uuid
   * @returns uuid
   */
  generateUuid() {
    return Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15) + Date.now().toString(36);
  }
  /**
   * 添加扫码信息监听
   * @param watchKey 监听的key
   * @param event 事件
   */
  addListener(event, scanCallback) {
    import_watch.default.subscribe(this.watchKey, event);
    const task = new import_task.default();
    task.addAction(
      event.key,
      (...args) => this.scanTaskAction(event.key, ...args)
    );
    this.listenerConfig.set(event.key, { scanCallback, task });
    return {
      remove: () => {
        this.removeListener(event);
      }
    };
  }
  /**
   * 移除扫码信息监听
   * @param watchKey 监听的key
   * @param event 事件
   */
  removeListener(event) {
    var _a, _b;
    import_watch.default.unsubscribe(this.watchKey, event);
    (_b = (_a = this.listenerConfig.get(event.key)) == null ? void 0 : _a.task) == null ? void 0 : _b.clear();
    this.listenerConfig.delete(event.key);
  }
  /**
   * 移除所有监听
   */
  removeAllListeners() {
    var _a, _b, _c, _d;
    import_watch.default.clearEvent(this.watchKey);
    (_b = (_a = this.listenerConfig) == null ? void 0 : _a.forEach) == null ? void 0 : _b.call(_a, (item) => {
      var _a2;
      (_a2 = item == null ? void 0 : item.task) == null ? void 0 : _a2.clear();
    });
    (_d = (_c = this.listenerConfig) == null ? void 0 : _c.clear) == null ? void 0 : _d.call(_c);
  }
  /**
   * 启用所有监听
   */
  enableAllListeners() {
    import_watch.default.enableAllEventByWatchKey(this.watchKey);
  }
  /**
   * 禁用所有监听，并清空任务执行队列
   */
  disableAllListeners() {
    var _a, _b;
    import_watch.default.disabledAllEventByWatchKey(this.watchKey);
    (_b = (_a = this.listenerConfig) == null ? void 0 : _a.forEach) == null ? void 0 : _b.call(_a, (item) => {
      var _a2;
      (_a2 = item == null ? void 0 : item.task) == null ? void 0 : _a2.clearTaskQueue();
    });
  }
  /**
   * 清空监听对应的任务执行队列，不传入eventKey则清空所有监听对应的任务执行队列
   * @param eventKey 事件key
   */
  clearTaskQueue(eventKey) {
    var _a, _b, _c;
    console.log("clearTaskQueue>>>>>>>", eventKey);
    if (eventKey) {
      (_a = this.listenerConfig.get(eventKey)) == null ? void 0 : _a.task.clearTaskQueue();
    } else {
      (_c = (_b = this.listenerConfig) == null ? void 0 : _b.forEach) == null ? void 0 : _c.call(_b, (item) => {
        var _a2;
        (_a2 = item == null ? void 0 : item.task) == null ? void 0 : _a2.clearTaskQueue();
      });
    }
  }
  /**
   * 获取监听配置
   * @param eventKey 事件key
   * @returns 监听配置
   */
  getListenerConfig(eventKey) {
    var _a;
    return (_a = this.listenerConfig) == null ? void 0 : _a.get(eventKey);
  }
  /**
   * 扫码任务执行函数
   * @param args 参数
   */
  async scanTaskAction(...args) {
    const [eventKey, other] = args;
    const { type, value, data } = (other == null ? void 0 : other.scanResult) || {};
    const taskItemUuid = other == null ? void 0 : other.uuid;
    const { scanCallback, task } = this.getListenerConfig(eventKey) || {};
    if (scanCallback) {
      try {
        const result = await scanCallback({ type, value });
        console.log(`${eventKey}_result>>>>>>>`, result);
        if (taskItemUuid && (task == null ? void 0 : task.isTaskExist(taskItemUuid))) {
          import_watch.default.publishByEventKey(this.watchKey, eventKey, {
            type: eventKey,
            status: "success",
            scanType: type,
            scanCode: value,
            scanData: data,
            result
          });
        } else {
          console.warn(`${eventKey}_result_task_not_exist>>>>>>>`, task);
        }
      } catch (error) {
        console.log(`${eventKey}_result_error>>>>>>>`, error);
        if (taskItemUuid && (task == null ? void 0 : task.isTaskExist(taskItemUuid))) {
          import_watch.default.publishByEventKey(this.watchKey, eventKey, {
            type: eventKey,
            status: "failed",
            scanType: type,
            scanCode: value,
            scanData: data
          });
        } else {
          console.warn(`${eventKey}_result_error_task_not_exist>>>>>>>`, task);
        }
      }
    }
  }
};
