const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_objectWithoutProperties = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js");
const require_store = require("../store/store.js");
let decimal_js = require("decimal.js");
decimal_js = require_runtime.__toESM(decimal_js);
//#region src/plus/skuOptionsSelection/utils/transformSelectorValueToValue.ts
const _excluded = ["_data"];
/**
* 将SelectorValue转换为Value
*/
const transformSelectorValueToValue = (selectorValue) => {
	var _dataSource$bundle_gr, _dataSource$option_gr, _dataSource$variant_g;
	if (!selectorValue) return {};
	const dataSource = require_store.default.getState().dataSource;
	const transformValue = {
		bundle: [],
		option: [],
		product_id: dataSource === null || dataSource === void 0 ? void 0 : dataSource.id,
		product_variant_id: 0,
		product_option_string: ""
	};
	if (dataSource === null || dataSource === void 0 || (_dataSource$bundle_gr = dataSource.bundle_group) === null || _dataSource$bundle_gr === void 0 ? void 0 : _dataSource$bundle_gr.length) {
		var _dataSource$bundle_gr2;
		dataSource === null || dataSource === void 0 || (_dataSource$bundle_gr2 = dataSource.bundle_group) === null || _dataSource$bundle_gr2 === void 0 || _dataSource$bundle_gr2.forEach((bundleGroupItem) => {
			const groupId = bundleGroupItem === null || bundleGroupItem === void 0 ? void 0 : bundleGroupItem.id;
			const groupItemValue = selectorValue === null || selectorValue === void 0 ? void 0 : selectorValue[groupId];
			if (!groupId || !groupItemValue) return;
			(Array.isArray(groupItemValue) ? groupItemValue : [groupItemValue]).forEach((item) => {
				const _ref = (item === null || item === void 0 ? void 0 : item.data) || {}, { _data } = _ref, rest = require_objectWithoutProperties._objectWithoutProperties(_ref, _excluded);
				if (_data) _data === null || _data === void 0 || _data.forEach((_item) => {
					if (_item === null || _item === void 0 ? void 0 : _item.data) {
						let _newData = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, rest), {}, { option: [] });
						let totalPrice = new decimal_js.default(0);
						Object.keys(_item === null || _item === void 0 ? void 0 : _item.data).forEach((key) => {
							var _item$data;
							const _itemDataItem = _item === null || _item === void 0 || (_item$data = _item.data) === null || _item$data === void 0 ? void 0 : _item$data[key];
							if (Array.isArray(_itemDataItem)) _itemDataItem.forEach((_itemDataItem_item) => {
								var _itemDataItem_item$da;
								totalPrice = totalPrice.plus(new decimal_js.default((_itemDataItem_item === null || _itemDataItem_item === void 0 ? void 0 : _itemDataItem_item.totalPrice) || 0));
								_newData.option.push(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, (_itemDataItem_item === null || _itemDataItem_item === void 0 ? void 0 : _itemDataItem_item.data) || {}), {}, { product_option_item_id: _itemDataItem_item === null || _itemDataItem_item === void 0 || (_itemDataItem_item$da = _itemDataItem_item.data) === null || _itemDataItem_item$da === void 0 ? void 0 : _itemDataItem_item$da.id }));
							});
							else {
								var _itemDataItem$data;
								totalPrice = totalPrice.plus(new decimal_js.default((_itemDataItem === null || _itemDataItem === void 0 ? void 0 : _itemDataItem.totalPrice) || 0));
								_newData.option.push(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, (_itemDataItem === null || _itemDataItem === void 0 ? void 0 : _itemDataItem.data) || {}), {}, { product_option_item_id: _itemDataItem === null || _itemDataItem === void 0 || (_itemDataItem$data = _itemDataItem.data) === null || _itemDataItem$data === void 0 ? void 0 : _itemDataItem$data.id }));
							}
						});
						const _restPricePrefix = (rest === null || rest === void 0 ? void 0 : rest.price_type) === "markdown" ? -1 : 1;
						_newData.price = totalPrice.plus(new decimal_js.default((rest === null || rest === void 0 ? void 0 : rest.price) || 0).times(_restPricePrefix)).toString();
						_newData.original_price = _newData === null || _newData === void 0 ? void 0 : _newData.price;
						_newData.original_total = _newData === null || _newData === void 0 ? void 0 : _newData.price;
						_newData.quantity = (_item === null || _item === void 0 ? void 0 : _item.quantity) || 0;
						_newData.num = (_item === null || _item === void 0 ? void 0 : _item.quantity) || 0;
						_newData._bundle_product_id = _newData === null || _newData === void 0 ? void 0 : _newData.bundle_product_id;
						transformValue.bundle.push(_newData);
					}
				});
				else {
					const _newData = require_objectSpread2._objectSpread2({}, (item === null || item === void 0 ? void 0 : item.data) || {});
					const _pricePrefix = (_newData === null || _newData === void 0 ? void 0 : _newData.price_type) === "markdown" ? -1 : 1;
					_newData.price = new decimal_js.default((_newData === null || _newData === void 0 ? void 0 : _newData.price) || 0).times(_pricePrefix).toString();
					_newData.original_price = _newData === null || _newData === void 0 ? void 0 : _newData.price;
					_newData.original_total = _newData === null || _newData === void 0 ? void 0 : _newData.price;
					_newData._bundle_product_id = _newData === null || _newData === void 0 ? void 0 : _newData.bundle_product_id;
					transformValue.bundle.push(_newData);
				}
			});
		});
	}
	if (dataSource === null || dataSource === void 0 || (_dataSource$option_gr = dataSource.option_group) === null || _dataSource$option_gr === void 0 ? void 0 : _dataSource$option_gr.length) {
		var _dataSource$option_gr2;
		dataSource === null || dataSource === void 0 || (_dataSource$option_gr2 = dataSource.option_group) === null || _dataSource$option_gr2 === void 0 || _dataSource$option_gr2.forEach((optionGroupItem) => {
			const groupId = optionGroupItem === null || optionGroupItem === void 0 ? void 0 : optionGroupItem.id;
			const groupItemValue = selectorValue === null || selectorValue === void 0 ? void 0 : selectorValue[groupId];
			if (!groupId || !groupItemValue) return;
			let _groupItemValue = null;
			if (Array.isArray(groupItemValue)) _groupItemValue = groupItemValue;
			else _groupItemValue = [groupItemValue];
			_groupItemValue === null || _groupItemValue === void 0 || _groupItemValue.forEach((item) => {
				if (item === null || item === void 0 ? void 0 : item.data) {
					var _item$data2;
					transformValue.option.push(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, (item === null || item === void 0 ? void 0 : item.data) || {}), {}, { product_option_item_id: item === null || item === void 0 || (_item$data2 = item.data) === null || _item$data2 === void 0 ? void 0 : _item$data2.id }));
				}
			});
		});
	}
	if (dataSource === null || dataSource === void 0 || (_dataSource$variant_g = dataSource.variant_group) === null || _dataSource$variant_g === void 0 ? void 0 : _dataSource$variant_g.length) {
		var _dataSource$variant_g2;
		const selectedVariantIds = [];
		const selectedVariantOptionString = [];
		dataSource === null || dataSource === void 0 || (_dataSource$variant_g2 = dataSource.variant_group) === null || _dataSource$variant_g2 === void 0 || _dataSource$variant_g2.forEach((variantGroupItem) => {
			const groupId = variantGroupItem === null || variantGroupItem === void 0 ? void 0 : variantGroupItem.id;
			const groupItemValue = selectorValue === null || selectorValue === void 0 ? void 0 : selectorValue[groupId];
			if (!groupId || !groupItemValue) return;
			let _groupItemValue = null;
			if (Array.isArray(groupItemValue)) _groupItemValue = groupItemValue;
			else _groupItemValue = [groupItemValue];
			_groupItemValue === null || _groupItemValue === void 0 || _groupItemValue.forEach((item) => {
				if (item === null || item === void 0 ? void 0 : item.data) {
					var _item$data3, _item$data4, _item$data5;
					selectedVariantIds.push(`${item === null || item === void 0 || (_item$data3 = item.data) === null || _item$data3 === void 0 ? void 0 : _item$data3.variant_group_id}:${item === null || item === void 0 || (_item$data4 = item.data) === null || _item$data4 === void 0 ? void 0 : _item$data4.id}`);
					selectedVariantOptionString.push(`${variantGroupItem === null || variantGroupItem === void 0 ? void 0 : variantGroupItem.name}:${item === null || item === void 0 || (_item$data5 = item.data) === null || _item$data5 === void 0 ? void 0 : _item$data5.name}`);
				}
			});
		});
		if (selectedVariantIds === null || selectedVariantIds === void 0 ? void 0 : selectedVariantIds.length) {
			var _dataSource$variant;
			const filteredVariant = dataSource === null || dataSource === void 0 || (_dataSource$variant = dataSource.variant) === null || _dataSource$variant === void 0 ? void 0 : _dataSource$variant.filter((item) => {
				var _item$sku_ids;
				const ids = (item === null || item === void 0 || (_item$sku_ids = item.sku_ids) === null || _item$sku_ids === void 0 ? void 0 : _item$sku_ids.split("_")) || [];
				return selectedVariantIds.every((idItem) => ids.includes(idItem));
			});
			if (filteredVariant === null || filteredVariant === void 0 ? void 0 : filteredVariant.length) {
				var _filteredVariant$;
				transformValue.product_variant_id = (_filteredVariant$ = filteredVariant[0]) === null || _filteredVariant$ === void 0 ? void 0 : _filteredVariant$.id;
				transformValue.product_option_string = selectedVariantOptionString.join(", ");
			}
		}
	}
	console.log("transformSelectorValueToValue>>>>>", transformValue);
	return transformValue;
};
//#endregion
exports.transformSelectorValueToValue = transformSelectorValueToValue;
