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 _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, { useMemo } from 'react';
import classNames from 'classnames';
import { EditOutlined } from '@ant-design/icons';
import { image as imageAli } from '@pisell/utils';
import { PREFIX } from "../constants";
import { toDisplayText } from "../utils/display";
import { formatMoney, stopPropagation } from "../utils/render";
import ProductSpecs from "./ProductSpecs";
var getQuantity = function getQuantity(value) {
  var quantity = Number(value);
  return Number.isFinite(quantity) && quantity > 0 ? quantity : 1;
};
var ProductImage = function ProductImage(_ref) {
  var image = _ref.image,
    title = _ref.title,
    isShowImage = _ref.isShowImage;
  if (!isShowImage) return null;
  if (image) {
    return /*#__PURE__*/React.createElement("img", {
      className: "".concat(PREFIX, "__product-image"),
      src: imageAli.ali(image, 72),
      alt: ""
    });
  }
  return /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__product-image-text")
  }, title.slice(0, 2));
};
var ProductBasicInfo = function ProductBasicInfo(_ref2) {
  var _product$_extend$disp, _product$_extend, _product$product_bund;
  var product = _ref2.product,
    symbol = _ref2.symbol,
    _ref2$type = _ref2.type,
    type = _ref2$type === void 0 ? 'a2' : _ref2$type,
    isShowImage = _ref2.isShowImage,
    isShowOriginalPrice = _ref2.isShowOriginalPrice,
    isShowEditProduct = _ref2.isShowEditProduct,
    disabledEditClick = _ref2.disabledEditClick,
    maxSpecsCount = _ref2.maxSpecsCount,
    onEditProduct = _ref2.onEditProduct;
  var title = toDisplayText(product.title);
  var image = product.cover;
  var quantity = getQuantity((_product$_extend$disp = (_product$_extend = product._extend) === null || _product$_extend === void 0 ? void 0 : _product$_extend.displayQuantity) !== null && _product$_extend$disp !== void 0 ? _product$_extend$disp : product.num);
  var unitPrice = product.selling_price;
  var originalPrice = product.original_price;
  var optionText = toDisplayText(product.product_option_string);
  var options = product.product_option_item;
  var hasProductBundles = Boolean((_product$product_bund = product.product_bundle) === null || _product$product_bund === void 0 ? void 0 : _product$product_bund.length);
  var hasMeta = Boolean(optionText || (options === null || options === void 0 ? void 0 : options.length) || hasProductBundles);
  var showOriginalPrice = useMemo(function () {
    if (!isShowOriginalPrice || hasMeta) return false;
    var original = Number(originalPrice || 0);
    var current = Number(unitPrice || 0);
    return original > 0 && original !== current;
  }, [hasMeta, isShowOriginalPrice, originalPrice, unitPrice]);
  var showAmount = type === 'a2';
  var showImageSlot = Boolean(isShowImage);
  var quantityClassName = classNames("".concat(PREFIX, "__quantity"), "".concat(PREFIX, "__quantity--main"), _defineProperty(_defineProperty({}, "".concat(PREFIX, "__quantity--with-image"), showImageSlot), "".concat(PREFIX, "__quantity--without-image"), !showImageSlot));
  var editButton = isShowEditProduct ? /*#__PURE__*/React.createElement("button", {
    className: "".concat(PREFIX, "__edit-btn"),
    type: "button",
    disabled: disabledEditClick,
    onClick: function onClick(event) {
      stopPropagation(event);
      if (!disabledEditClick) onEditProduct === null || onEditProduct === void 0 || onEditProduct(product);
    }
  }, /*#__PURE__*/React.createElement(EditOutlined, null)) : null;
  if (type === 'a5') {
    return /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "__basic-info ").concat(PREFIX, "__basic-info--a5")
    }, /*#__PURE__*/React.createElement("span", {
      className: quantityClassName
    }, quantity < 100 ? quantity : '99+'), /*#__PURE__*/React.createElement(ProductImage, {
      image: image,
      title: title,
      isShowImage: isShowImage
    }), /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "__product-info ").concat(PREFIX, "__product-info--a5")
    }, /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "__product-title-row")
    }, /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "__product-title")
    }, title), editButton), /*#__PURE__*/React.createElement(ProductSpecs, {
      optionText: optionText,
      options: options,
      maxSpecsCount: maxSpecsCount,
      symbol: symbol,
      type: type
    }), /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "__product-price")
    }, formatMoney(unitPrice, symbol))));
  }
  if (type === 'a9') {
    return /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "__basic-info ").concat(PREFIX, "__basic-info--a9")
    }, /*#__PURE__*/React.createElement("span", {
      className: quantityClassName
    }, quantity < 100 ? quantity : '99+'), /*#__PURE__*/React.createElement(ProductImage, {
      image: image,
      title: title,
      isShowImage: isShowImage
    }), /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "__product-info ").concat(PREFIX, "__product-info--a9")
    }, /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "__product-basic-row")
    }, /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "__product-title-row")
    }, /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "__product-title")
    }, title), editButton), /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "__product-price")
    }, /*#__PURE__*/React.createElement("span", null, formatMoney(unitPrice, symbol)), showOriginalPrice ? /*#__PURE__*/React.createElement("span", {
      className: "".concat(PREFIX, "__product-original-price")
    }, formatMoney(originalPrice, symbol)) : null)), /*#__PURE__*/React.createElement(ProductSpecs, {
      optionText: optionText,
      options: options,
      maxSpecsCount: maxSpecsCount,
      symbol: symbol,
      type: type
    })));
  }
  return /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__basic-info ").concat(PREFIX, "__basic-info--a2")
  }, /*#__PURE__*/React.createElement("span", {
    className: quantityClassName
  }, quantity < 100 ? quantity : '99+'), /*#__PURE__*/React.createElement(ProductImage, {
    image: image,
    title: title,
    isShowImage: isShowImage
  }), /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__product-info")
  }, /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__product-title")
  }, title), hasMeta ? /*#__PURE__*/React.createElement(ProductSpecs, {
    optionText: optionText,
    options: options,
    maxSpecsCount: maxSpecsCount,
    symbol: symbol,
    type: type
  }) : /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__product-price")
  }, formatMoney(unitPrice, symbol))), showAmount ? /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__product-amount")
  }, /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__product-total")
  }, formatMoney(Number(product.selling_price || 0) * quantity, symbol)), showOriginalPrice ? /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__product-origin-total")
  }, formatMoney(Number(product.original_price || 0) * quantity, symbol)) : null) : null);
};
export default ProductBasicInfo;