import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf";
import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";

/**
 * index.jsx
 *
 * @Author: jruif
 * @Date: 2017/7/1 下午2:14
 */
import React, { isValidElement, cloneElement } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import Rolling from '../icons/internals/Rolling';
import chainPropTypes from './chainPropTypes';
var colors = ['primary', 'secondary', 'success', 'warning', 'info', 'error'];
/**
 * Button
 * */

var Button =
/*#__PURE__*/
function (_React$PureComponent) {
  _inherits(Button, _React$PureComponent);

  function Button(props) {
    var _this;

    _classCallCheck(this, Button);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(Button).call(this, props));

    _defineProperty(_assertThisInitialized(_this), "handleClick", function (e) {
      var _this$props = _this.props,
          disabled = _this$props.disabled,
          onClick = _this$props.onClick;

      if (!disabled) {
        _this.setState({
          clicked: true
        });

        clearTimeout(_this.timeout);
        _this.timeout = setTimeout(function () {
          return _this.setState({
            clicked: false
          });
        }, 300);

        if (onClick) {
          onClick(e);
        }
      }
    });

    _defineProperty(_assertThisInitialized(_this), "handleMouseUp", function (e) {
      var onMouseUp = _this.props.onMouseUp;

      if (onMouseUp) {
        onMouseUp(e);
      }
    });

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

  _createClass(Button, [{
    key: "render",
    value: function render() {
      var _classNames;

      var _this$props2 = this.props,
          type = _this$props2.type,
          variant = _this$props2.variant,
          shape = _this$props2.shape,
          size = _this$props2.size,
          className = _this$props2.className,
          htmlType = _this$props2.htmlType,
          children = _this$props2.children,
          icon = _this$props2.icon,
          prefixCls = _this$props2.prefixCls,
          hollow = _this$props2.hollow,
          loading = _this$props2.loading,
          prefix = _this$props2.prefix,
          color = _this$props2.color,
          active = _this$props2.active,
          component = _this$props2.component,
          disabled = _this$props2.disabled,
          tabIndex = _this$props2.tabIndex,
          other = _objectWithoutProperties(_this$props2, ["type", "variant", "shape", "size", "className", "htmlType", "children", "icon", "prefixCls", "hollow", "loading", "prefix", "color", "active", "component", "disabled", "tabIndex"]);

      var clicked = this.state.clicked;
      var buttonVariant = type === 'primary' ? 'contained' : hollow ? 'hollow' : variant; // 兼容

      var buttonColor = colors.includes(type) ? type : color; // 兼容

      var hasIcon = loading ? 'loading' : icon || prefix;
      var classes = classNames(prefixCls, "".concat(prefixCls, "-color-").concat(buttonColor), "".concat(prefixCls, "-shape-").concat(shape === 'none' ? 'default' : shape), "".concat(prefixCls, "-").concat(buttonVariant), (_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-").concat(size), size), _defineProperty(_classNames, "".concat(prefixCls, "-loading"), loading), _defineProperty(_classNames, "".concat(prefixCls, "-clicked"), clicked), _defineProperty(_classNames, "".concat(prefixCls, "-active"), active), _defineProperty(_classNames, "".concat(prefixCls, "-disabled"), disabled), _classNames), className);
      var ComponentProp = component;

      if (ComponentProp === 'button' && other.href) {
        ComponentProp = 'a';
      }

      var buttonProps = {};

      if (ComponentProp === 'button') {
        buttonProps.type = htmlType || type || 'button';
        buttonProps.disabled = disabled || loading;
      } else {
        buttonProps.role = 'button';
      }

      var prefixIcon = prefix && isValidElement(prefix) ? cloneElement(prefix, {
        className: classNames(prefix.props.className, "".concat(prefixCls, "-prefix"))
      }) : null;
      return React.createElement(ComponentProp, _extends({}, buttonProps, other, {
        className: classes,
        onMouseUp: this.handleMouseUp,
        tabIndex: disabled ? '-1' : tabIndex,
        onClick: this.handleClick
      }), loading ? React.createElement(Rolling, {
        className: "".concat(prefixCls, "-loading-icon ").concat(prefixCls, "-prefix")
      }) : prefixIcon, children);
    }
  }]);

  return Button;
}(React.PureComponent);

_defineProperty(Button, "defaultProps", {
  loading: false,
  disabled: false,
  variant: 'flat',
  size: 'medium',
  prefixCls: 'ne-button',
  type: 'button',
  color: 'secondary',
  shape: 'default',
  checked: false,
  component: 'button',
  tabIndex: '0',
  children: chainPropTypes(PropTypes.node, function (props) {
    var found = React.Children.toArray(props.children).some(function (child) {
      return React.isValidElement(child) && child.props.onClick;
    });

    if (found) {
      return new Error(['NeatUI: 你正试图给button的子元素添加点击事件回调。', 'Firefox 将不会触发该事件', 'https://github.com/mui-org/material-ui/issues/13957'].join('\n'));
    }

    return null;
  })
});

_defineProperty(Button, "propTypes", {
  /** @ignore */
  children: PropTypes.node,

  /**
   * 设置按钮类型
   * */
  variant: PropTypes.oneOf(['contained', 'flat', 'text', 'outlined', 'hollow']),

  /**
   * 设置按钮颜色
   * */
  color: PropTypes.oneOf(colors),

  /**
   * 图形按钮
   * */
  prefix: PropTypes.node,

  /**
   * 类前缀
   * */
  prefixCls: PropTypes.string,
  className: PropTypes.string,

  /**
   * 设置按钮形状，可选值为 circle 或者不设
   * circle: 圆形
   * dashed: 虚线
   * borderless: 虚线
   * default: 默认
   * */
  shape: PropTypes.oneOf(['circle', 'dash', 'default', 'none', 'borderless']),

  /**
   * 尺寸
   * */
  size: PropTypes.oneOf(['large', 'medium', 'small']),

  /**
   * 按钮类型
   * */
  type: PropTypes.oneOf(['submit', 'button', 'reset']),

  /**
   * 加载中
   * */
  loading: PropTypes.bool,

  /**
   * 是否禁用
   * */
  disabled: PropTypes.bool,

  /**
   * @ignore
   * */
  active: PropTypes.bool,

  /**
   * 节点元素
   * */
  component: PropTypes.oneOfType([PropTypes.string, PropTypes.element])
});

export default Button;