"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 _utils = require("../utils");

var _checkbox = require("../checkbox");

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 getDate(timeStamp) {
  var date = new Date(timeStamp * 1000);
  return date.getFullYear() + "." + (0, _utils.padZero)(date.getMonth() + 1) + "." + (0, _utils.padZero)(date.getDate());
}

function formatDiscount(discount) {
  return (discount / 10).toFixed(discount % 10 === 0 ? 0 : 1);
}

function formatAmount(amount) {
  // eslint-disable-next-line no-nested-ternary
  return (amount / 100).toFixed(amount % 100 === 0 ? 0 : amount % 10 === 0 ? 1 : 2);
}

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

  var _createNamespace = createNamespace('coupon', prefixCls),
      bem = _createNamespace[0];

  var validPeriod = (0, _react.useMemo)(function () {
    var _props$coupon = props.coupon,
        startAt = _props$coupon.startAt,
        endAt = _props$coupon.endAt;
    return getDate(startAt) + " - " + getDate(endAt);
  }, [props.coupon]);
  var faceAmount = (0, _react.useMemo)(function () {
    var coupon = props.coupon,
        currency = props.currency;

    if (coupon.valueDesc) {
      return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, coupon.valueDesc, " ", /*#__PURE__*/_react.default.createElement("span", null, coupon.unitDesc || ''));
    }

    if (coupon.denominations) {
      var denominations = formatAmount(coupon.denominations);
      return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement("span", null, currency), " $", denominations);
    }

    if (coupon.discount) {
      return locale.vanCoupon.discount(+formatDiscount(coupon.discount));
    }

    return '';
  }, [props.coupon, props.coupon]);
  var conditionMessage = (0, _react.useMemo)(function () {
    var condition = formatAmount(props.coupon.originCondition || 0);
    return condition === '0' ? locale.vanCoupon.unlimited : locale.vanCoupon.condition(+condition);
  }, [props.coupon.originCondition]);
  var chosen = props.chosen,
      coupon = props.coupon,
      disabled = props.disabled;
  var description = disabled && coupon.reason || coupon.description;
  return /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _clsx.default)(props.className, bem({
      disabled: disabled
    })),
    style: props.style,
    onClick: props.onClick
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _clsx.default)(bem('content'))
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _clsx.default)(bem('head'))
  }, /*#__PURE__*/_react.default.createElement("h2", {
    className: (0, _clsx.default)(bem('amount'))
  }, faceAmount), /*#__PURE__*/_react.default.createElement("p", {
    className: (0, _clsx.default)(bem('condition'))
  }, coupon.condition || conditionMessage)), /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _clsx.default)(bem('body'))
  }, /*#__PURE__*/_react.default.createElement("p", {
    className: (0, _clsx.default)(bem('name'))
  }, coupon.name), /*#__PURE__*/_react.default.createElement("p", {
    className: (0, _clsx.default)(bem('valid'))
  }, validPeriod), !disabled && /*#__PURE__*/_react.default.createElement(_checkbox.Checkbox, {
    className: (0, _clsx.default)(bem('corner')),
    checked: chosen
  }))), description && /*#__PURE__*/_react.default.createElement("p", {
    className: (0, _clsx.default)(bem('description'))
  }, description));
}; // defaultProps defined if need


Coupon.defaultProps = {
  currency: '¥'
};
var _default = Coupon;
exports.default = _default;