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
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;
		adjustedProducts.push(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, product), {}, { _extend: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, product._extend), {}, { total: parseFloat(adjustedPrice) }) }));
		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);
	}
	return adjustedProducts;
}
//#endregion
exports.distributeDiscountOrIncrease = distributeDiscountOrIncrease;
