import { formatScanGlobal } from "../../utils/index.js";
import { useBookingTicket } from "./bookingTicket.js";
import { useActiveExecute } from "./useActiveExecute.js";
import { useRef } from "react";
import { locales } from "@pisell/utils";
import { useToast } from "@pisell/materials";
//#region src/components/ticketBooking/hooks/pisellos/useScanGlobal.ts
const useScanGlobal = () => {
	const bookingTicket = useBookingTicket();
	const scanRef = useRef(null);
	const Toast = useToast();
	const { executeIfActive } = useActiveExecute();
	const handleScanGlobal = (callback) => {
		var _bookingTicket$initPe;
		bookingTicket === null || bookingTicket === void 0 || (_bookingTicket$initPe = bookingTicket.initPeripheralsListener) === null || _bookingTicket$initPe === void 0 || _bookingTicket$initPe.call(bookingTicket);
		scanRef.current = bookingTicket === null || bookingTicket === void 0 ? void 0 : bookingTicket.scanGlobalListener(executeIfActive((data) => {
			console.log("scanGlobalListener>>>>>>>", data);
			if (data.status === "failed") {
				Toast.fail(locales.getText("pisell2.text.scan-global-failed"));
				return;
			}
			callback(formatScanGlobal(data));
		}));
	};
	return {
		openScan: handleScanGlobal,
		closeScan: () => {
			var _scanRef$current, _scanRef$current$remo;
			(_scanRef$current = scanRef.current) === null || _scanRef$current === void 0 || (_scanRef$current$remo = _scanRef$current.remove) === null || _scanRef$current$remo === void 0 || _scanRef$current$remo.call(_scanRef$current);
		}
	};
};
//#endregion
export { useScanGlobal as default };
