"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 _tabs = _interopRequireDefault(require("../tabs"));

var _hooks = require("../hooks");

var _button = require("../button");

var _field = require("../field");

var _Coupon = _interopRequireDefault(require("./Coupon"));

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 _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

var CouponList = function CouponList(props) {
  var _props$tabsProps;

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

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

  var innerEffect = (0, _react.useRef)(false);

  var _useSetState = (0, _hooks.useSetState)({
    tab: 0,
    code: props.code
  }),
      state = _useSetState[0],
      updateState = _useSetState[1];

  var buttonDisabled = (0, _react.useMemo)(function () {
    return !props.exchangeButtonLoading && (props.exchangeButtonDisabled || !state.code || state.code.length < props.exchangeMinLength);
  }, [props.exchangeButtonLoading, props.exchangeButtonDisabled, state.code, props.exchangeMinLength]);

  var innerChange = function innerChange(code) {
    updateState({
      code: code
    });
    innerEffect.current = true;
  };

  var onExchange = function onExchange() {
    if (props.onExchange) props.onExchange(state.code); // auto clear currentCode when not use v-model

    if (!props.code) {
      innerChange('');
    }
  };

  var renderEmpty = function renderEmpty() {
    return /*#__PURE__*/_react.default.createElement("div", {
      className: (0, _clsx.default)(bem('empty'))
    }, /*#__PURE__*/_react.default.createElement("img", {
      alt: "empty",
      src: props.emptyImage
    }), /*#__PURE__*/_react.default.createElement("p", null, locale.noCoupon));
  };

  var renderExchangeBar = function renderExchangeBar() {
    if (props.showExchangeBar) {
      return /*#__PURE__*/_react.default.createElement("div", {
        className: (0, _clsx.default)(bem('exchange-bar'))
      }, /*#__PURE__*/_react.default.createElement(_field.Field, {
        value: state.code,
        onChange: innerChange,
        clearable: true,
        border: false,
        className: (0, _clsx.default)(bem('field')),
        placeholder: props.inputPlaceholder || locale.vanCouponList.placeholder,
        maxlength: 20
      }), /*#__PURE__*/_react.default.createElement(_button.Button, {
        plain: true,
        type: "primary",
        className: (0, _clsx.default)(bem('exchange')),
        text: props.exchangeButtonText || locale.vanCouponList.exchange,
        loading: props.exchangeButtonLoading,
        disabled: buttonDisabled,
        onClick: onExchange
      }));
    }

    return null;
  };

  var renderCouponTab = function renderCouponTab() {
    var coupons = props.coupons;
    var count = props.showCount ? " (" + coupons.length + ")" : '';
    var title = (props.enabledTitle || locale.vanCouponList.enable) + count;
    return /*#__PURE__*/_react.default.createElement(_tabs.default.TabPane, {
      title: title
    }, /*#__PURE__*/_react.default.createElement("div", {
      className: (0, _clsx.default)(bem('list', {
        'with-bar': props.showExchangeBar,
        'with-bottom': props.showCloseButton
      }))
    }, coupons.map(function (coupon, index) {
      return /*#__PURE__*/_react.default.createElement(_Coupon.default, {
        key: coupon.id,
        coupon: coupon,
        chosen: index === props.chosenCoupon,
        currency: props.currency,
        onClick: function onClick() {
          return props.onChange(index);
        }
      });
    }), !coupons.length && renderEmpty(), props.listFooter));
  };

  var renderDisabledTab = function renderDisabledTab() {
    var disabledCoupons = props.disabledCoupons;
    var count = props.showCount ? " (" + disabledCoupons.length + ")" : '';
    var title = (props.disabledTitle || locale.vanCouponList.disabled) + count;
    return /*#__PURE__*/_react.default.createElement(_tabs.default.TabPane, {
      title: title
    }, /*#__PURE__*/_react.default.createElement("div", {
      className: (0, _clsx.default)(bem('list', {
        'with-bar': props.showExchangeBar,
        'with-bottom': props.showCloseButton
      }))
    }, disabledCoupons.map(function (coupon) {
      return /*#__PURE__*/_react.default.createElement(_Coupon.default, {
        disabled: true,
        key: coupon.id,
        coupon: coupon,
        currency: props.currency
      });
    }), !disabledCoupons.length && renderEmpty(), props.disabledListFooter));
  };

  (0, _react.useEffect)(function () {
    if (innerEffect.current) {
      innerEffect.current = true;
      return;
    }

    updateState({
      code: props.code
    });
  }, [props.code]);
  return /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _clsx.default)(bem(), props.className),
    style: props.style
  }, renderExchangeBar(), /*#__PURE__*/_react.default.createElement(_tabs.default, _extends({
    active: state.tab,
    border: false
  }, props.tabsProps, {
    className: (0, _clsx.default)(bem('tab'), (_props$tabsProps = props.tabsProps) == null ? void 0 : _props$tabsProps.className)
  }), renderCouponTab(), renderDisabledTab()), /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _clsx.default)(bem('bottom'))
  }, /*#__PURE__*/_react.default.createElement(_button.Button, {
    "v-show": props.showCloseButton,
    round: true,
    block: true,
    type: "primary",
    className: (0, _clsx.default)(bem('close')),
    text: props.closeButtonText || locale.vanCouponList.close,
    onClick: function onClick() {
      return props.onChange == null ? void 0 : props.onChange(-1);
    }
  })));
};

var EMPTY_IMAGE = 'https://img.yzcdn.cn/vant/coupon-empty.png'; // defaultProps defined if need

CouponList.defaultProps = {
  showCount: true,
  showExchangeBar: true,
  showCloseButton: true,
  code: '',
  exchangeMinLength: 1,
  chosenCoupon: -1,
  coupons: [],
  disabledCoupons: [],
  displayedCouponIndex: -1,
  currency: '¥',
  emptyImage: EMPTY_IMAGE
};
var _default = CouponList;
exports.default = _default;