import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import { createBEM } from '@zarm-design/bem';
import React, { useContext } from 'react';
import { ConfigContext } from '../config-provider';
import Loading from '../loading';
var Button = /*#__PURE__*/React.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 = _objectWithoutProperties(props, ["className", "theme", "size", "shape", "icon", "block", "ghost", "shadow", "disabled", "loading", "htmlType", "onClick", "children"]);

  var buttonRef = ref || /*#__PURE__*/React.createRef();
  var iconRender = loading ? /*#__PURE__*/React.createElement(Loading, null) : icon;
  var childrenRender = children && /*#__PURE__*/React.createElement("span", null, children);

  var _useContext = useContext(ConfigContext),
      prefixCls = _useContext.prefixCls;

  var bem = createBEM('button', {
    prefixCls: prefixCls
  });
  var cls = bem([(_ref = {}, _defineProperty(_ref, "".concat(theme), !!theme), _defineProperty(_ref, "".concat(size), !!size), _defineProperty(_ref, "".concat(shape), !!shape), _defineProperty(_ref, "block", block), _defineProperty(_ref, "ghost", ghost), _defineProperty(_ref, "shadow", shadow), _defineProperty(_ref, "disabled", disabled), _defineProperty(_ref, "loading", loading), _defineProperty(_ref, "link", restProps.href !== undefined), _ref), className]);
  var contentRender = !!icon || loading ? /*#__PURE__*/React.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 = _objectWithoutProperties(restProps, ["mimeType"]);

    return /*#__PURE__*/React.createElement("a", _extends({}, anchorRest, {
      type: _mimeType,
      "aria-disabled": disabled,
      className: cls,
      onClick: onPress,
      ref: buttonRef
    }), contentRender);
  }

  var mimeType = restProps.mimeType,
      target = restProps.target,
      nativeRest = _objectWithoutProperties(restProps, ["mimeType", "target"]);

  return /*#__PURE__*/React.createElement("button", _extends({}, 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'
};
export default Button;