import React, { useContext, useMemo } from 'react';
import cls from 'clsx';
import ConfigProviderContext from '../config-provider/ConfigProviderContext';
import { padZero } from '../utils';
import { Checkbox } from '../checkbox';

function getDate(timeStamp) {
  var date = new Date(timeStamp * 1000);
  return date.getFullYear() + "." + padZero(date.getMonth() + 1) + "." + 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 = useContext(ConfigProviderContext),
      prefixCls = _useContext.prefixCls,
      createNamespace = _useContext.createNamespace,
      locale = _useContext.locale;

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

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

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

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

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

    return '';
  }, [props.coupon, props.coupon]);
  var conditionMessage = 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.createElement("div", {
    className: cls(props.className, bem({
      disabled: disabled
    })),
    style: props.style,
    onClick: props.onClick
  }, /*#__PURE__*/React.createElement("div", {
    className: cls(bem('content'))
  }, /*#__PURE__*/React.createElement("div", {
    className: cls(bem('head'))
  }, /*#__PURE__*/React.createElement("h2", {
    className: cls(bem('amount'))
  }, faceAmount), /*#__PURE__*/React.createElement("p", {
    className: cls(bem('condition'))
  }, coupon.condition || conditionMessage)), /*#__PURE__*/React.createElement("div", {
    className: cls(bem('body'))
  }, /*#__PURE__*/React.createElement("p", {
    className: cls(bem('name'))
  }, coupon.name), /*#__PURE__*/React.createElement("p", {
    className: cls(bem('valid'))
  }, validPeriod), !disabled && /*#__PURE__*/React.createElement(Checkbox, {
    className: cls(bem('corner')),
    checked: chosen
  }))), description && /*#__PURE__*/React.createElement("p", {
    className: cls(bem('description'))
  }, description));
}; // defaultProps defined if need


Coupon.defaultProps = {
  currency: '¥'
};
export default Coupon;