import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import React, { useRef } from 'react';
import classNames from 'classnames';
import Ripple from '../utils/Ripple';
import { ConfigContext } from '../config-provider/context';
import Icon from '../icon';
var LoadingLine = Icon.LoadingLine;
var Button = /*#__PURE__*/React.forwardRef(function (props, ref) {
  var _classNames;

  var _React$useContext = React.useContext(ConfigContext),
      getPrefixClassName = _React$useContext.getPrefixClassName,
      size = _React$useContext.size;

  var btnRef = ref || useRef(null);

  var className = props.className,
      type = props.type,
      children = props.children,
      customSize = props.size,
      htmlType = props.htmlType,
      href = props.href,
      loading = props.loading,
      onClick = props.onClick,
      danger = props.danger,
      warning = props.warning,
      success = props.success,
      ghost = props.ghost,
      icon = props.icon,
      reset = _objectWithoutProperties(props, ["className", "type", "children", "size", "htmlType", "href", "loading", "onClick", "danger", "warning", "success", "ghost", "icon"]);

  var btnCls = getPrefixClassName('btn');
  var endSize = customSize || size;
  var endCls = classNames(btnCls, (_classNames = {}, _defineProperty(_classNames, "".concat(btnCls, "-").concat(type), type), _defineProperty(_classNames, "".concat(btnCls, "-a"), href), _defineProperty(_classNames, "".concat(btnCls, "-").concat(endSize), endSize), _defineProperty(_classNames, "".concat(btnCls, "-danger"), danger), _defineProperty(_classNames, "".concat(btnCls, "-success"), success), _defineProperty(_classNames, "".concat(btnCls, "-warning"), warning), _defineProperty(_classNames, "".concat(btnCls, "-ghost"), ghost), _defineProperty(_classNames, "".concat(btnCls, "-loading"), loading), _classNames), className);

  var handleClick = function handleClick(e) {
    if (type === 'link' && !href) {
      e.preventDefault();
    }

    if (props.disabled || loading) return;
    onClick === null || onClick === void 0 ? void 0 : onClick(e);
  };

  var renderDom = function renderDom() {
    var btnNode;

    var comProps = _objectSpread({
      ref: btnRef,
      className: endCls,
      onClick: handleClick
    }, reset);

    var iconNode = null;
    var showIcon = false;

    if (loading || icon) {
      iconNode = icon || /*#__PURE__*/React.createElement(LoadingLine, null);
      showIcon = true;
    }

    btnNode =
    /*#__PURE__*/

    /* eslint-disable react/button-has-type */
    React.createElement(Ripple, null, /*#__PURE__*/React.createElement("button", Object.assign({
      type: htmlType
    }, comProps), showIcon ? /*#__PURE__*/React.createElement("span", {
      className: "".concat(btnCls, "-icon")
    }, iconNode) : null, /*#__PURE__*/React.createElement("span", null, children)));

    if (href || type === 'link') {
      btnNode = /*#__PURE__*/React.createElement("a", Object.assign({
        href: href || ''
      }, comProps), iconNode, /*#__PURE__*/React.createElement("span", null, children));
    }

    return /*#__PURE__*/React.createElement(React.Fragment, null, btnNode);
  };

  return /*#__PURE__*/React.createElement(React.Fragment, null, renderDom());
});
Button.displayName = 'Button';
Button.defaultProps = {
  type: 'default',
  htmlType: 'button',
  danger: false,
  ghost: false
};
export default Button;