Liferay.Loader.define("dynamic-data-mapping-form-builder@3.0.77/js/components/LayoutProvider/handlers/fieldAddedHandler.es", ['module', 'exports', 'require', 'dynamic-data-mapping-form-renderer', '../../../util/fieldSupport.es', '../util/settingsContext.es'], function (module, exports, require) {
  var define = undefined;
  var global = window;
  {
    Object.defineProperty(exports, "__esModule", {
      value: true
    });
    exports.default = exports.addField = void 0;

    var _dynamicDataMappingFormRenderer = require("dynamic-data-mapping-form-renderer");

    var _fieldSupport = require("../../../util/fieldSupport.es");

    var _settingsContext = require("../util/settingsContext.es");

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

    function _toConsumableArray(arr) {
      return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread();
    }

    function _nonIterableSpread() {
      throw new TypeError("Invalid attempt to spread non-iterable instance");
    }

    function _iterableToArray(iter) {
      if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter);
    }

    function _arrayWithoutHoles(arr) {
      if (Array.isArray(arr)) {
        for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) {
          arr2[i] = arr[i];
        }return arr2;
      }
    }

    var addField = function addField(props, _ref) {
      var indexes = _ref.indexes,
          newField = _ref.newField,
          pages = _ref.pages,
          parentFieldName = _ref.parentFieldName;
      var columnIndex = indexes.columnIndex,
          pageIndex = indexes.pageIndex,
          rowIndex = indexes.rowIndex;
      var newPages;

      if (parentFieldName) {
        var visitor = new _dynamicDataMappingFormRenderer.PagesVisitor(pages);
        newPages = visitor.mapFields(function (field) {
          if (field.fieldName === parentFieldName) {
            var nestedFields = field.nestedFields ? [].concat(_toConsumableArray(field.nestedFields), [newField]) : [newField];
            field = (0, _settingsContext.updateField)(props, field, 'nestedFields', nestedFields);

            var _pages = _dynamicDataMappingFormRenderer.FormSupport.addFieldToColumn([{
              rows: field.rows
            }], 0, rowIndex, columnIndex, newField.fieldName);

            return (0, _settingsContext.updateField)(props, field, 'rows', _pages[0].rows);
          }

          return field;
        }, true, true);
      } else {
        newPages = _dynamicDataMappingFormRenderer.FormSupport.addFieldToColumn(pages, pageIndex, rowIndex, columnIndex, newField);
      }

      return {
        activePage: pageIndex,
        focusedField: _objectSpread({}, newField),
        pages: newPages,
        previousFocusedField: newField
      };
    };

    exports.addField = addField;

    var handleFieldAdded = function handleFieldAdded(props, state, event) {
      var data = event.data,
          indexes = event.indexes;
      var pages = state.pages;
      var parentFieldName = data.parentFieldName;
      var newField = event.newField || (0, _fieldSupport.createField)(props, event);
      return addField(props, {
        indexes: indexes,
        newField: newField,
        pages: pages,
        parentFieldName: parentFieldName
      });
    };

    var _default = handleFieldAdded;
    exports.default = _default;
    //# sourceMappingURL=fieldAddedHandler.es.js.map
  }
});
//# sourceMappingURL=fieldAddedHandler.es.js.map