const require_runtime = require("../../../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
let decimal_js = require("decimal.js");
decimal_js = require_runtime.__toESM(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_js.default(0);
	if (bundles.length > 0) {
		const discountRatio = new decimal_js.default(extend.total).div(extend.origin_total);
		bundles.forEach((bundle) => {
			const discountedPrice = new decimal_js.default(bundle.original_price).mul(discountRatio).toDecimalPlaces(2, decimal_js.default.ROUND_HALF_UP);
			childProductsTotal = childProductsTotal.add(discountedPrice.mul(bundle.quantity));
		});
	}
	const mainProductTotal = new decimal_js.default(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_js.default(totalAdjustment);
	let totalAdjustmentApplied = new decimal_js.default(0);
	const adjustedProducts = [];
	products.forEach((product) => {
		const productPrice = new decimal_js.default(product._extend.origin_total);
		let adjustmentAmount = productPrice.div(totalPrice).times(totalAdjustmentDecimal).toFixed(2, decimal_js.default.ROUND_HALF_UP);
		const newPrice = productPrice.plus(adjustmentAmount).toFixed(2, decimal_js.default.ROUND_HALF_UP);
		const adjustedPrice = new decimal_js.default(newPrice).lessThan(0) ? "0.00" : newPrice;
		const newExtend = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, product._extend), {}, { total: parseFloat(adjustedPrice) });
		updateMainProductSellingPrice(newExtend);
		adjustedProducts.push(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, product), {}, { _extend: newExtend }));
		totalAdjustmentApplied = totalAdjustmentApplied.plus(new decimal_js.default(adjustmentAmount));
	});
	const finalTotalPrice = adjustedProducts.reduce((sum, product) => sum.plus(new decimal_js.default(product._extend.total)), new decimal_js.default(0));
	const adjustmentDifference = new decimal_js.default(totalPrice).minus(finalTotalPrice).plus(new decimal_js.default(totalAdjustment));
	if (!adjustmentDifference.isZero()) {
		let lastProduct = adjustedProducts[adjustedProducts.length - 1];
		let lastPrice = new decimal_js.default(lastProduct._extend.total).plus(adjustmentDifference).toFixed(2, decimal_js.default.ROUND_HALF_UP);
		lastPrice = new decimal_js.default(lastPrice).lessThan(0) ? "0.00" : lastPrice;
		lastProduct._extend.total = parseFloat(lastPrice);
		updateMainProductSellingPrice(lastProduct._extend);
	}
	return adjustedProducts;
}
//#endregion
exports.distributeDiscountOrIncrease = distributeDiscountOrIncrease;
exports.updateMainProductSellingPrice = updateMainProductSellingPrice;
