import _regeneratorRuntime from "@babel/runtime/regenerator";

var _marked =
/*#__PURE__*/
_regeneratorRuntime.mark(matchPattern),
    _marked2 =
/*#__PURE__*/
_regeneratorRuntime.mark(stateTemplateSaga);

import { all, takeEvery, put, select } from 'redux-saga/effects';
import * as selectors from './selectors';
import * as actions from './actions'; // catches all requests to update loading status and errors

export function matchPattern(action) {
  var type, isLoading, error;
  return _regeneratorRuntime.wrap(function matchPattern$(_context) {
    while (1) {
      switch (_context.prev = _context.next) {
        case 0:
          type = action.type;
          _context.next = 3;
          return select(selectors.getIsLoading());

        case 3:
          isLoading = _context.sent;
          error = null;

          if (!type.match('_REQUEST')) {
            _context.next = 8;
            break;
          }

          _context.next = 8;
          return put(actions.updateStatus({
            isLoading: isLoading + 1
          }));

        case 8:
          if (!type.match('_SUCCESS')) {
            _context.next = 11;
            break;
          }

          _context.next = 11;
          return put(actions.updateStatus({
            isLoading: isLoading - 1
          }));

        case 11:
          if (!type.match('_FAILURE')) {
            _context.next = 15;
            break;
          }

          error = action.payload && action.payload.message;
          _context.next = 15;
          return put(actions.updateStatus({
            isLoading: isLoading - 1,
            error: error
          }));

        case 15:
        case "end":
          return _context.stop();
      }
    }
  }, _marked, this);
}
export default function stateTemplateSaga() {
  return _regeneratorRuntime.wrap(function stateTemplateSaga$(_context2) {
    while (1) {
      switch (_context2.prev = _context2.next) {
        case 0:
          _context2.next = 2;
          return all([takeEvery('*', matchPattern)]);

        case 2:
        case "end":
          return _context2.stop();
      }
    }
  }, _marked2, this);
}