"use strict";

exports.__esModule = true;
exports.mapModel = exports.mapModifiedData = void 0;

var _lodash = require("lodash");

(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 mapModifiedData = function mapModifiedData(model) {
  var mapModifiedObj = function mapModifiedObj(obj) {
    if (obj === null || obj === undefined) return obj;
    if (obj.wasModified != null) return mapModifiedObj(obj.value);
    return mapModifiedData(obj);
  };

  var result;

  if (typeof model === "object" || Array.isArray(model)) {
    result = Array.isArray(model) ? [] : {};
    var modifiedFields = Object.keys(model);
    modifiedFields.forEach(function (modifiedField) {
      result[modifiedField] = mapModifiedObj(model[modifiedField]);
    });
  } else {
    result = model;
  }

  return result;
};

exports.mapModifiedData = mapModifiedData;

var mapModel = function mapModel(model, initialModel, mappingRules) {
  if (mappingRules === void 0) {
    mappingRules = [];
  }

  // if need to map fields with different model keys then
  // mappingRules should be an array like
  // [
  // notice you need to define a full pass to you property to get it correctly
  // 	{
  // 		modelName: "modelName3.path1.path2",
  // 		transform: (objValue, srcValue, object, source) => { /* Custom mapping code. */ }
  // 	},
  // ]
  // if you don't need this - just don't pass any value for that parameter
  var mergedModel = (0, _lodash.cloneDeep)(initialModel);
  var modifiedModel = mapModifiedData(model);
  mergedModel = (0, _lodash.merge)(mergedModel, modifiedModel);
  mappingRules.forEach(function (rule) {
    var initialField = (0, _lodash.get)(mergedModel, rule.modelName);
    var modifiedField = (0, _lodash.get)(modifiedModel, rule.modelName);

    if (modifiedField !== undefined) {
      rule.transform(initialField, modifiedField, mergedModel, modifiedModel);
    }
  });
  return (0, _lodash.merge)({}, mergedModel);
};

exports.mapModel = mapModel;
;

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

  if (!reactHotLoader) {
    return;
  }

  reactHotLoader.register(mapModifiedData, "mapModifiedData", "/home/vsts/work/1/s/src/utils/mapHelper.js");
  reactHotLoader.register(mapModel, "mapModel", "/home/vsts/work/1/s/src/utils/mapHelper.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(mapModifiedData, "mapModifiedData", "/home/vsts/work/1/s/src/utils/mapHelper.js");
  reactHotLoader.register(mapModel, "mapModel", "/home/vsts/work/1/s/src/utils/mapHelper.js");
})();

;

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