import { _objectSpread2 } from "../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { getBundleDiscountList } from "../../../components/footer/utils.js";
import { isArr, isBoolean } from "@pisell/utils";
import Decimal from "decimal.js";
//#region src/components/booking/info2/service/editService/utils.tsx
/**
1. 在进入编辑服务前

计算出各种需要的列表

设置默认值

判断是否需要进入编辑服务页
*/
const getAutoAllocationStorage = (id) => {
	const value = localStorage.getItem("resource-auto-allocation");
	if (value) {
		const valueObj = JSON.parse(value);
		return isBoolean(valueObj[id]) ? valueObj[id] : true;
	}
	return true;
};
const setAutoAllocationStorage = (data) => {
	const value = localStorage.getItem("resource-auto-allocation");
	if (value) {
		const valueObj = JSON.parse(value);
		localStorage.setItem("resource-auto-allocation", JSON.stringify(_objectSpread2(_objectSpread2({}, valueObj), data)));
	} else localStorage.setItem("resource-auto-allocation", JSON.stringify(_objectSpread2({}, data)));
};
/**
* 手动折扣后给商品价格和子商品价格重新计算
*
* 计算逻辑：
* 1. 子商品（bundle + option）按折扣比例计算，直接截取两位小数（不四舍五入）
* 2. 主商品价格 = newTotal - 所有子商品价格总和（确保总价精确等于 newTotal）
*
* @param cacheItem - 缓存的商品项
* @param newTotal - 折扣后的新总价
* @param origin_total - 原始总价
*/
const setProductPrice = (cacheItem, { newTotal, origin_total }) => {
	var _cacheItem$discount_l, _cacheItem$_extend$ot, _cacheItem$_extend$ot2, _cacheItem$_extend$ot4, _cacheItem$_extend$ot5, _cacheItem$_extend;
	if ((cacheItem === null || cacheItem === void 0 || (_cacheItem$discount_l = cacheItem.discount_list) === null || _cacheItem$discount_l === void 0 ? void 0 : _cacheItem$discount_l.length) && ((cacheItem === null || cacheItem === void 0 ? void 0 : cacheItem.discount_list) || []).some((item) => [
		"good_pass",
		"discount_card",
		"product_discount_card"
	].includes(item.type))) return cacheItem;
	if (cacheItem.discount_amount) return cacheItem;
	const discountRatio = new Decimal(newTotal).div(new Decimal(origin_total));
	let subTotal = new Decimal(0);
	if (((_cacheItem$_extend$ot = cacheItem._extend.other) === null || _cacheItem$_extend$ot === void 0 ? void 0 : _cacheItem$_extend$ot.bundle) && isArr((_cacheItem$_extend$ot2 = cacheItem._extend.other) === null || _cacheItem$_extend$ot2 === void 0 ? void 0 : _cacheItem$_extend$ot2.bundle)) {
		var _cacheItem$_extend$ot3;
		(_cacheItem$_extend$ot3 = cacheItem._extend.other) === null || _cacheItem$_extend$ot3 === void 0 || (_cacheItem$_extend$ot3 = _cacheItem$_extend$ot3.bundle) === null || _cacheItem$_extend$ot3 === void 0 || _cacheItem$_extend$ot3.forEach((bundle) => {
			var _ref, _bundle$num;
			const newPrice = new Decimal(bundle.original_price || bundle.price).mul(discountRatio).toDecimalPlaces(2, Decimal.ROUND_DOWN).toNumber();
			const quantity = (_ref = (_bundle$num = bundle.num) !== null && _bundle$num !== void 0 ? _bundle$num : bundle.quantity) !== null && _ref !== void 0 ? _ref : 1;
			subTotal = subTotal.add(new Decimal(newPrice).mul(quantity));
			bundle.price = newPrice;
		});
	}
	if (((_cacheItem$_extend$ot4 = cacheItem._extend.other) === null || _cacheItem$_extend$ot4 === void 0 ? void 0 : _cacheItem$_extend$ot4.option) && isArr((_cacheItem$_extend$ot5 = cacheItem._extend.other) === null || _cacheItem$_extend$ot5 === void 0 ? void 0 : _cacheItem$_extend$ot5.option)) {
		var _cacheItem$_extend$ot6;
		(_cacheItem$_extend$ot6 = cacheItem._extend.other) === null || _cacheItem$_extend$ot6 === void 0 || (_cacheItem$_extend$ot6 = _cacheItem$_extend$ot6.option) === null || _cacheItem$_extend$ot6 === void 0 || _cacheItem$_extend$ot6.forEach((option) => {
			var _ref2, _option$num;
			const newPrice = new Decimal(option.price).mul(discountRatio).toDecimalPlaces(2, Decimal.ROUND_DOWN).toNumber();
			const quantity = (_ref2 = (_option$num = option.num) !== null && _option$num !== void 0 ? _option$num : option.quantity) !== null && _ref2 !== void 0 ? _ref2 : 1;
			subTotal = subTotal.add(new Decimal(newPrice).mul(quantity));
			option.price = newPrice;
		});
	}
	cacheItem._extend.price = new Decimal(newTotal).sub(subTotal).toNumber();
	if ((_cacheItem$_extend = cacheItem._extend) === null || _cacheItem$_extend === void 0 ? void 0 : _cacheItem$_extend.priceOverride) {
		var _cacheItem$_extend2;
		cacheItem._extend.total = new Decimal(((_cacheItem$_extend2 = cacheItem._extend) === null || _cacheItem$_extend2 === void 0 ? void 0 : _cacheItem$_extend2.priceOverride) || 0).toNumber();
	}
	return cacheItem;
};
/**
* 获取商品所有折扣列表 包含子商品
* @param cacheItem - 缓存的商品项
* @returns 折扣列表
*/
const getProductAllDiscountList = (cacheItem) => {
	var _cacheItem$_extend3;
	let discountList = [];
	discountList = cacheItem.discount_list || [];
	const bundleDiscountList = getBundleDiscountList(((_cacheItem$_extend3 = cacheItem._extend) === null || _cacheItem$_extend3 === void 0 || (_cacheItem$_extend3 = _cacheItem$_extend3.other) === null || _cacheItem$_extend3 === void 0 ? void 0 : _cacheItem$_extend3.bundle) || []) || [];
	discountList.push(...bundleDiscountList);
	return discountList;
};
const mergeBundles = (other) => {
	const bundle = (other === null || other === void 0 ? void 0 : other.bundle) || [];
	const newBundle = [];
	bundle.forEach((item) => {
		const newItem = newBundle.find((newItem) => newItem.id === item.id);
		if (newItem) {
			newItem.num += item.num || 1;
			newItem.quantity += item.quantity || 1;
		} else newBundle.push(_objectSpread2({}, item));
	});
	return newBundle;
};
/**
* 合并 bundle 数据，基于 id 和 product_id 匹配，price 优先使用 originalOther 中的值
* @param other - callback 返回的新数据
* @param originalOther - 编辑前的原始数据 (item._extend.other)
* @returns 合并后的 other
*/
const mergeBundleWithOriginalPrice = (other, originalOther) => {
	var _other$bundle, _originalOther$bundle;
	if (!(other === null || other === void 0 || (_other$bundle = other.bundle) === null || _other$bundle === void 0 ? void 0 : _other$bundle.length) || !(originalOther === null || originalOther === void 0 || (_originalOther$bundle = originalOther.bundle) === null || _originalOther$bundle === void 0 ? void 0 : _originalOther$bundle.length)) return other;
	other.bundle = other.bundle.map((bundle) => {
		const matched = originalOther.bundle.find((orig) => orig.id === bundle.id && orig.product_id === bundle.product_id);
		if (matched && matched.price !== void 0) {
			var _matched$original_tot, _matched$original_pri, _matched$custom_price;
			return _objectSpread2(_objectSpread2({}, bundle), {}, {
				price: matched.price,
				original_total: (_matched$original_tot = matched === null || matched === void 0 ? void 0 : matched.original_total) !== null && _matched$original_tot !== void 0 ? _matched$original_tot : bundle.original_total,
				original_price: (_matched$original_pri = matched === null || matched === void 0 ? void 0 : matched.original_price) !== null && _matched$original_pri !== void 0 ? _matched$original_pri : bundle.original_price,
				custom_price: (_matched$custom_price = matched === null || matched === void 0 ? void 0 : matched.custom_price) !== null && _matched$custom_price !== void 0 ? _matched$custom_price : bundle.custom_price
			});
		}
		return bundle;
	});
	return other;
};
//#endregion
export { getAutoAllocationStorage, getProductAllDiscountList, mergeBundleWithOriginalPrice, mergeBundles, setAutoAllocationStorage, setProductPrice };
