"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(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

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) {
    var _initialValues$id$fie, _initialValues$id, _modifiedStates$id$fi, _modifiedStates$id, _editState$value;

    if (errorTypes === void 0) {
      errorTypes = [];
    }

    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) {
      var valueToValidate = value != null ? value : editState.value;
      var hasAnyValidationErrors = false;
      errorTypes.forEach(function (errorType) {
        var isValid = mergedValidationRules[errorType](valueToValidate, id, fieldName);

        if (isValid === false) {
          dispatch(_view2.setEditModelFieldError, [keys, errorType, entityId, sectionName]);
          hasAnyValidationErrors = true;
          return;
        }
      });
      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 = _default2;
exports.default = _default3;
;

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