const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_useStore = require("../../hooks/useStore.js");
const require_index = require("../../../../pro/Cards/components/SKUCard/index.js");
const require_index$1 = require("../OptionsModal/index.js");
const require_OptionItemCard = require("./OptionItemCard.js");
const require_transformOptionGroup = require("../../utils/transformOptionGroup.js");
const require_calculateOption = require("../../utils/calculateOption.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
let _pisell_materials = require("@pisell/materials");
require("./index.less");
//#region src/plus/skuOptionsSelection/components/OptionsCard/index.tsx
/**
* 单规格商品卡片，点击后需要拉起弹窗展示具体的规格信息
*/
const OptionsCard = (props) => {
	var _props$dataSource, _props$parentDataSour2, _props$optionItemValu2, _props$dataSource3, _props$optionItemValu4, _props$dataSource4;
	const { _key: uniqueKey, selected, disabled } = (props === null || props === void 0 ? void 0 : props.dataSource) || {};
	const { cover, title, name, product_subtitle, price, price_type, option_group } = (props === null || props === void 0 || (_props$dataSource = props.dataSource) === null || _props$dataSource === void 0 ? void 0 : _props$dataSource._original) || {};
	const symbol = require_useStore.default((s) => s.symbol);
	const skuCardConfig = require_useStore.default((s) => s.skuCardConfig);
	const [isOpen, setIsOpen] = (0, react.useState)(false);
	const [modalDataSource, setModalDataSource] = (0, react.useState)([]);
	const [modalValue, setModalValue] = (0, react.useState)({});
	const currentEditIndexRef = (0, react.useRef)(-1);
	const [currentEditIndex, setCurrentEditIndex] = (0, react.useState)(-1);
	const _indicator = react.default.cloneElement(props === null || props === void 0 ? void 0 : props.indicator);
	const selectorRemainingCount = (0, react.useMemo)(() => {
		var _props$parentDataSour;
		const globalMax = (props === null || props === void 0 || (_props$parentDataSour = props.parentDataSource) === null || _props$parentDataSour === void 0 || (_props$parentDataSour = _props$parentDataSour.ruleConfig) === null || _props$parentDataSour === void 0 ? void 0 : _props$parentDataSour.max) || 0;
		if (globalMax <= 0) return Infinity;
		const totalSelected = (Array.isArray(props === null || props === void 0 ? void 0 : props.values) ? props === null || props === void 0 ? void 0 : props.values : [props === null || props === void 0 ? void 0 : props.values]).reduce((sum, i) => sum + ((i === null || i === void 0 ? void 0 : i.quantity) || 0), 0) || 0;
		return Math.max(0, globalMax - totalSelected);
	}, [props === null || props === void 0 || (_props$parentDataSour2 = props.parentDataSource) === null || _props$parentDataSour2 === void 0 || (_props$parentDataSour2 = _props$parentDataSour2.ruleConfig) === null || _props$parentDataSour2 === void 0 ? void 0 : _props$parentDataSour2.max, props === null || props === void 0 ? void 0 : props.values]);
	const optionRemainingCount = (0, react.useMemo)(() => {
		var _props$optionItemValu, _props$dataSource2;
		const currentQuantity = (props === null || props === void 0 || (_props$optionItemValu = props.optionItemValue) === null || _props$optionItemValu === void 0 ? void 0 : _props$optionItemValu.quantity) || 0;
		const optionMax = (props === null || props === void 0 || (_props$dataSource2 = props.dataSource) === null || _props$dataSource2 === void 0 || (_props$dataSource2 = _props$dataSource2.ruleConfig) === null || _props$dataSource2 === void 0 ? void 0 : _props$dataSource2.max) || 0;
		const optionRemaining = optionMax > 0 ? optionMax - currentQuantity : Infinity;
		return Math.max(0, Math.min(selectorRemainingCount, optionRemaining));
	}, [
		props === null || props === void 0 || (_props$optionItemValu2 = props.optionItemValue) === null || _props$optionItemValu2 === void 0 ? void 0 : _props$optionItemValu2.quantity,
		selectorRemainingCount,
		props === null || props === void 0 || (_props$dataSource3 = props.dataSource) === null || _props$dataSource3 === void 0 || (_props$dataSource3 = _props$dataSource3.ruleConfig) === null || _props$dataSource3 === void 0 ? void 0 : _props$dataSource3.max
	]);
	const modalRemainingCount = (0, react.useMemo)(() => {
		if ((props === null || props === void 0 ? void 0 : props.mode) === "single" && currentEditIndex === -1) return optionRemainingCount <= 0 ? 1 : optionRemainingCount;
		return optionRemainingCount;
	}, [
		props === null || props === void 0 ? void 0 : props.mode,
		optionRemainingCount,
		currentEditIndex
	]);
	const priceText = (0, react.useMemo)(() => {
		return `${price_type === "markdown" ? "-" : ""}${symbol}${Math.abs(Number(price || 0))}`;
	}, [
		price,
		price_type,
		symbol
	]);
	const selectedOptions = (0, react.useMemo)(() => {
		var _props$optionItemValu3;
		return (props === null || props === void 0 || (_props$optionItemValu3 = props.optionItemValue) === null || _props$optionItemValu3 === void 0 || (_props$optionItemValu3 = _props$optionItemValu3.data) === null || _props$optionItemValu3 === void 0 ? void 0 : _props$optionItemValu3._data) || [];
	}, [props === null || props === void 0 || (_props$optionItemValu4 = props.optionItemValue) === null || _props$optionItemValu4 === void 0 || (_props$optionItemValu4 = _props$optionItemValu4.data) === null || _props$optionItemValu4 === void 0 ? void 0 : _props$optionItemValu4._data]);
	const transformDataSource = () => {
		if (!!(option_group === null || option_group === void 0 ? void 0 : option_group.length)) setModalDataSource(require_transformOptionGroup.transformOptionGroup(option_group) || []);
	};
	const handleClick = () => {
		if (disabled) {
			console.log("handleClick_disabled>>>>>", disabled);
			return;
		}
		let needShowToast = false;
		if (optionRemainingCount <= 0) if ((props === null || props === void 0 ? void 0 : props.mode) === "single" && !selected) needShowToast = false;
		else needShowToast = true;
		else needShowToast = false;
		if (needShowToast) {
			(0, _pisell_materials.PisellToast)({ content: _pisell_utils.locales.getText("pisell2.skuOptionsSelection.optionItemCard.overSelected") });
			return;
		}
		setModalValue({});
		currentEditIndexRef.current = -1;
		setCurrentEditIndex(-1);
		setIsOpen(true);
		transformDataSource();
	};
	const handleUpdateClick = (index) => {
		var _props$optionItemValu5;
		currentEditIndexRef.current = index;
		setCurrentEditIndex(index);
		setIsOpen(true);
		setModalValue((props === null || props === void 0 || (_props$optionItemValu5 = props.optionItemValue) === null || _props$optionItemValu5 === void 0 || (_props$optionItemValu5 = _props$optionItemValu5.data) === null || _props$optionItemValu5 === void 0 || (_props$optionItemValu5 = _props$optionItemValu5._data) === null || _props$optionItemValu5 === void 0 ? void 0 : _props$optionItemValu5[index]) || {});
		transformDataSource();
	};
	const handleConfirm = (v) => {
		var _props$actions;
		console.log("onConfirm_v>>>>>", v, props === null || props === void 0 ? void 0 : props.optionItemValue, props === null || props === void 0 ? void 0 : props.values);
		const [newOriginal, totalPrice, totalQuantity] = require_calculateOption.calculateOptionItem({
			editIndex: currentEditIndexRef.current,
			optionItemValue: props === null || props === void 0 ? void 0 : props.optionItemValue,
			optionItemDataSource: props === null || props === void 0 ? void 0 : props.dataSource,
			data: v
		});
		props === null || props === void 0 || (_props$actions = props.actions) === null || _props$actions === void 0 || _props$actions.update(uniqueKey, {
			quantity: totalQuantity,
			totalPrice,
			data: newOriginal
		});
		setIsOpen(false);
		setModalValue({});
	};
	const handleQuantityChange = (_index, _quantity) => {
		var _props$optionItemValu6, _props$actions2;
		const currentData = (props === null || props === void 0 || (_props$optionItemValu6 = props.optionItemValue) === null || _props$optionItemValu6 === void 0 || (_props$optionItemValu6 = _props$optionItemValu6.data) === null || _props$optionItemValu6 === void 0 || (_props$optionItemValu6 = _props$optionItemValu6._data) === null || _props$optionItemValu6 === void 0 ? void 0 : _props$optionItemValu6[_index]) || {};
		const [newOriginal, totalPrice, totalQuantity] = require_calculateOption.calculateOptionItem({
			editIndex: _index,
			optionItemValue: props === null || props === void 0 ? void 0 : props.optionItemValue,
			optionItemDataSource: props === null || props === void 0 ? void 0 : props.dataSource,
			data: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, currentData), {}, { quantity: _quantity })
		});
		props === null || props === void 0 || (_props$actions2 = props.actions) === null || _props$actions2 === void 0 || _props$actions2.update(uniqueKey, {
			quantity: totalQuantity,
			totalPrice,
			data: newOriginal
		});
	};
	return /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement("div", {
		className: "pisell-plus-sku-options-card",
		onClick: handleClick
	}, /* @__PURE__ */ react.default.createElement("div", {
		onClick: (e) => {
			e.stopPropagation();
			if (selected) {
				var _props$actions3;
				props === null || props === void 0 || (_props$actions3 = props.actions) === null || _props$actions3 === void 0 || _props$actions3.update(uniqueKey, { quantity: 0 });
			} else handleClick();
		},
		className: "pisell-plus-sku-options-card-indicator-wrapper"
	}, _indicator, /* @__PURE__ */ react.default.createElement("div", { className: "pisell-plus-sku-options-card-indicator-wrapper-overlay" })), /* @__PURE__ */ react.default.createElement(require_index.default, {
		cover,
		title: title || name || "",
		subtitle: product_subtitle,
		price: priceText,
		arrow: true,
		selected,
		disabled,
		skuCardConfig,
		skuDisabled: (props === null || props === void 0 || (_props$dataSource4 = props.dataSource) === null || _props$dataSource4 === void 0 || (_props$dataSource4 = _props$dataSource4._extra) === null || _props$dataSource4 === void 0 ? void 0 : _props$dataSource4.disabled) === true
	})), (selectedOptions === null || selectedOptions === void 0 ? void 0 : selectedOptions.length) > 0 && /* @__PURE__ */ react.default.createElement(require_OptionItemCard.default, {
		optionItemValue: props === null || props === void 0 ? void 0 : props.optionItemValue,
		parentDataSource: props === null || props === void 0 ? void 0 : props.parentDataSource,
		dataSource: props === null || props === void 0 ? void 0 : props.dataSource,
		data: selectedOptions,
		remainingCount: optionRemainingCount,
		onEdit: handleUpdateClick,
		onQuantityChange: handleQuantityChange
	}), isOpen && /* @__PURE__ */ react.default.createElement(require_index$1.default, {
		parentDataSource: props === null || props === void 0 ? void 0 : props.parentDataSource,
		dataSource: props === null || props === void 0 ? void 0 : props.dataSource,
		modalDataSource,
		values: props === null || props === void 0 ? void 0 : props.values,
		optionItemValue: props === null || props === void 0 ? void 0 : props.optionItemValue,
		modalValue,
		remainingCount: modalRemainingCount,
		open: isOpen,
		title: title || name || "",
		onCancel: () => setIsOpen(false),
		onConfirm: handleConfirm
	}));
};
//#endregion
exports.default = OptionsCard;
