import _extends from "@babel/runtime/helpers/extends";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _inherits from "@babel/runtime/helpers/inherits";
import _createSuper from "@babel/runtime/helpers/createSuper";
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import Button from '../button';
import { convertLegacyProps } from '../button/button';

var ActionButton = /*#__PURE__*/function (_React$Component) {
  _inherits(ActionButton, _React$Component);

  var _super = _createSuper(ActionButton);

  function ActionButton() {
    var _this;

    _classCallCheck(this, ActionButton);

    _this = _super.apply(this, arguments);
    _this.state = {
      loading: false
    };

    _this.onClick = function () {
      var _this$props = _this.props,
          actionFn = _this$props.actionFn,
          closeModal = _this$props.closeModal;

      if (_this.clicked) {
        return;
      }

      _this.clicked = true;

      if (!actionFn) {
        closeModal();
        return;
      }

      var returnValueOfOnOk;

      if (actionFn.length) {
        returnValueOfOnOk = actionFn(closeModal); // https://github.com/ant-design/ant-design/issues/23358

        _this.clicked = false;
      } else {
        returnValueOfOnOk = actionFn();

        if (!returnValueOfOnOk) {
          closeModal();
          return;
        }
      }

      _this.handlePromiseOnOk(returnValueOfOnOk);
    };

    return _this;
  }

  _createClass(ActionButton, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      if (this.props.autoFocus) {
        var $this = ReactDOM.findDOMNode(this);
        this.timeoutId = setTimeout(function () {
          return $this.focus();
        });
      }
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      clearTimeout(this.timeoutId);
    }
  }, {
    key: "handlePromiseOnOk",
    value: function handlePromiseOnOk(returnValueOfOnOk) {
      var _this2 = this;

      var closeModal = this.props.closeModal;

      if (!returnValueOfOnOk || !returnValueOfOnOk.then) {
        return;
      }

      this.setState({
        loading: true
      });
      returnValueOfOnOk.then(function () {
        // It's unnecessary to set loading=false, for the Modal will be unmounted after close.
        // this.setState({ loading: false });
        closeModal.apply(void 0, arguments);
      }, function (e) {
        // Emit error when catch promise reject
        // eslint-disable-next-line no-console
        console.error(e); // See: https://github.com/ant-design/ant-design/issues/6183

        _this2.setState({
          loading: false
        });

        _this2.clicked = false;
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props2 = this.props,
          type = _this$props2.type,
          children = _this$props2.children,
          buttonProps = _this$props2.buttonProps;
      var loading = this.state.loading;
      return /*#__PURE__*/React.createElement(Button, _extends({}, convertLegacyProps(type), {
        onClick: this.onClick,
        loading: loading
      }, buttonProps), children);
    }
  }]);

  return ActionButton;
}(React.Component);

export { ActionButton as default };