"use strict";

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

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

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

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

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

var _Box = require("../Box");

var _useVariantColorWarning = require("../hooks/useVariantColorWarning");

var _Icon = require("../Icon");

var _Link = require("../Link");

var _PseudoBox = require("../PseudoBox");

var _Spinner = require("../Spinner");

var _styles = _interopRequireDefault(require("./styles"));

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

var __jsx = _react["default"].createElement;

var ButtonIcon = function ButtonIcon(_ref) {
  var icon = _ref.icon,
      props = (0, _objectWithoutProperties2["default"])(_ref, ["icon"]);

  if (typeof icon === 'string') {
    return __jsx(_Icon.Icon, (0, _extends2["default"])({
      focusable: false,
      name: icon,
      color: "currentColor"
    }, props));
  }

  var MdiIcon = icon;
  return __jsx(_Box.Box, props, __jsx(MdiIcon, {
    as: icon,
    "data-custom-icon": true,
    focusable: false,
    color: "currentColor"
  }));
};

var Button = /*#__PURE__*/(0, _react.forwardRef)(function (_ref2, ref) {
  var isDisabled = _ref2.isDisabled,
      isLoading = _ref2.isLoading,
      isFullWidth = _ref2.isFullWidth,
      children = _ref2.children,
      _ref2$as = _ref2.as,
      Comp = _ref2$as === void 0 ? 'button' : _ref2$as,
      _ref2$variantColor = _ref2.variantColor,
      variantColor = _ref2$variantColor === void 0 ? 'button' : _ref2$variantColor,
      href = _ref2.href,
      leftIcon = _ref2.leftIcon,
      rightIcon = _ref2.rightIcon,
      _ref2$variant = _ref2.variant,
      variant = _ref2$variant === void 0 ? 'primary' : _ref2$variant,
      loadingText = _ref2.loadingText,
      _ref2$type = _ref2.type,
      type = _ref2$type === void 0 ? 'button' : _ref2$type,
      _ref2$size = _ref2.size,
      size = _ref2$size === void 0 ? 'md' : _ref2$size,
      iconOnly = _ref2.iconOnly,
      rest = (0, _objectWithoutProperties2["default"])(_ref2, ["isDisabled", "isLoading", "isFullWidth", "children", "as", "variantColor", "href", "leftIcon", "rightIcon", "variant", "loadingText", "type", "size", "iconOnly"]);
  // Wrong usage of `variantColor` prop is quite common
  // Let's add a warning hook that validates the passed variantColor
  (0, _useVariantColorWarning.useVariantColorWarning)('Button', variantColor);

  var _isDisabled = isDisabled || isLoading;

  var buttonStyleProps = (0, _styles["default"])({
    color: variantColor,
    variant: variant,
    size: size,
    isDisabled: _isDisabled,
    isFullWidth: isFullWidth,
    iconOnly: iconOnly
  });
  var asButton = href ? _Link.Link : Comp;
  return __jsx(_PseudoBox.PseudoBox, (0, _extends2["default"])({
    disabled: _isDisabled,
    "aria-disabled": _isDisabled,
    ref: ref,
    href: href,
    as: asButton,
    type: type
  }, buttonStyleProps, rest), leftIcon && !isLoading && __jsx(ButtonIcon, {
    mr: !iconOnly && 'spacing-sm',
    icon: leftIcon
  }), isLoading && __jsx(_Spinner.Spinner, {
    position: loadingText ? 'relative' : 'absolute',
    mr: loadingText ? 'spacing-xs' : 0,
    color: "currentColor",
    size: "sm"
  }), isLoading ? loadingText || __jsx(_Box.Box, {
    as: "span",
    opacity: "0"
  }, children) : children, rightIcon && !isLoading && __jsx(ButtonIcon, {
    ml: !iconOnly && 'spacing-sm',
    icon: rightIcon
  }));
});
exports.Button = Button;
//# sourceMappingURL=Button.js.map