const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_IntersectionObserver = require("../components/IntersectionObserver.js");
const require_index = require("../components/pisellPriceText/index.js");
require("./ProductCardSkeleton.js");
const require_index$1 = require("./SkuCard/index.js");
const require_utils = require("./utils.js");
const require_useTranslationOriginal = require("../../../hooks/useTranslationOriginal.js");
const require_index$2 = require("../index.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
let decimal_js = require("decimal.js");
decimal_js = require_runtime.__toESM(decimal_js);
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
require("./index.less");
//#region src/plus/productSelect/ProductCard/index.tsx
const defaultConfig = {
	isProductCover: 1,
	imgWidth: "180px",
	imgHeight: "126px",
	coverTextFontSize: 60,
	skuCardPaddingX: 8,
	skuCardPaddingY: 8,
	radius: 8,
	isTitle: 1,
	isSubtitle: 0,
	showSpec: true,
	isTag: 0,
	isPrice: 0,
	isOriginPrice: 0,
	showZeroPrice: false,
	showMarketingTag: true,
	showRemark: true,
	isStock: 0,
	layoutDirection: "row",
	gap: 12,
	rightStyle: {
		alignItems: "initial",
		minHeight: "92px"
	}
};
const ProductCard = (0, react.memo)(({ item, index: index$1, context, onSelect, onSelectCover, onSelectInfo, isSelected, className, decorateConfig = {}, scenario = "default", customSkuCardAllConfig }) => {
	const finalConfig = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, defaultConfig), decorateConfig);
	const { imgHeight, imgWidth, isOriginPrice, isPrice, isProductCover, isStock, isSubtitle, isTitle, radius, showZeroPrice, gap, rightStyle } = finalConfig;
	const { __typename } = item;
	const translationOriginal = require_useTranslationOriginal.default();
	if (__typename === "category") return /* @__PURE__ */ react.default.createElement("div", {
		className: (0, classnames.default)(`${require_index$2.PREFIX}__product-category`, className),
		"data-index": index$1,
		"data-item-index": index$1
	}, /* @__PURE__ */ react.default.createElement("div", { className: "category-title" }, item.name));
	const getStockText = (item) => {
		if (item.is_track === 0) return false;
		return `${_pisell_utils.locales.getText("pisell2.product-select.stock")}：${item.stock_quantity}`;
	};
	const getTextCover = (title) => {
		if (!title) return "";
		const cleanTitle = title.trim();
		if (cleanTitle.length === 0) return "";
		return cleanTitle.substring(0, 2).toUpperCase();
	};
	const renderPrice = (item) => {
		const { price: _price, original_price: _original_price, is_bundle, minimum_markup_price } = item;
		let price = _price;
		let original_price = _original_price;
		if (is_bundle) {
			price = new decimal_js.default(price).plus(new decimal_js.default(minimum_markup_price)).toFixed(2);
			original_price = new decimal_js.default(original_price).plus(new decimal_js.default(minimum_markup_price)).toFixed(2);
		}
		if (!showZeroPrice && !parseFloat(price)) return null;
		return !!isPrice || !!isOriginPrice ? /* @__PURE__ */ react.default.createElement("div", { className: "price-info" }, isPrice ? /* @__PURE__ */ react.default.createElement("div", { className: "price" }, /* @__PURE__ */ react.default.createElement(require_index.default, {
			value: price,
			isMini: is_bundle
		})) : null, !!isOriginPrice && parseFloat(original_price) ? /* @__PURE__ */ react.default.createElement("div", { className: "original-price" }, /* @__PURE__ */ react.default.createElement(require_index.default, {
			value: original_price,
			isMini: is_bundle
		})) : null) : null;
	};
	const cardContent = /* @__PURE__ */ react.default.createElement("div", {
		className: (0, classnames.default)(`${require_index$2.PREFIX}__product-card`, className, { selected: isSelected }),
		onClick: () => {
			onSelect === null || onSelect === void 0 || onSelect(item);
		},
		"data-index": index$1,
		"data-item-index": index$1
	}, isProductCover ? /* @__PURE__ */ react.default.createElement("div", {
		className: "card-left",
		style: {
			width: imgWidth || "auto",
			height: imgHeight || "auto"
		},
		onClick: () => {
			onSelectCover === null || onSelectCover === void 0 || onSelectCover(item);
		}
	}, item.cover ? /* @__PURE__ */ react.default.createElement("img", {
		loading: "lazy",
		src: _pisell_utils.image.ali(item.cover, 360),
		alt: item.title,
		style: { borderRadius: radius }
	}) : /* @__PURE__ */ react.default.createElement("div", {
		className: "text-cover",
		style: { borderRadius: radius }
	}, getTextCover(item.title))) : null, /* @__PURE__ */ react.default.createElement("div", {
		className: "card-right",
		onClick: () => {
			onSelectInfo === null || onSelectInfo === void 0 || onSelectInfo(item);
		}
	}, !!isTitle ? /* @__PURE__ */ react.default.createElement("div", { className: "title" }, item.title) : null, !!isSubtitle && item.subtitle ? /* @__PURE__ */ react.default.createElement("div", { className: "subtitle" }, item.subtitle) : null, !!isStock && !!item.is_track ? /* @__PURE__ */ react.default.createElement("div", { className: "stock" }, getStockText(item)) : null, renderPrice(item)));
	/**
	* @description: 准备 SkuCard 数据源
	* @return {*}
	*/
	const skuCardDataSource = (0, react.useMemo)(() => {
		const { price, original_price } = require_utils.formatProductPrice(item);
		return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, item), {}, {
			sortKeys: [
				"title",
				"subtitle",
				"stock",
				"price",
				"promotions"
			],
			price,
			original_price,
			isMini: !!(item === null || item === void 0 ? void 0 : item.is_bundle),
			promotions: ((item === null || item === void 0 ? void 0 : item._promotionTags) || []).map((promotionItem) => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, promotionItem), {}, { title: translationOriginal === null || translationOriginal === void 0 ? void 0 : translationOriginal(promotionItem === null || promotionItem === void 0 ? void 0 : promotionItem.text) }))
		});
	}, [item, translationOriginal]);
	/**
	* @description: 新版 SkuCard 渲染
	* @return {*}
	*/
	const skuCardRender = (0, react.useMemo)(() => {
		const skuCardAllConfig = customSkuCardAllConfig || require_utils.getSkuCardConfig(finalConfig);
		return /* @__PURE__ */ react.default.createElement(require_index$1.default, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({ key: item.id }, skuCardAllConfig), {}, {
			dataSource: skuCardDataSource,
			onCardClick: () => onSelect === null || onSelect === void 0 ? void 0 : onSelect(item),
			onSelectCover: () => onSelectCover === null || onSelectCover === void 0 ? void 0 : onSelectCover(item),
			onSelectInfo: () => onSelectInfo === null || onSelectInfo === void 0 ? void 0 : onSelectInfo(item),
			isSelected,
			"data-index": index$1,
			"data-item-index": index$1,
			style: require_objectSpread2._objectSpread2({
				gap,
				alignItems: "center"
			}, skuCardAllConfig.style),
			rightStyle: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, rightStyle), skuCardAllConfig.rightStyle),
			className
		}));
	}, [
		finalConfig,
		item,
		skuCardDataSource,
		onSelect,
		onSelectCover,
		onSelectInfo,
		isSelected,
		index$1,
		gap,
		rightStyle,
		className,
		customSkuCardAllConfig
	]);
	/**
	* @description: 根据 scenario 决定渲染内容
	* @return {*}
	*/
	const renderContent = (0, react.useMemo)(() => {
		switch (scenario) {
			case "booking2": return skuCardRender;
			case "default": return cardContent;
			default: return cardContent;
		}
	}, [
		scenario,
		skuCardRender,
		cardContent
	]);
	if (context) return /* @__PURE__ */ react.default.createElement(require_IntersectionObserver.IntersectionTrigger, { onShow: () => context.onItemIntersectionIn(item) }, renderContent);
	return renderContent;
});
ProductCard.displayName = "ProductCard";
const DefaultItemContent = (0, react.memo)((props) => {
	return /* @__PURE__ */ react.default.createElement(ProductCard, props);
});
DefaultItemContent.displayName = "DefaultItemContent";
//#endregion
exports.default = ProductCard;
