const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_asyncToGenerator = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_SalesSdkContext = require("./SalesSdkContext.js");
let react = require("react");
react = require_runtime.__toESM(react);
let ahooks = require("ahooks");
//#region src/plus/salesSdk/context/SalesSdkProductContext.tsx
const SalesSdkProductContext = (0, react.createContext)(null);
SalesSdkProductContext.displayName = "SalesSdkProductContext";
const EMPTY_PRODUCTS = [];
/**
* 商品列表 Provider。
*
* 订阅源：`bookingTicket:onProductsLoaded`（OS 在 loadProducts 完成后 emit）。
*
* products 读取 BookingTicketImpl.getProductCatalog() 的 UI 视图；
* React state 仅作为订阅后的渲染镜像。
*
* Action 引用纪律：每个 action 用 `useMemoizedFn` 单独包装；详见 docs.md "Action 引用稳定性纪律"。
*/
const SalesSdkProductProvider = ({ children }) => {
	var _sales$bookingTicket;
	const sales = (0, react.useContext)(require_SalesSdkContext.SalesSdkContext);
	const bookingTicket = (_sales$bookingTicket = sales === null || sales === void 0 ? void 0 : sales.bookingTicket) !== null && _sales$bookingTicket !== void 0 ? _sales$bookingTicket : null;
	const [products, setProducts] = (0, react.useState)(EMPTY_PRODUCTS);
	const [loading, setLoading] = (0, react.useState)(false);
	const [error, setError] = (0, react.useState)(null);
	(0, react.useEffect)(() => {
		var _bookingTicket$getPro, _bookingTicket$getPro2;
		setProducts((_bookingTicket$getPro = bookingTicket === null || bookingTicket === void 0 || (_bookingTicket$getPro2 = bookingTicket.getProductCatalog) === null || _bookingTicket$getPro2 === void 0 ? void 0 : _bookingTicket$getPro2.call(bookingTicket).products) !== null && _bookingTicket$getPro !== void 0 ? _bookingTicket$getPro : EMPTY_PRODUCTS);
	}, [bookingTicket]);
	(0, react.useEffect)(() => {
		if (!bookingTicket) return;
		const off = bookingTicket.effectsOn("onProductsLoaded", (payload) => {
			var _bookingTicket$getPro3, _catalog$products;
			const catalog = (_bookingTicket$getPro3 = bookingTicket.getProductCatalog) === null || _bookingTicket$getPro3 === void 0 ? void 0 : _bookingTicket$getPro3.call(bookingTicket);
			setProducts((_catalog$products = catalog === null || catalog === void 0 ? void 0 : catalog.products) !== null && _catalog$products !== void 0 ? _catalog$products : Array.isArray(payload) ? payload : EMPTY_PRODUCTS);
		});
		return () => {
			try {
				off();
			} catch (_unused) {}
		};
	}, [bookingTicket]);
	const load = (0, ahooks.useMemoizedFn)(require_asyncToGenerator._asyncToGenerator(function* (params = {}, options) {
		if (!bookingTicket) throw new Error("[SalesSdk] Products: bookingTicket 未初始化");
		setLoading(true);
		setError(null);
		try {
			var _ref, _bookingTicket$getPro4, _bookingTicket$getPro5;
			const list = yield bookingTicket.loadProducts(params, { subscriberId: options === null || options === void 0 ? void 0 : options.subscriberId });
			setProducts((_ref = (_bookingTicket$getPro4 = (_bookingTicket$getPro5 = bookingTicket.getProductCatalog) === null || _bookingTicket$getPro5 === void 0 ? void 0 : _bookingTicket$getPro5.call(bookingTicket).products) !== null && _bookingTicket$getPro4 !== void 0 ? _bookingTicket$getPro4 : list) !== null && _ref !== void 0 ? _ref : EMPTY_PRODUCTS);
			return list || EMPTY_PRODUCTS;
		} catch (e) {
			var _e$message;
			const err = e instanceof Error ? e : new Error(String((_e$message = e === null || e === void 0 ? void 0 : e.message) !== null && _e$message !== void 0 ? _e$message : e));
			setError(err);
			throw err;
		} finally {
			setLoading(false);
		}
	}));
	const loadDetail = (0, ahooks.useMemoizedFn)(function() {
		var _ref2 = require_asyncToGenerator._asyncToGenerator(function* (params) {
			if (!bookingTicket) throw new Error("[SalesSdk] Products: bookingTicket 未初始化");
			return bookingTicket.loadProductDetail(params);
		});
		return function(_x) {
			return _ref2.apply(this, arguments);
		};
	}());
	const findByIds = (0, ahooks.useMemoizedFn)(function() {
		var _ref3 = require_asyncToGenerator._asyncToGenerator(function* (ids) {
			var _bookingTicket$getPro6, _bookingTicket$getPro7;
			if (!bookingTicket) throw new Error("[SalesSdk] Products: bookingTicket 未初始化");
			const result = yield bookingTicket.getProductByIds(ids);
			setProducts((_bookingTicket$getPro6 = (_bookingTicket$getPro7 = bookingTicket.getProductCatalog) === null || _bookingTicket$getPro7 === void 0 ? void 0 : _bookingTicket$getPro7.call(bookingTicket).products) !== null && _bookingTicket$getPro6 !== void 0 ? _bookingTicket$getPro6 : products);
			return result || EMPTY_PRODUCTS;
		});
		return function(_x2) {
			return _ref3.apply(this, arguments);
		};
	}());
	const scanGlobalListener = (0, ahooks.useMemoizedFn)((cb) => {
		if (!bookingTicket) return () => {};
		return bookingTicket.scanGlobalListener(cb);
	});
	const scanCustomerListener = (0, ahooks.useMemoizedFn)((cb) => {
		if (!bookingTicket) return () => {};
		return bookingTicket.scanCustomerListener(cb);
	});
	const scanUniversal = (0, ahooks.useMemoizedFn)((cb, key) => {
		if (!bookingTicket) return () => {};
		return bookingTicket.scanUniversalListener(cb, key);
	});
	const activateCamera = (0, ahooks.useMemoizedFn)((data) => {
		bookingTicket === null || bookingTicket === void 0 || bookingTicket.activateCamera(data);
	});
	const enableScan = (0, ahooks.useMemoizedFn)(() => {
		bookingTicket === null || bookingTicket === void 0 || bookingTicket.enableAllScanListeners();
	});
	const disableScan = (0, ahooks.useMemoizedFn)(() => {
		bookingTicket === null || bookingTicket === void 0 || bookingTicket.disableAllScanListeners();
	});
	const value = (0, react.useMemo)(() => ({
		products,
		loading,
		error,
		load,
		loadDetail,
		findByIds,
		scanGlobalListener,
		scanCustomerListener,
		scanUniversal,
		activateCamera,
		enableScan,
		disableScan
	}), [
		products,
		loading,
		error,
		load,
		loadDetail,
		findByIds,
		scanGlobalListener,
		scanCustomerListener,
		scanUniversal,
		activateCamera,
		enableScan,
		disableScan
	]);
	return /* @__PURE__ */ react.default.createElement(SalesSdkProductContext.Provider, { value }, children);
};
//#endregion
exports.SalesSdkProductProvider = SalesSdkProductProvider;
