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); }

import React, { useContext, useEffect, useMemo, useRef } from 'react';
import cls from 'clsx';
import ConfigProviderContext from '../config-provider/ConfigProviderContext';
import Tabs from '../tabs';
import { useSetState } from '../hooks';
import { Button } from '../button';
import { Field } from '../field';
import Coupon from './Coupon';

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

  var _useContext = useContext(ConfigProviderContext),
      prefixCls = _useContext.prefixCls,
      createNamespace = _useContext.createNamespace,
      locale = _useContext.locale;

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

  var innerEffect = useRef(false);

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

  var buttonDisabled = 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.createElement("div", {
      className: cls(bem('empty'))
    }, /*#__PURE__*/React.createElement("img", {
      alt: "empty",
      src: props.emptyImage
    }), /*#__PURE__*/React.createElement("p", null, locale.noCoupon));
  };

  var renderExchangeBar = function renderExchangeBar() {
    if (props.showExchangeBar) {
      return /*#__PURE__*/React.createElement("div", {
        className: cls(bem('exchange-bar'))
      }, /*#__PURE__*/React.createElement(Field, {
        value: state.code,
        onChange: innerChange,
        clearable: true,
        border: false,
        className: cls(bem('field')),
        placeholder: props.inputPlaceholder || locale.vanCouponList.placeholder,
        maxlength: 20
      }), /*#__PURE__*/React.createElement(Button, {
        plain: true,
        type: "primary",
        className: cls(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.createElement(Tabs.TabPane, {
      title: title
    }, /*#__PURE__*/React.createElement("div", {
      className: cls(bem('list', {
        'with-bar': props.showExchangeBar,
        'with-bottom': props.showCloseButton
      }))
    }, coupons.map(function (coupon, index) {
      return /*#__PURE__*/React.createElement(Coupon, {
        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.createElement(Tabs.TabPane, {
      title: title
    }, /*#__PURE__*/React.createElement("div", {
      className: cls(bem('list', {
        'with-bar': props.showExchangeBar,
        'with-bottom': props.showCloseButton
      }))
    }, disabledCoupons.map(function (coupon) {
      return /*#__PURE__*/React.createElement(Coupon, {
        disabled: true,
        key: coupon.id,
        coupon: coupon,
        currency: props.currency
      });
    }), !disabledCoupons.length && renderEmpty(), props.disabledListFooter));
  };

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

    updateState({
      code: props.code
    });
  }, [props.code]);
  return /*#__PURE__*/React.createElement("div", {
    className: cls(bem(), props.className),
    style: props.style
  }, renderExchangeBar(), /*#__PURE__*/React.createElement(Tabs, _extends({
    active: state.tab,
    border: false
  }, props.tabsProps, {
    className: cls(bem('tab'), (_props$tabsProps = props.tabsProps) == null ? void 0 : _props$tabsProps.className)
  }), renderCouponTab(), renderDisabledTab()), /*#__PURE__*/React.createElement("div", {
    className: cls(bem('bottom'))
  }, /*#__PURE__*/React.createElement(Button, {
    "v-show": props.showCloseButton,
    round: true,
    block: true,
    type: "primary",
    className: cls(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
};
export default CouponList;