"use strict";

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

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

var _useDispatchWithModulesData = require("./../hooks/useDispatchWithModulesData");

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

var _modelValidationHelper = require("../utils/modelValidationHelper");

var _lodash = require("lodash");

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

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

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

// if you need to override default validation rules just pass new rules with default keys
// as properties of extendedValidationRules
var useFullEntityEditState = function useFullEntityEditState(entityId, getFullEntityModelProperties, extendedValidationRules, dependencies) {
  if (extendedValidationRules === void 0) {
    extendedValidationRules = {};
  }

  if (dependencies === void 0) {
    dependencies = {};
  }

  var dispatchWithModulesData = (0, _useDispatchWithModulesData.useDispatchWithModulesData)();

  var mergedValidationRules = _objectSpread(_objectSpread({}, _modelValidationHelper.validationRules), extendedValidationRules);

  var isEditStateValid = function isEditStateValid(newValue, errorTypes, dependencies) {
    var error = null;
    errorTypes.forEach(function (errorType) {
      var isValid = mergedValidationRules[errorType] ? mergedValidationRules[errorType](newValue, dependencies) : true;

      if (isValid === false && error == null) {
        error = errorType;
      }
    });
    return error;
  };

  var buildFullEntityEditState = function buildFullEntityEditState(initializationContext) {
    var fullEntityEditModel = _immutable.default.fromJS({});

    var fullEntityModelProperties = getFullEntityModelProperties(initializationContext != null ? initializationContext : {});
    Object.entries(fullEntityModelProperties).forEach(function (_ref) {
      var _ref2 = _slicedToArray(_ref, 2),
          sectionName = _ref2[0],
          editStateFieldDefinitions = _ref2[1];

      Object.entries(editStateFieldDefinitions).forEach(function (_ref3) {
        var _fieldDefinition$erro;

        var _ref4 = _slicedToArray(_ref3, 2),
            fieldName = _ref4[0],
            fieldDefinition = _ref4[1];

        var allDependencies = _objectSpread(_objectSpread({}, dependencies), fieldDefinition.dependencies);

        var error = isEditStateValid(fieldDefinition.newValue, (_fieldDefinition$erro = fieldDefinition.errorTypes) != null ? _fieldDefinition$erro : [], allDependencies);
        var value = {
          value: fieldDefinition.newValue,
          wasModified: !(0, _lodash.isEqual)(fieldDefinition.initialValue, fieldDefinition.newValue)
        };

        if (error) {
          value.error = error;
        }

        var path = [entityId, sectionName, "model"].concat(fieldDefinition.keys);
        fullEntityEditModel = fullEntityEditModel.setIn(path, _immutable.default.fromJS(value));
      });
    });
    dispatchWithModulesData(_view.setFullEntityEditModel, [fullEntityEditModel]);
  };

  return buildFullEntityEditState;
};

exports.useFullEntityEditState = useFullEntityEditState;

__signature__(useFullEntityEditState, "useDispatchWithModulesData{dispatchWithModulesData}", function () {
  return [_useDispatchWithModulesData.useDispatchWithModulesData];
});

__signature__(useFullEntityEditState, "useDispatchWithModulesData{dispatchWithModulesData}", function () {
  return [_useDispatchWithModulesData.useDispatchWithModulesData];
});

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

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

  if (!reactHotLoader) {
    return;
  }

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

;

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