function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import React, { memo, useMemo } from 'react';
import classNames from 'classnames';
import { locales, image as imageAli } from '@pisell/utils';
import Decimal from 'decimal.js';
import { IntersectionTrigger } from "../components/IntersectionObserver";
import PisellPriceText from "../components/pisellPriceText";
import ProductCardSkeleton from "./ProductCardSkeleton";
import SkuCard from "./SkuCard";
import { formatProductPrice, getSkuCardConfig } from "./utils";
import useTranslationOriginal from "../../../hooks/useTranslationOriginal";
import "./index.less";
import { PREFIX } from "../index";

// 产品卡片配置接口

// 产品卡片属性接口

// 默认配置
var 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'
  }
};

// 产品卡片组件
export var ProductCard = /*#__PURE__*/memo(function (_ref) {
  var item = _ref.item,
    index = _ref.index,
    context = _ref.context,
    onSelect = _ref.onSelect,
    _onSelectCover = _ref.onSelectCover,
    _onSelectInfo = _ref.onSelectInfo,
    isSelected = _ref.isSelected,
    className = _ref.className,
    _ref$decorateConfig = _ref.decorateConfig,
    decorateConfig = _ref$decorateConfig === void 0 ? {} : _ref$decorateConfig,
    _ref$scenario = _ref.scenario,
    scenario = _ref$scenario === void 0 ? 'default' : _ref$scenario,
    customSkuCardAllConfig = _ref.customSkuCardAllConfig;
  var finalConfig = _objectSpread(_objectSpread({}, defaultConfig), decorateConfig);
  var imgHeight = finalConfig.imgHeight,
    imgWidth = finalConfig.imgWidth,
    isOriginPrice = finalConfig.isOriginPrice,
    isPrice = finalConfig.isPrice,
    isProductCover = finalConfig.isProductCover,
    isStock = finalConfig.isStock,
    isSubtitle = finalConfig.isSubtitle,
    isTitle = finalConfig.isTitle,
    radius = finalConfig.radius,
    showZeroPrice = finalConfig.showZeroPrice,
    gap = finalConfig.gap,
    rightStyle = finalConfig.rightStyle;
  var __typename = item.__typename;
  var translationOriginal = useTranslationOriginal();

  // 如果是分类，显示分类样式
  if (__typename === 'category') {
    return /*#__PURE__*/React.createElement("div", {
      className: classNames("".concat(PREFIX, "__product-category"), className),
      "data-index": index,
      "data-item-index": index
    }, /*#__PURE__*/React.createElement("div", {
      className: "category-title"
    }, item.name));
  }

  // 获取库存文本
  var getStockText = function getStockText(item) {
    if (item.is_track === 0) {
      return false;
    }
    return "".concat(locales.getText('pisell2.product-select.stock'), "\uFF1A").concat(item.stock_quantity);
  };
  var getTextCover = function getTextCover(title) {
    if (!title) return '';
    var cleanTitle = title.trim();
    if (cleanTitle.length === 0) return '';
    return cleanTitle.substring(0, 2).toUpperCase();
  };

  // 渲染价格
  var renderPrice = function renderPrice(item) {
    var _price = item.price,
      _original_price = item.original_price,
      is_bundle = item.is_bundle,
      minimum_markup_price = item.minimum_markup_price;
    var price = _price;
    var 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(PisellPriceText, {
      value: price,
      isMini: is_bundle
    })) : null, !!isOriginPrice && parseFloat(original_price) ? /*#__PURE__*/React.createElement("div", {
      className: "original-price"
    }, /*#__PURE__*/React.createElement(PisellPriceText, {
      value: original_price,
      isMini: is_bundle
    })) : null) : null;
  };

  // 产品卡片内容
  var cardContent = /*#__PURE__*/React.createElement("div", {
    className: classNames("".concat(PREFIX, "__product-card"), className, {
      selected: isSelected
    }),
    onClick: function onClick() {
      // 使用onClick 会有点击300ms延迟问题
      // 使用onPointerUp的话 会触发到点击位置弹窗内 onClick 所以增加两次 requestAnimationFrame解决该问题
      onSelect === null || onSelect === void 0 || onSelect(item);
      // requestAnimationFrame(() => requestAnimationFrame(() => onSelect?.(item)));
    },
    "data-index": index,
    "data-item-index": index
  }, isProductCover ? /*#__PURE__*/React.createElement("div", {
    className: "card-left",
    style: {
      width: imgWidth || 'auto',
      height: imgHeight || 'auto'
    },
    onClick: function onClick() {
      // 使用onClick 会有点击300ms延迟问题
      // 使用onPointerUp的话 会触发到点击位置弹窗内 onClick 所以增加两次 requestAnimationFrame解决该问题
      _onSelectCover === null || _onSelectCover === void 0 || _onSelectCover(item);
      // requestAnimationFrame(() => requestAnimationFrame(() => onSelectCover?.(item)));
    }
  }, item.cover ? /*#__PURE__*/React.createElement("img", {
    loading: "lazy",
    src: imageAli.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: function onClick() {
      // 使用onClick 会有点击300ms延迟问题
      // 使用onPointerUp的话 会触发到点击位置弹窗内 onClick 所以增加两次 requestAnimationFrame解决该问题
      _onSelectInfo === null || _onSelectInfo === void 0 || _onSelectInfo(item);
      // requestAnimationFrame(() => requestAnimationFrame(() => 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 {*}
   */
  var skuCardDataSource = useMemo(function () {
    var _formatProductPrice = formatProductPrice(item),
      price = _formatProductPrice.price,
      original_price = _formatProductPrice.original_price;
    return _objectSpread(_objectSpread({}, item), {}, {
      sortKeys: ['title', 'subtitle', 'stock', 'price', 'promotions'],
      price: price,
      original_price: original_price,
      isMini: !!(item !== null && item !== void 0 && item.is_bundle),
      promotions: ((item === null || item === void 0 ? void 0 : item._promotionTags) || []).map(function (promotionItem) {
        return _objectSpread(_objectSpread({}, promotionItem), {}, {
          title: translationOriginal === null || translationOriginal === void 0 ? void 0 : translationOriginal(promotionItem === null || promotionItem === void 0 ? void 0 : promotionItem.text)
        });
      })
    });
  }, [item, translationOriginal]);

  /**
   * @description: 新版 SkuCard 渲染
   * @return {*}
   */
  var skuCardRender = useMemo(function () {
    var skuCardAllConfig = customSkuCardAllConfig || getSkuCardConfig(finalConfig);
    return /*#__PURE__*/React.createElement(SkuCard, _extends({
      key: item.id
    }, skuCardAllConfig, {
      dataSource: skuCardDataSource,
      onCardClick: function onCardClick() {
        return onSelect === null || onSelect === void 0 ? void 0 : onSelect(item);
      },
      onSelectCover: function onSelectCover() {
        return _onSelectCover === null || _onSelectCover === void 0 ? void 0 : _onSelectCover(item);
      },
      onSelectInfo: function onSelectInfo() {
        return _onSelectInfo === null || _onSelectInfo === void 0 ? void 0 : _onSelectInfo(item);
      },
      isSelected: isSelected,
      "data-index": index,
      "data-item-index": index,
      style: _objectSpread({
        gap: gap,
        alignItems: 'center'
      }, skuCardAllConfig.style),
      rightStyle: _objectSpread(_objectSpread({}, rightStyle), skuCardAllConfig.rightStyle),
      className: className
    }));
  }, [finalConfig, item, skuCardDataSource, onSelect, _onSelectCover, _onSelectInfo, isSelected, index, gap, rightStyle, className, customSkuCardAllConfig]);

  /**
   * @description: 根据 scenario 决定渲染内容
   * @return {*}
   */
  var renderContent = useMemo(function () {
    switch (scenario) {
      case 'booking2':
        return skuCardRender;
      case 'default':
        return cardContent;
      default:
        return cardContent;
    }
  }, [scenario, skuCardRender, cardContent]);
  if (context) {
    return /*#__PURE__*/React.createElement(IntersectionTrigger, {
      onShow: function onShow() {
        return context.onItemIntersectionIn(item);
      }
      // onHide={() => context.onItemIntersectionOut(item)}
    }, renderContent);
  }
  return renderContent;
});
ProductCard.displayName = 'ProductCard';

// 默认的ItemContent组件（保持向后兼容）
export var DefaultItemContent = /*#__PURE__*/memo(function (props) {
  return /*#__PURE__*/React.createElement(ProductCard, props);
});
DefaultItemContent.displayName = 'DefaultItemContent';
export default ProductCard;
export { ProductCardSkeleton };