Liferay.Loader.define("dynamic-data-mapping-form-field-type@5.0.73/Options/util.es", ['module', 'exports', 'require', 'dynamic-data-mapping-form-builder/js/util/fieldSupport.es', 'dynamic-data-mapping-form-renderer'], function (module, exports, require) {
  var define = undefined;
  var global = window;
  {
    Object.defineProperty(exports, "__esModule", {
      value: true
    });
    exports.normalizeFields = exports.normalizeValue = exports.normalizeReference = exports.getDefaultOptionValue = exports.findDuplicateReference = exports.dedupValue = exports.isOptionValueGenerated = exports.compose = exports.random = void 0;

    var _fieldSupport = require("dynamic-data-mapping-form-builder/js/util/fieldSupport.es");

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

    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 random = function random(a) {
      return a ? (a ^ Math.random() * 16 >> a / 4).toString(16) : ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, random);
    };

    exports.random = random;

    var compose = function compose() {
      for (var _len = arguments.length, fns = new Array(_len), _key = 0; _key < _len; _key++) {
        fns[_key] = arguments[_key];
      }

      return fns.reduceRight(function (f, g) {
        return function () {
          var r = g.apply(void 0, arguments);
          return Array.isArray(r) ? f.apply(void 0, _toConsumableArray(r)) : f(r);
        };
      });
    };

    exports.compose = compose;

    var isOptionValueGenerated = function isOptionValueGenerated(defaultLanguageId, editingLanguageId, options, option) {
      if (defaultLanguageId !== editingLanguageId) {
        return false;
      }

      if (option.value === '') {
        return true;
      }

      var optionIndex = options.indexOf(option);
      var duplicated = options.some(function (_ref, index) {
        var value = _ref.value;
        return value === option.value && index !== optionIndex;
      });

      if (duplicated) {
        return true;
      }

      if (option.edited) {
        return false;
      }

      if (new RegExp("^".concat(Liferay.Language.get('option'), "\\d*$")).test(option.value)) {
        return true;
      }

      if (new RegExp("^".concat(option.value.replace(/\d+$/, ''), "\\d*")).test((0, _dynamicDataMappingFormRenderer.normalizeFieldName)(option.label))) {
        return true;
      }

      return true;
    };
    /**
     * Deduplicates the value by checking if there is a
     * value in the fields, always incrementing an integer
     * in front of the value to be friendly for the user.
     */

    exports.isOptionValueGenerated = isOptionValueGenerated;

    var dedupValue = function dedupValue(fields, value, id, generateValueUsingLabel) {
      var counter = 0;

      var recursive = function recursive(fields, currentValue) {
        var field = fields.find(function (field) {
          var _field$value;

          return ((_field$value = field.value) === null || _field$value === void 0 ? void 0 : _field$value.toLowerCase()) === (currentValue === null || currentValue === void 0 ? void 0 : currentValue.toLowerCase());
        });

        if (field && field.id !== id) {
          if (generateValueUsingLabel) {
            counter += 1;
            recursive(fields, value + counter);
          } else {
            recursive(fields, (0, _fieldSupport.getDefaultFieldName)(true));
          }
        } else {
          value = currentValue;
        }
      };

      recursive(fields, value);
      return value;
    };

    exports.dedupValue = dedupValue;

    var findDuplicateReference = function findDuplicateReference(fields, currentIndex, currentReference) {
      return fields.filter(function (field, index) {
        return index !== currentIndex;
      }).some(function (_ref2) {
        var reference = _ref2.reference;
        return (reference === null || reference === void 0 ? void 0 : reference.toLowerCase()) === (currentReference === null || currentReference === void 0 ? void 0 : currentReference.toLowerCase());
      });
    };

    exports.findDuplicateReference = findDuplicateReference;

    var getDefaultOptionValue = function getDefaultOptionValue(generateValueUsingLabel, optionLabel) {
      var defaultValue = generateValueUsingLabel ? optionLabel : (0, _fieldSupport.getDefaultFieldName)(true);
      return defaultValue;
    };

    exports.getDefaultOptionValue = getDefaultOptionValue;

    var normalizeReference = function normalizeReference(fields, currentField, index) {
      var reference = currentField.reference,
          value = currentField.value;

      if (!reference || findDuplicateReference(fields, index, reference)) {
        return value ? value : (0, _fieldSupport.getDefaultFieldName)(true);
      }

      return reference;
    };
    /**
     * If the value is null or undefined, normalize follows a
     * verification order and the final stage of normalization
     * is to deduplicate the value if necessary.
     *
     * 1. If the current value is null, use the default value that can be the label
     * or the default option name, the parameter generateValueUsingLabel
     * decides which of these two values will be used.
     * 2. If the default value is null, use the string Option.
     */

    exports.normalizeReference = normalizeReference;

    var normalizeValue = function normalizeValue(fields, currentField, generateValueUsingLabel) {
      var label = currentField.label,
          prevValue = currentField.value;
      var value = prevValue ? prevValue : getDefaultOptionValue(generateValueUsingLabel, label);

      if (!value) {
        value = Liferay.Language.get('option');
      }

      value = dedupValue(fields, value, currentField.id, generateValueUsingLabel);
      return (0, _dynamicDataMappingFormRenderer.normalizeFieldName)(value);
    };

    exports.normalizeValue = normalizeValue;

    var normalizeFields = function normalizeFields(fields, generateValueUsingLabel) {
      return fields.map(function (field, index) {
        var value = normalizeValue(fields, field, generateValueUsingLabel);
        return _objectSpread(_objectSpread({}, field), {}, {
          reference: normalizeReference(fields, _objectSpread(_objectSpread({}, field), {}, {
            value: value
          }), index),
          value: value
        });
      });
    };

    exports.normalizeFields = normalizeFields;
    //# sourceMappingURL=util.es.js.map
  }
});
//# sourceMappingURL=util.es.js.map