"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _react = require("react");

var _react2 = _interopRequireDefault(_react);

var _core = require("@emotion/core");

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _gatsby = require("gatsby");

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

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

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

function textIntoSpan(text) {
  return _react2["default"].createElement("span", null, text);
}

function Content(_ref) {
  var children = _ref.children,
      label = _ref.label,
      DefaultIcon = _ref.DefaultIcon;

  if (label) {
    return _react2["default"].createElement(_react.Fragment, null, _react2["default"].createElement("span", null, label), " ", DefaultIcon && _react2["default"].createElement(DefaultIcon, null));
  }

  if (children && children.type && children.type === _react2["default"].Fragment) {
    return _react2["default"].createElement(_react.Fragment, null, _react2["default"].Children.map(children.props.children, function (child) {
      return typeof child === "string" ? textIntoSpan(child) : child;
    }));
  }

  if (children && children.length > 1) {
    return _react2["default"].Children.map(children, function (child) {
      return typeof child === "string" ? textIntoSpan(child) : child;
    });
  }

  return children;
}

function BaseButton(props) {
  var children = props.children,
      _props$disabled = props.disabled,
      disabled = _props$disabled === void 0 ? false : _props$disabled,
      href = props.href,
      label = props.label,
      DefaultIcon = props.DefaultIcon,
      _props$loading = props.loading,
      loading = _props$loading === void 0 ? false : _props$loading,
      _props$loadingLabel = props.loadingLabel,
      loadingLabel = _props$loadingLabel === void 0 ? "Loading" : _props$loadingLabel,
      LoadingIcon = props.LoadingIcon,
      rel = props.rel,
      _props$role = props.role,
      role = _props$role === void 0 ? "button" : _props$role,
      _props$target = props.target,
      target = _props$target === void 0 ? "_blank" : _props$target,
      to = props.to,
      _props$type = props.type,
      type = _props$type === void 0 ? "button" : _props$type,
      rest = _objectWithoutProperties(props, ["children", "disabled", "href", "label", "DefaultIcon", "loading", "loadingLabel", "LoadingIcon", "rel", "role", "target", "to", "type"]);

  if (href) {
    return _react2["default"].createElement("a", Object.assign({
      href: href,
      target: target,
      role: role,
      rel: (0, _helpers.secureTargetBlankLink)({
        rel: rel,
        target: target
      })
    }, rest), _react2["default"].createElement(Content, {
      children: children,
      label: label,
      DefaultIcon: DefaultIcon
    }));
  }

  if (to) {
    return _react2["default"].createElement(_gatsby.Link, Object.assign({
      to: to,
      role: role
    }, rest), _react2["default"].createElement(Content, {
      children: children,
      label: label,
      DefaultIcon: DefaultIcon
    }));
  }

  return _react2["default"].createElement("button", Object.assign({
    disabled: loading ? true : disabled,
    type: type
  }, rest), loading ? _react2["default"].createElement(_react.Fragment, null, loadingLabel && _react2["default"].createElement("span", null, loadingLabel), " ", LoadingIcon && _react2["default"].createElement(LoadingIcon, null)) : _react2["default"].createElement(Content, {
    children: children,
    label: label,
    DefaultIcon: DefaultIcon
  }));
}

BaseButton.propTypes = {
  children: _propTypes2["default"].node,
  disabled: _propTypes2["default"].bool,
  href: _propTypes2["default"].string,
  loading: _propTypes2["default"].bool,
  loadingLabel: _propTypes2["default"].string,
  onClick: _propTypes2["default"].func,
  rel: _propTypes2["default"].string,
  target: _propTypes2["default"].string,
  to: _propTypes2["default"].string,
  type: _propTypes2["default"].oneOf(["button", "reset", "submit"])
};
exports["default"] = BaseButton;