import { _objectSpread2 } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { _asyncToGenerator } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import NumericStepper from "../../../../pro/NumericStepper/index.js";
import useStore_default from "../../hooks/useStore.js";
import Selector from "../../../../pro/Selector/index.js";
import React, { useEffect, useMemo, useRef, useState } from "react";
import { locales } from "@pisell/utils";
import { Button, PisellModal } from "@pisell/materials";
//#region src/plus/skuOptionsSelection/components/OptionsModal/index.tsx
const SelectorGroup = Selector.Group;
const OptionsModal = (props) => {
	var _props$optionItemValu, _props$dataSource2, _props$modalValue4, _props$dataSource4;
	const { open, title, remainingCount, onCancel, onConfirm } = props;
	const selectorGroupRef = useRef(null);
	const [stepNumber, setStepNumber] = useState(1);
	const [modalValue, setModalValue] = useState((props === null || props === void 0 ? void 0 : props.modalValue) || {});
	const customConfig = useStore_default((s) => s.customConfig);
	console.log(customConfig, "customConfig>>>>>>>");
	useEffect(() => {
		var _props$modalValue;
		if (!!(props === null || props === void 0 || (_props$modalValue = props.modalValue) === null || _props$modalValue === void 0 ? void 0 : _props$modalValue.quantity)) {
			var _props$modalValue2;
			setStepNumber(props === null || props === void 0 || (_props$modalValue2 = props.modalValue) === null || _props$modalValue2 === void 0 ? void 0 : _props$modalValue2.quantity);
		} else if (remainingCount > 0) setStepNumber(1);
		else setStepNumber(0);
	}, [remainingCount, props === null || props === void 0 || (_props$optionItemValu = props.optionItemValue) === null || _props$optionItemValu === void 0 ? void 0 : _props$optionItemValu.quantity]);
	const canSelectedMax = useMemo(() => {
		var _props$modalValue3, _props$dataSource;
		const effectiveRemainingCount = remainingCount + ((props === null || props === void 0 || (_props$modalValue3 = props.modalValue) === null || _props$modalValue3 === void 0 ? void 0 : _props$modalValue3.quantity) || 0);
		if (effectiveRemainingCount <= 0) return 0;
		const _max = (props === null || props === void 0 || (_props$dataSource = props.dataSource) === null || _props$dataSource === void 0 || (_props$dataSource = _props$dataSource.ruleConfig) === null || _props$dataSource === void 0 ? void 0 : _props$dataSource.max) || 0;
		const _effectiveMax = Math.min(effectiveRemainingCount, _max);
		return _effectiveMax > 0 ? _effectiveMax : Infinity;
	}, [
		remainingCount,
		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,
		props === null || props === void 0 || (_props$modalValue4 = props.modalValue) === null || _props$modalValue4 === void 0 ? void 0 : _props$modalValue4.quantity
	]);
	const canSelectedMin = useMemo(() => {
		var _props$dataSource3;
		return (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.min) || 1;
	}, [props === null || props === void 0 || (_props$dataSource4 = props.dataSource) === null || _props$dataSource4 === void 0 || (_props$dataSource4 = _props$dataSource4.ruleConfig) === null || _props$dataSource4 === void 0 ? void 0 : _props$dataSource4.min]);
	const handleConfirm = function() {
		var _ref = _asyncToGenerator(function* () {
			try {
				var _selectorGroupRef$cur;
				if (yield (_selectorGroupRef$cur = selectorGroupRef.current) === null || _selectorGroupRef$cur === void 0 ? void 0 : _selectorGroupRef$cur.validate()) onConfirm(_objectSpread2(_objectSpread2({}, modalValue), {}, { quantity: stepNumber }));
			} catch (error) {
				console.log("handleConfirm_error>>>>>", error);
			}
		});
		return function handleConfirm() {
			return _ref.apply(this, arguments);
		};
	}();
	return /* @__PURE__ */ React.createElement(PisellModal, {
		open,
		onCancel,
		title,
		footer: /* @__PURE__ */ React.createElement("div", { className: "pisell-plus-sku-options-card-footer" }, /* @__PURE__ */ React.createElement("div", null, /* @__PURE__ */ React.createElement(NumericStepper, {
			min: canSelectedMin,
			max: canSelectedMax,
			value: stepNumber,
			onChange: (value) => {
				setStepNumber(value);
			}
		})), /* @__PURE__ */ React.createElement(Button, {
			type: "primary",
			onClick: handleConfirm,
			size: "large",
			block: true
		}, locales.getText("pisell2.skuOptionsSelection.optionItemCard.confirm"))),
		footerDivider: false,
		destroyOnClose: true,
		bodyStyle: {
			paddingLeft: 0,
			paddingRight: 0,
			overflow: "hidden",
			display: "flex",
			flexDirection: "column"
		},
		width: (customConfig === null || customConfig === void 0 ? void 0 : customConfig.modalWidth) || "480px",
		mobileModalHeight: "auto"
	}, /* @__PURE__ */ React.createElement(SelectorGroup, {
		ref: selectorGroupRef,
		dataSource: props === null || props === void 0 ? void 0 : props.modalDataSource,
		value: (modalValue === null || modalValue === void 0 ? void 0 : modalValue.data) || {},
		onChange: (value) => {
			setModalValue(_objectSpread2(_objectSpread2({}, modalValue), {}, { data: value }));
		},
		style: {
			height: "100%",
			padding: "0 24px",
			overflowY: "auto"
		}
	}));
};
//#endregion
export { OptionsModal as default };
