"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 _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

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

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

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

var _classnames = _interopRequireDefault(require("classnames"));

var _Ripple = _interopRequireDefault(require("../utils/Ripple"));

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

var _icon = _interopRequireDefault(require("../icon"));

var LoadingLine = _icon.default.LoadingLine;

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

  var _React$useContext = _react.default.useContext(_context.ConfigContext),
      getPrefixClassName = _React$useContext.getPrefixClassName,
      size = _React$useContext.size;

  var btnRef = ref || (0, _react.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 = (0, _objectWithoutProperties2.default)(props, ["className", "type", "children", "size", "htmlType", "href", "loading", "onClick", "danger", "warning", "success", "ghost", "icon"]);
  var btnCls = getPrefixClassName('btn');
  var endSize = customSize || size;
  var endCls = (0, _classnames.default)(btnCls, (_classNames = {}, (0, _defineProperty2.default)(_classNames, "".concat(btnCls, "-").concat(type), type), (0, _defineProperty2.default)(_classNames, "".concat(btnCls, "-a"), href), (0, _defineProperty2.default)(_classNames, "".concat(btnCls, "-").concat(endSize), endSize), (0, _defineProperty2.default)(_classNames, "".concat(btnCls, "-danger"), danger), (0, _defineProperty2.default)(_classNames, "".concat(btnCls, "-success"), success), (0, _defineProperty2.default)(_classNames, "".concat(btnCls, "-warning"), warning), (0, _defineProperty2.default)(_classNames, "".concat(btnCls, "-ghost"), ghost), (0, _defineProperty2.default)(_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 = (0, _objectSpread2.default)({
      ref: btnRef,
      className: endCls,
      onClick: handleClick
    }, reset);
    var iconNode = null;
    var showIcon = false;

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

    btnNode =
    /*#__PURE__*/

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

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

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

  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, renderDom());
});

Button.displayName = 'Button';
Button.defaultProps = {
  type: 'default',
  htmlType: 'button',
  danger: false,
  ghost: false
};
var _default = Button;
exports.default = _default;