import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import Decimal from "decimal.js";
//#region src/plus/skuOptionsSelection/utils/transformValueToSelectorValue.ts
/**
* 将value转换为SelectorGroup需要的格式
*/
const transformValueToSelectorValue = (value, dataSource) => {
	var _Object$keys, _Object$keys2;
	console.log("transformValueToSelectorValue_value>>>>>>>", value);
	if (!value || !dataSource || ((_Object$keys = Object.keys(value || {})) === null || _Object$keys === void 0 ? void 0 : _Object$keys.length) === 0 || ((_Object$keys2 = Object.keys(dataSource || {})) === null || _Object$keys2 === void 0 ? void 0 : _Object$keys2.length) === 0) return {};
	let transformValue = {};
	if (value === null || value === void 0 ? void 0 : value.bundle) {
		var _value$bundle, _newBundle$forEach;
		const newBundle = value === null || value === void 0 || (_value$bundle = value.bundle) === null || _value$bundle === void 0 ? void 0 : _value$bundle.reduce((acc, v) => {
			const _bundleItemId = v === null || v === void 0 ? void 0 : v.id;
			const idx = acc === null || acc === void 0 ? void 0 : acc.findIndex((v1) => (v1 === null || v1 === void 0 ? void 0 : v1.id) === _bundleItemId);
			if (idx !== -1) {
				var _acc$idx, _acc$idx$push;
				(_acc$idx = acc[idx]) === null || _acc$idx === void 0 || (_acc$idx = _acc$idx.bundleOptions) === null || _acc$idx === void 0 || (_acc$idx$push = _acc$idx.push) === null || _acc$idx$push === void 0 || _acc$idx$push.call(_acc$idx, v);
			} else {
				var _acc$push;
				acc === null || acc === void 0 || (_acc$push = acc.push) === null || _acc$push === void 0 || _acc$push.call(acc, {
					id: _bundleItemId,
					groupId: v === null || v === void 0 ? void 0 : v.group_id,
					bundleOptions: [v]
				});
			}
			return acc;
		}, []);
		newBundle === null || newBundle === void 0 || (_newBundle$forEach = newBundle.forEach) === null || _newBundle$forEach === void 0 || _newBundle$forEach.call(newBundle, (v1) => {
			const _bundleGroupId = v1 === null || v1 === void 0 ? void 0 : v1.groupId;
			const _bundleValue = createBundleValue(v1, dataSource);
			if (transformValue[_bundleGroupId]) transformValue[_bundleGroupId] = [...transformValue[_bundleGroupId] || [], _bundleValue];
			else transformValue[_bundleGroupId] = [_bundleValue];
		});
	}
	if (value === null || value === void 0 ? void 0 : value.option) transformValue = createOptionValue(transformValue, value, dataSource);
	if (value === null || value === void 0 ? void 0 : value.product_variant_id) {
		var _dataSource$variant;
		const filteredVariant = dataSource === null || dataSource === void 0 || (_dataSource$variant = dataSource.variant) === null || _dataSource$variant === void 0 ? void 0 : _dataSource$variant.find((item) => {
			return (item === null || item === void 0 ? void 0 : item.id) === (value === null || value === void 0 ? void 0 : value.product_variant_id);
		});
		if (!!filteredVariant) {
			var _filteredVariant$sku_;
			((filteredVariant === null || filteredVariant === void 0 || (_filteredVariant$sku_ = filteredVariant.sku_ids) === null || _filteredVariant$sku_ === void 0 ? void 0 : _filteredVariant$sku_.split("_")) || []).forEach((id) => {
				var _dataSource$variant_g;
				const [variantGroupId, variantId] = id.split(":");
				const _variantGroupId = Number(variantGroupId);
				const _variantId = Number(variantId);
				let _variantGroupItem = null;
				dataSource === null || dataSource === void 0 || (_dataSource$variant_g = dataSource.variant_group) === null || _dataSource$variant_g === void 0 || _dataSource$variant_g.find((v1) => {
					if ((v1 === null || v1 === void 0 ? void 0 : v1.id) === _variantGroupId) {
						var _v1$variant_item;
						v1 === null || v1 === void 0 || (_v1$variant_item = v1.variant_item) === null || _v1$variant_item === void 0 || _v1$variant_item.find((v2) => {
							if ((v2 === null || v2 === void 0 ? void 0 : v2.id) === _variantId) {
								_variantGroupItem = v2;
								return true;
							}
							return false;
						});
						return true;
					}
					return false;
				});
				if (!!_variantGroupItem) transformValue[_variantGroupId] = {
					data: _objectSpread2({}, _variantGroupItem || {}),
					quantity: 1,
					value: _variantId
				};
			});
		}
	}
	return transformValue;
};
/**
* 创建套餐商品数据
*/
const createBundleValue = (v, dataSource) => {
	var _dataSource$bundle_gr, _v$bundleOptions, _v$bundleOptions$forE;
	let _createdValue = null;
	const bundleGroupItem = dataSource === null || dataSource === void 0 || (_dataSource$bundle_gr = dataSource.bundle_group) === null || _dataSource$bundle_gr === void 0 ? void 0 : _dataSource$bundle_gr.find((v1) => {
		return (v1 === null || v1 === void 0 ? void 0 : v1.id) === (v === null || v === void 0 ? void 0 : v.groupId);
	});
	let findedBundleItem = null;
	if (!!bundleGroupItem) {
		var _bundleGroupItem$bund;
		findedBundleItem = bundleGroupItem === null || bundleGroupItem === void 0 || (_bundleGroupItem$bund = bundleGroupItem.bundle_item) === null || _bundleGroupItem$bund === void 0 ? void 0 : _bundleGroupItem$bund.find((v1) => {
			return (v1 === null || v1 === void 0 ? void 0 : v1.id) === (v === null || v === void 0 ? void 0 : v.id);
		});
		if (!!findedBundleItem) _createdValue = {
			value: findedBundleItem === null || findedBundleItem === void 0 ? void 0 : findedBundleItem.id,
			data: _objectSpread2({}, findedBundleItem || {})
		};
	}
	if (!_createdValue) return null;
	let totalQuantity = 0;
	v === null || v === void 0 || (_v$bundleOptions = v.bundleOptions) === null || _v$bundleOptions === void 0 || (_v$bundleOptions$forE = _v$bundleOptions.forEach) === null || _v$bundleOptions$forE === void 0 || _v$bundleOptions$forE.call(_v$bundleOptions, (v1) => {
		var _v1$option;
		if (v1 === null || v1 === void 0 || (_v1$option = v1.option) === null || _v1$option === void 0 ? void 0 : _v1$option.length) {
			var _createdValue$data;
			const _data = (_createdValue === null || _createdValue === void 0 || (_createdValue$data = _createdValue.data) === null || _createdValue$data === void 0 ? void 0 : _createdValue$data._data) || [];
			const _createdValueData = createOptionValue({}, v1, findedBundleItem);
			_data.push({
				value: v1 === null || v1 === void 0 ? void 0 : v1.id,
				data: _createdValueData,
				quantity: (v1 === null || v1 === void 0 ? void 0 : v1.quantity) || 1
			});
			_createdValue.data._data = _data;
		}
		totalQuantity = totalQuantity + ((v1 === null || v1 === void 0 ? void 0 : v1.quantity) || (v1 === null || v1 === void 0 ? void 0 : v1.num) || 1);
		const _price = (v1 === null || v1 === void 0 ? void 0 : v1.price) || (findedBundleItem === null || findedBundleItem === void 0 ? void 0 : findedBundleItem.price) || 0;
		_createdValue.totalPrice = new Decimal(_createdValue.totalPrice || 0).plus(new Decimal(_price).times((v1 === null || v1 === void 0 ? void 0 : v1.quantity) || (v1 === null || v1 === void 0 ? void 0 : v1.num) || 1)).toNumber();
	});
	_createdValue.quantity = totalQuantity;
	_createdValue.data.quantity = totalQuantity;
	_createdValue.data.num = totalQuantity;
	return _createdValue;
};
/**
* 创建单规格商品value
*/
const createOptionValue = (initValue, value, dataSource) => {
	var _value$option, _value$option$forEach;
	let _createdValue = initValue || {};
	value === null || value === void 0 || (_value$option = value.option) === null || _value$option === void 0 || (_value$option$forEach = _value$option.forEach) === null || _value$option$forEach === void 0 || _value$option$forEach.call(_value$option, (v1) => {
		var _dataSource$option_gr, _matchedGroup$option_;
		const _optionGroupId = v1 === null || v1 === void 0 ? void 0 : v1.option_group_id;
		const matchedGroup = dataSource === null || dataSource === void 0 || (_dataSource$option_gr = dataSource.option_group) === null || _dataSource$option_gr === void 0 ? void 0 : _dataSource$option_gr.find((v2) => (v2 === null || v2 === void 0 ? void 0 : v2.id) === _optionGroupId);
		const matchedOptionItem = matchedGroup === null || matchedGroup === void 0 || (_matchedGroup$option_ = matchedGroup.option_item) === null || _matchedGroup$option_ === void 0 ? void 0 : _matchedGroup$option_.find((v3) => (v3 === null || v3 === void 0 ? void 0 : v3.id) === (v1 === null || v1 === void 0 ? void 0 : v1.id));
		const _num = (v1 === null || v1 === void 0 ? void 0 : v1.quantity) || (v1 === null || v1 === void 0 ? void 0 : v1.num) || 1;
		const _price = (matchedOptionItem === null || matchedOptionItem === void 0 ? void 0 : matchedOptionItem.add_price) || 0;
		const _tempOptionValue = {
			data: _objectSpread2(_objectSpread2(_objectSpread2({}, matchedOptionItem || {}), v1 || {}), {}, {
				num: _num,
				price: _price,
				original_price: _price,
				original_total: _price
			}),
			quantity: _num,
			value: v1 === null || v1 === void 0 ? void 0 : v1.id,
			totalPrice: new Decimal((matchedOptionItem === null || matchedOptionItem === void 0 ? void 0 : matchedOptionItem.add_price) || 0).times(_num).toNumber()
		};
		if (_createdValue[_optionGroupId]) _createdValue[_optionGroupId] = [..._createdValue[_optionGroupId] || [], _tempOptionValue];
		else if ((matchedGroup === null || matchedGroup === void 0 ? void 0 : matchedGroup.is_multiple) == 1) _createdValue[_optionGroupId] = [_tempOptionValue];
		else _createdValue[_optionGroupId] = _tempOptionValue;
	});
	return _createdValue;
};
//#endregion
export { transformValueToSelectorValue };
