"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));

var _createSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/createSuper"));

var React = _interopRequireWildcard(require("react"));

var ReactDOM = _interopRequireWildcard(require("react-dom"));

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

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

var ActionButton = /*#__PURE__*/function (_React$Component) {
  (0, _inherits2["default"])(ActionButton, _React$Component);

  var _super = (0, _createSuper2["default"])(ActionButton);

  function ActionButton() {
    var _this;

    (0, _classCallCheck2["default"])(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;
  }

  (0, _createClass2["default"])(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["default"], (0, _extends2["default"])({}, (0, _button2.convertLegacyProps)(type), {
        onClick: this.onClick,
        loading: loading
      }, buttonProps), children);
    }
  }]);
  return ActionButton;
}(React.Component);

exports["default"] = ActionButton;