'use strict';

exports.__esModule = true;

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

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

var _ref = _react2.default.createElement(
  'h2',
  null,
  'Something went wrong.'
);

var _ref2 = _react2.default.createElement('br', null);

var ErrorBoundary = function (_React$Component) {
  (0, _inherits3.default)(ErrorBoundary, _React$Component);

  function ErrorBoundary() {
    var _temp, _this, _ret;

    (0, _classCallCheck3.default)(this, ErrorBoundary);

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

    return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, _React$Component.call.apply(_React$Component, [this].concat(args))), _this), _this.state = {
      error: null,
      errorInfo: null
    }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  }

  ErrorBoundary.prototype.componentDidCatch = function componentDidCatch(error, errorInfo) {
    this.setState({
      error: error,
      errorInfo: errorInfo
    });

    if (this.props.errorCallback) {

      this.props.errorCallback(error, errorInfo);
    }
  };

  ErrorBoundary.prototype.render = function render() {
    var _state = this.state,
        error = _state.error,
        errorInfo = _state.errorInfo;
    var _props = this.props,
        children = _props.children,
        fallbackComponent = _props.fallbackComponent;


    var errorComponent = function errorComponent() {
      return fallbackComponent ? _react2.default.cloneElement(fallbackComponent, {
        error: error,
        errorInfo: errorInfo
      }) : _react2.default.createElement(
        'div',
        null,
        _ref,
        _react2.default.createElement(
          'details',
          { style: { whiteSpace: 'pre-wrap' } },
          error && error.toString(),
          _ref2,
          errorInfo.componentStack
        )
      );
    };

    return errorInfo ? errorComponent() : children;
  };

  return ErrorBoundary;
}(_react2.default.Component);

ErrorBoundary.displayName = 'ErrorBoundary';


ErrorBoundary.defaultProps = {
  fallbackComponent: null,
  errorCallback: null,
  children: null
};

exports.default = ErrorBoundary;
module.exports = exports['default'];