function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

import React, { Component, Children } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import Icon from '../../icon';

function mapIconSize(size) {
  return {
    large: 'large',
    medium: 'medium',
    small: 'medium',
    xs: 'medium'
  }[size];
}
/** Button */


var Button =
/*#__PURE__*/
function (_Component) {
  _inherits(Button, _Component);

  function Button() {
    var _getPrototypeOf2;

    var _this;

    _classCallCheck(this, Button);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(Button)).call.apply(_getPrototypeOf2, [this].concat(args)));

    _defineProperty(_assertThisInitialized(_this), "onMouseUp", function (e) {
      _this.button.blur();

      if (_this.props.onMouseUp) {
        _this.props.onMouseUp(e);
      }
    });

    _defineProperty(_assertThisInitialized(_this), "buttonRefHandler", function (button) {
      _this.button = button;
    });

    return _this;
  }

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

      var _this$props = this.props,
          prefix = _this$props.prefix,
          className = _this$props.className,
          type = _this$props.type,
          size = _this$props.size,
          htmlType = _this$props.htmlType,
          loading = _this$props.loading,
          text = _this$props.text,
          warning = _this$props.warning,
          ghost = _this$props.ghost,
          component = _this$props.component,
          iconSize = _this$props.iconSize,
          children = _this$props.children,
          rtl = _this$props.rtl,
          others = _objectWithoutProperties(_this$props, ["prefix", "className", "type", "size", "htmlType", "loading", "text", "warning", "ghost", "component", "iconSize", "children", "rtl"]);

      var ghostType = ['light', 'dark'].indexOf(ghost) >= 0 ? ghost : 'dark';
      var btnCls = classNames((_classNames = {}, _defineProperty(_classNames, "".concat(prefix, "btn"), true), _defineProperty(_classNames, "".concat(prefix).concat(size), size), _defineProperty(_classNames, "".concat(prefix, "btn-").concat(type), type && !ghost), _defineProperty(_classNames, "".concat(prefix, "btn-text"), text), _defineProperty(_classNames, "".concat(prefix, "btn-warning"), warning), _defineProperty(_classNames, "".concat(prefix, "btn-loading"), loading), _defineProperty(_classNames, "".concat(prefix, "btn-ghost"), ghost), _defineProperty(_classNames, "".concat(prefix, "btn-").concat(ghostType), ghost), _defineProperty(_classNames, className, className), _classNames));
      var count = Children.count(children);
      var clonedChildren = Children.map(children, function (child, index) {
        if (child && typeof child.type === 'function' && child.type._typeMark === 'icon') {
          var _classNames2;

          var iconCls = classNames((_classNames2 = {}, _defineProperty(_classNames2, "".concat(prefix, "btn-icon"), !iconSize), _defineProperty(_classNames2, "".concat(prefix, "icon-first"), count > 1 && index === 0), _defineProperty(_classNames2, "".concat(prefix, "icon-last"), count > 1 && index === count - 1), _defineProperty(_classNames2, "".concat(prefix, "icon-alone"), count === 1), _defineProperty(_classNames2, child.props.className, !!child.props.className), _classNames2));
          return React.cloneElement(child, {
            className: iconCls,
            size: iconSize || mapIconSize(size)
          });
        } // 两个字的中文中间添加间距
        // if (count === 1 && typeof child === 'string' && /^[\u4e00-\u9fa5]{2}$/.test(child)) {
        //   return <>{child[0]}<span className={`${prefix}btn-space`} />{child[1]}</>;
        // }


        return child;
      });
      var TagName = component;

      var tagAttrs = _objectSpread({}, others, {
        type: htmlType,
        className: btnCls
      });

      if (TagName !== 'button') {
        delete tagAttrs.type;

        if (tagAttrs.disabled) {
          delete tagAttrs.onClick; // a 标签的 onClick 浏览器默认不会禁用

          tagAttrs.href && delete tagAttrs.href; // a 标签在禁用状态下无跳转
        }
      }

      return React.createElement(TagName, _extends({}, tagAttrs, {
        dir: rtl ? 'rtl' : undefined,
        onMouseUp: this.onMouseUp,
        ref: this.buttonRefHandler
      }), React.createElement("span", {
        className: "".concat(prefix, "btn-children")
      }, clonedChildren), loading ? React.createElement("span", {
        className: "".concat(prefix, "btn-loading-container")
      }, React.createElement(Icon, {
        type: "loading",
        size: iconSize || mapIconSize(size)
      })) : null);
    }
  }]);

  return Button;
}(Component);

_defineProperty(Button, "propTypes", {
  prefix: PropTypes.string,
  rtl: PropTypes.bool,

  /**
   * 按钮的类型
   */
  type: PropTypes.oneOf(['primary', 'secondary', 'normal']),

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

  /**
   * 按钮中 Icon 的尺寸，用于替代 Icon 的默认大小
   */
  iconSize: PropTypes.oneOf(['xs', 'small', 'medium', 'large', 'xl', 'xxl', 'xxxl']),

  /**
   * 当 component = 'button' 时，设置 button 标签的 type 值
   */
  htmlType: PropTypes.oneOf(['submit', 'reset', 'button']),

  /**
   * 设置标签类型
   */
  component: PropTypes.oneOf(['button', 'a', 'div', 'span']),

  /**
   * 设置按钮的载入状态
   */
  loading: PropTypes.bool,

  /**
   * 是否为幽灵按钮
   */
  ghost: PropTypes.oneOf([true, false, 'light', 'dark']),

  /**
   * 是否为文本按钮
   */
  text: PropTypes.bool,

  /**
   * 是否为警告按钮
   */
  warning: PropTypes.bool,

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

  /**
   * 点击按钮的回调
   * @param {Object} e Event Object
   */
  onClick: PropTypes.func,
  className: PropTypes.string,
  onMouseUp: PropTypes.func,
  children: PropTypes.node
});

_defineProperty(Button, "defaultProps", {
  prefix: 'next-',
  type: 'normal',
  size: 'medium',
  htmlType: 'button',
  component: 'button',
  loading: false,
  ghost: false,
  text: false,
  warning: false,
  disabled: false,
  onClick: function onClick() {}
});

export { Button as default };