"use strict";

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

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

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

var _reactRouterDom = require("react-router-dom");

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

var _withStyles = _interopRequireDefault(require("../../hoc/withStyles"));

var _ErrorBoundary = _interopRequireDefault(require("../../hoc/ErrorBoundary"));

var _ButtonBase = _interopRequireDefault(require("@material-ui/core/ButtonBase"));

var _helpers = require("../../helpers");

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

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

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

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var Button =
/*#__PURE__*/
function (_React$Component) {
  _inherits(Button, _React$Component);

  function Button() {
    var _getPrototypeOf2;

    var _this;

    _classCallCheck(this, Button);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(Button)).call.apply(_getPrototypeOf2, [this].concat(args)));

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "_renderAfterIcon", function () {
      var _this$props = _this.props,
          iconAfter = _this$props.iconAfter,
          classes = _this$props.classes,
          loading = _this$props.loading,
          loadingIcon = _this$props.loadingIcon;
      var AfterIconComponent = null;

      if (loading) {
        if (loadingIcon) {
          AfterIconComponent = loadingIcon;
        } else {
          AfterIconComponent = React.createElement("i", {
            className: "fa fa-pulse fa-spinner"
          });
        }
      } else if (iconAfter) {
        AfterIconComponent = iconAfter;
      } else {
        return null;
      }

      return React.createElement("div", {
        className: (0, _classnames.default)(classes.icon, classes.afterIcon)
      }, AfterIconComponent);
    });

    return _this;
  }

  _createClass(Button, [{
    key: "_renderChildren",
    value: function _renderChildren() {
      var _this$props2 = this.props,
          iconBefore = _this$props2.iconBefore,
          title = _this$props2.title,
          classes = _this$props2.classes;
      return React.createElement(React.Fragment, null, iconBefore && React.createElement("div", {
        className: (0, _classnames.default)(classes.icon, classes.beforeIcon)
      }, iconBefore), title && React.createElement("div", {
        className: (0, _classnames.default)(classes.text)
      }, title), this._renderAfterIcon());
    }
  }, {
    key: "_renderContainer",
    value: function _renderContainer() {
      var _this2 = this;

      var _this$props3 = this.props,
          id = _this$props3.id,
          onClick = _this$props3.onClick,
          isDisabled = _this$props3.isDisabled,
          type = _this$props3.type,
          href = _this$props3.href,
          classes = _this$props3.classes,
          target = _this$props3.target,
          loading = _this$props3.loading;
      isDisabled = isDisabled || loading;
      var buttonClass = (0, _classnames.default)(classes.component, {
        disabled: isDisabled
      });

      if (href) {
        var isExternal = (0, _helpers.isExternalUrl)(href);

        if (isExternal) {
          // render <a> tag
          return React.createElement(_ButtonBase.default, {
            component: 'a',
            classes: {
              root: buttonClass
            },
            target: target ? target : '_blank',
            id: id,
            href: href
          }, this._renderChildren());
        }

        return React.createElement(_ButtonBase.default, {
          component: function component() {
            return React.createElement(_reactRouterDom.Link, {
              to: href,
              className: buttonClass,
              target: target,
              id: id
            }, _this2._renderChildren());
          }
        });
      }

      return React.createElement(_ButtonBase.default, {
        component: "button",
        classes: {
          root: buttonClass
        },
        id: id,
        type: type,
        onClick: onClick
      }, this._renderChildren());
    }
  }, {
    key: "render",
    value: function render() {
      return React.createElement(_ErrorBoundary.default, null, this._renderContainer());
    }
  }]);

  return Button;
}(React.Component);

_defineProperty(Button, "propTypes", {
  isDisabled: _propTypes.default.bool,
  title: _propTypes.default.node,
  type: _propTypes.default.string,
  iconBefore: _propTypes.default.node,
  iconAfter: _propTypes.default.node,
  onClick: _propTypes.default.func,
  id: _propTypes.default.string,
  height: _propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.number]),
  width: _propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.number]),
  maxWidth: _propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.number]),
  buttonColor: _propTypes.default.string,
  href: _propTypes.default.string,
  applyBorderRadius: _propTypes.default.bool,
  compactMode: _propTypes.default.bool,
  withoutBackground: _propTypes.default.bool,
  target: _propTypes.default.string,
  loading: _propTypes.default.bool,
  loadingIcon: _propTypes.default.node,
  classes: _propTypes.default.object // will come from withStyles HOC

});

_defineProperty(Button, "defaultProps", {
  onClick: function onClick() {},
  isDisabled: false,
  iconBefore: null,
  iconAfter: null,
  title: null,
  id: undefined,
  target: '_self',
  applyBorderRadius: false,
  compactMode: false,
  loading: false,
  loadingIcon: null,
  withoutBackground: false
});

var _default = (0, _withStyles.default)(_styles.default)(Button);

exports.default = _default;