"use strict";

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

exports.__esModule = true;
exports.default = void 0;

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

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

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

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

var _propTypes = _interopRequireDefault(require("prop-types"));

var _SafeAnchor = _interopRequireDefault(require("../SafeAnchor"));

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

var _utils = require("../utils");

function _templateObject() {
  var data = (0, _taggedTemplateLiteralLoose2.default)(["spin"]);

  _templateObject = function _templateObject() {
    return data;
  };

  return data;
}

var defaultProps = {
  appearance: 'default',
  classPrefix: 'btn',
  ripple: true
};

var Button = /*#__PURE__*/_react.default.forwardRef(function (props, ref) {
  var as = props.as,
      active = props.active,
      appearance = props.appearance,
      block = props.block,
      className = props.className,
      children = props.children,
      classPrefix = props.classPrefix,
      color = props.color,
      disabled = props.disabled,
      loading = props.loading,
      ripple = props.ripple,
      size = props.size,
      typeProp = props.type,
      rest = (0, _objectWithoutPropertiesLoose2.default)(props, ["as", "active", "appearance", "block", "className", "children", "classPrefix", "color", "disabled", "loading", "ripple", "size", "type"]);

  var _useClassNames = (0, _utils.useClassNames)(classPrefix),
      withClassPrefix = _useClassNames.withClassPrefix,
      prefix = _useClassNames.prefix,
      merge = _useClassNames.merge;

  var classes = merge(className, withClassPrefix(appearance, color, size, {
    active: active,
    disabled: disabled,
    loading: loading,
    block: block
  }));
  var rippleElement = ripple && !(0, _utils.isOneOf)(appearance, ['link', 'ghost']) ? /*#__PURE__*/_react.default.createElement(_Ripple.default, null) : null;

  var spin = /*#__PURE__*/_react.default.createElement("span", {
    className: prefix(_templateObject())
  });

  if (rest.href) {
    return /*#__PURE__*/_react.default.createElement(_SafeAnchor.default, (0, _extends2.default)({}, rest, {
      as: as,
      ref: ref,
      "aria-disabled": disabled,
      disabled: disabled,
      className: classes
    }), loading && spin, children, rippleElement);
  }

  var Component = as || 'button';
  var type = typeProp || (Component === 'button' ? 'button' : undefined);
  var role = rest.role || (Component !== 'button' ? 'button' : undefined);
  return /*#__PURE__*/_react.default.createElement(Component, (0, _extends2.default)({}, rest, {
    role: role,
    type: type,
    ref: ref,
    disabled: disabled,
    "aria-disabled": disabled,
    className: classes
  }), loading && spin, children, rippleElement);
});

Button.displayName = 'Button';
Button.defaultProps = defaultProps;
Button.propTypes = {
  as: _propTypes.default.elementType,
  active: _propTypes.default.bool,
  appearance: _propTypes.default.oneOf(['default', 'primary', 'link', 'subtle', 'ghost']),
  block: _propTypes.default.bool,
  children: _propTypes.default.node,
  color: _propTypes.default.oneOf(['red', 'orange', 'yellow', 'green', 'cyan', 'blue', 'violet']),
  disabled: _propTypes.default.bool,
  href: _propTypes.default.string,
  loading: _propTypes.default.bool,
  ripple: _propTypes.default.bool,
  size: _propTypes.default.oneOf(['lg', 'md', 'sm', 'xs']),
  type: _propTypes.default.oneOf(['button', 'reset', 'submit'])
};
var _default = Button;
exports.default = _default;