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/components/ecocup/utils/scanListener.ts
var scanListener_exports = {};
__export(scanListener_exports, {
  default: () => scanListener_default
});
module.exports = __toCommonJS(scanListener_exports);
var ScanListener = class {
  isInitialized = false;
  listeners = [];
  constructor() {
    this.listeners = [];
  }
  /**
   * 初始化外设扫码结果监听
   */
  initPeripheralsListener(interaction) {
    var _a, _b;
    if (this.isInitialized) {
      return;
    }
    this.isInitialized = true;
    (_b = (_a = interaction == null ? void 0 : interaction.utils) == null ? void 0 : _a.mountFunction) == null ? void 0 : _b.call(
      _a,
      "global",
      "peripheralsResult",
      (strVal) => {
        try {
          const result = JSON.parse(
            strVal || "{}"
          );
          const activeListener = this.listeners[0];
          if (activeListener) {
            activeListener.callback(result);
          }
        } catch (err) {
          console.error(err);
        }
      }
    );
  }
  addListener(interaction, key, callback) {
    const existingIndex = this.listeners.findIndex((item) => item.key === key);
    if (existingIndex !== -1) {
      this.listeners.splice(existingIndex, 1);
    }
    this.listeners.unshift({ key, callback });
    this.initPeripheralsListener(interaction);
  }
  removeListener(key) {
    const index = this.listeners.findIndex((item) => item.key === key);
    if (index !== -1) {
      this.listeners.splice(index, 1);
    }
  }
  getActiveListener() {
    var _a;
    return ((_a = this.listeners[0]) == null ? void 0 : _a.key) || null;
  }
  setActiveListener(key) {
    const index = this.listeners.findIndex((item) => item.key === key);
    if (index === -1 || index === 0) {
      return;
    }
    const [listener] = this.listeners.splice(index, 1);
    this.listeners.unshift(listener);
  }
  destroy() {
    this.listeners = [];
  }
};
var scanListener = new ScanListener();
var scanListener_default = scanListener;
