import Decimal from "decimal.js";
//#region src/components/Sales/Cart/utils.ts
const handleProductInfo = (eventDetail, num) => {
	const { productDetail, bookingOptionItem } = eventDetail;
	const normalProduct = bookingOptionItem === null || bookingOptionItem === void 0 ? void 0 : bookingOptionItem._originData;
	let price = Number((normalProduct === null || normalProduct === void 0 ? void 0 : normalProduct.price) || 0);
	let totalPrice = Number((normalProduct === null || normalProduct === void 0 ? void 0 : normalProduct.price) || "");
	let originTotal = Number((normalProduct === null || normalProduct === void 0 ? void 0 : normalProduct.original_price) || "");
	let is_charge_tax = normalProduct === null || normalProduct === void 0 ? void 0 : normalProduct.is_charge_tax;
	if (productDetail) {
		const { origin, bundle, options, product_variant_id } = productDetail;
		let variant = null;
		if (product_variant_id) {
			var _origin$variant;
			variant = origin === null || origin === void 0 || (_origin$variant = origin.variant) === null || _origin$variant === void 0 ? void 0 : _origin$variant.find((item) => (item === null || item === void 0 ? void 0 : item.id) === product_variant_id);
			is_charge_tax = variant === null || variant === void 0 ? void 0 : variant.is_charge_tax;
		}
		const params = {
			product: origin,
			variant,
			bundle,
			options,
			num
		};
		price = getPrice(params);
		totalPrice = getTotalPrice(params);
		originTotal = getOriginTotalPrice(params);
	}
	return {
		price,
		totalPrice,
		originTotal,
		is_charge_tax
	};
};
/**
* 获取商品价格
*/
const getPrice = (params) => {
	const { product, variant } = params;
	let price = Number(product.price);
	if (variant) price = Number(variant === null || variant === void 0 ? void 0 : variant.price);
	return price;
};
/**
* 获取商品总价
* @description 当前总价计算基于商品数量为1
* @param item 商品
* @returns 商品总价
*/
const getTotalPrice = (params) => {
	const { product, bundle, options, variant } = params;
	const num = params.num || 1;
	let price = Number(product.price);
	if (variant) price = Number(variant === null || variant === void 0 ? void 0 : variant.price);
	if (bundle === null || bundle === void 0 ? void 0 : bundle.length) price = bundle.reduce((accumulator, currentValue) => {
		return accumulator + Number(currentValue.price) * Number(currentValue.num);
	}, price);
	if (options === null || options === void 0 ? void 0 : options.length) price = options.reduce((accumulator, currentValue) => {
		return accumulator + Number(currentValue.price) * Number(currentValue.num);
	}, price);
	return price * num;
};
/**
* 获取商品原始总价
* @description 当前总价计算基于商品数量为1
* @param item 商品
* @returns 商品原始总价
*/
const getOriginTotalPrice = (params) => {
	const { product, bundle, options, variant } = params;
	const num = (params === null || params === void 0 ? void 0 : params.num) || 1;
	let price = Number(product.original_price);
	if (variant) price = Number(variant === null || variant === void 0 ? void 0 : variant.original_price);
	if (isNaN(price) || price === 0) return;
	if (bundle === null || bundle === void 0 ? void 0 : bundle.length) price = bundle.reduce((accumulator, currentValue) => {
		return accumulator + Number(currentValue.price) * Number(currentValue.num);
	}, price);
	if (options === null || options === void 0 ? void 0 : options.length) price = options.reduce((accumulator, currentValue) => {
		return accumulator + Number(currentValue.price) * Number(currentValue.num);
	}, price);
	return price * num;
};
const handleCartItemOrigin = (cartItem, eventDetail) => {
	const { productDetail } = eventDetail;
	const originData = (cartItem === null || cartItem === void 0 ? void 0 : cartItem._originData) || {};
	if (productDetail) {
		const { bundle, options, product_variant_id } = productDetail;
		if (!originData.product) originData.product = {};
		if (product_variant_id) originData.product.product_variant_id = product_variant_id;
		if (bundle === null || bundle === void 0 ? void 0 : bundle.length) originData.product.product_bundle = formatBundleToOrigin(bundle);
		if (options === null || options === void 0 ? void 0 : options.length) originData.product.product_option_item = formatOptionsToOrigin(options);
	}
	console.log("originData>>>>>>", originData);
	return originData;
};
const formatBundleToOrigin = (bundle) => {
	if (!(bundle === null || bundle === void 0 ? void 0 : bundle.length)) return [];
	return bundle.map((d) => {
		return {
			bundle_group_id: d.group_id,
			bundle_id: d.id,
			bundle_product_id: (d === null || d === void 0 ? void 0 : d._bundle_product_id) || (d === null || d === void 0 ? void 0 : d.product_id),
			bundle_variant_id: d.bundle_variant_id,
			num: d.num,
			extension_id: d.extension_id,
			extension_type: d.extension_type,
			option: formatOptionsToOrigin(d.option)
		};
	});
};
const formatOptionsToOrigin = (options) => {
	if (!(options === null || options === void 0 ? void 0 : options.length)) return [];
	return options.map((d) => {
		return {
			num: d.num,
			option_group_item_id: d.product_option_item_id,
			option_group_id: d.option_group_id
		};
	});
};
const handleProductDeposit = (cartItem, eventDetail, num) => {
	var _origin$custom_deposi;
	const { productDetail, bookingOptionItem } = eventDetail;
	const normalProduct = bookingOptionItem === null || bookingOptionItem === void 0 ? void 0 : bookingOptionItem._originData;
	if (!productDetail && normalProduct) {
		const { custom_deposit_data } = normalProduct;
		if ((custom_deposit_data === null || custom_deposit_data === void 0 ? void 0 : custom_deposit_data.has_deposit) == 1) return getProductDeposit({
			cartItem,
			product: normalProduct,
			num
		});
		else return null;
	}
	if (!productDetail) return null;
	const { origin, bundle, options, product_variant_id } = productDetail || {};
	let deposit = null;
	const product = origin;
	if (product_variant_id) product.product_variant_id = product_variant_id;
	if ((origin === null || origin === void 0 || (_origin$custom_deposi = origin.custom_deposit_data) === null || _origin$custom_deposi === void 0 ? void 0 : _origin$custom_deposi.has_deposit) == 1) deposit = getProductDeposit({
		cartItem,
		product,
		bundle,
		options,
		num
	});
	return deposit;
};
/**
* 获取商品定金
* @description 定金基于商品售价来算，最终乘商品数量
* 1、如果套餐主商品有定金规则，则定金为：（套餐商品总售价 * 定金百分比 + 定金固定金额）* 商品数量
* 2、如果套餐主商品没有定金规则，子商品有定金规则，则定金为：（套餐子商品售价 * 套餐子商品定金百分比 + 套餐子商品定金固定金额）* 商品数量，最终将所有子商品定金相加
* 3、普通商品/单规格商品/组合规则商品，定金规则：（商品总售价 * 定金百分比 + 定金固定金额）* 商品数量
* @param params 参数
* @returns 商品定金
*/
const getProductDeposit = (params) => {
	var _product$custom_depos;
	const { cartItem, product, bundle, options } = params;
	const num = (params === null || params === void 0 ? void 0 : params.num) || 1;
	if ((product === null || product === void 0 || (_product$custom_depos = product.custom_deposit_data) === null || _product$custom_depos === void 0 ? void 0 : _product$custom_depos.has_deposit) == 1) {
		let total = new Decimal(0);
		const { deposit_fixed, deposit_percentage, deposit_policy_data, self_deposit_policy_ids } = (product === null || product === void 0 ? void 0 : product.custom_deposit_data) || {};
		const allProtocols = deposit_policy_data || [];
		let productHasDeposit = false;
		const protocolIds = [];
		if (typeof deposit_fixed === "string" && typeof deposit_percentage === "string") {
			const { depositTotal, result } = calculateProductDeposit({
				depositData: product === null || product === void 0 ? void 0 : product.custom_deposit_data,
				total: (cartItem === null || cartItem === void 0 ? void 0 : cartItem.total) || 0,
				num
			});
			if (result) {
				total = depositTotal;
				protocolIds.push(...self_deposit_policy_ids || []);
				productHasDeposit = true;
			}
		} else {
			if (product === null || product === void 0 ? void 0 : product.product_variant_id) {
				var _product$variant;
				const variantProduct = product === null || product === void 0 || (_product$variant = product.variant) === null || _product$variant === void 0 ? void 0 : _product$variant.find((item) => (item === null || item === void 0 ? void 0 : item.id) === (product === null || product === void 0 ? void 0 : product.product_variant_id));
				if (variantProduct) {
					const depositData = variantProduct === null || variantProduct === void 0 ? void 0 : variantProduct.custom_deposit_data;
					const { depositTotal, result } = calculateProductDeposit({
						depositData,
						total: (cartItem === null || cartItem === void 0 ? void 0 : cartItem.total) || 0,
						num
					});
					if (result) {
						total = depositTotal;
						protocolIds.push(...(depositData === null || depositData === void 0 ? void 0 : depositData.self_deposit_policy_ids) || []);
						productHasDeposit = true;
					}
				}
			}
			if (bundle === null || bundle === void 0 ? void 0 : bundle.length) total = bundle.reduce((accumulator, currBundleProduct) => {
				const depositData = currBundleProduct === null || currBundleProduct === void 0 ? void 0 : currBundleProduct.custom_deposit_data;
				const { depositTotal, result } = calculateProductDeposit({
					depositData,
					total: currBundleProduct === null || currBundleProduct === void 0 ? void 0 : currBundleProduct.price,
					num: (currBundleProduct === null || currBundleProduct === void 0 ? void 0 : currBundleProduct.num) || 1
				});
				if (result) {
					protocolIds.push(...(depositData === null || depositData === void 0 ? void 0 : depositData.self_deposit_policy_ids) || []);
					productHasDeposit = true;
					return accumulator.plus(depositTotal);
				}
				return accumulator;
			}, new Decimal(0));
		}
		if (productHasDeposit) {
			const ids = new Set(protocolIds);
			const protocols = allProtocols.filter((item) => ids.has(item.id));
			return {
				total: total.toNumber(),
				protocols
			};
		}
		return null;
	}
	return null;
};
/**
* 计算商品定金
* @param params 参数
* @returns 商品定金
*/
const calculateProductDeposit = (params) => {
	const { depositData, total, num } = params;
	if (!depositData) return {
		result: false,
		depositTotal: new Decimal(0)
	};
	const { deposit_fixed, deposit_percentage } = depositData || {};
	if (typeof deposit_fixed === "string" && typeof deposit_percentage === "string") {
		const depositFixed = new Decimal(deposit_fixed || 0);
		const depositPercent = new Decimal(deposit_percentage || 0);
		const productTotalPrice = new Decimal(total || 0);
		return {
			depositTotal: depositFixed.plus(depositPercent.times(productTotalPrice)).times(num),
			result: true
		};
	}
	return {
		result: false,
		depositTotal: new Decimal(0)
	};
};
//#endregion
export { handleCartItemOrigin, handleProductDeposit, handleProductInfo };
