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);
let lodash = require("lodash");
//#region src/plus/skuOptionsSelection/utils/calculateOption.ts
const calculateOptionItem = (params) => {
	var _optionItemValue$data, _newOriginal$_data, _newOriginal$_data$fo;
	const { editIndex, optionItemValue, optionItemDataSource, data } = params;
	const currentData = (optionItemValue === null || optionItemValue === void 0 || (_optionItemValue$data = optionItemValue.data) === null || _optionItemValue$data === void 0 ? void 0 : _optionItemValue$data._data) || [];
	const uniqueKey = optionItemDataSource === null || optionItemDataSource === void 0 ? void 0 : optionItemDataSource._key;
	let newOriginal = (0, lodash.cloneDeep)((optionItemDataSource === null || optionItemDataSource === void 0 ? void 0 : optionItemDataSource._original) || {});
	if (data === null || data === void 0 ? void 0 : data.quantity) if (editIndex === -1) newOriginal._data = [...currentData, require_objectSpread2._objectSpread2({ value: uniqueKey }, data)];
	else {
		currentData[editIndex] = require_objectSpread2._objectSpread2({ value: uniqueKey }, data);
		newOriginal._data = [...currentData];
	}
	else if (editIndex !== -1) {
		currentData.splice(editIndex, 1);
		newOriginal._data = [...currentData];
	}
	let totalQuantity = 0;
	let totalPrice = new decimal_js.default(0);
	newOriginal === null || newOriginal === void 0 || (_newOriginal$_data = newOriginal._data) === null || _newOriginal$_data === void 0 || (_newOriginal$_data$fo = _newOriginal$_data.forEach) === null || _newOriginal$_data$fo === void 0 || _newOriginal$_data$fo.call(_newOriginal$_data, (item) => {
		totalQuantity += (item === null || item === void 0 ? void 0 : item.quantity) || 0;
		let itemTotalPrice = new decimal_js.default(0);
		if (Object.prototype.toString.call(item.data) === "[object Object]") Object.keys(item.data).forEach((key) => {
			var _item$data;
			const itemDataItem = (_item$data = item.data) === null || _item$data === void 0 ? void 0 : _item$data[key];
			console.log("newOriginal_item_key>>>>>", key, itemDataItem);
			let tempPrice = new decimal_js.default(0);
			if (Array.isArray(itemDataItem)) itemDataItem.forEach((_item) => {
				var _item$data2, _item$data3;
				tempPrice = tempPrice.plus(new decimal_js.default((_item === null || _item === void 0 || (_item$data2 = _item.data) === null || _item$data2 === void 0 ? void 0 : _item$data2.price) || 0).times(new decimal_js.default((_item === null || _item === void 0 || (_item$data3 = _item.data) === null || _item$data3 === void 0 ? void 0 : _item$data3.quantity) || 0)));
			});
			else {
				var _itemDataItem$data, _itemDataItem$data2;
				tempPrice = new decimal_js.default((itemDataItem === null || itemDataItem === void 0 || (_itemDataItem$data = itemDataItem.data) === null || _itemDataItem$data === void 0 ? void 0 : _itemDataItem$data.price) || 0).times(new decimal_js.default((itemDataItem === null || itemDataItem === void 0 || (_itemDataItem$data2 = itemDataItem.data) === null || _itemDataItem$data2 === void 0 ? void 0 : _itemDataItem$data2.quantity) || 0));
			}
			itemTotalPrice = itemTotalPrice.plus(tempPrice);
		});
		totalPrice = totalPrice.plus(itemTotalPrice.times(new decimal_js.default((item === null || item === void 0 ? void 0 : item.quantity) || 0)));
	});
	const _pricePrefix = (newOriginal === null || newOriginal === void 0 ? void 0 : newOriginal.price_type) === "markdown" ? -1 : 1;
	totalPrice = totalPrice.plus(new decimal_js.default((newOriginal === null || newOriginal === void 0 ? void 0 : newOriginal.price) || 0).times(new decimal_js.default(totalQuantity || 0)).times(_pricePrefix));
	const totalPriceNumber = totalPrice.toNumber();
	newOriginal = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, newOriginal), {}, {
		quantity: totalQuantity,
		num: totalQuantity
	});
	return [
		newOriginal,
		totalPriceNumber,
		totalQuantity
	];
};
//#endregion
exports.calculateOptionItem = calculateOptionItem;
