const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_store = require("../store/store.js");
const require_checkSkuStatus = require("./checkSkuStatus.js");
const require_index = require("../components/VariantCard/index.js");
let react = require("react");
react = require_runtime.__toESM(react);
//#region src/plus/skuOptionsSelection/utils/transformVariantGroup.tsx
const transformVariantGroup = (variantGroup) => {
	var _store$getState$dataS, _store$getState$custo;
	if (!Array.isArray(variantGroup)) return [];
	const variantProducts = ((_store$getState$dataS = require_store.default.getState().dataSource) === null || _store$getState$dataS === void 0 ? void 0 : _store$getState$dataS.variant) || [];
	const selectorLayout = ((_store$getState$custo = require_store.default.getState().customConfig) === null || _store$getState$custo === void 0 ? void 0 : _store$getState$custo.selectorLayout) || null;
	return variantGroup.map((item) => {
		var _item$variant_item;
		const selectorOptionsDataSource = item === null || item === void 0 || (_item$variant_item = item.variant_item) === null || _item$variant_item === void 0 ? void 0 : _item$variant_item.map((variantItem) => {
			const { canSell, hasStock } = require_checkSkuStatus.checkVariantStockCanSell(variantItem);
			const disabled = require_checkSkuStatus.checkSkuDisabled(variantItem, canSell);
			const price = getVariantItemFixedPrice({
				selectedVariantIds: [`${variantItem === null || variantItem === void 0 ? void 0 : variantItem.variant_group_id}:${variantItem === null || variantItem === void 0 ? void 0 : variantItem.id}`],
				variantProducts
			});
			return {
				_original: require_objectSpread2._objectSpread2({}, variantItem || {}),
				id: variantItem === null || variantItem === void 0 ? void 0 : variantItem.id,
				title: variantItem === null || variantItem === void 0 ? void 0 : variantItem.name,
				value: variantItem === null || variantItem === void 0 ? void 0 : variantItem.id,
				disabled,
				price,
				ruleConfig: { autoValidate: true },
				_extra: {
					canSell,
					hasStock,
					disabled
				}
			};
		});
		const selectorDataSource = {
			id: item.id,
			title: item.name || "",
			mode: "single",
			valueType: "object",
			fieldNames: {
				value: "value",
				label: "title"
			},
			dataSource: selectorOptionsDataSource || [],
			disabled: (item === null || item === void 0 ? void 0 : item.is_enabled) === 0,
			ruleConfig: {
				autoValidate: true,
				required: true
			},
			variant: "card",
			preset: { variant: "1" },
			itemProps: { styles: { container: { padding: 0 } } },
			stepperProps: { horizontalPadding: "8px" }
		};
		if (!!selectorLayout) selectorDataSource.layout = selectorLayout;
		selectorDataSource.renderItem = (itemProps) => {
			var _itemProps$dataSource, _itemProps$dataSource2, _itemProps$dataSource3;
			const { canSell, hasStock } = require_checkSkuStatus.checkVariantStockByValue(itemProps === null || itemProps === void 0 || (_itemProps$dataSource = itemProps.dataSource) === null || _itemProps$dataSource === void 0 ? void 0 : _itemProps$dataSource._original);
			const price = getVariantItemFixedPriceByValue({
				variantItem: itemProps === null || itemProps === void 0 || (_itemProps$dataSource2 = itemProps.dataSource) === null || _itemProps$dataSource2 === void 0 ? void 0 : _itemProps$dataSource2._original,
				variantProducts
			});
			const _dataSource = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, (itemProps === null || itemProps === void 0 ? void 0 : itemProps.dataSource) || {}), {}, {
				disabled: require_checkSkuStatus.checkSkuDisabled(itemProps === null || itemProps === void 0 || (_itemProps$dataSource3 = itemProps.dataSource) === null || _itemProps$dataSource3 === void 0 ? void 0 : _itemProps$dataSource3._original, canSell),
				price,
				_extra: {
					canSell,
					hasStock
				}
			});
			return /* @__PURE__ */ react.default.createElement(require_index.default, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, itemProps || {}), {}, {
				dataSource: _dataSource,
				parentDataSource: selectorDataSource
			}));
		};
		return selectorDataSource;
	});
};
/**
* @description 在给定匹配范围内计算“固定价格”
* - 若匹配到多个不同价格，则认为不是固定价格，返回 null（不展示）
*/
function getVariantItemFixedPrice(params) {
	const selectedVariantIds = params.selectedVariantIds;
	const variantProducts = params.variantProducts;
	const prices = (Array.isArray(variantProducts) ? variantProducts.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((selectedId) => ids.includes(selectedId));
	}) : []).map((item) => {
		var _ref, _item$price;
		return Number((_ref = (_item$price = item === null || item === void 0 ? void 0 : item.price) !== null && _item$price !== void 0 ? _item$price : item === null || item === void 0 ? void 0 : item.base_price) !== null && _ref !== void 0 ? _ref : 0);
	}).filter((n) => !Number.isNaN(n));
	if (prices.length === 0) return null;
	const uniquePrices = Array.from(new Set(prices));
	if (uniquePrices.length !== 1) return null;
	return uniquePrices[0];
}
/**
* @description 基于当前 selectorValue（已选项）计算某个 variantItem 的固定价格
* - 用当前选项 + 其它规格组已选项筛选组合 SKU
* - 若筛出来是多价，则不展示
*/
function getVariantItemFixedPriceByValue(params) {
	var _dataSource$variant_g;
	const variantItem = params.variantItem;
	const variantProducts = params.variantProducts;
	const dataSource = require_store.default.getState().dataSource;
	const selectorValue = require_store.default.getState().selectorValue;
	const selectedVariantIds = [`${variantItem === null || variantItem === void 0 ? void 0 : variantItem.variant_group_id}:${variantItem === null || variantItem === void 0 ? void 0 : variantItem.id}`];
	dataSource === null || dataSource === void 0 || (_dataSource$variant_g = dataSource.variant_group) === null || _dataSource$variant_g === void 0 || _dataSource$variant_g.forEach((group) => {
		var _selectorValue$group$;
		const selectedData = selectorValue === null || selectorValue === void 0 || (_selectorValue$group$ = selectorValue[group.id]) === null || _selectorValue$group$ === void 0 ? void 0 : _selectorValue$group$.data;
		if (selectedData && group.id !== (variantItem === null || variantItem === void 0 ? void 0 : variantItem.variant_group_id)) selectedVariantIds.push(`${selectedData === null || selectedData === void 0 ? void 0 : selectedData.variant_group_id}:${selectedData === null || selectedData === void 0 ? void 0 : selectedData.id}`);
	});
	return getVariantItemFixedPrice({
		selectedVariantIds,
		variantProducts
	});
}
//#endregion
exports.transformVariantGroup = transformVariantGroup;
