import { _objectSpread2 } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import useEngineContext from "../../hooks/useEngineContext.js";
import NumericStepper from "../../pro/NumericStepper/index.js";
import index from "../productSelect/components/pisellPriceText/index.js";
import locales_default from "./locales.js";
import { cubicBezier } from "./utils.js";
import React, { useMemo } from "react";
import { image, locales } from "@pisell/utils";
import { Button, PisellToast } from "@pisell/materials";
import classNames from "classnames";
import "./index.less";
//#region src/plus/pisellSalesProductCard/index.tsx
const PREFIX = "pisell-plus-sales-product-card";
/**
* 解析多语言/普通字符串标题
*/
function resolveText(text) {
	if (!text) return "";
	if (typeof text === "string") return text;
	return Object.values(text || {}).find((item) => !!item) || "";
}
function toStringValue(value) {
	if (value === void 0 || value === null) return "";
	return String(value);
}
function toNumber(value) {
	const parsed = Number(value);
	if (Number.isNaN(parsed)) return 0;
	return parsed;
}
function getTextCover(text) {
	const cleanText = text.trim();
	if (!cleanText) return "";
	return cleanText.substring(0, 2).toUpperCase();
}
/**
* 普通商品: 可直接增减数量
* 其它商品(单规格/组合规格/套餐): 需要进入 options 进一步选择
*/
function isNormalProduct(product) {
	if (!product) return true;
	const { is_variant, is_bundle } = product;
	return !(Array.isArray(product.option_group) && product.option_group.length > 0 || is_bundle || is_variant);
}
const PisellSalesProductCard = ({ product, value, disabled = false, stepperProps, optionsText, skuCardConfig, cartElementId, style, className, onChange, onClick }) => {
	var _context$appHelper$ut, _product$price, _value$num, _stepperProps$min3, _stepperProps$step;
	const context = useEngineContext();
	locales.init(locales_default, ((_context$appHelper$ut = context.appHelper.utils) === null || _context$appHelper$ut === void 0 || (_context$appHelper$ut = _context$appHelper$ut.storage) === null || _context$appHelper$ut === void 0 ? void 0 : _context$appHelper$ut.get("umi_locale")) || "en");
	const normalProduct = useMemo(() => isNormalProduct(product), [product]);
	const mergedDisabled = disabled || (product === null || product === void 0 ? void 0 : product.disabled) === 1 || (product === null || product === void 0 ? void 0 : product.disabled) === true;
	const title = resolveText((product === null || product === void 0 ? void 0 : product.title) || (product === null || product === void 0 ? void 0 : product.name));
	const textCover = getTextCover(title);
	const imageStyle = useMemo(() => {
		var _skuCardConfig$image, _skuCardConfig$image3;
		let _style = {};
		if (skuCardConfig === null || skuCardConfig === void 0 || (_skuCardConfig$image = skuCardConfig.image) === null || _skuCardConfig$image === void 0 ? void 0 : _skuCardConfig$image.width) {
			var _skuCardConfig$image2;
			_style.width = skuCardConfig === null || skuCardConfig === void 0 || (_skuCardConfig$image2 = skuCardConfig.image) === null || _skuCardConfig$image2 === void 0 ? void 0 : _skuCardConfig$image2.width;
		}
		if (skuCardConfig === null || skuCardConfig === void 0 || (_skuCardConfig$image3 = skuCardConfig.image) === null || _skuCardConfig$image3 === void 0 ? void 0 : _skuCardConfig$image3.height) {
			var _skuCardConfig$image4;
			_style.height = skuCardConfig === null || skuCardConfig === void 0 || (_skuCardConfig$image4 = skuCardConfig.image) === null || _skuCardConfig$image4 === void 0 ? void 0 : _skuCardConfig$image4.height;
		}
		return _style;
	}, [skuCardConfig]);
	const buildNextValue = (nextNum) => {
		var _is_charge_tax, _ref;
		if (value) return _objectSpread2(_objectSpread2({}, value), {}, { num: nextNum });
		return {
			product_id: product === null || product === void 0 ? void 0 : product.id,
			num: nextNum,
			product_option_item: [],
			original_price: toStringValue(product === null || product === void 0 ? void 0 : product.original_price),
			is_charge_tax: (_is_charge_tax = product === null || product === void 0 ? void 0 : product.is_charge_tax) !== null && _is_charge_tax !== void 0 ? _is_charge_tax : 0,
			discount_list: [],
			product_bundle: (_ref = product === null || product === void 0 ? void 0 : product.product_bundle) !== null && _ref !== void 0 ? _ref : [],
			metadata: {}
		};
	};
	const cardMaxQuantity = useMemo(() => {
		var _stepperProps$min, _stepperProps$min2;
		let maxNum = 200;
		const detailAny = product;
		if (!(Array.isArray(detailAny === null || detailAny === void 0 ? void 0 : detailAny.variant_group) && detailAny.variant_group.length > 0) && (detailAny === null || detailAny === void 0 ? void 0 : detailAny.is_track) === 1 && (detailAny === null || detailAny === void 0 ? void 0 : detailAny.over_sold) === 0) {
			const stock = toNumber(detailAny === null || detailAny === void 0 ? void 0 : detailAny.stock_quantity);
			maxNum = stock >= 200 ? 200 : stock;
		}
		const selectedVariantId = toNumber(value === null || value === void 0 ? void 0 : value.product_variant_id);
		if (selectedVariantId > 0 && Array.isArray(detailAny === null || detailAny === void 0 ? void 0 : detailAny.variant)) {
			const selectedVariant = detailAny.variant.find((item) => toNumber(item === null || item === void 0 ? void 0 : item.id) === selectedVariantId);
			if ((selectedVariant === null || selectedVariant === void 0 ? void 0 : selectedVariant.is_track) === 1 && (selectedVariant === null || selectedVariant === void 0 ? void 0 : selectedVariant.over_sold) === 0) {
				const variantStock = toNumber(selectedVariant === null || selectedVariant === void 0 ? void 0 : selectedVariant.stock_quantity);
				maxNum = variantStock >= 200 ? 200 : variantStock;
			}
		}
		const propMax = Number.isFinite(stepperProps === null || stepperProps === void 0 ? void 0 : stepperProps.max) ? Number(stepperProps === null || stepperProps === void 0 ? void 0 : stepperProps.max) : Infinity;
		const mergedMax = Math.min(maxNum, propMax);
		if (!Number.isFinite(mergedMax)) return Math.max((_stepperProps$min = stepperProps === null || stepperProps === void 0 ? void 0 : stepperProps.min) !== null && _stepperProps$min !== void 0 ? _stepperProps$min : 0, maxNum);
		return Math.max((_stepperProps$min2 = stepperProps === null || stepperProps === void 0 ? void 0 : stepperProps.min) !== null && _stepperProps$min2 !== void 0 ? _stepperProps$min2 : 0, mergedMax);
	}, [
		product,
		value === null || value === void 0 ? void 0 : value.product_variant_id,
		stepperProps === null || stepperProps === void 0 ? void 0 : stepperProps.max,
		stepperProps === null || stepperProps === void 0 ? void 0 : stepperProps.min
	]);
	const handleCardClick = () => {
		if (mergedDisabled) return;
		const meta = {
			intent: normalProduct ? "detail" : "options",
			trigger: "card",
			product
		};
		onClick === null || onClick === void 0 || onClick(value || { product_id: product === null || product === void 0 ? void 0 : product.id }, meta);
	};
	return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement("div", {
		className: classNames(PREFIX, className),
		style
	}, /* @__PURE__ */ React.createElement("div", {
		className: classNames(`${PREFIX}__inner`, { [`${PREFIX}__inner-disabled`]: mergedDisabled }),
		onClick: handleCardClick
	}, /* @__PURE__ */ React.createElement("div", {
		className: `${PREFIX}__image`,
		style: imageStyle
	}, (product === null || product === void 0 ? void 0 : product.cover) ? /* @__PURE__ */ React.createElement("img", {
		src: image.ali(product.cover, 360),
		alt: title
	}) : /* @__PURE__ */ React.createElement("div", { className: `${PREFIX}__image-placeholder` }, textCover)), /* @__PURE__ */ React.createElement("div", { className: `${PREFIX}__content` }, /* @__PURE__ */ React.createElement("div", { className: `${PREFIX}__title` }, title), /* @__PURE__ */ React.createElement(index, {
		value: (_product$price = product.price) !== null && _product$price !== void 0 ? _product$price : 0,
		isMini: !!product.is_bundle
	})), /* @__PURE__ */ React.createElement("div", { className: `${PREFIX}__action` }, normalProduct ? /* @__PURE__ */ React.createElement("div", { className: `${PREFIX}__stepper` }, /* @__PURE__ */ React.createElement(NumericStepper, {
		value: (_value$num = value === null || value === void 0 ? void 0 : value.num) !== null && _value$num !== void 0 ? _value$num : 0,
		min: (_stepperProps$min3 = stepperProps === null || stepperProps === void 0 ? void 0 : stepperProps.min) !== null && _stepperProps$min3 !== void 0 ? _stepperProps$min3 : 0,
		max: cardMaxQuantity,
		step: (_stepperProps$step = stepperProps === null || stepperProps === void 0 ? void 0 : stepperProps.step) !== null && _stepperProps$step !== void 0 ? _stepperProps$step : 1,
		disabled: mergedDisabled,
		onLimitExceeded: (limitMeta) => {
			const raw = stepperProps === null || stepperProps === void 0 ? void 0 : stepperProps.limitMessage;
			const limitMsg = typeof raw === "string" ? { max: raw } : raw || {};
			const content = limitMeta.reason === "max" ? limitMsg.max : limitMsg.min;
			if (!content) return;
			PisellToast({
				content,
				duration: 2
			});
		},
		onChange: (next, stepperMeta) => {
			var _stepperProps$min4;
			const safeNext = Math.max((_stepperProps$min4 = stepperProps === null || stepperProps === void 0 ? void 0 : stepperProps.min) !== null && _stepperProps$min4 !== void 0 ? _stepperProps$min4 : 0, Math.min(cardMaxQuantity, next));
			if ((stepperMeta === null || stepperMeta === void 0 ? void 0 : stepperMeta.action) === "increase" && stepperMeta.event && cartElementId) cubicBezier({
				position: {
					x: stepperMeta.event.clientX,
					y: stepperMeta.event.clientY
				},
				cartElementId
			});
			const nextValue = buildNextValue(safeNext);
			onChange === null || onChange === void 0 || onChange(nextValue, {
				intent: "quantity",
				trigger: "stepper",
				product
			});
		}
	})) : /* @__PURE__ */ React.createElement(Button, {
		type: "default",
		size: "small",
		className: `${PREFIX}__options-button`,
		disabled: mergedDisabled,
		onClick: (e) => {
			e.stopPropagation();
			const meta = {
				intent: "options",
				trigger: "button",
				product
			};
			onClick === null || onClick === void 0 || onClick(value || { product_id: product === null || product === void 0 ? void 0 : product.id }, meta);
		}
	}, optionsText || locales.getText("pisell2.text.options"))))));
};
//#endregion
export { PisellSalesProductCard as default };
