"use strict";

exports.__esModule = true;
exports.default = void 0;

var _immutable = _interopRequireDefault(require("immutable"));

var _view = require("../actions/view");

var _scopes = require("../actions/scopes");

var _lodash = require("lodash");

var _mapHelper = require("./../utils/mapHelper");

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

(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.Map({});

var viewStateReducer = function viewStateReducer(state, action) {
  if (state === void 0) {
    state = initialState;
  }

  switch (action.type) {
    case _view.VIEW_SET:
      {
        return state.set(action.payload.name, _immutable.default.fromJS(action.payload.value));
      }

    case _view.VIEW_SET_FIELD:
      {
        return state.setIn([action.payload.name, action.payload.field], _immutable.default.fromJS(action.payload.value));
      }

    case _view.VIEW_REMOVE_EDIT_NODE:
      {
        var _action$payload = action.payload,
            moduleName = _action$payload.moduleName,
            entityId = _action$payload.entityId;
        return state.removeIn(["edit", moduleName, entityId]);
      }

    case _view.VIEW_REMOVE_EDIT_MODEL:
      {
        var _action$payload2 = action.payload,
            _moduleName = _action$payload2.moduleName,
            sectionName = _action$payload2.sectionName,
            _entityId = _action$payload2.entityId,
            keys = _action$payload2.keys;
        var path = ["edit", _moduleName, _entityId, sectionName, "model"].concat(keys);
        return state.removeIn(path);
      }

    case _view.VIEW_SET_EDIT_MODEL_FIELD:
      {
        var _action$payload3 = action.payload,
            _keys = _action$payload3.keys,
            value = _action$payload3.value,
            storeValue = _action$payload3.storeValue,
            _entityId2 = _action$payload3.entityId,
            _sectionName = _action$payload3.sectionName,
            _moduleName2 = _action$payload3.moduleName;

        var _path = ["edit", _moduleName2, _entityId2, _sectionName, "model"].concat(_keys);

        return state.setIn(_path, _immutable.default.fromJS({
          value: value,
          wasModified: !(0, _lodash.isEqual)(value, storeValue)
        }));
      }

    case _view.VIEW_SET_FULL_ENTITY_EDIT_MODEL:
      {
        var _action$payload4 = action.payload,
            entityFullModel = _action$payload4.entityFullModel,
            _moduleName3 = _action$payload4.moduleName;
        return state.setIn(["edit", _moduleName3], entityFullModel);
      }

    case _view.VIEW_REMOVE_EDIT_MODEL_FIELD:
      {
        var _action$payload5 = action.payload,
            _keys2 = _action$payload5.keys,
            _storeValue = _action$payload5.storeValue,
            _entityId3 = _action$payload5.entityId,
            _sectionName2 = _action$payload5.sectionName,
            _moduleName4 = _action$payload5.moduleName;
        var pathToDelete = ["edit", _moduleName4, _entityId3, _sectionName2, "model"].concat(_keys2);
        var removed = state.removeIn(pathToDelete);
        var newPath = (0, _lodash.dropRight)(pathToDelete, 1);

        var _value = removed.getIn(newPath);

        var pathToUpdate = newPath[newPath.length - 1] === "value" ? (0, _lodash.dropRight)(newPath, 1) : newPath;
        var unpackedValue = (0, _mapHelper.mapModifiedData)(_value.toJS());
        return state.setIn(pathToUpdate, _immutable.default.fromJS({
          value: _value,
          wasModified: !(0, _lodash.isEqual)(unpackedValue, _storeValue)
        }));
      }

    case _view.VIEW_REMOVE_EDIT_ALL_MODEL_FIELDS:
      {
        var _action$payload6 = action.payload,
            _keys3 = _action$payload6.keys,
            _entityId4 = _action$payload6.entityId,
            _sectionName3 = _action$payload6.sectionName,
            _moduleName5 = _action$payload6.moduleName;

        var _pathToDelete = ["edit", _moduleName5, _entityId4, _sectionName3, "model"].concat(_keys3);

        return state.removeIn(_pathToDelete);
      }

    case _view.VIEW_SET_EDIT_MODEL_FIELD_ERRORS:
      {
        var _action$payload7 = action.payload,
            _keys4 = _action$payload7.keys,
            error = _action$payload7.error,
            _entityId5 = _action$payload7.entityId,
            _sectionName4 = _action$payload7.sectionName,
            _moduleName6 = _action$payload7.moduleName;

        var _path2 = ["edit", _moduleName6, _entityId5, _sectionName4, "model"].concat(_keys4, ["error"]);

        return state.setIn(_path2, error);
      }

    case _view.VIEW_REMOVE_EDIT_MODEL_FIELD_ERRORS:
      {
        var _action$payload8 = action.payload,
            _keys5 = _action$payload8.keys,
            _entityId6 = _action$payload8.entityId,
            _sectionName5 = _action$payload8.sectionName,
            _moduleName7 = _action$payload8.moduleName;

        var _path3 = ["edit", _moduleName7, _entityId6, _sectionName5, "model"].concat(_keys5, ["error"]);

        return state.removeIn(_path3);
      }

    case _view.VIEW_SET_EDIT_MODEL_ERRORS:
      {
        var _action$payload9 = action.payload,
            errors = _action$payload9.errors,
            _entityId7 = _action$payload9.entityId,
            _sectionName6 = _action$payload9.sectionName,
            _moduleName8 = _action$payload9.moduleName;
        errors.forEach(function (item) {
          if (item.keys) {
            var _path4 = ["edit", _moduleName8, _entityId7, _sectionName6, "model"].concat(item.keys, ["error"]);

            state = state.setIn(_path4, item.error);
          }
        });
        return state;
      }

    case _scopes.APPLICATION_SCOPE_HAS_CHANGED:
      {
        return state.remove("edit");
      }

    default:
      return state;
  }
};

var _default = viewStateReducer;
var _default2 = _default;
var _default3 = _default2;
exports.default = _default3;
;

(function () {
  var reactHotLoader = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default : undefined;

  if (!reactHotLoader) {
    return;
  }

  reactHotLoader.register(initialState, "initialState", "/home/vsts/work/1/s/src/reducers/view.js");
  reactHotLoader.register(viewStateReducer, "viewStateReducer", "/home/vsts/work/1/s/src/reducers/view.js");
  reactHotLoader.register(_default, "default", "/home/vsts/work/1/s/src/reducers/view.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/view.js");
  reactHotLoader.register(viewStateReducer, "viewStateReducer", "/home/vsts/work/1/s/src/reducers/view.js");
  reactHotLoader.register(_default2, "default", "/home/vsts/work/1/s/src/reducers/view.js");
})();

;

(function () {
  var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;
  leaveModule && leaveModule(module);
})();