"use strict";

exports.__esModule = true;
exports.hasUnsavedDataSelector = exports.getModifiedModel = exports.getModifiedModels = exports.getTabsWithErrors = exports.getModifiedTabs = exports.getInstanceKeysWithErrors = exports.getSectionsWithErrors = exports.getModifiedSections = exports.isEntityUnderEditing = void 0;

var _reselect = require("reselect");

var _navigation = require("./navigation");

var _propertyHelper = require("./../utils/propertyHelper");

var _urlHelper = require("./../utils/urlHelper");

(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 modulesData = function modulesData(state) {
  return state.get("view");
};

var editData = (0, _reselect.createSelector)(modulesData, function (data) {
  return data.get("edit");
});

var isEntityUnderEditing = function isEntityUnderEditing(entityId) {
  return (0, _reselect.createSelector)(editData, _navigation.selectCurrentModuleName, function (data, moduleName) {
    if (data != null) {
      var _dataJS$moduleName;

      var dataJS = data.toJS();
      var sections = (_dataJS$moduleName = dataJS[moduleName]) == null ? void 0 : _dataJS$moduleName[entityId];

      if (sections != null) {
        return true;
      }
    }

    return false;
  });
};

exports.isEntityUnderEditing = isEntityUnderEditing;

var getModifiedSections = function getModifiedSections(entityId) {
  return (0, _reselect.createSelector)(editData, _navigation.selectCurrentModuleName, function (data, moduleName) {
    return getModifiedSectionsFromModule(data, moduleName, entityId);
  });
};

exports.getModifiedSections = getModifiedSections;

var getModifiedSectionsFromModule = function getModifiedSectionsFromModule(editData, moduleName, entityId) {
  var modifiedSections = [];

  if (editData != null) {
    var _dataJS$moduleName2;

    var dataJS = editData.toJS();
    var sections = (_dataJS$moduleName2 = dataJS[moduleName]) == null ? void 0 : _dataJS$moduleName2[entityId];

    if (sections != null) {
      var sectionsKeys = Object.keys(sections);

      for (var _i = 0, _sectionsKeys = sectionsKeys; _i < _sectionsKeys.length; _i++) {
        var sectionKey = _sectionsKeys[_i];
        var sectionModel = sections[sectionKey].model;

        if (sectionModel != null) {
          var wasModified = (0, _propertyHelper.isObjectContainsPropertyWithValue)(sectionModel, "wasModified", true);

          if (wasModified === true) {
            modifiedSections.push(sectionKey);
          }
        }
      }
    }
  }

  return modifiedSections;
};

var getSectionsWithErrors = function getSectionsWithErrors(entityId) {
  return (0, _reselect.createSelector)(editData, _navigation.selectCurrentModuleName, function (data, moduleName) {
    return getSectionsWithErrorsFromModule(data, moduleName, entityId);
  });
};

exports.getSectionsWithErrors = getSectionsWithErrors;

var getSectionsWithErrorsFromModule = function getSectionsWithErrorsFromModule(editData, moduleName, entityId) {
  var errorSections = [];

  if (editData != null) {
    var _dataJS$moduleName3;

    var dataJS = editData.toJS();
    var sections = (_dataJS$moduleName3 = dataJS[moduleName]) == null ? void 0 : _dataJS$moduleName3[entityId];

    if (sections != null) {
      var sectionsKeys = Object.keys(sections);

      for (var _i2 = 0, _sectionsKeys2 = sectionsKeys; _i2 < _sectionsKeys2.length; _i2++) {
        var sectionKey = _sectionsKeys2[_i2];
        var sectionModel = sections[sectionKey].model;

        if (sectionModel != null) {
          var hasError = (0, _propertyHelper.isObjectContainsPropertyWithAnyValue)(sectionModel, "error");

          if (hasError === true) {
            errorSections.push(sectionKey);
          }
        }
      }
    }
  }

  return errorSections;
};

var getInstanceKeysWithErrors = function getInstanceKeysWithErrors(entityId, sectionName, path) {
  return (0, _reselect.createSelector)(editData, _navigation.selectCurrentModuleName, function (data, moduleName) {
    var errorKeys = [];
    var sectionData = data == null ? void 0 : data.getIn([moduleName, entityId, sectionName, "model"].concat(path));

    if (sectionData) {
      var section = sectionData.toJS();
      var instanceKeys = Object.keys(section);
      instanceKeys.forEach(function (instanceKey) {
        var hasError = (0, _propertyHelper.isObjectContainsPropertyWithAnyValue)(section[instanceKey], "error");

        if (hasError === true) {
          errorKeys.push(instanceKey);
        }
      });
    }

    return errorKeys;
  });
};

exports.getInstanceKeysWithErrors = getInstanceKeysWithErrors;

var getModifiedTabs = function getModifiedTabs(tabsParams) {
  return (0, _reselect.createSelector)(editData, _navigation.selectCurrentModuleName, function (data, moduleName) {
    var modifiedTabs = [];
    tabsParams.forEach(function (tabParams) {
      var newEntityId = (0, _urlHelper.tryGetNewEntityIdKey)(tabParams.href);
      if (newEntityId) tabParams.params.push(newEntityId);

      for (var i = 0; i < tabParams.params.length; i++) {
        var modifiedSections = getModifiedSectionsFromModule(data, moduleName, tabParams.params[i]);

        if (modifiedSections.length > 0) {
          modifiedTabs.push(tabParams.href);
          break;
        }
      }
    });
    return modifiedTabs;
  });
};

exports.getModifiedTabs = getModifiedTabs;

var getTabsWithErrors = function getTabsWithErrors(tabsParams) {
  return (0, _reselect.createSelector)(editData, _navigation.selectCurrentModuleName, function (data, moduleName) {
    var tabsWithErrors = [];
    tabsParams.forEach(function (tabParams) {
      var newEntityId = (0, _urlHelper.tryGetNewEntityIdKey)(tabParams.href);
      if (newEntityId) tabParams.params.push(newEntityId);

      for (var i = 0; i < tabParams.params.length; i++) {
        var sectionsWithErrors = getSectionsWithErrorsFromModule(data, moduleName, tabParams.params[i]);

        if (sectionsWithErrors.length > 0) {
          tabsWithErrors.push(tabParams.href);
          break;
        }
      }
    });
    return tabsWithErrors;
  });
};

exports.getTabsWithErrors = getTabsWithErrors;

var getModifiedModels = function getModifiedModels(entityId) {
  return (0, _reselect.createSelector)(editData, _navigation.selectCurrentModuleName, function (data, moduleName) {
    var models = {};

    if (data != null) {
      var _dataJS$moduleName4;

      var dataJS = data.toJS();
      var sections = (_dataJS$moduleName4 = dataJS[moduleName]) == null ? void 0 : _dataJS$moduleName4[entityId];

      if (sections != null) {
        var sectionsKeys = Object.keys(sections);
        sectionsKeys.forEach(function (sectionKey) {
          models[sectionKey] = sections[sectionKey].model;
        });
      }
    }

    return models;
  });
}; // the difference between this one and getModifiedModels (with 's' at the end) is that
// this selector will retrieve you modified model for all the sections
// so, the result won't be grouped by sections


exports.getModifiedModels = getModifiedModels;

var getModifiedModel = function getModifiedModel(entityId) {
  return (0, _reselect.createSelector)(editData, _navigation.selectCurrentModuleName, function (data, moduleName) {
    var model = {};

    if (data != null) {
      var _dataJS$moduleName5;

      var dataJS = data.toJS();
      var sections = (_dataJS$moduleName5 = dataJS[moduleName]) == null ? void 0 : _dataJS$moduleName5[entityId];

      if (sections != null) {
        var sectionsKeys = Object.keys(sections);
        sectionsKeys.forEach(function (sectionKey) {
          Object.assign(model, sections[sectionKey].model);
        });
      }
    }

    return model;
  });
};

exports.getModifiedModel = getModifiedModel;
var hasUnsavedDataSelector = (0, _reselect.createSelector)(editData, function (data) {
  if (!data) {
    return false;
  }

  return data.some(function (module, moduleName) {
    return module.some(function (moduleData, entityId) {
      return getModifiedSectionsFromModule(data, moduleName, entityId).length > 0;
    });
  });
});
exports.hasUnsavedDataSelector = hasUnsavedDataSelector;
;

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

  if (!reactHotLoader) {
    return;
  }

  reactHotLoader.register(modulesData, "modulesData", "/home/vsts/work/1/s/src/selectors/view.js");
  reactHotLoader.register(editData, "editData", "/home/vsts/work/1/s/src/selectors/view.js");
  reactHotLoader.register(isEntityUnderEditing, "isEntityUnderEditing", "/home/vsts/work/1/s/src/selectors/view.js");
  reactHotLoader.register(getModifiedSections, "getModifiedSections", "/home/vsts/work/1/s/src/selectors/view.js");
  reactHotLoader.register(getModifiedSectionsFromModule, "getModifiedSectionsFromModule", "/home/vsts/work/1/s/src/selectors/view.js");
  reactHotLoader.register(getSectionsWithErrors, "getSectionsWithErrors", "/home/vsts/work/1/s/src/selectors/view.js");
  reactHotLoader.register(getSectionsWithErrorsFromModule, "getSectionsWithErrorsFromModule", "/home/vsts/work/1/s/src/selectors/view.js");
  reactHotLoader.register(getInstanceKeysWithErrors, "getInstanceKeysWithErrors", "/home/vsts/work/1/s/src/selectors/view.js");
  reactHotLoader.register(getModifiedTabs, "getModifiedTabs", "/home/vsts/work/1/s/src/selectors/view.js");
  reactHotLoader.register(getTabsWithErrors, "getTabsWithErrors", "/home/vsts/work/1/s/src/selectors/view.js");
  reactHotLoader.register(getModifiedModels, "getModifiedModels", "/home/vsts/work/1/s/src/selectors/view.js");
  reactHotLoader.register(getModifiedModel, "getModifiedModel", "/home/vsts/work/1/s/src/selectors/view.js");
  reactHotLoader.register(hasUnsavedDataSelector, "hasUnsavedDataSelector", "/home/vsts/work/1/s/src/selectors/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(modulesData, "modulesData", "/home/vsts/work/1/s/src/selectors/view.js");
  reactHotLoader.register(editData, "editData", "/home/vsts/work/1/s/src/selectors/view.js");
  reactHotLoader.register(isEntityUnderEditing, "isEntityUnderEditing", "/home/vsts/work/1/s/src/selectors/view.js");
  reactHotLoader.register(getModifiedSections, "getModifiedSections", "/home/vsts/work/1/s/src/selectors/view.js");
  reactHotLoader.register(getModifiedSectionsFromModule, "getModifiedSectionsFromModule", "/home/vsts/work/1/s/src/selectors/view.js");
  reactHotLoader.register(getSectionsWithErrors, "getSectionsWithErrors", "/home/vsts/work/1/s/src/selectors/view.js");
  reactHotLoader.register(getSectionsWithErrorsFromModule, "getSectionsWithErrorsFromModule", "/home/vsts/work/1/s/src/selectors/view.js");
  reactHotLoader.register(getInstanceKeysWithErrors, "getInstanceKeysWithErrors", "/home/vsts/work/1/s/src/selectors/view.js");
  reactHotLoader.register(getModifiedTabs, "getModifiedTabs", "/home/vsts/work/1/s/src/selectors/view.js");
  reactHotLoader.register(getTabsWithErrors, "getTabsWithErrors", "/home/vsts/work/1/s/src/selectors/view.js");
  reactHotLoader.register(getModifiedModels, "getModifiedModels", "/home/vsts/work/1/s/src/selectors/view.js");
  reactHotLoader.register(getModifiedModel, "getModifiedModel", "/home/vsts/work/1/s/src/selectors/view.js");
  reactHotLoader.register(hasUnsavedDataSelector, "hasUnsavedDataSelector", "/home/vsts/work/1/s/src/selectors/view.js");
})();

;

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