'use strict';

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

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 _RestApi = require('./RestApi');

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

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

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; } /* eslint-disable linebreak-style */


var _React$createContext = _react2.default.createContext(),
    Provider = _React$createContext.Provider,
    Consumer = _React$createContext.Consumer;

var Rest = function (_Component) {
  _inherits(Rest, _Component);

  function Rest(props) {
    _classCallCheck(this, Rest);

    var _this = _possibleConstructorReturn(this, (Rest.__proto__ || Object.getPrototypeOf(Rest)).call(this, props));

    _this.state = {
      error: null,
      data: null,
      loading: true
    };
    return _this;
  }

  _createClass(Rest, [{
    key: 'callApi',
    value: function () {
      var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
        var _props, method, route, data;

        return regeneratorRuntime.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                this.setState({
                  error: null,
                  data: null,
                  loading: true
                });

                _props = this.props, method = _props.method, route = _props.route;
                _context.next = 4;
                return (0, _RestApi._callApi)({
                  method: method,
                  url: route
                });

              case 4:
                data = _context.sent;

                this.setState({
                  error: null,
                  data: data,
                  loading: false
                });

              case 6:
              case 'end':
                return _context.stop();
            }
          }
        }, _callee, this);
      }));

      function callApi() {
        return _ref.apply(this, arguments);
      }

      return callApi;
    }()
  }, {
    key: 'componentWillMount',
    value: function componentWillMount() {
      var _props2 = this.props,
          method = _props2.method,
          route = _props2.route,
          run = _props2.run;

      if (run) {
        this.callApi();
      } else {
        this.setState({
          error: null,
          data: null,
          loading: false
        });
      }
    }
  }, {
    key: 'render',
    value: function render() {
      var _state = this.state,
          data = _state.data,
          loading = _state.loading,
          error = _state.error;
      var run = this.props.run;


      return _react2.default.createElement(
        Provider,
        { value: { data: data, loading: loading, error: error, callApi: run ? undefined : this.callApi } },
        _react2.default.createElement(
          Consumer,
          null,
          this.props.children
        )
      );
    }
  }]);

  return Rest;
}(_react.Component);

exports.default = Rest;