import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { IntersectionTrigger } from "../components/IntersectionObserver.js";
import index from "../components/pisellPriceText/index.js";
import "./ProductCardSkeleton.js";
import SkuCard from "./SkuCard/index.js";
import { formatProductPrice, getSkuCardConfig } from "./utils.js";
import useTranslationOriginal from "../../../hooks/useTranslationOriginal.js";
import { PREFIX } from "../index.js";
import React, { memo, useMemo } from "react";
import { image, locales } from "@pisell/utils";
import Decimal from "decimal.js";
import classNames from "classnames";
import "./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 = memo(({ item, index: index$1, context, onSelect, onSelectCover, onSelectInfo, isSelected, className, decorateConfig = {}, scenario = "default", customSkuCardAllConfig }) => {
	const finalConfig = _objectSpread2(_objectSpread2({}, defaultConfig), decorateConfig);
	const { imgHeight, imgWidth, isOriginPrice, isPrice, isProductCover, isStock, isSubtitle, isTitle, radius, showZeroPrice, gap, rightStyle } = finalConfig;
	const { __typename } = item;
	const translationOriginal = useTranslationOriginal();
	if (__typename === "category") return /* @__PURE__ */ React.createElement("div", {
		className: classNames(`${PREFIX}__product-category`, className),
		"data-index": index$1,
		"data-item-index": index$1
	}, /* @__PURE__ */ React.createElement("div", { className: "category-title" }, item.name));
	const getStockText = (item) => {
		if (item.is_track === 0) return false;
		return `${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(price).plus(new Decimal(minimum_markup_price)).toFixed(2);
			original_price = new Decimal(original_price).plus(new Decimal(minimum_markup_price)).toFixed(2);
		}
		if (!showZeroPrice && !parseFloat(price)) return null;
		return !!isPrice || !!isOriginPrice ? /* @__PURE__ */ React.createElement("div", { className: "price-info" }, isPrice ? /* @__PURE__ */ React.createElement("div", { className: "price" }, /* @__PURE__ */ React.createElement(index, {
			value: price,
			isMini: is_bundle
		})) : null, !!isOriginPrice && parseFloat(original_price) ? /* @__PURE__ */ React.createElement("div", { className: "original-price" }, /* @__PURE__ */ React.createElement(index, {
			value: original_price,
			isMini: is_bundle
		})) : null) : null;
	};
	const cardContent = /* @__PURE__ */ React.createElement("div", {
		className: classNames(`${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.createElement("div", {
		className: "card-left",
		style: {
			width: imgWidth || "auto",
			height: imgHeight || "auto"
		},
		onClick: () => {
			onSelectCover === null || onSelectCover === void 0 || onSelectCover(item);
		}
	}, item.cover ? /* @__PURE__ */ React.createElement("img", {
		loading: "lazy",
		src: image.ali(item.cover, 360),
		alt: item.title,
		style: { borderRadius: radius }
	}) : /* @__PURE__ */ React.createElement("div", {
		className: "text-cover",
		style: { borderRadius: radius }
	}, getTextCover(item.title))) : null, /* @__PURE__ */ React.createElement("div", {
		className: "card-right",
		onClick: () => {
			onSelectInfo === null || onSelectInfo === void 0 || onSelectInfo(item);
		}
	}, !!isTitle ? /* @__PURE__ */ React.createElement("div", { className: "title" }, item.title) : null, !!isSubtitle && item.subtitle ? /* @__PURE__ */ React.createElement("div", { className: "subtitle" }, item.subtitle) : null, !!isStock && !!item.is_track ? /* @__PURE__ */ React.createElement("div", { className: "stock" }, getStockText(item)) : null, renderPrice(item)));
	/**
	* @description: 准备 SkuCard 数据源
	* @return {*}
	*/
	const skuCardDataSource = useMemo(() => {
		const { price, original_price } = formatProductPrice(item);
		return _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) => _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 = useMemo(() => {
		const skuCardAllConfig = customSkuCardAllConfig || getSkuCardConfig(finalConfig);
		return /* @__PURE__ */ React.createElement(SkuCard, _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: _objectSpread2({
				gap,
				alignItems: "center"
			}, skuCardAllConfig.style),
			rightStyle: _objectSpread2(_objectSpread2({}, rightStyle), skuCardAllConfig.rightStyle),
			className
		}));
	}, [
		finalConfig,
		item,
		skuCardDataSource,
		onSelect,
		onSelectCover,
		onSelectInfo,
		isSelected,
		index$1,
		gap,
		rightStyle,
		className,
		customSkuCardAllConfig
	]);
	/**
	* @description: 根据 scenario 决定渲染内容
	* @return {*}
	*/
	const renderContent = useMemo(() => {
		switch (scenario) {
			case "booking2": return skuCardRender;
			case "default": return cardContent;
			default: return cardContent;
		}
	}, [
		scenario,
		skuCardRender,
		cardContent
	]);
	if (context) return /* @__PURE__ */ React.createElement(IntersectionTrigger, { onShow: () => context.onItemIntersectionIn(item) }, renderContent);
	return renderContent;
});
ProductCard.displayName = "ProductCard";
const DefaultItemContent = memo((props) => {
	return /* @__PURE__ */ React.createElement(ProductCard, props);
});
DefaultItemContent.displayName = "DefaultItemContent";
//#endregion
export { ProductCard as default };
