import { _asyncToGenerator } from "../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import { handleGoToStep } from "../components/appointmentBooking/utils.js";
import { useCallback } from "react";
//#region src/hooks/useCartProductStockCheck.ts
/**
* 下单接口商品库存检测逻辑
* 购物车不可用商品清理 + 返回清空缓存 Hook
*/
const useCartProductStockCheck = () => {
	/**
	* 过滤不可用商品并更新购物车
	* @param {Array} cartProducts - 当前购物车商品列表
	* @param {Object} data - 接口返回的不可用商品数据
	* @param {Function} dispatch - redux dispatch
	* @param {Function} [callback] - 过滤完成后的回调
	* @returns {Array} 过滤后的新购物车
	*/
	const filterUnavailableProducts = function() {
		var _ref = _asyncToGenerator(function* (cartProducts, data, callback, appointmentBooking) {
			if (!(cartProducts === null || cartProducts === void 0 ? void 0 : cartProducts.length) || !(data === null || data === void 0 ? void 0 : data.length)) return;
			const unavailableIds = data.map((item) => item === null || item === void 0 ? void 0 : item.product_id);
			let newCart = (cartProducts === null || cartProducts === void 0 ? void 0 : cartProducts.filter((item) => {
				var _item$_origin;
				return unavailableIds.includes((item === null || item === void 0 || (_item$_origin = item._origin) === null || _item$_origin === void 0 || (_item$_origin = _item$_origin.product) === null || _item$_origin === void 0 ? void 0 : _item$_origin.product_id) || (item === null || item === void 0 ? void 0 : item.id));
			})) || [];
			if (appointmentBooking) {
				newCart.forEach((item) => {
					appointmentBooking.deleteCart(item === null || item === void 0 ? void 0 : item._id);
				});
				newCart = yield appointmentBooking.getCart();
			}
			callback && callback(newCart);
			return newCart;
		});
		return function filterUnavailableProducts(_x, _x2, _x3, _x4) {
			return _ref.apply(this, arguments);
		};
	}();
	/**
	* 处理继续下单：清理不可用商品 + 重新初始化
	* @param preparDetail 购物车详情数据
	* @param data 接口返回的不可用商品数据
	* @param _init 重新初始化方法（外部传入）
	*/
	const handleContinue = ({ cartProducts, data, callback, appointmentBooking }) => {
		filterUnavailableProducts(cartProducts, data, callback, appointmentBooking);
	};
	return {
		handleContinue,
		handleReturn: function() {
			var _ref2 = _asyncToGenerator(function* ({ cartProducts, data, callback, appointmentBooking, setTimes, isSkipClearData = true }) {
				yield filterUnavailableProducts(cartProducts, data, callback, appointmentBooking);
				handleGoToStep({
					appointmentBooking,
					setTimes,
					isSkipClearData
				});
			});
			return function handleReturn(_x5) {
				return _ref2.apply(this, arguments);
			};
		}(),
		checkProductStock: useCallback((productStockModalRef, res, cartProducts) => {
			var _productStockModalRef;
			if (!(productStockModalRef === null || productStockModalRef === void 0 ? void 0 : productStockModalRef.current)) return false;
			const { unavailable_products } = res || {};
			if (!(unavailable_products === null || unavailable_products === void 0 ? void 0 : unavailable_products.length) || !(cartProducts === null || cartProducts === void 0 ? void 0 : cartProducts.length)) return false;
			let _isAllItemsSoldOut = false;
			const unavailableIds = unavailable_products.map((item) => item.product_id);
			_isAllItemsSoldOut = cartProducts === null || cartProducts === void 0 ? void 0 : cartProducts.every((item) => {
				var _item$_origin2;
				return unavailableIds.includes((item === null || item === void 0 || (_item$_origin2 = item._origin) === null || _item$_origin2 === void 0 || (_item$_origin2 = _item$_origin2.product) === null || _item$_origin2 === void 0 ? void 0 : _item$_origin2.product_id) || (item === null || item === void 0 ? void 0 : item.id));
			});
			return (_productStockModalRef = productStockModalRef.current) === null || _productStockModalRef === void 0 ? void 0 : _productStockModalRef.open({
				unavailableProducts: unavailable_products,
				isAllItemsSoldOut: _isAllItemsSoldOut
			});
		}, [])
	};
};
//#endregion
export { useCartProductStockCheck as default };
