import { _defineProperty } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/defineProperty.js";
//#region src/components/ecocup/utils/scanListener.ts
var ScanListener = class {
	constructor() {
		_defineProperty(this, "isInitialized", false);
		_defineProperty(this, "listeners", []);
		this.listeners = [];
	}
	/**
	* 初始化外设扫码结果监听
	*/
	initPeripheralsListener(interaction) {
		var _interaction$utils, _interaction$utils$mo;
		if (this.isInitialized) return;
		this.isInitialized = true;
		interaction === null || interaction === void 0 || (_interaction$utils = interaction.utils) === null || _interaction$utils === void 0 || (_interaction$utils$mo = _interaction$utils.mountFunction) === null || _interaction$utils$mo === void 0 || _interaction$utils$mo.call(_interaction$utils, "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 _this$listeners$;
		return ((_this$listeners$ = this.listeners[0]) === null || _this$listeners$ === void 0 ? void 0 : _this$listeners$.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 = [];
	}
};
const scanListener = new ScanListener();
//#endregion
export { scanListener as default };
