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");
let react = require("react");
react = require_runtime.__toESM(react);
let decimal_js = require("decimal.js");
decimal_js = require_runtime.__toESM(decimal_js);
//#region src/plus/skuOptionsSelection/components/NormalCard/index.tsx
const NormalCard = (props) => {
	var _props$dataSource;
	const { _key: uniqueKey, ruleConfig, _original, disabled, selected } = (props === null || props === void 0 ? void 0 : props.dataSource) || {};
	const { cover, title, name, product_subtitle, price, price_type, add_price } = _original || {};
	const symbol = require_useStore.default((s) => s.symbol);
	const skuCardConfig = require_useStore.default((s) => s.skuCardConfig);
	const renderIndicator = () => {
		if (!(props === null || props === void 0 ? void 0 : props.indicator) || typeof (props === null || props === void 0 ? void 0 : props.indicator) !== "object") return null;
		return react.default.cloneElement(props === null || props === void 0 ? void 0 : props.indicator, { onChange: handleChange });
	};
	const renderNumberSelector = () => {
		if (!(props === null || props === void 0 ? void 0 : props.numberSelector) || typeof (props === null || props === void 0 ? void 0 : props.numberSelector) !== "object") return null;
		return react.default.cloneElement(props === null || props === void 0 ? void 0 : props.numberSelector, { onChange: (v) => {
			var _props$actions;
			const _price = add_price !== void 0 && add_price !== null ? add_price : price;
			const priceType = price_type === "markdown" ? -1 : 1;
			const totalPrice = new decimal_js.default(_price).times(new decimal_js.default(v)).times(priceType).toNumber();
			const newOriginal = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, _original), {}, {
				quantity: v,
				num: v,
				price: _price,
				original_price: _price,
				original_total: _price
			});
			props === null || props === void 0 || (_props$actions = props.actions) === null || _props$actions === void 0 || _props$actions.update(uniqueKey, {
				quantity: v,
				data: newOriginal,
				totalPrice
			});
		} });
	};
	const isNumberSelector = (0, react.useMemo)(() => {
		return !!(props === null || props === void 0 ? void 0 : props.numberSelector) && react.default.isValidElement(props === null || props === void 0 ? void 0 : props.numberSelector);
	}, [props === null || props === void 0 ? void 0 : props.numberSelector]);
	const priceText = (0, react.useMemo)(() => {
		if (add_price !== void 0 && add_price !== null) return `${Number(add_price) > 0 ? "+" : ""}${symbol}${Math.abs(Number(add_price || 0))}`;
		return `${price_type === "markdown" ? "-" : ""}${symbol}${price || 0}`;
	}, [
		price,
		price_type,
		symbol,
		add_price
	]);
	const handleChange = () => {
		if (disabled || isNumberSelector) return;
		if (selected) {
			var _props$actions2;
			props === null || props === void 0 || (_props$actions2 = props.actions) === null || _props$actions2 === void 0 || _props$actions2.update(uniqueKey, { quantity: 0 });
		} else {
			var _props$actions3;
			const _price = add_price !== void 0 && add_price !== null ? add_price : price;
			const priceType = price_type === "markdown" ? -1 : 1;
			const newOriginal = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, _original), {}, {
				quantity: 1,
				num: 1,
				price: _price,
				original_price: _price,
				original_total: _price
			});
			props === null || props === void 0 || (_props$actions3 = props.actions) === null || _props$actions3 === void 0 || _props$actions3.update(uniqueKey, {
				quantity: 1,
				data: newOriginal,
				totalPrice: new decimal_js.default(_price).times(priceType).toNumber()
			});
		}
	};
	return /* @__PURE__ */ react.default.createElement(require_index.default, {
		cover,
		title: title || name || "",
		subtitle: product_subtitle,
		price: priceText,
		selected,
		disabled,
		renderRight: isNumberSelector ? /* @__PURE__ */ react.default.createElement("div", { style: {
			position: "relative",
			height: "100%"
		} }, renderNumberSelector()) : /* @__PURE__ */ react.default.createElement("div", { style: {
			position: "relative",
			height: "100%",
			padding: "0 8px"
		} }, /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, renderIndicator(), /* @__PURE__ */ react.default.createElement("div", { style: {
			position: "absolute",
			top: 0,
			left: 0,
			width: "100%",
			height: "100%"
		} }))),
		onClick: handleChange,
		skuCardConfig,
		skuDisabled: (props === null || props === void 0 || (_props$dataSource = props.dataSource) === null || _props$dataSource === void 0 || (_props$dataSource = _props$dataSource._extra) === null || _props$dataSource === void 0 ? void 0 : _props$dataSource.disabled) === true
	});
};
//#endregion
exports.default = NormalCard;
