Liferay.Loader.define("layout-content-page-editor-web@2.0.157/page_editor/app/utils/getSelectedField", ['module', 'exports', 'require'], function (module, exports, require) {
  var define = undefined;
  var global = window;
  {
    Object.defineProperty(exports, "__esModule", {
      value: true
    });
    exports.default = getSelectedField;

    /**
     * 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 cache = {};
    /**
     * Returns the selected field from the given key or return null.
     * This also check try to add a prefix to look for a specific field since older
     * Liferay versions didn't prefix ddm structure and vocabulary fields.
     *
     * @param {object} data
     * @param {Array}: data.fields Array of available fields for a given info type.
     * @param {string}: [data.mappingFieldsKey] Key used to store the mapping fields.
     * @param {string}: data.value Field key.
     * @return {object}
     */

    function getSelectedField(_ref) {
      var fieldSets = _ref.fieldSets,
          mappingFieldsKey = _ref.mappingFieldsKey,
          value = _ref.value;

      if (!value || !(fieldSets === null || fieldSets === void 0 ? void 0 : fieldSets.length)) {
        return null;
      }

      var cacheKey = "".concat(mappingFieldsKey).concat(value);

      if (mappingFieldsKey && cache[cacheKey]) {
        return cache[cacheKey];
      }

      var flattenField = fieldSets.flatMap(function (fieldSet) {
        return fieldSet.fields;
      });
      var selectedField = flattenField.find(function (field) {
        return field.key === value;
      }) || flattenField.find(function (field) {
        return field.name === value;
      });

      if (selectedField) {
        if (mappingFieldsKey) {
          cache[cacheKey] = selectedField;
        }

        return selectedField;
      }

      return null;
    }
    //# sourceMappingURL=getSelectedField.js.map
  }
});
//# sourceMappingURL=getSelectedField.js.map