import _objectWithoutProperties from 'babel-runtime/helpers/objectWithoutProperties';
import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
import _createClass from 'babel-runtime/helpers/createClass';
import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
import _inherits from 'babel-runtime/helpers/inherits';
var _jsxFileName = 'src/modal/ActionButton.jsx';
/**
 * ActionButton.jsx
 *
 * @Author: jruif
 * @Date: 2017/7/11 下午11:35
 */

import React from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
import Button from '../button';
import { isBoolean } from '../util/isType';

/**
 * @ignore
 * */

var ActionButton = function (_React$PureComponent) {
    _inherits(ActionButton, _React$PureComponent);

    function ActionButton(props, context) {
        _classCallCheck(this, ActionButton);

        var _this = _possibleConstructorReturn(this, (ActionButton.__proto__ || Object.getPrototypeOf(ActionButton)).call(this, props, context));

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

            if (onClick) {
                var ret = onClick();
                // 无返回值 || 是bool类型，且为false则不关闭弹窗
                if (ret === void 0 || isBoolean(ret) && ret) {
                    return closeModal();
                } else if (ret.then) {
                    // 可使用Promise
                    _this.setState({ loading: true });
                    ret.then(function () {
                        closeModal.apply(undefined, arguments);
                    }, function () {
                        _this.setState({ loading: false });
                    });
                }
                // 其他类型返回值不处理
            } else {
                closeModal();
            }
        };

        _this.state = {
            loading: false
        };
        return _this;
    }

    _createClass(ActionButton, [{
        key: 'componentDidMount',
        value: function componentDidMount() {
            if (this.props.autoFocus) {
                var $this = ReactDOM.findDOMNode(this);
                if ($this) {
                    this.timeoutId = setTimeout(function () {
                        return $this.focus();
                    });
                }
            }
        }
    }, {
        key: 'componentWillUnmount',
        value: function componentWillUnmount() {
            clearTimeout(this.timeoutId);
        }
    }, {
        key: 'render',
        value: function render() {
            var _props = this.props,
                onClick = _props.onClick,
                closeModal = _props.closeModal,
                children = _props.children,
                other = _objectWithoutProperties(_props, ['onClick', 'closeModal', 'children']);

            return React.createElement(
                Button,
                Object.assign({ onClick: this.onClick, loading: this.state.loading, disabled: this.state.loading }, other, {
                    __source: {
                        fileName: _jsxFileName,
                        lineNumber: 76
                    },
                    __self: this
                }),
                children
            );
        }
    }]);

    return ActionButton;
}(React.PureComponent);

ActionButton.propTypes = {
    onClick: PropTypes.func,
    closeModal: PropTypes.func,
    autoFocus: PropTypes.bool
};
ActionButton.defaultProps = {
    autoFocus: true
};


export default ActionButton;