'use strict';

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

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _reactDom = require('react-dom');

var _reactDom2 = _interopRequireDefault(_reactDom);

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

var _button2 = _interopRequireDefault(_button);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

var ActionButton = function (_React$Component) {
    (0, _inherits3["default"])(ActionButton, _React$Component);

    function ActionButton(props) {
        (0, _classCallCheck3["default"])(this, ActionButton);

        var _this = (0, _possibleConstructorReturn3["default"])(this, _React$Component.call(this, props));

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

            if (actionFn) {
                var ret = void 0;
                if (actionFn.length) {
                    ret = actionFn(closeModal);
                } else {
                    ret = actionFn();
                    if (!ret) {
                        closeModal();
                    }
                }
                if (ret && ret.then) {
                    _this.setState({ loading: true });
                    ret.then(function () {
                        // It's unnecessary to set loading=false, for the Modal will be unmounted after close.
                        // this.setState({ loading: false });
                        closeModal.apply(undefined, arguments);
                    });
                }
            } else {
                closeModal();
            }
        };
        _this.state = {
            loading: false
        };
        return _this;
    }

    ActionButton.prototype.componentDidMount = function componentDidMount() {
        if (this.props.autoFocus) {
            var $this = _reactDom2["default"].findDOMNode(this);
            this.timeoutId = setTimeout(function () {
                return $this.focus();
            });
        }
    };

    ActionButton.prototype.componentWillUnmount = function componentWillUnmount() {
        clearTimeout(this.timeoutId);
    };

    ActionButton.prototype.render = function render() {
        var _props = this.props,
            type = _props.type,
            children = _props.children;

        var loading = this.state.loading;
        return _react2["default"].createElement(
            _button2["default"],
            { type: type, size: 'large', onClick: this.onClick, loading: loading },
            children
        );
    };

    return ActionButton;
}(_react2["default"].Component);

exports["default"] = ActionButton;
module.exports = exports['default'];