import { _objectSpread2 } from "../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import Decimal from "decimal.js";
//#region src/components/booking/info/service/serviceManager/distributeDiscount.ts
/**
* 根据 total 与 origin_total 的折扣比例，重新计算主商品单价 main_product_selling_price
* 如果存在子商品(bundle)，会先按折扣比例计算子商品总价，再用总价减去子商品总价得到主商品总价
*/
function updateMainProductSellingPrice(extend) {
	var _extend$other, _extend$other2;
	const bundles = (extend === null || extend === void 0 || (_extend$other = extend.other) === null || _extend$other === void 0 ? void 0 : _extend$other.bundle) || [];
	let childProductsTotal = new Decimal(0);
	if (bundles.length > 0) {
		const discountRatio = new Decimal(extend.total).div(extend.origin_total);
		bundles.forEach((bundle) => {
			const discountedPrice = new Decimal(bundle.original_price).mul(discountRatio).toDecimalPlaces(2, Decimal.ROUND_HALF_UP);
			childProductsTotal = childProductsTotal.add(discountedPrice.mul(bundle.quantity));
		});
	}
	const mainProductTotal = new Decimal(extend.total).sub(childProductsTotal);
	const mainProductQuantity = ((_extend$other2 = extend.other) === null || _extend$other2 === void 0 ? void 0 : _extend$other2.quantity) || extend.quantity || 1;
	extend.main_product_selling_price = mainProductTotal.div(mainProductQuantity).toNumber();
}
function distributeDiscountOrIncrease(products, totalAdjustment, totalPrice) {
	const totalAdjustmentDecimal = new Decimal(totalAdjustment);
	let totalAdjustmentApplied = new Decimal(0);
	const adjustedProducts = [];
	products.forEach((product) => {
		const productPrice = new Decimal(product._extend.origin_total);
		let adjustmentAmount = productPrice.div(totalPrice).times(totalAdjustmentDecimal).toFixed(2, Decimal.ROUND_HALF_UP);
		const newPrice = productPrice.plus(adjustmentAmount).toFixed(2, Decimal.ROUND_HALF_UP);
		const adjustedPrice = new Decimal(newPrice).lessThan(0) ? "0.00" : newPrice;
		const newExtend = _objectSpread2(_objectSpread2({}, product._extend), {}, { total: parseFloat(adjustedPrice) });
		updateMainProductSellingPrice(newExtend);
		adjustedProducts.push(_objectSpread2(_objectSpread2({}, product), {}, { _extend: newExtend }));
		totalAdjustmentApplied = totalAdjustmentApplied.plus(new Decimal(adjustmentAmount));
	});
	const finalTotalPrice = adjustedProducts.reduce((sum, product) => sum.plus(new Decimal(product._extend.total)), new Decimal(0));
	const adjustmentDifference = new Decimal(totalPrice).minus(finalTotalPrice).plus(new Decimal(totalAdjustment));
	if (!adjustmentDifference.isZero()) {
		let lastProduct = adjustedProducts[adjustedProducts.length - 1];
		let lastPrice = new Decimal(lastProduct._extend.total).plus(adjustmentDifference).toFixed(2, Decimal.ROUND_HALF_UP);
		lastPrice = new Decimal(lastPrice).lessThan(0) ? "0.00" : lastPrice;
		lastProduct._extend.total = parseFloat(lastPrice);
		updateMainProductSellingPrice(lastProduct._extend);
	}
	return adjustedProducts;
}
//#endregion
export { distributeDiscountOrIncrease, updateMainProductSellingPrice };
