Liferay.Loader.define("dynamic-data-mapping-form-field-type@5.0.68/FieldBase/ReactFieldBase.es", ['module', 'exports', 'require', './FieldBase.scss', '@frontend-taglib-clay$clayui/button', '@frontend-taglib-clay$clayui/icon', '@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 _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);
    }

    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 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"
        })), 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
        })))
      );
    };

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

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

      var fieldDetails = '';
      var fieldDetailsId = 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) {
        fieldDetails += label + '<br>';
      } else {
        parentDivTabIndex = 0;
        parentDivAriaLabelledby = fieldDetailsId;
      }

      if (tip) {
        fieldDetails += tip + '<br>';
      }

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

      if (hasError) {
        fieldDetails += 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, {
          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, {
          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, {
          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, {
          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