const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
const require_asyncToGenerator = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_useEngineContext = require("../../../../hooks/useEngineContext.js");
const require_useActiveExecute = require("../../../ticketBooking/hooks/pisellos/useActiveExecute.js");
const require_context = require("./context.js");
const require_utils = require("./utils.js");
let react = require("react");
react = require_runtime.__toESM(react);
//#region src/components/pay/toB/store/hooks.ts
/**
* 使用商家端支付的Hook
* @throws {Error} 如果在ToBPayProvider外部使用会抛出错误
* @returns {ToBPayContextType} 商家端支付上下文
*/
const useToBPay = () => {
	const context = react.default.useContext(require_context.ToBPayContext);
	if (!context) throw new Error("useToBPay must be used within a ToBPayProvider");
	return context;
};
const useOSPayment = () => {
	return require_useEngineContext.usePisellOS().getModule("payment");
};
/**
* 使用OS支付的Hook
*/
const useOSPaymentEffect = () => {
	const pisellOS = require_useEngineContext.usePisellOS();
	const { setOrder, setPayMethodGroupList } = useToBPay();
	const { executeIfActive } = require_useActiveExecute.useActiveExecute();
	const unlisten1 = (0, react.useRef)(null);
	const unlisten2 = (0, react.useRef)(null);
	const osPaymentEffectOn = () => {
		const wrappedOnPaymentMethodsChanged = executeIfActive(onPaymentMethodsChanged);
		const wrappedOnOrderChanged = executeIfActive(onOrderChanged);
		unlisten1.current = pisellOS.effects.on("payment:onPaymentMethodsChanged", wrappedOnPaymentMethodsChanged);
		unlisten2.current = pisellOS.effects.on("payment:onOrderChanged", wrappedOnOrderChanged);
	};
	const osPaymentEffectOff = () => {
		if (unlisten1.current) {
			var _unlisten1$current;
			(_unlisten1$current = unlisten1.current) === null || _unlisten1$current === void 0 || _unlisten1$current.call(unlisten1);
		}
		if (unlisten2.current) {
			var _unlisten2$current;
			(_unlisten2$current = unlisten2.current) === null || _unlisten2$current === void 0 || _unlisten2$current.call(unlisten2);
		}
	};
	const onPaymentMethodsChanged = function() {
		var _ref = require_asyncToGenerator._asyncToGenerator(function* (res) {
			console.log("tob_pay_onPaymentMethodsChanged", res);
			const { newMethods } = res || {};
			if ((newMethods === null || newMethods === void 0 ? void 0 : newMethods.length) > 0) setPayMethodGroupList(require_utils.handlePayMethodList(newMethods));
			else setPayMethodGroupList([]);
		});
		return function onPaymentMethodsChanged(_x) {
			return _ref.apply(this, arguments);
		};
	}();
	const onOrderChanged = function() {
		var _ref2 = require_asyncToGenerator._asyncToGenerator(function* (res) {
			console.log("tob_pay_onOrderChanged", res);
			setOrder((res === null || res === void 0 ? void 0 : res.order) || {});
		});
		return function onOrderChanged(_x2) {
			return _ref2.apply(this, arguments);
		};
	}();
	return {
		osPaymentEffectOn,
		osPaymentEffectOff
	};
};
//#endregion
exports.useOSPayment = useOSPayment;
exports.useOSPaymentEffect = useOSPaymentEffect;
exports.useToBPay = useToBPay;
