"use strict";

exports.__esModule = true;
exports.default = void 0;
var _reactRedux = require("react-redux");
var _modelValidationHelper = require("../utils/modelValidationHelper");
var _view = require("../selectors/view");
var _view2 = require("../actions/view");
var _useDispatchWithModulesData = require("./../hooks/useDispatchWithModulesData");
(function () {
  var enterModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.enterModule : undefined;
  enterModule && enterModule(module);
})();
(function () {
  var enterModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.enterModule : undefined;
  enterModule && enterModule(module);
})();
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
var __signature__ = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default.signature : function (a) {
  return a;
};
var __signature__ = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default.signature : function (a) {
  return a;
};
/* This hook is used when a component has a dynamic number of fields that can be edited (e.g.: Orders' Custom Configuration Parameters).
   It calls `useSelector` only once and handles the fields' states itself using in a similar structure to useEditState.

   This method expects initialValues to be an object with the format below. The extended validation rules uses a different function than the
   usual useEditState: instead of receiving only the `value` it receives `value, id, fieldName`.
   {
        id1: {
            prop1: 'another value',
            prop2: 'some value'
        },
        id2: {
            prop1: 'another value',
            prop2: 'some value'
        }
   }
*/
var useMultipleFieldEditState = function useMultipleFieldEditState(entityId, sectionName, initialValues, extendedValidationRules) {
  if (extendedValidationRules === void 0) {
    extendedValidationRules = {};
  }
  var dispatch = (0, _useDispatchWithModulesData.useDispatchWithModulesData)();
  var mergedValidationRules = _objectSpread(_objectSpread({}, _modelValidationHelper.validationRules), extendedValidationRules);
  var modifiedStates = (0, _reactRedux.useSelector)((0, _view.getModifiedModels)(entityId))[sectionName] || {};
  var useDynamicFieldState = function useDynamicFieldState(id, fieldName, errorTypes, fieldDependencies) {
    var _initialValues$id$fie, _initialValues$id, _modifiedStates$id$fi, _modifiedStates$id, _editState$value;
    if (errorTypes === void 0) {
      errorTypes = [];
    }
    if (fieldDependencies === void 0) {
      fieldDependencies = {};
    }
    var keys = [id, fieldName];
    var initialValue = (_initialValues$id$fie = (_initialValues$id = initialValues[id]) == null ? void 0 : _initialValues$id[fieldName]) != null ? _initialValues$id$fie : "";
    var editState = (_modifiedStates$id$fi = (_modifiedStates$id = modifiedStates[id]) == null ? void 0 : _modifiedStates$id[fieldName]) != null ? _modifiedStates$id$fi : {};
    editState.value = (_editState$value = editState.value) != null ? _editState$value : initialValue;
    var updateEditState = function updateEditState(newValue) {
      dispatch(_view2.setEditModelField, [keys, newValue, initialValue, entityId, sectionName]);
      isEditStateValid(newValue);
    };
    var resetEditState = function resetEditState() {
      dispatch(_view2.setEditModelField, [keys, initialValue, initialValue, entityId, sectionName]);
    };
    var isEditStateValid = function isEditStateValid(value, dependencies) {
      if (dependencies === void 0) {
        dependencies = {};
      }
      var valueToValidate = value != null ? value : editState.value;
      var hasAnyValidationErrors = false;
      errorTypes.forEach(function (errorType) {
        var isValid = mergedValidationRules[errorType](valueToValidate, id, fieldName, _objectSpread(_objectSpread({}, fieldDependencies), dependencies));
        if (isValid === false) {
          dispatch(_view2.setEditModelFieldError, [keys, errorType, entityId, sectionName]);
          hasAnyValidationErrors = true;
          return;
        }
      });
      if (!hasAnyValidationErrors) {
        dispatch(_view2.removeEditModelFieldError, [keys, entityId, sectionName]);
      }
      return !hasAnyValidationErrors;
    };
    return {
      state: editState,
      update: updateEditState,
      reset: resetEditState,
      isValid: isEditStateValid
    };
  };
  return [useDynamicFieldState, modifiedStates];
};
__signature__(useMultipleFieldEditState, "useDispatchWithModulesData{dispatch}\nuseSelector{}", function () {
  return [_useDispatchWithModulesData.useDispatchWithModulesData, _reactRedux.useSelector];
});
__signature__(useMultipleFieldEditState, "useDispatchWithModulesData{dispatch}\nuseSelector{}", function () {
  return [_useDispatchWithModulesData.useDispatchWithModulesData, _reactRedux.useSelector];
});
var _default = useMultipleFieldEditState;
var _default2 = _default;
var _default3 = exports.default = _default2;
;
(function () {
  var reactHotLoader = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default : undefined;
  if (!reactHotLoader) {
    return;
  }
  reactHotLoader.register(useMultipleFieldEditState, "useMultipleFieldEditState", "/home/vsts/work/1/s/src/hooks/useMultipleFieldEditState.js");
  reactHotLoader.register(_default, "default", "/home/vsts/work/1/s/src/hooks/useMultipleFieldEditState.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(useMultipleFieldEditState, "useMultipleFieldEditState", "/home/vsts/work/1/s/src/hooks/useMultipleFieldEditState.js");
  reactHotLoader.register(_default2, "default", "/home/vsts/work/1/s/src/hooks/useMultipleFieldEditState.js");
})();
;
(function () {
  var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;
  leaveModule && leaveModule(module);
})();