Liferay.Loader.define("layout-content-page-editor-web@2.0.166/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;

    /**
     * SPDX-FileCopyrightText: (c) 2000 Liferay, Inc. https://liferay.com
     * SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
     */
    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