"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _bem = require("@zarm-design/bem");

var _react = _interopRequireWildcard(require("react"));

var _configProvider = require("../config-provider");

var _loading = _interopRequireDefault(require("../loading"));

var Button = /*#__PURE__*/_react.default.forwardRef(function (props, ref) {
  var _ref;

  var className = props.className,
      theme = props.theme,
      size = props.size,
      shape = props.shape,
      icon = props.icon,
      block = props.block,
      ghost = props.ghost,
      shadow = props.shadow,
      disabled = props.disabled,
      loading = props.loading,
      htmlType = props.htmlType,
      onClick = props.onClick,
      children = props.children,
      restProps = (0, _objectWithoutProperties2.default)(props, ["className", "theme", "size", "shape", "icon", "block", "ghost", "shadow", "disabled", "loading", "htmlType", "onClick", "children"]);

  var buttonRef = ref || /*#__PURE__*/_react.default.createRef();

  var iconRender = loading ? /*#__PURE__*/_react.default.createElement(_loading.default, null) : icon;

  var childrenRender = children && /*#__PURE__*/_react.default.createElement("span", null, children);

  var _useContext = (0, _react.useContext)(_configProvider.ConfigContext),
      prefixCls = _useContext.prefixCls;

  var bem = (0, _bem.createBEM)('button', {
    prefixCls: prefixCls
  });
  var cls = bem([(_ref = {}, (0, _defineProperty2.default)(_ref, "".concat(theme), !!theme), (0, _defineProperty2.default)(_ref, "".concat(size), !!size), (0, _defineProperty2.default)(_ref, "".concat(shape), !!shape), (0, _defineProperty2.default)(_ref, "block", block), (0, _defineProperty2.default)(_ref, "ghost", ghost), (0, _defineProperty2.default)(_ref, "shadow", shadow), (0, _defineProperty2.default)(_ref, "disabled", disabled), (0, _defineProperty2.default)(_ref, "loading", loading), (0, _defineProperty2.default)(_ref, "link", restProps.href !== undefined), _ref), className]);
  var contentRender = !!icon || loading ? /*#__PURE__*/_react.default.createElement("div", {
    className: bem('content')
  }, iconRender, childrenRender) : childrenRender;

  var onPress = function onPress(e) {
    if (disabled) {
      return;
    }

    if (typeof onClick === 'function') {
      onClick(e);
    }
  };

  if (restProps.href !== undefined) {
    var _mimeType = restProps.mimeType,
        anchorRest = (0, _objectWithoutProperties2.default)(restProps, ["mimeType"]);
    return /*#__PURE__*/_react.default.createElement("a", (0, _extends2.default)({}, anchorRest, {
      type: _mimeType,
      "aria-disabled": disabled,
      className: cls,
      onClick: onPress,
      ref: buttonRef
    }), contentRender);
  }

  var mimeType = restProps.mimeType,
      target = restProps.target,
      nativeRest = (0, _objectWithoutProperties2.default)(restProps, ["mimeType", "target"]);
  return /*#__PURE__*/_react.default.createElement("button", (0, _extends2.default)({}, nativeRest, {
    type: htmlType,
    "aria-disabled": disabled,
    className: cls,
    onClick: onPress,
    ref: buttonRef
  }), contentRender);
});

Button.displayName = 'Button';
Button.defaultProps = {
  theme: 'default',
  size: 'md',
  shape: 'radius',
  block: false,
  ghost: false,
  shadow: false,
  disabled: false,
  loading: false,
  htmlType: 'button'
};
var _default = Button;
exports.default = _default;