"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

function _react() {
  const data = _interopRequireWildcard(require("react"));

  _react = function () {
    return data;
  };

  return data;
}

function _jsxRuntime() {
  const data = require("react/jsx-runtime");

  _jsxRuntime = function () {
    return data;
  };

  return data;
}

function _clsx() {
  const data = _interopRequireDefault(require("clsx"));

  _clsx = function () {
    return data;
  };

  return data;
}

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

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

var _image = _interopRequireDefault(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 (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';
}

const ProductCard = props => {
  const {
    prefixCls,
    createNamespace
  } = (0, _react().useContext)(_ConfigProviderContext.default);
  const [bem] = createNamespace('product-card', prefixCls);

  const renderTitle = () => {
    if (props.title) {
      return (0, _jsxRuntime().jsx)("div", Object.assign({
        className: (0, _clsx().default)(bem('title'), 'rv-multi-ellipsis--l2')
      }, {
        children: props.title
      }));
    }

    return null;
  };

  const renderThumbTag = () => {
    if (props.tag) {
      return (0, _jsxRuntime().jsx)("div", Object.assign({
        className: (0, _clsx().default)(bem('tag'))
      }, {
        children: isStringOrNumber(props.tag) ? (0, _jsxRuntime().jsx)(_tag.default, Object.assign({
          mark: true,
          type: "danger"
        }, {
          children: props.tag
        })) : props.tag
      }));
    }

    return null;
  };

  const renderThumbImage = () => {
    if (!props.thumb) return null;

    if (typeof props.thumb === 'string') {
      return (0, _jsxRuntime().jsx)(_image.default, {
        src: props.thumb,
        fit: "cover",
        width: "100%",
        height: "100%",
        lazyload: props.lazyload
      });
    }

    return props.thumb;
  };

  const renderThumb = () => {
    if (props.thumb) {
      return (0, _jsxRuntime().jsxs)("a", Object.assign({
        href: props.thumbLink,
        className: (0, _clsx().default)(bem('thumb')),
        onClick: props.onClickThumb
      }, {
        children: [renderThumbImage(), renderThumbTag()]
      }));
    }

    return null;
  };

  const renderDesc = () => {
    if (props.desc) {
      return (0, _jsxRuntime().jsx)("div", Object.assign({
        className: (0, _clsx().default)(bem('desc'), 'rv-ellipsis')
      }, {
        children: props.desc
      }));
    }

    return null;
  };

  const renderPriceText = () => {
    if (isStringOrNumber(props.price)) {
      const priceArr = props.price.toString().split('.');
      return (0, _jsxRuntime().jsxs)("div", {
        children: [(0, _jsxRuntime().jsx)("span", Object.assign({
          className: (0, _clsx().default)(bem('price-currency'))
        }, {
          children: props.currency
        })), (0, _jsxRuntime().jsx)("span", Object.assign({
          className: (0, _clsx().default)(bem('price-integer'))
        }, {
          children: priceArr[0]
        })), props.decimal && (0, _jsxRuntime().jsxs)(_jsxRuntime().Fragment, {
          children: [".", (0, _jsxRuntime().jsx)("span", Object.assign({
            className: (0, _clsx().default)(bem('price-decimal'))
          }, {
            children: priceArr[1] || '00'
          }))]
        })]
      });
    }

    return props.price;
  };

  const renderOriginalPrice = () => {
    return (0, _jsxRuntime().jsx)("div", Object.assign({
      className: (0, _clsx().default)(bem('origin-price'))
    }, {
      children: isStringOrNumber(props.originPrice) ? `${props.currency} ${props.originPrice}` : props.originPrice
    }));
  };

  const showNum = (0, _utils.isDef)(props.num);
  const showPrice = (0, _utils.isDef)(props.price);
  const showOriginPrice = (0, _utils.isDef)(props.originPrice);
  const showBottom = showNum || showPrice || showOriginPrice || props.bottom;
  const Price = showPrice && (0, _jsxRuntime().jsx)("div", Object.assign({
    className: (0, _clsx().default)(bem('price'))
  }, {
    children: renderPriceText()
  }));
  const OriginPrice = showOriginPrice && renderOriginalPrice();
  const Num = showNum && (0, _jsxRuntime().jsx)("div", Object.assign({
    className: (0, _clsx().default)(bem('num'))
  }, {
    children: isStringOrNumber(props.num) ? `x${props.num}` : props.num
  }));
  const Footer = props.footer && (0, _jsxRuntime().jsx)("div", Object.assign({
    className: (0, _clsx().default)(bem('footer'))
  }, {
    children: props.footer
  }));
  const Bottom = showBottom && (0, _jsxRuntime().jsxs)("div", Object.assign({
    className: (0, _clsx().default)(bem('bottom'))
  }, {
    children: [props.priceTop, Price, OriginPrice, Num, props.bottom]
  }));
  return (0, _jsxRuntime().jsxs)("div", Object.assign({
    className: (0, _clsx().default)(bem(), props.className),
    style: props.style,
    onClick: props.onClick
  }, {
    children: [(0, _jsxRuntime().jsxs)("div", Object.assign({
      className: (0, _clsx().default)(bem('header'))
    }, {
      children: [renderThumb(), (0, _jsxRuntime().jsxs)("div", Object.assign({
        className: (0, _clsx().default)(bem('content', {
          centered: props.centered
        }))
      }, {
        children: [(0, _jsxRuntime().jsxs)("div", {
          children: [renderTitle(), renderDesc(), props.tags]
        }), Bottom]
      }))]
    })), Footer]
  }));
}; // defaultProps defined if need


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