import _defineProperty from 'babel-runtime/helpers/defineProperty';
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/button/Button.jsx';
/**
 * index.jsx
 *
 * @Author: jruif
 * @Date: 2017/7/1 下午2:14
 */

import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import Icon from '../icon';
import omit from '../util/omit';
import { isString } from '../util/isType';

var rxTwoCNChar = /^[\u4e00-\u9fa5]{2}$/;
var isTwoCNChar = rxTwoCNChar.test.bind(rxTwoCNChar);

// Insert one space between two chinese characters automatically.
function insertSpace(child, needInserted) {
    // Check the child if is undefined or null.
    if (child === null) {
        return;
    }
    var SPACE = needInserted ? ' ' : '';
    // strictNullChecks oops.
    if (!isString(child) && typeof child !== 'number' && isString(child.type) && isTwoCNChar(child.props.children)) {
        return React.cloneElement(child, {}, child.props.children.split('').join(SPACE));
    }
    if (isString(child)) {
        if (isTwoCNChar(child)) {
            child = child.split('').join(SPACE);
        }

        return React.createElement(
            'span',
            {
                __source: {
                    fileName: _jsxFileName,
                    lineNumber: 34
                },
                __self: this
            },
            child
        );
    }
    return child;
}

/**
 * Button
 * */

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

    function Button(props) {
        _classCallCheck(this, Button);

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

        _this.handleClick = function (e) {
            // 添加点击效果
            _this.setState({ clicked: true });
            // 消除多次点击影响
            clearTimeout(_this.timeout);
            _this.timeout = setTimeout(function () {
                return _this.setState({ clicked: false });
            }, 300);

            var onClick = _this.props.onClick;
            if (onClick) {
                onClick(e);
            }
        };

        _this.handleMouseUp = function (e) {
            if (_this.props.onMouseUp) {
                _this.props.onMouseUp(e);
            }
        };

        _this.state = {
            loading: false, // 始终是bool类型
            clicked: false
        };
        return _this;
    }

    _createClass(Button, [{
        key: 'componentDidMount',
        value: function componentDidMount() {
            var _this2 = this;

            var loading = this.props.loading;

            if (typeof loading !== 'boolean' && loading && loading.delay) {
                this.setState({ loading: true });
                this.delayTimeout = setTimeout(function () {
                    return _this2.setState({ loading: false });
                }, loading.delay);
            } else {
                this.setState({ loading: loading });
            }
        }
    }, {
        key: 'componentWillReceiveProps',
        value: function componentWillReceiveProps(nextProps) {
            var _this3 = this;

            var currentLoading = this.props.loading;
            var loading = nextProps.loading;

            if (currentLoading) {
                clearTimeout(this.delayTimeout);
            }

            if (typeof loading !== 'boolean' && loading && loading.delay) {
                this.delayTimeout = setTimeout(function () {
                    return _this3.setState({ loading: !_this3.state.loading });
                }, loading.delay);
            } else {
                this.setState({ loading: loading });
            }
        }
    }, {
        key: 'componentWillUnmount',
        value: function componentWillUnmount() {
            clearTimeout(this.timeout);
            clearTimeout(this.delayTimeout);
        }

        // Handle auto focus when click button in Chrome

    }, {
        key: 'render',
        value: function render() {
            var _classNames;

            var _props = this.props,
                type = _props.type,
                shape = _props.shape,
                size = _props.size,
                className = _props.className,
                htmlType = _props.htmlType,
                children = _props.children,
                icon = _props.icon,
                prefixCls = _props.prefixCls,
                hollow = _props.hollow,
                others = _objectWithoutProperties(_props, ['type', 'shape', 'size', 'className', 'htmlType', 'children', 'icon', 'prefixCls', 'hollow']);

            var _state = this.state,
                loading = _state.loading,
                clicked = _state.clicked;

            // 兼容旧版本

            var sizeCls = void 0;
            switch (size) {
                case 'lg':
                    sizeCls = 'large';
                    break;
                case 'sm':
                    sizeCls = 'small';
                    break;
                case 'md':
                    sizeCls = 'medium';
                    break;
                default:
                    sizeCls = size;
                    break;
            }

            var _hollow = hollow;
            if (shape === 'dashed') {
                _hollow = true;
            }

            var classes = classNames(prefixCls, (_classNames = {}, _defineProperty(_classNames, prefixCls + '-' + (type === 'danger' ? 'error' : type), type), _defineProperty(_classNames, prefixCls + '-' + shape, shape), _defineProperty(_classNames, prefixCls + '-' + sizeCls, sizeCls), _defineProperty(_classNames, prefixCls + '-icon-only', !children && icon), _defineProperty(_classNames, prefixCls + '-loading', loading), _defineProperty(_classNames, prefixCls + '-clicked', clicked), _defineProperty(_classNames, prefixCls + '-hollow', _hollow), _classNames), className);

            var iconType = loading ? 'loading' : icon;
            var iconNode = iconType ? React.createElement(Icon, { type: iconType, className: '', __source: {
                    fileName: _jsxFileName,
                    lineNumber: 192
                },
                __self: this
            }) : null;
            var needInserted = React.Children.count(children) === 1 && !iconType;
            var kids = React.Children.map(children, function (child) {
                return insertSpace(child, needInserted);
            });

            return React.createElement(
                'button',
                Object.assign({}, omit(others, ['loading', 'clicked']), {
                    type: htmlType || 'button',
                    className: classes,
                    onMouseUp: this.handleMouseUp,
                    onClick: this.handleClick, __source: {
                        fileName: _jsxFileName,
                        lineNumber: 197
                    },
                    __self: this
                }),
                iconNode,
                kids
            );
        }
    }]);

    return Button;
}(React.PureComponent);

Button.defaultProps = {
    loading: false,
    hollow: false,
    disabled: false,
    size: 'medium',
    prefixCls: 'ne-btn',
    autoFocus: false
};
Button.propTypes = {
    /**
     * 设置按钮类型
     * */
    type: PropTypes.oneOf(['primary', 'secondary', 'error', 'success', 'warning', 'info', 'danger']),
    /**
     * 图形按钮
     * */
    icon: PropTypes.string,
    prefixCls: PropTypes.string,
    className: PropTypes.string,
    /**
     * 设置按钮形状，可选值为 circle 或者不设
     * circle: 圆形
     * dashed: 虚线
     * none: 无边框
     * */
    shape: PropTypes.oneOf(['circle', 'dashed', 'none']),
    /**
     * 尺寸
     * */
    size: PropTypes.oneOf(['large', 'medium', 'small']),
    /** 设置 button 原生的 type 值 */
    htmlType: PropTypes.oneOf(['submit', 'button', 'reset']),
    onClick: PropTypes.func,
    /**
     * 设置按钮载入状态 boolean | { delay: number }
     * */
    loading: PropTypes.oneOfType([PropTypes.bool, PropTypes.shape({
        delay: PropTypes.number
    })]),
    /** 是否虚线*/
    hollow: PropTypes.bool,
    disabled: PropTypes.bool,
    autoFocus: PropTypes.bool
};


export default Button;