Liferay.Loader.define("commerce-frontend-js@3.0.33/utilities/forms/formsHelper", ['module', 'exports', 'require'], function (module, exports, require) {
  var define = undefined;
  var global = window;
  {
    Object.defineProperty(exports, "__esModule", {
      value: true
    });
    exports.isFormElement = isFormElement;
    exports.toJSON = toJSON;
    exports.getDefaultFieldsShape = getDefaultFieldsShape;
    exports.updateFields = updateFields;

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

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

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

    function _iterableToArrayLimit(arr, i) {
      if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) {
        return;
      }var _arr = [];var _n = true;var _d = false;var _e = undefined;try {
        for (var _i = arr[Symbol.iterator](), _s; !(_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;
    }

    /**
     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
     *
     * This library is free software; you can redistribute it and/or modify it under
     * the terms of the GNU Lesser General Public License as published by the Free
     * Software Foundation; either version 2.1 of the License, or (at your option)
     * any later version.
     *
     * This library is distributed in the hope that it will be useful, but WITHOUT
     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
     * details.
     */
    var FIELD_BLACKLIST = ['formDate', 'p_auth'];

    function isFormElement(form) {
      return form instanceof HTMLFormElement;
    }

    function isBlacklistedField(field) {
      return FIELD_BLACKLIST.includes(field);
    }

    function toJSON(formData) {
      var json = {}; // eslint-disable-next-line no-for-of-loops/no-for-of-loops, no-unused-vars

      var _iteratorNormalCompletion = true;
      var _didIteratorError = false;
      var _iteratorError = undefined;

      try {
        for (var _iterator = formData.entries()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
          var _step$value = _slicedToArray(_step.value, 2),
              key = _step$value[0],
              value = _step$value[1];

          if (!isBlacklistedField(key)) {
            if (!(key in json)) {
              json[key] = value;
              continue;
            }

            if (!Array.isArray(json[key])) {
              json[key] = [json[key]];
            }

            json[key].push(value);
          }
        }
      } catch (err) {
        _didIteratorError = true;
        _iteratorError = err;
      } finally {
        try {
          if (!_iteratorNormalCompletion && _iterator.return != null) {
            _iterator.return();
          }
        } finally {
          if (_didIteratorError) {
            throw _iteratorError;
          }
        }
      }

      return json;
    }

    function toArray(value) {
      return Array.isArray(value) ? _toConsumableArray(value) : [value];
    }

    function getDefaultFieldsShape(formInstance) {
      try {
        var options = formInstance.props.pages[0].rows;
        var fields = options.map(function (option) {
          return option.columns[0].fields[0];
        });
        return fields.map(function (field) {
          var key = field.fieldName,
              predefinedValue = field.predefinedValue;
          return {
            key: key,
            value: toArray(predefinedValue)
          };
        });
      } catch (_ignore) {
        return [];
      }
    }

    function updateFields(currentFields, nextField) {
      return currentFields.reduce(function (nextFields, currentField) {
        var key = currentField.key;
        var fieldInstance = nextField.fieldInstance,
            nextValue = nextField.value;
        var fieldName = fieldInstance.fieldName;

        if (fieldName === key) {
          nextFields.push({
            key: fieldName,
            value: toArray(nextValue)
          });
        } else {
          nextFields.push(currentField);
        }

        return nextFields;
      }, []);
    }
    //# sourceMappingURL=formsHelper.js.map
  }
});
//# sourceMappingURL=formsHelper.js.map