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