import React, { useContext } from 'react';
import cls from 'clsx';
import ConfigProviderContext from '../config-provider/ConfigProviderContext';
import { isDef } from '../utils';
import { Cell } from '../cell';

function formatValue(coupons, chosenCoupon, currency, locale) {
  var coupon = coupons[+chosenCoupon];

  if (coupon) {
    var value = 0;

    if (isDef(coupon.value)) {
      value = coupon.value;
    } else if (isDef(coupon.denominations)) {
      value = coupon.denominations;
    }

    return "-" + currency + " " + (value / 100).toFixed(2);
  }

  return coupons.length === 0 ? locale.noCoupon : locale.vanCouponCell.count(coupons.length);
}

var CouponCell = function CouponCell(props) {
  var _useContext = useContext(ConfigProviderContext),
      prefixCls = _useContext.prefixCls,
      createNamespace = _useContext.createNamespace,
      locale = _useContext.locale;

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

  var selected = props.coupons[+props.chosenCoupon];
  var value = formatValue(props.coupons, props.chosenCoupon, props.currency, locale);
  return /*#__PURE__*/React.createElement(Cell, {
    style: props.style,
    className: cls(bem(), props.className),
    value: value,
    title: props.title || locale.vanCouponCell.title,
    border: props.border,
    isLink: props.editable,
    valueClass: cls(bem('value', {
      selected: selected
    })),
    onClick: props.onClick
  });
}; // defaultProps defined if need


CouponCell.defaultProps = {
  border: true,
  editable: true,
  coupons: [],
  currency: '¥',
  chosenCoupon: -1
};
export default CouponCell;