Liferay.Loader.define("dynamic-data-mapping-form-field-type@5.0.83/FieldBase/ReactFieldBase.es", ['module', 'exports', 'require', './FieldBase.scss', '@frontend-taglib-clay$clayui/button', '@frontend-taglib-clay$clayui/icon', '@frontend-taglib-clay$clayui/popover', '@frontend-taglib-clay$clayui/tooltip', 'frontend-js-react-web$classnames', 'dynamic-data-mapping-form-renderer', 'dynamic-data-mapping-form-field-type$moment', 'frontend-js-react-web$react'], function (module, exports, require) {
  var define = undefined;
  var global = window;
  {
    Object.defineProperty(exports, "__esModule", {
      value: true
    });
    exports.FieldBase = FieldBase;

    require("./FieldBase.scss");

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

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

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

    var _tooltip = require("@frontend-taglib-clay$clayui/tooltip");

    var _classnames = _interopRequireDefault(require("frontend-js-react-web$classnames"));

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

    var _moment = _interopRequireDefault(require("dynamic-data-mapping-form-field-type$moment"));

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

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

    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 convertInputValue = function convertInputValue(fieldType, value) {
      if (fieldType === 'date') {
        var date = (0, _moment.default)(value).toDate();

        if ((0, _moment.default)(date).isValid()) {
          return (0, _moment.default)(date).format('YYYY-MM-DD');
        }
      } else if (fieldType === 'document_library' || fieldType === 'grid' || fieldType === 'image') {
        if (Object.keys(value).length === 0) {
          return '';
        }

        return JSON.stringify(value);
      }

      return value;
    };

    var getDefaultRows = function getDefaultRows(nestedFields) {
      return nestedFields.map(function (nestedField) {
        return {
          columns: [{
            fields: [nestedField],
            size: 12
          }]
        };
      });
    };

    var FieldProperties = function FieldProperties(_ref) {
      var popover = _ref.popover,
          required = _ref.required,
          tooltip = _ref.tooltip;
      return (/*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, required && /*#__PURE__*/_react.default.createElement("span", {
          className: "ddm-label-required reference-mark"
        }, /*#__PURE__*/_react.default.createElement(_icon.default, {
          symbol: "asterisk"
        })), popover && /*#__PURE__*/_react.default.createElement(Popover, popover), !popover && tooltip && /*#__PURE__*/_react.default.createElement("span", {
          className: "ddm-tooltip"
        }, /*#__PURE__*/_react.default.createElement(_icon.default, {
          "data-tooltip-align": "right",
          symbol: "question-circle-full",
          title: tooltip
        })))
      );
    };

    var Popover = function Popover(_ref2) {
      var alignPosition = _ref2.alignPosition,
          closeOnClickOutside = _ref2.closeOnClickOutside,
          content = _ref2.content,
          header = _ref2.header,
          image = _ref2.image;

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

      var popoverElement = (0, _react.useRef)();
      var triggerElement = (0, _react.useRef)();

      (0, _react.useEffect)(function () {
        if (closeOnClickOutside) {
          var handleClickOutside = function handleClickOutside(event) {
            var pathElement = triggerElement.current.querySelector('g > path');

            if (popoverElement.current && pathElement !== event.target && !popoverElement.current.contains(event.target)) {
              setIsPopoverVisible(false);
            }
          };

          document.addEventListener('mousedown', handleClickOutside);
          return function () {
            document.removeEventListener('mousedown', handleClickOutside);
          };
        }
      }, [closeOnClickOutside]);
      return (/*#__PURE__*/_react.default.createElement(_popover.default, {
          alignPosition: alignPosition,
          "data-testid": "clayPopover",
          disableScroll: true,
          header: header,
          onShowChange: setIsPopoverVisible,
          ref: popoverElement,
          show: isPopoverVisible,
          style: {
            maxWidth: 256
          },
          trigger: /*#__PURE__*/_react.default.createElement("span", {
            className: "ddm-tooltip"
          }, /*#__PURE__*/_react.default.createElement(_icon.default, {
            onClick: function onClick() {
              return setIsPopoverVisible(!isPopoverVisible);
            },
            ref: triggerElement,
            symbol: "question-circle-full"
          }))
        }, /*#__PURE__*/_react.default.createElement("p", {
          className: "mb-4",
          dangerouslySetInnerHTML: {
            __html: content
          }
        }), image && /*#__PURE__*/_react.default.createElement("img", {
          alt: image.alt,
          height: image.height,
          src: image.src,
          width: image.width
        }))
      );
    };

    function FieldBase(_ref3) {
      var children = _ref3.children,
          displayErrors = _ref3.displayErrors,
          errorMessage = _ref3.errorMessage,
          id = _ref3.id,
          label = _ref3.label,
          _ref3$localizedValue = _ref3.localizedValue,
          localizedValue = _ref3$localizedValue === void 0 ? {} : _ref3$localizedValue,
          name = _ref3.name,
          nestedFields = _ref3.nestedFields,
          onClick = _ref3.onClick,
          _ref3$overMaximumRepe = _ref3.overMaximumRepetitionsLimit,
          overMaximumRepetitionsLimit = _ref3$overMaximumRepe === void 0 ? false : _ref3$overMaximumRepe,
          popover = _ref3.popover,
          readOnly = _ref3.readOnly,
          repeatable = _ref3.repeatable,
          required = _ref3.required,
          _ref3$showLabel = _ref3.showLabel,
          showLabel = _ref3$showLabel === void 0 ? true : _ref3$showLabel,
          style = _ref3.style,
          text = _ref3.text,
          tip = _ref3.tip,
          tooltip = _ref3.tooltip,
          type = _ref3.type,
          valid = _ref3.valid,
          visible = _ref3.visible;

      var _usePage = (0, _dynamicDataMappingFormRenderer.usePage)(),
          _usePage$editingLangu = _usePage.editingLanguageId,
          editingLanguageId = _usePage$editingLangu === void 0 ? themeDisplay.getLanguageId() : _usePage$editingLangu;

      var fieldDetails = '';
      var fieldDetailsId = id ? id + '_fieldDetails' : name + '_fieldDetails';
      var dispatch = (0, _dynamicDataMappingFormRenderer.useForm)();
      var hasError = displayErrors && errorMessage && !valid;
      var localizedValueArray = (0, _react.useMemo)(function () {
        var languageValues = [];

        if (!localizedValue) {
          return languageValues;
        }

        Object.keys(localizedValue).forEach(function (key) {
          if (key !== editingLanguageId && localizedValue[key] !== '') {
            languageValues.push({
              name: name.replace(editingLanguageId, key),
              value: localizedValue[key]
            });
          }
        });
        return languageValues;
      }, [localizedValue, editingLanguageId, name]);
      var parentDivAriaLabelledby;
      var parentDivTabIndex;
      var renderLabel = label && showLabel || required || tooltip || repeatable;
      var repeatedIndex = (0, _react.useMemo)(function () {
        return (0, _dynamicDataMappingFormRenderer.getRepeatedIndex)(name);
      }, [name]);
      var requiredText = Liferay.Language.get('required');


      if (!renderLabel) {
        parentDivTabIndex = 0;
        parentDivAriaLabelledby = fieldDetailsId;
      }

      if (tip) {
        fieldDetails += Liferay.Util.escape(tip) + '<br>';
      }

      if (text) {
        fieldDetails += (_typeof(text) === 'object' ? Liferay.Util.escape(text.content) : Liferay.Util.escape(text)) + '<br>';
      }

      if (hasError) {
        fieldDetails += Liferay.Util.escape(errorMessage);
      } else if (required) {
        fieldDetails += requiredText;
      }

      return (/*#__PURE__*/_react.default.createElement(_tooltip.ClayTooltipProvider, null, /*#__PURE__*/_react.default.createElement("div", {
          "aria-labelledby": parentDivAriaLabelledby,
          className: (0, _classnames.default)('form-group', {
            'has-error': hasError,
            hide: !visible
          }),
          "data-field-name": name,
          onClick: onClick,
          style: style,
          tabIndex: parentDivTabIndex
        }, repeatable && /*#__PURE__*/_react.default.createElement("div", {
          className: "lfr-ddm-form-field-repeatable-toolbar"
        }, repeatable && repeatedIndex > 0 && /*#__PURE__*/_react.default.createElement(_button.default, {
          "aria-label": Liferay.Util.sub(Liferay.Language.get('remove-duplicate-field'), label ? label : type),
          className: "ddm-form-field-repeatable-delete-button p-0",
          disabled: readOnly,
          onClick: function onClick() {
            return dispatch({
              payload: name,
              type: _dynamicDataMappingFormRenderer.EVENT_TYPES.FIELD_REMOVED
            });
          },
          small: true,
          title: Liferay.Language.get('remove'),
          type: "button"
        }, /*#__PURE__*/_react.default.createElement(_icon.default, {
          symbol: "hr"
        })), /*#__PURE__*/_react.default.createElement(_button.default, {
          "aria-label": Liferay.Util.sub(Liferay.Language.get('add-duplicate-field'), label ? label : type),
          className: (0, _classnames.default)('ddm-form-field-repeatable-add-button p-0', {
            hide: overMaximumRepetitionsLimit
          }),
          disabled: readOnly,
          onClick: function onClick() {
            return dispatch({
              payload: name,
              type: _dynamicDataMappingFormRenderer.EVENT_TYPES.FIELD_REPEATED
            });
          },
          small: true,
          title: Liferay.Language.get('duplicate'),
          type: "button"
        }, /*#__PURE__*/_react.default.createElement(_icon.default, {
          symbol: "plus"
        }))), renderLabel && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, type && (type === 'checkbox_multiple' || type === 'grid' || type === 'paragraph' || type === 'radio') ? /*#__PURE__*/_react.default.createElement("fieldset", null, /*#__PURE__*/_react.default.createElement("legend", {
          "aria-labelledby": fieldDetailsId,
          className: "lfr-ddm-legend",
          tabIndex: "0"
        }, label && showLabel && label, /*#__PURE__*/_react.default.createElement(FieldProperties, {
          popover: popover,
          required: required,
          tooltip: tooltip
        })), children) : /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement("label", {
          "aria-describedby": fieldDetailsId,
          className: (0, _classnames.default)({
            'ddm-empty': !showLabel && !required,
            'ddm-label': showLabel || required
          }),
          tabIndex: "0"
        }, label && showLabel && label, /*#__PURE__*/_react.default.createElement(FieldProperties, {
          popover: popover,
          required: required,
          tooltip: tooltip
        })), children)), !renderLabel && children, localizedValueArray.length > 0 && localizedValueArray.map(function (language) {
          return (/*#__PURE__*/_react.default.createElement("input", {
              key: language.name,
              name: language.name,
              type: "hidden",
              value: language.value ? convertInputValue(type, language.value) : ''
            })
          );
        }), tip && /*#__PURE__*/_react.default.createElement("span", {
          "aria-hidden": "true",
          className: "form-text"
        }, tip), hasError && /*#__PURE__*/_react.default.createElement("span", {
          className: "form-feedback-group"
        }, /*#__PURE__*/_react.default.createElement("div", {
          "aria-hidden": "true",
          className: "form-feedback-item"
        }, errorMessage)), fieldDetails && /*#__PURE__*/_react.default.createElement("span", {
          className: "sr-only",
          dangerouslySetInnerHTML: {
            __html: fieldDetails
          },
          id: fieldDetailsId
        }), nestedFields && /*#__PURE__*/_react.default.createElement(_dynamicDataMappingFormRenderer.Layout, {
          rows: getDefaultRows(nestedFields)
        })))
      );
    }
    //# sourceMappingURL=ReactFieldBase.es.js.map
  }
});
//# sourceMappingURL=ReactFieldBase.es.js.map