"use strict";

exports.__esModule = true;
exports.default = void 0;

var _react = _interopRequireWildcard(require("react"));

var _clsx = _interopRequireDefault(require("clsx"));

var _ConfigProviderContext = _interopRequireDefault(require("../config-provider/ConfigProviderContext"));

var _tag = require("../tag");

var _image = require("../image");

var _utils = require("../utils");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function isStringOrNumber(target) {
  return typeof target === 'string' || typeof target === 'number';
}

var ProductCard = function ProductCard(props) {
  var _useContext = (0, _react.useContext)(_ConfigProviderContext.default),
      prefixCls = _useContext.prefixCls,
      createNamespace = _useContext.createNamespace;

  var _createNamespace = createNamespace('product-card', prefixCls),
      bem = _createNamespace[0];

  var renderTitle = function renderTitle() {
    if (props.title) {
      return /*#__PURE__*/_react.default.createElement("div", {
        className: (0, _clsx.default)(bem('title'), 'rv-multi-ellipsis--l2')
      }, props.title);
    }

    return null;
  };

  var renderThumbTag = function renderThumbTag() {
    if (props.tag) {
      return /*#__PURE__*/_react.default.createElement("div", {
        className: (0, _clsx.default)(bem('tag'))
      }, isStringOrNumber(props.tag) ? /*#__PURE__*/_react.default.createElement(_tag.Tag, {
        mark: true,
        type: "danger"
      }, props.tag) : props.tag);
    }

    return null;
  };

  var renderThumbImage = function renderThumbImage() {
    if (!props.thumb) return null;

    if (typeof props.thumb === 'string') {
      return /*#__PURE__*/_react.default.createElement(_image.Image, {
        src: props.thumb,
        fit: "cover",
        width: "100%",
        height: "100%",
        lazyload: props.lazyload
      });
    }

    return props.thumb;
  };

  var renderThumb = function renderThumb() {
    if (props.thumb) {
      return /*#__PURE__*/_react.default.createElement("a", {
        href: props.thumbLink,
        className: (0, _clsx.default)(bem('thumb')),
        onClick: props.onClickThumb
      }, renderThumbImage(), renderThumbTag());
    }

    return null;
  };

  var renderDesc = function renderDesc() {
    if (props.desc) {
      return /*#__PURE__*/_react.default.createElement("div", {
        className: (0, _clsx.default)(bem('desc'), 'rv-ellipsis')
      }, props.desc);
    }

    return null;
  };

  var renderPriceText = function renderPriceText() {
    if (isStringOrNumber(props.price)) {
      var priceArr = props.price.toString().split('.');
      return /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement("span", {
        className: (0, _clsx.default)(bem('price-currency'))
      }, props.currency), /*#__PURE__*/_react.default.createElement("span", {
        className: (0, _clsx.default)(bem('price-integer'))
      }, priceArr[0]), props.decimal && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, ".", /*#__PURE__*/_react.default.createElement("span", {
        className: (0, _clsx.default)(bem('price-decimal'))
      }, priceArr[1] || '00')));
    }

    return props.price;
  };

  var renderOriginalPrice = function renderOriginalPrice() {
    return /*#__PURE__*/_react.default.createElement("div", {
      className: (0, _clsx.default)(bem('origin-price'))
    }, isStringOrNumber(props.originPrice) ? props.currency + " " + props.originPrice : props.originPrice);
  };

  var showNum = (0, _utils.isDef)(props.num);
  var showPrice = (0, _utils.isDef)(props.price);
  var showOriginPrice = (0, _utils.isDef)(props.originPrice);
  var showBottom = showNum || showPrice || showOriginPrice || props.bottom;

  var Price = showPrice && /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _clsx.default)(bem('price'))
  }, renderPriceText());

  var OriginPrice = showOriginPrice && renderOriginalPrice();

  var Num = showNum && /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _clsx.default)(bem('num'))
  }, isStringOrNumber(props.num) ? "x" + props.num : props.num);

  var Footer = props.footer && /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _clsx.default)(bem('footer'))
  }, props.footer);

  var Bottom = showBottom && /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _clsx.default)(bem('bottom'))
  }, props.priceTop, Price, OriginPrice, Num, props.bottom);

  return /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _clsx.default)(bem(), props.className),
    style: props.style,
    onClick: props.onClick
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _clsx.default)(bem('header'))
  }, renderThumb(), /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _clsx.default)(bem('content', {
      centered: props.centered
    }))
  }, /*#__PURE__*/_react.default.createElement("div", null, renderTitle(), renderDesc(), props.tags), Bottom)), Footer);
}; // defaultProps defined if need


ProductCard.defaultProps = {
  currency: '¥',
  decimal: true
};
var _default = ProductCard;
exports.default = _default;