Liferay.Loader.define("dynamic-data-mapping-form-field-type@5.0.83/Text/Text.es", ['module', 'exports', 'require', '@frontend-taglib-clay$clayui/autocomplete', '@frontend-taglib-clay$clayui/drop-down', '@frontend-taglib-clay$clayui/form', 'dynamic-data-mapping-form-renderer', 'frontend-js-react-web', 'frontend-js-react-web$react', '../FieldBase/ReactFieldBase.es', '../hooks/useSyncValue.es', '../util/fieldPopoverSettings'], function (module, exports, require) {
  var define = undefined;
  var global = window;
  {
    function _typeof(obj) {
      "@babel/helpers - typeof";
      if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
        _typeof = function _typeof(obj) {
          return typeof obj;
        };
      } else {
        _typeof = function _typeof(obj) {
          return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
        };
      }return _typeof(obj);
    }

    Object.defineProperty(exports, "__esModule", {
      value: true
    });
    exports.default = void 0;

    var _autocomplete = _interopRequireDefault(require("@frontend-taglib-clay$clayui/autocomplete"));

    var _dropDown = _interopRequireDefault(require("@frontend-taglib-clay$clayui/drop-down"));

    var _form = require("@frontend-taglib-clay$clayui/form");

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

    var _frontendJsReactWeb = require("frontend-js-react-web");

    var _react = _interopRequireWildcard(require("frontend-js-react-web$react"));

    var _ReactFieldBase = require("../FieldBase/ReactFieldBase.es");

    var _useSyncValue7 = require("../hooks/useSyncValue.es");

    var _fieldPopoverSettings = _interopRequireDefault(require("../util/fieldPopoverSettings"));

    function _getRequireWildcardCache() {
      if (typeof WeakMap !== "function") return null;var cache = new WeakMap();_getRequireWildcardCache = function _getRequireWildcardCache() {
        return cache;
      };return cache;
    }

    function _interopRequireWildcard(obj) {
      if (obj && obj.__esModule) {
        return obj;
      }if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") {
        return { default: obj };
      }var cache = _getRequireWildcardCache();if (cache && cache.has(obj)) {
        return cache.get(obj);
      }var newObj = {};var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;for (var key in obj) {
        if (Object.prototype.hasOwnProperty.call(obj, key)) {
          var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;if (desc && (desc.get || desc.set)) {
            Object.defineProperty(newObj, key, desc);
          } else {
            newObj[key] = obj[key];
          }
        }
      }newObj.default = obj;if (cache) {
        cache.set(obj, newObj);
      }return newObj;
    }

    function _interopRequireDefault(obj) {
      return obj && obj.__esModule ? obj : { default: obj };
    }

    function _objectWithoutProperties(source, excluded) {
      if (source == null) return {};var target = _objectWithoutPropertiesLoose(source, excluded);var key, i;if (Object.getOwnPropertySymbols) {
        var sourceSymbolKeys = Object.getOwnPropertySymbols(source);for (i = 0; i < sourceSymbolKeys.length; i++) {
          key = sourceSymbolKeys[i];if (excluded.indexOf(key) >= 0) continue;if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;target[key] = source[key];
        }
      }return target;
    }

    function _objectWithoutPropertiesLoose(source, excluded) {
      if (source == null) return {};var target = {};var sourceKeys = Object.keys(source);var key, i;for (i = 0; i < sourceKeys.length; i++) {
        key = sourceKeys[i];if (excluded.indexOf(key) >= 0) continue;target[key] = source[key];
      }return target;
    }

    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 _extends() {
      _extends = Object.assign || function (target) {
        for (var i = 1; i < arguments.length; i++) {
          var source = arguments[i];for (var key in source) {
            if (Object.prototype.hasOwnProperty.call(source, key)) {
              target[key] = source[key];
            }
          }
        }return target;
      };return _extends.apply(this, arguments);
    }

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

    var Text = function Text(_ref) {
      var defaultLanguageId = _ref.defaultLanguageId,
          disabled = _ref.disabled,
          editingLanguageId = _ref.editingLanguageId,
          fieldName = _ref.fieldName,
          htmlAutocompleteAttribute = _ref.htmlAutocompleteAttribute,
          id = _ref.id,
          invalidCharacters = _ref.invalidCharacters,
          localizable = _ref.localizable,
          localizedValue = _ref.localizedValue,
          maxLength = _ref.maxLength,
          name = _ref.name,
          _onBlur = _ref.onBlur,
          onChange = _ref.onChange,
          onFocus = _ref.onFocus,
          placeholder = _ref.placeholder,
          shouldUpdateValue = _ref.shouldUpdateValue,
          syncDelay = _ref.syncDelay,
          initialValue = _ref.value;

      var _useSyncValue = (0, _useSyncValue7.useSyncValue)(initialValue, syncDelay),
          _useSyncValue2 = _slicedToArray(_useSyncValue, 2),
          value = _useSyncValue2[0],
          setValue = _useSyncValue2[1];

      var inputRef = (0, _react.useRef)(null);
      var prevEditingLanguageId = (0, _frontendJsReactWeb.usePrevious)(editingLanguageId);
      (0, _react.useEffect)(function () {
        if (prevEditingLanguageId !== editingLanguageId && localizable) {
          var newValue = localizedValue[editingLanguageId] !== undefined ? localizedValue[editingLanguageId] : localizedValue[defaultLanguageId];
          setValue(newValue);
        }
      }, [defaultLanguageId, editingLanguageId, localizable, localizedValue, prevEditingLanguageId, setValue]);
      (0, _react.useEffect)(function () {
        if (fieldName === 'fieldReference' && inputRef.current && inputRef.current.value !== initialValue && (inputRef.current.value === '' || shouldUpdateValue)) {
          setValue(initialValue);
          onChange({
            target: {
              value: initialValue
            }
          });
        }
      }, [initialValue, inputRef, fieldName, onChange, setValue, shouldUpdateValue]);

      var handleChangeInput = function handleChangeInput(event) {
        var value = event.target.value;

        if (fieldName === 'fieldReference' || fieldName === 'name') {
          event.target.value = (0, _dynamicDataMappingFormRenderer.normalizeFieldName)(value);
        } else if (invalidCharacters) {
          var regex = new RegExp(invalidCharacters, 'g');
          event.target.value = value.replace(regex, '');
        }

        onChange(event);
        setValue(event.target.value);
      };

      return (/*#__PURE__*/_react.default.createElement(_form.ClayInput, _extends({}, htmlAutocompleteAttribute && {
          autoComplete: htmlAutocompleteAttribute
        }, maxLength && {
          maxLength: maxLength
        }, {
          "aria-labelledby": id,
          className: "ddm-field-text",
          disabled: disabled,
          id: id,
          name: name,
          onBlur: function onBlur(event) {
            _onBlur(event);

            handleChangeInput(event);
          },
          onChange: handleChangeInput,
          onFocus: onFocus,
          placeholder: placeholder,
          ref: inputRef,
          type: "text",
          value: value
        }))
      );
    };

    var Textarea = function Textarea(_ref2) {
      var disabled = _ref2.disabled,
          htmlAutocompleteAttribute = _ref2.htmlAutocompleteAttribute,
          id = _ref2.id,
          name = _ref2.name,
          onBlur = _ref2.onBlur,
          _onChange = _ref2.onChange,
          onFocus = _ref2.onFocus,
          placeholder = _ref2.placeholder,
          syncDelay = _ref2.syncDelay,
          initialValue = _ref2.value;

      var _useSyncValue3 = (0, _useSyncValue7.useSyncValue)(initialValue, syncDelay),
          _useSyncValue4 = _slicedToArray(_useSyncValue3, 2),
          value = _useSyncValue4[0],
          setValue = _useSyncValue4[1];

      return (/*#__PURE__*/_react.default.createElement("textarea", _extends({}, htmlAutocompleteAttribute && {
          autoComplete: htmlAutocompleteAttribute
        }, {
          "aria-labelledby": id,
          className: "ddm-field-text form-control",
          disabled: disabled,
          id: id,
          name: name,
          onBlur: onBlur,
          onChange: function onChange(event) {
            setValue(event.target.value);

            _onChange(event);
          },
          onFocus: onFocus,
          placeholder: placeholder,
          type: "text",
          value: value
        }))
      );
    };

    var Autocomplete = function Autocomplete(_ref3) {
      var disabled = _ref3.disabled,
          htmlAutocompleteAttribute = _ref3.htmlAutocompleteAttribute,
          id = _ref3.id,
          name = _ref3.name,
          onBlur = _ref3.onBlur,
          _onChange2 = _ref3.onChange,
          onFocus = _ref3.onFocus,
          options = _ref3.options,
          placeholder = _ref3.placeholder,
          syncDelay = _ref3.syncDelay,
          initialValue = _ref3.value;

      var _useSyncValue5 = (0, _useSyncValue7.useSyncValue)(initialValue, syncDelay),
          _useSyncValue6 = _slicedToArray(_useSyncValue5, 2),
          value = _useSyncValue6[0],
          setValue = _useSyncValue6[1];

      var _useState = (0, _react.useState)(false),
          _useState2 = _slicedToArray(_useState, 2),
          visible = _useState2[0],
          setVisible = _useState2[1];

      var inputRef = (0, _react.useRef)(null);
      var itemListRef = (0, _react.useRef)(null);

      var escapeChars = function escapeChars(string) {
        return string.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&');
      };

      var filteredItems = options.filter(function (item) {
        return item && item.match(escapeChars(value));
      });
      (0, _react.useEffect)(function () {
        if (filteredItems.length === 1 && filteredItems.includes(value)) {
          setVisible(false);
        } else {
          var ddmPageContainerLayout = inputRef.current.closest('.ddm-page-container-layout');

          if (ddmPageContainerLayout && ddmPageContainerLayout.classList.contains('hide')) {
            setVisible(false);
          } else {
            setVisible(!!value);
          }
        }
      }, [filteredItems, value]);

      var handleFocus = function handleFocus(event, direction) {
        var target = event.target;
        var focusabledElements = event.currentTarget.querySelectorAll('button');

        var targetIndex = _toConsumableArray(focusabledElements).findIndex(function (current) {
          return current === target;
        });

        var nextElement;

        if (direction) {
          nextElement = focusabledElements[targetIndex - 1];
        } else {
          nextElement = focusabledElements[targetIndex + 1];
        }

        if (nextElement) {
          event.preventDefault();
          event.stopPropagation();
          nextElement.focus();
        } else if (targetIndex === 0 && direction) {
          event.preventDefault();
          event.stopPropagation();
          inputRef.current.focus();
        }
      };

      return (/*#__PURE__*/_react.default.createElement(_autocomplete.default, null, /*#__PURE__*/_react.default.createElement(_autocomplete.default.Input, _extends({}, htmlAutocompleteAttribute && {
          autoComplete: htmlAutocompleteAttribute
        }, {
          "aria-labelledby": id,
          disabled: disabled,
          id: id,
          name: name,
          onBlur: onBlur,
          onChange: function onChange(event) {
            setValue(event.target.value);

            _onChange2(event);
          },
          onFocus: onFocus,
          onKeyDown: function onKeyDown(event) {
            if ((event.key === 'Tab' || event.key === 'ArrowDown') && !event.shiftKey && filteredItems.length > 0 && visible) {
              event.preventDefault();
              event.stopPropagation();
              var firstElement = itemListRef.current.querySelector('button');
              firstElement.focus();
            }
          },
          placeholder: placeholder,
          ref: inputRef,
          value: value
        })), /*#__PURE__*/_react.default.createElement(_autocomplete.default.DropDown, {
          active: visible && !disabled,
          onSetActive: setVisible
        }, /*#__PURE__*/_react.default.createElement("ul", {
          className: "list-unstyled",
          onKeyDown: function onKeyDown(event) {
            switch (event.key) {
              case 'ArrowDown':
                handleFocus(event, false);
                break;

              case 'ArrowUp':
                handleFocus(event, true);
                break;

              case 'Tab':
                handleFocus(event, event.shiftKey);
                break;

              default:
                break;
            }
          },
          ref: itemListRef
        }, filteredItems.length === 0 && /*#__PURE__*/_react.default.createElement(_dropDown.default.Item, {
          className: "disabled"
        }, Liferay.Language.get('no-results-were-found')), filteredItems.map(function (label) {
          return (/*#__PURE__*/_react.default.createElement(_autocomplete.default.Item, {
              key: label,
              match: value,
              onClick: function onClick() {
                setValue(label);

                _onChange2({
                  target: {
                    value: label
                  }
                });
              },
              value: label
            })
          );
        }))))
      );
    };

    var DISPLAY_STYLE = {
      autocomplete: Autocomplete,
      multiline: Textarea,
      singleline: Text
    };

    var Main = function Main(_ref4) {
      var autocomplete = _ref4.autocomplete,
          autocompleteEnabled = _ref4.autocompleteEnabled,
          defaultLanguageId = _ref4.defaultLanguageId,
          _ref4$displayStyle = _ref4.displayStyle,
          displayStyle = _ref4$displayStyle === void 0 ? 'singleline' : _ref4$displayStyle,
          editingLanguageId = _ref4.editingLanguageId,
          fieldName = _ref4.fieldName,
          htmlAutocompleteAttribute = _ref4.htmlAutocompleteAttribute,
          invalidCharacters = _ref4.invalidCharacters,
          id = _ref4.id,
          localizable = _ref4.localizable,
          _ref4$localizedValue = _ref4.localizedValue,
          localizedValue = _ref4$localizedValue === void 0 ? {} : _ref4$localizedValue,
          maxLength = _ref4.maxLength,
          name = _ref4.name,
          onBlur = _ref4.onBlur,
          onChange = _ref4.onChange,
          onFocus = _ref4.onFocus,
          _ref4$options = _ref4.options,
          options = _ref4$options === void 0 ? [] : _ref4$options,
          placeholder = _ref4.placeholder,
          _ref4$predefinedValue = _ref4.predefinedValue,
          predefinedValue = _ref4$predefinedValue === void 0 ? '' : _ref4$predefinedValue,
          readOnly = _ref4.readOnly,
          _ref4$shouldUpdateVal = _ref4.shouldUpdateValue,
          shouldUpdateValue = _ref4$shouldUpdateVal === void 0 ? false : _ref4$shouldUpdateVal,
          _ref4$syncDelay = _ref4.syncDelay,
          syncDelay = _ref4$syncDelay === void 0 ? true : _ref4$syncDelay,
          value = _ref4.value,
          otherProps = _objectWithoutProperties(_ref4, ["autocomplete", "autocompleteEnabled", "defaultLanguageId", "displayStyle", "editingLanguageId", "fieldName", "htmlAutocompleteAttribute", "invalidCharacters", "id", "localizable", "localizedValue", "maxLength", "name", "onBlur", "onChange", "onFocus", "options", "placeholder", "predefinedValue", "readOnly", "shouldUpdateValue", "syncDelay", "value"]);

      var optionsMemo = (0, _react.useMemo)(function () {
        return options.map(function (option) {
          return option.label;
        });
      }, [options]);
      var Component = DISPLAY_STYLE[autocomplete || autocompleteEnabled ? 'autocomplete' : displayStyle ? displayStyle : "singleline"];
      var fieldDetailsId = id ? id + '_fieldDetails' : name + '_fieldDetails';
      return (/*#__PURE__*/_react.default.createElement(_ReactFieldBase.FieldBase, _extends({}, otherProps, {
          id: id,
          localizedValue: localizedValue,
          name: name,
          popover: _fieldPopoverSettings.default[fieldName],
          readOnly: readOnly
        }), /*#__PURE__*/_react.default.createElement(Component, {
          defaultLanguageId: defaultLanguageId,
          disabled: readOnly,
          editingLanguageId: editingLanguageId,
          fieldName: fieldName,
          htmlAutocompleteAttribute: htmlAutocompleteAttribute,
          id: fieldDetailsId,
          invalidCharacters: invalidCharacters,
          localizable: localizable,
          localizedValue: localizedValue,
          maxLength: maxLength,
          name: name,
          onBlur: onBlur,
          onChange: onChange,
          onFocus: onFocus,
          options: optionsMemo,
          placeholder: placeholder,
          shouldUpdateValue: shouldUpdateValue,
          syncDelay: syncDelay,
          value: value ? value : predefinedValue
        }))
      );
    };

    Main.displayName = 'Text';
    var _default = Main;
    exports.default = _default;
    //# sourceMappingURL=Text.es.js.map
  }
});
//# sourceMappingURL=Text.es.js.map