import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { _objectWithoutProperties } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js";
import Decimal from "decimal.js";
//#region src/components/Sales/Summary/utils.ts
const _excluded = ["selectedBundle", "selectedOptions"];
/**
* 计算商品小计（不含其他费用）
* @param items - 购物车商品数组
* @returns 商品总价字符串，保留2位小数
*/
const calculateSubtotal = (items) => {
	if (!(items === null || items === void 0 ? void 0 : items.length)) return "0.00";
	return items.reduce((sum, item) => {
		return sum.plus(item.total || 0);
	}, new Decimal(0)).toFixed(2);
};
/**
* @description: 将购物车数据格式化成summaryModule需要的格式
* @param {any} items 购物车数据
* @return {*}
*/
const formatCartsToSummaryModule = (items) => {
	if (!Array.isArray(items) || !(items === null || items === void 0 ? void 0 : items.length)) return [];
	return items.map((item) => {
		const { selectedBundle, selectedOptions } = item;
		return _objectSpread2(_objectSpread2({}, _objectWithoutProperties(item, _excluded)), {}, {
			bundle: selectedBundle || [],
			option: selectedOptions || []
		});
	});
};
/**
* @title: 单个商品的税费
* @description:
* 单个商品的税费 = 商品销售单价（折扣后） * 税率 * is_charge_tax /( 1+ 税率 * is_price_include_tax)
* $taxFee = $price * $taxRate * $isChargeTax / (1 + $taxRate * $isPriceIncludeTax);
* @return {*}
* @Author: xiangfeng.xue
*/
const calculateTaxFee = (shopInfo, items, summaryModule) => {
	if (!(items === null || items === void 0 ? void 0 : items.length)) return "0.00";
	const { is_price_include_tax, tax_rate } = shopInfo || {};
	console.log("shopInfo>>>", is_price_include_tax, tax_rate);
	const subtotal = new Decimal(calculateSubtotal(items));
	const totalTax = summaryModule.getTaxforUtils(items, {
		tax_rate: Number(tax_rate),
		is_price_include_tax: Number(is_price_include_tax || 0)
	}, subtotal);
	return totalTax === null || totalTax === void 0 ? void 0 : totalTax.tax;
};
/**
* 计算所有价格明细
* @param items - 购物车商品数组
* @param options - 其他费用配置
* @returns 价格明细对象
*/
const calculatePriceDetails = (shopInfo, items, summaryModule) => {
	const subtotal = new Decimal(calculateSubtotal(items));
	const _carts = formatCartsToSummaryModule(items);
	const surcharge = summaryModule.getSurchargeforUtils(_carts);
	const surchargeFee = new Decimal((surcharge === null || surcharge === void 0 ? void 0 : surcharge.surchargeAmount) || 0);
	const totalTaxFee = new Decimal(calculateTaxFee(shopInfo, _carts, summaryModule));
	const taxRate = new Decimal((shopInfo === null || shopInfo === void 0 ? void 0 : shopInfo.tax_rate) || 0).div(100);
	let total = (shopInfo === null || shopInfo === void 0 ? void 0 : shopInfo.is_price_include_tax) ? subtotal : subtotal.plus(totalTaxFee);
	total = total.plus(surchargeFee);
	const deposit = calculateDeposit(items);
	return {
		subtotal: subtotal.toFixed(2),
		total: total.toFixed(2),
		taxTitle: shopInfo === null || shopInfo === void 0 ? void 0 : shopInfo.tax_title,
		totalTaxFee: totalTaxFee.toFixed(2),
		isPriceIncludeTax: shopInfo === null || shopInfo === void 0 ? void 0 : shopInfo.is_price_include_tax,
		taxRate: taxRate.toNumber(),
		surcharge,
		deposit
	};
};
/**
* @title: 计算定金
* @param items - 购物车商品数组
* @returns 定金字符串，保留2位小数
*/
const calculateDeposit = (items) => {
	if (!(items === null || items === void 0 ? void 0 : items.length)) return;
	const protocols = [];
	let hasDeposit = false;
	const total = items.reduce((sum, item) => {
		if (item === null || item === void 0 ? void 0 : item.deposit) {
			var _item$deposit, _item$deposit2;
			hasDeposit = true;
			const cartItemTotalPrice = new Decimal((item === null || item === void 0 || (_item$deposit = item.deposit) === null || _item$deposit === void 0 ? void 0 : _item$deposit.total) || 0);
			item === null || item === void 0 || (_item$deposit2 = item.deposit) === null || _item$deposit2 === void 0 || (_item$deposit2 = _item$deposit2.protocols) === null || _item$deposit2 === void 0 || _item$deposit2.forEach((protocol) => {
				if (protocols.findIndex((p) => p.id === protocol.id) === -1) protocols.push(protocol);
			});
			return sum.plus(cartItemTotalPrice);
		}
		return sum;
	}, new Decimal(0));
	if (hasDeposit) return {
		total: total.toFixed(2),
		protocols,
		hasDeposit
	};
};
//#endregion
export { calculatePriceDetails };
