import { _objectSpread2 } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { _asyncToGenerator } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import useEngineContext, { usePisellOS } from "../../../../hooks/useEngineContext.js";
import { useSales } from "../../../../businessModel/SalesModel/Sales/index.js";
import { useCart } from "../../../Sales/Cart/index.js";
import { getProductPrice } from "../../../Sales/Event/serve.js";
import { useRequest } from "ahooks";
//#region src/components/eventBooking/components/Voucher/hooks.ts
const useShopDiscount = () => {
	return usePisellOS().getModule("shopDiscount:eventBooking");
};
/**
* @title 登录后重新获取应用报价单后的价格
*/
const useVoucherLogin = () => {
	var _context$appHelper;
	const { getStore } = ((_context$appHelper = useEngineContext().appHelper) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.utils) || {};
	const { setData } = useSales();
	const { cartData } = useCart();
	/**
	* 登录后根据返回商品价格更新cartItem price
	*/
	const _getProductPrice = useRequest(getProductPrice, {
		manual: true,
		onSuccess: (res) => {
			const productById = (res.data || []).reduce((pre, cur) => {
				pre[cur.id] = cur;
				return pre;
			}, {});
			console.log("start update price");
			const newCartItems = cartData.cartItems.map((item) => {
				const { product } = item._originData || {};
				let newProduct = productById[product.product_id];
				let _totalPrice = Number(newProduct.price);
				if (!(item === null || item === void 0 ? void 0 : item._productDetail)) {
					item.price = _totalPrice;
					item.total = _totalPrice;
					return item;
				}
				const { bundle = [], options } = item._productDetail;
				if (Array.isArray(product === null || product === void 0 ? void 0 : product.product_bundle) && product.product_bundle.length > 0) {
					const { bundle_group = [] } = newProduct;
					for (let i = 0; i < product.product_bundle.length; i++) {
						var _targetBundleGroup$bu;
						const { bundle_group_id, bundle_id, option } = product.product_bundle[i];
						const _originBundleItemInfo = bundle.find((bi) => bi.id === bundle_id);
						const _targetBundleGroup = bundle_group.find((bg) => bg.id === bundle_group_id);
						if (_targetBundleGroup === null || _targetBundleGroup === void 0 || (_targetBundleGroup$bu = _targetBundleGroup.bundle_item) === null || _targetBundleGroup$bu === void 0 ? void 0 : _targetBundleGroup$bu.length) {
							let _newBundleItemInfo = _targetBundleGroup.bundle_item.find((bi) => bi.id === bundle_id);
							if ((_originBundleItemInfo === null || _originBundleItemInfo === void 0 ? void 0 : _originBundleItemInfo._bundle_product_id) === (_newBundleItemInfo === null || _newBundleItemInfo === void 0 ? void 0 : _newBundleItemInfo.bundle_product_id)) {
								const { price_type, num = 1, option: _originBundleItemOptions } = _originBundleItemInfo;
								const _bundleItemPrice = Number(_newBundleItemInfo.price);
								if (price_type === "markup") _totalPrice += _bundleItemPrice * num;
								else if (price_type === "markdown") _totalPrice -= _bundleItemPrice * num;
								else _totalPrice += _bundleItemPrice * num;
								if (Array.isArray(option) && (option === null || option === void 0 ? void 0 : option.length) > 0) for (let j = 0; j < option.length; j++) {
									const { option_group_item_id } = option[j];
									const _originOptionItem = _originBundleItemOptions.find((op) => op.product_option_item_id === option_group_item_id);
									if (_originOptionItem) {
										const { price, num: _optionNum = 1 } = _originOptionItem;
										_totalPrice += Number(price) * _optionNum;
									}
								}
							}
						}
					}
				} else {
					if (product === null || product === void 0 ? void 0 : product.product_variant_id) {
						const { variant = [] } = newProduct;
						const _variant = variant.find((v) => v.id === product.product_variant_id);
						if (_variant) _totalPrice = Number(_variant.price);
					}
					if (Array.isArray(product === null || product === void 0 ? void 0 : product.product_option_item) && product.product_option_item.length > 0) for (let i = 0; i < product.product_option_item.length; i++) {
						const { option_group_item_id } = product.product_option_item[i];
						const _originOptionItem = options.find((op) => op.product_option_item_id === option_group_item_id);
						if (_originOptionItem) {
							const { price, num = 1 } = _originOptionItem;
							_totalPrice += Number(price) * num;
						}
					}
				}
				item.price = _totalPrice;
				item.total = _totalPrice;
				return item;
			});
			console.log("new cart data", newCartItems);
			setData({ cartData: _objectSpread2(_objectSpread2({}, cartData), {}, { cartItems: newCartItems }) });
		}
	});
	return { getCartItemProductsWithQuotation: function() {
		var _ref = _asyncToGenerator(function* () {
			var _store$getState;
			const ids = cartData.cartItems.map((item) => {
				const { product = {} } = item._originData || {};
				const { product_id } = product;
				return product_id;
			}).filter((id) => !!id);
			const store = getStore();
			const customer = store === null || store === void 0 || (_store$getState = store.getState()) === null || _store$getState === void 0 || (_store$getState = _store$getState.global) === null || _store$getState === void 0 ? void 0 : _store$getState.userInfo;
			if (!(ids === null || ids === void 0 ? void 0 : ids.length)) return;
			_getProductPrice.run({
				ids,
				customer_id: customer === null || customer === void 0 ? void 0 : customer.id
			});
		});
		return function getCartItemProductsWithQuotation() {
			return _ref.apply(this, arguments);
		};
	}() };
};
//#endregion
export { useShopDiscount, useVoucherLogin };
