import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import { 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) {
  const 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);
}

const Coupon = props => {
  const {
    prefixCls,
    createNamespace,
    locale
  } = useContext(ConfigProviderContext);
  const [bem] = createNamespace('coupon', prefixCls);
  const validPeriod = useMemo(() => {
    const {
      startAt,
      endAt
    } = props.coupon;
    return `${getDate(startAt)} - ${getDate(endAt)}`;
  }, [props.coupon]);
  const faceAmount = useMemo(() => {
    const {
      coupon,
      currency
    } = props;

    if (coupon.valueDesc) {
      return _jsxs(_Fragment, {
        children: [coupon.valueDesc, " ", _jsx("span", {
          children: coupon.unitDesc || ''
        })]
      });
    }

    if (coupon.denominations) {
      const denominations = formatAmount(coupon.denominations);
      return _jsxs(_Fragment, {
        children: [_jsx("span", {
          children: currency
        }), " $", denominations]
      });
    }

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

    return '';
  }, [props.coupon, props.coupon]);
  const conditionMessage = useMemo(() => {
    const condition = formatAmount(props.coupon.originCondition || 0);
    return condition === '0' ? locale.vanCoupon.unlimited : locale.vanCoupon.condition(+condition);
  }, [props.coupon.originCondition]);
  const {
    chosen,
    coupon,
    disabled
  } = props;
  const description = disabled && coupon.reason || coupon.description;
  return _jsxs("div", Object.assign({
    className: cls(props.className, bem({
      disabled
    })),
    style: props.style,
    onClick: props.onClick
  }, {
    children: [_jsxs("div", Object.assign({
      className: cls(bem('content'))
    }, {
      children: [_jsxs("div", Object.assign({
        className: cls(bem('head'))
      }, {
        children: [_jsx("h2", Object.assign({
          className: cls(bem('amount'))
        }, {
          children: faceAmount
        })), _jsx("p", Object.assign({
          className: cls(bem('condition'))
        }, {
          children: coupon.condition || conditionMessage
        }))]
      })), _jsxs("div", Object.assign({
        className: cls(bem('body'))
      }, {
        children: [_jsx("p", Object.assign({
          className: cls(bem('name'))
        }, {
          children: coupon.name
        })), _jsx("p", Object.assign({
          className: cls(bem('valid'))
        }, {
          children: validPeriod
        })), !disabled && _jsx(Checkbox, {
          className: cls(bem('corner')),
          checked: chosen
        })]
      }))]
    })), description && _jsx("p", Object.assign({
      className: cls(bem('description'))
    }, {
      children: description
    }))]
  }));
}; // defaultProps defined if need


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