"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 _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 (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) {
  const 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);
}

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

    if (coupon.valueDesc) {
      return (0, _jsxRuntime().jsxs)(_jsxRuntime().Fragment, {
        children: [coupon.valueDesc, " ", (0, _jsxRuntime().jsx)("span", {
          children: coupon.unitDesc || ''
        })]
      });
    }

    if (coupon.denominations) {
      const denominations = formatAmount(coupon.denominations);
      return (0, _jsxRuntime().jsxs)(_jsxRuntime().Fragment, {
        children: [(0, _jsxRuntime().jsx)("span", {
          children: currency
        }), " $", denominations]
      });
    }

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

    return '';
  }, [props.coupon, props.coupon]);
  const conditionMessage = (0, _react().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 (0, _jsxRuntime().jsxs)("div", Object.assign({
    className: (0, _clsx().default)(props.className, bem({
      disabled
    })),
    style: props.style,
    onClick: props.onClick
  }, {
    children: [(0, _jsxRuntime().jsxs)("div", Object.assign({
      className: (0, _clsx().default)(bem('content'))
    }, {
      children: [(0, _jsxRuntime().jsxs)("div", Object.assign({
        className: (0, _clsx().default)(bem('head'))
      }, {
        children: [(0, _jsxRuntime().jsx)("h2", Object.assign({
          className: (0, _clsx().default)(bem('amount'))
        }, {
          children: faceAmount
        })), (0, _jsxRuntime().jsx)("p", Object.assign({
          className: (0, _clsx().default)(bem('condition'))
        }, {
          children: coupon.condition || conditionMessage
        }))]
      })), (0, _jsxRuntime().jsxs)("div", Object.assign({
        className: (0, _clsx().default)(bem('body'))
      }, {
        children: [(0, _jsxRuntime().jsx)("p", Object.assign({
          className: (0, _clsx().default)(bem('name'))
        }, {
          children: coupon.name
        })), (0, _jsxRuntime().jsx)("p", Object.assign({
          className: (0, _clsx().default)(bem('valid'))
        }, {
          children: validPeriod
        })), !disabled && (0, _jsxRuntime().jsx)(_checkbox.Checkbox, {
          className: (0, _clsx().default)(bem('corner')),
          checked: chosen
        })]
      }))]
    })), description && (0, _jsxRuntime().jsx)("p", Object.assign({
      className: (0, _clsx().default)(bem('description'))
    }, {
      children: description
    }))]
  }));
}; // defaultProps defined if need


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