import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import store from "../store/store.js";
import NormalCard from "../components/NormalCard/index.js";
import TitleExtra from "../components/TitleExtra/index.js";
import { checkSkuDisabled, checkSkuStockCanSell } from "./checkSkuStatus.js";
import React from "react";
//#region src/plus/skuOptionsSelection/utils/transformOptionGroup.tsx
const transformOptionGroup = (optionGroup) => {
	var _store$getState$custo;
	if (!Array.isArray(optionGroup)) return [];
	const selectorLayout = ((_store$getState$custo = store.getState().customConfig) === null || _store$getState$custo === void 0 ? void 0 : _store$getState$custo.selectorLayout) || null;
	return optionGroup.map((item) => {
		var _item$option_item;
		const selectorOptionsDataSource = item === null || item === void 0 || (_item$option_item = item.option_item) === null || _item$option_item === void 0 ? void 0 : _item$option_item.map((optionItem) => {
			const { canSell, hasStock } = checkSkuStockCanSell(optionItem);
			const disabled = checkSkuDisabled(optionItem, canSell);
			return {
				_original: _objectSpread2(_objectSpread2({}, optionItem || {}), {}, { product_option_item_id: optionItem === null || optionItem === void 0 ? void 0 : optionItem.id }),
				id: optionItem === null || optionItem === void 0 ? void 0 : optionItem.id,
				title: optionItem === null || optionItem === void 0 ? void 0 : optionItem.name,
				value: optionItem === null || optionItem === void 0 ? void 0 : optionItem.id,
				disabled,
				ruleConfig: {
					min: optionItem === null || optionItem === void 0 ? void 0 : optionItem.min_quantity,
					max: optionItem === null || optionItem === void 0 ? void 0 : optionItem.max_quantity,
					autoValidate: true
				},
				_extra: {
					canSell,
					hasStock,
					disabled
				}
			};
		});
		const isMultiple = (item === null || item === void 0 ? void 0 : item.is_multiple) == 1;
		const selectorDataSource = {
			id: item.id,
			title: item.name || "",
			mode: isMultiple ? "multiple" : "single",
			valueType: "object",
			fieldNames: {
				value: "value",
				label: "title"
			},
			dataSource: selectorOptionsDataSource || [],
			disabled: (item === null || item === void 0 ? void 0 : item.is_enabled) === 0,
			ruleConfig: {
				min: isMultiple ? item === null || item === void 0 ? void 0 : item.min_quantity : void 0,
				max: isMultiple ? Number(item === null || item === void 0 ? void 0 : item.max_quantity) == 0 ? void 0 : item === null || item === void 0 ? void 0 : item.max_quantity : void 0,
				autoValidate: true,
				required: isMultiple ? Number((item === null || item === void 0 ? void 0 : item.min_quantity) || 0) > 0 : (item === null || item === void 0 ? void 0 : item.required) === 1
			},
			headerProps: { renderExtra: (props) => {
				return /* @__PURE__ */ React.createElement(TitleExtra, props || {});
			} },
			stepperProps: { horizontalPadding: "8px" }
		};
		if (!!selectorLayout) selectorDataSource.layout = selectorLayout;
		selectorDataSource.renderItem = (itemProps) => {
			return /* @__PURE__ */ React.createElement(NormalCard, _objectSpread2(_objectSpread2({}, itemProps || {}), {}, { parentDataSource: selectorDataSource }));
		};
		return selectorDataSource;
	});
};
//#endregion
export { transformOptionGroup };
