"use strict";

exports.__esModule = true;
exports.default = void 0;
var _immutable = _interopRequireDefault(require("immutable"));
var _lodash = require("lodash");
var _constants = require("../constants");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
(function () {
  var enterModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.enterModule : undefined;
  enterModule && enterModule(module);
})();
(function () {
  var enterModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.enterModule : undefined;
  enterModule && enterModule(module);
})();
var __signature__ = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default.signature : function (a) {
  return a;
};
var __signature__ = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default.signature : function (a) {
  return a;
};
var initialState = _immutable.default.fromJS({
  creates: _immutable.default.Map(),
  deletes: _immutable.default.Map(),
  fetches: _immutable.default.Map(),
  updates: _immutable.default.Map()
});
var containsRequestStateData = function containsRequestStateData(action) {
  var _action$meta;
  return !!(action != null && (_action$meta = action.meta) != null && _action$meta.requestState);
};
var getPathFromAction = function getPathFromAction(action) {
  var firstSegment = _constants.requestStateOperationMap[action.meta.requestState.operation];
  if (!firstSegment) {
    return null;
  }
  return [firstSegment].concat((0, _lodash.castArray)(action.meta.requestState.keys));
};
var requestStateReducer = function requestStateReducer(state, action) {
  if (state === void 0) {
    state = initialState;
  }
  if (!containsRequestStateData(action)) {
    return state;
  }
  if (action.type.endsWith("_REQUEST")) {
    var path = getPathFromAction(action);
    if (path === null) {
      return state;
    }
    return state.setIn([].concat(path, ["state"]), _immutable.default.fromJS({
      inProgress: true,
      value: false,
      error: false,
      errorResponse: null
    }));
  }
  if (action.type.endsWith("_SUCCESS")) {
    var _path = getPathFromAction(action);
    if (_path === null) {
      return state;
    }
    return state.setIn([].concat(_path, ["state"]), _immutable.default.fromJS({
      inProgress: false,
      value: true,
      error: false,
      errorResponse: null
    }));
  }
  if (action.type.endsWith("_FAILURE")) {
    var _action$payload;
    var _path2 = getPathFromAction(action);
    if (_path2 === null) {
      return state;
    }
    return state.setIn([].concat(_path2, ["state"]), _immutable.default.fromJS({
      inProgress: false,
      value: false,
      error: true,
      errorResponse: (_action$payload = action.payload) != null ? _action$payload : null
    }));
  }
  switch (action.type) {
    case "RESET_REQUEST_STATE":
      {
        var _path3 = getPathFromAction(action);
        if (_path3 === null) {
          return state;
        }
        return state.setIn([].concat(_path3, ["state"]), _immutable.default.fromJS({
          inProgress: false,
          value: false,
          error: false,
          errorResponse: null
        }));
      }
    default:
      return state;
  }
};
var _default = requestStateReducer;
var _default2 = _default;
var _default3 = exports.default = _default2;
;
(function () {
  var reactHotLoader = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default : undefined;
  if (!reactHotLoader) {
    return;
  }
  reactHotLoader.register(initialState, "initialState", "/home/vsts/work/1/s/src/reducers/requestStates.js");
  reactHotLoader.register(containsRequestStateData, "containsRequestStateData", "/home/vsts/work/1/s/src/reducers/requestStates.js");
  reactHotLoader.register(getPathFromAction, "getPathFromAction", "/home/vsts/work/1/s/src/reducers/requestStates.js");
  reactHotLoader.register(requestStateReducer, "requestStateReducer", "/home/vsts/work/1/s/src/reducers/requestStates.js");
  reactHotLoader.register(_default, "default", "/home/vsts/work/1/s/src/reducers/requestStates.js");
})();
;
(function () {
  var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;
  leaveModule && leaveModule(module);
})();
;
(function () {
  var reactHotLoader = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default : undefined;
  if (!reactHotLoader) {
    return;
  }
  reactHotLoader.register(initialState, "initialState", "/home/vsts/work/1/s/src/reducers/requestStates.js");
  reactHotLoader.register(containsRequestStateData, "containsRequestStateData", "/home/vsts/work/1/s/src/reducers/requestStates.js");
  reactHotLoader.register(getPathFromAction, "getPathFromAction", "/home/vsts/work/1/s/src/reducers/requestStates.js");
  reactHotLoader.register(requestStateReducer, "requestStateReducer", "/home/vsts/work/1/s/src/reducers/requestStates.js");
  reactHotLoader.register(_default2, "default", "/home/vsts/work/1/s/src/reducers/requestStates.js");
})();
;
(function () {
  var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;
  leaveModule && leaveModule(module);
})();