'use strict';

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

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _omit = require('lodash/omit');

var _omit2 = _interopRequireDefault(_omit);

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

var _Spinner2 = _interopRequireDefault(_Spinner);

var _Alert = require('../Alert');

var _Alert2 = _interopRequireDefault(_Alert);

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

function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

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

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

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

var propTypes = {
  data: _propTypes2.default.oneOfType([_propTypes2.default.array, _propTypes2.default.object]),
  loading: _propTypes2.default.bool.isRequired,
  error: _propTypes2.default.object,
  label: _propTypes2.default.string.isRequired,
  children: _propTypes2.default.func,
  renderEmpty: _propTypes2.default.bool,
  componentAlertWrapper: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.element, _propTypes2.default.node, _propTypes2.default.object, _propTypes2.default.func]),
  className: _propTypes2.default.string
};

var defaultProps = {
  data: null,
  error: null,
  children: null,
  renderEmpty: false,
  componentAlertWrapper: 'div',
  className: undefined
};

// This component uses the prop Component to pass the children
// coz otherwise it is not elegant to do the propTypes in the
// included Component, some prop inject may be only available
// when the loading is done or no error occured for instance

var UIStates = function (_React$Component) {
  _inherits(UIStates, _React$Component);

  function UIStates() {
    _classCallCheck(this, UIStates);

    return _possibleConstructorReturn(this, (UIStates.__proto__ || Object.getPrototypeOf(UIStates)).apply(this, arguments));
  }

  _createClass(UIStates, [{
    key: 'renderLoading',
    value: function renderLoading() {
      var _props = this.props,
          AlertWrapper = _props.componentAlertWrapper,
          className = _props.className;

      return _react2.default.createElement(
        AlertWrapper,
        _extends({ className: className }, (0, _omit2.default)(this.props, Object.keys(propTypes))),
        _react2.default.createElement(_Spinner2.default, null)
      );
    }
  }, {
    key: 'renderError',
    value: function renderError() {
      var _props2 = this.props,
          label = _props2.label,
          AlertWrapper = _props2.componentAlertWrapper,
          className = _props2.className;

      return _react2.default.createElement(
        AlertWrapper,
        _extends({ className: className }, (0, _omit2.default)(this.props, Object.keys(propTypes))),
        _react2.default.createElement(
          _Alert2.default,
          { type: 'danger' },
          'Couldn\'t load ' + label
        )
      );
    }
  }, {
    key: 'renderEmpty',
    value: function renderEmpty() {
      var _props3 = this.props,
          label = _props3.label,
          AlertWrapper = _props3.componentAlertWrapper,
          className = _props3.className;

      return _react2.default.createElement(
        AlertWrapper,
        _extends({ className: className }, (0, _omit2.default)(this.props, Object.keys(propTypes))),
        _react2.default.createElement(
          _Alert2.default,
          { type: 'info' },
          'No ',
          label,
          ' has been found.'
        )
      );
    }
  }, {
    key: 'render',
    value: function render() {
      var _props4 = this.props,
          data = _props4.data,
          loading = _props4.loading,
          error = _props4.error,
          children = _props4.children,
          renderEmpty = _props4.renderEmpty,
          props = _objectWithoutProperties(_props4, ['data', 'loading', 'error', 'children', 'renderEmpty']);

      if (loading) return this.renderLoading();
      if (error) return this.renderError();
      if (!renderEmpty) {
        if (data === null || data === undefined || Array.isArray(data) && data.length === 0) {
          return this.renderEmpty();
        }
      }
      return children.apply(undefined, _toConsumableArray(props));
    }
  }]);

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

UIStates.propTypes = propTypes;
UIStates.defaultProps = defaultProps;

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