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/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 _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 ownKeys(object, enumerableOnly) {
      var keys = Object.keys(object);if (Object.getOwnPropertySymbols) {
        var symbols = Object.getOwnPropertySymbols(object);if (enumerableOnly) symbols = symbols.filter(function (sym) {
          return Object.getOwnPropertyDescriptor(object, sym).enumerable;
        });keys.push.apply(keys, symbols);
      }return keys;
    }

    function _objectSpread(target) {
      for (var i = 1; i < arguments.length; i++) {
        var source = arguments[i] != null ? arguments[i] : {};if (i % 2) {
          ownKeys(Object(source), true).forEach(function (key) {
            _defineProperty(target, key, source[key]);
          });
        } else if (Object.getOwnPropertyDescriptors) {
          Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
        } else {
          ownKeys(Object(source)).forEach(function (key) {
            Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
          });
        }
      }return target;
    }

    function _defineProperty(obj, key, value) {
      if (key in obj) {
        Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true });
      } else {
        obj[key] = value;
      }return 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", {
          "aria-hidden": "true",
          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,
          id = _ref2.id,
          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 = "".concat(id !== null && id !== void 0 ? id : 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');

      var showFor = type === 'text' || type === 'numeric' || type === 'select';
      var readFieldDetails = !showFor || type === 'select';

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

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

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

      var hasFieldDetails = fieldDetails && readFieldDetails;

      var accessibleProps = _objectSpread(_objectSpread(_objectSpread({}, hasFieldDetails && {
        'aria-labelledby': fieldDetailsId
      }), showFor && {
        htmlFor: id !== null && id !== void 0 ? id : name
      }), readFieldDetails && {
        tabIndex: 0
      });

      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", _extends({}, accessibleProps, {
          className: "lfr-ddm-legend"
        }), 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", _extends({}, accessibleProps, {
          className: (0, _classnames.default)({
            'ddm-empty': !showLabel && !required,
            'ddm-label': showLabel || required
          })
        }), 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": readFieldDetails,
          className: "form-text",
          id: "".concat(id !== null && id !== void 0 ? id : name, "_fieldHelp")
        }, tip), hasError && /*#__PURE__*/_react.default.createElement("div", {
          className: "form-feedback-group"
        }, /*#__PURE__*/_react.default.createElement("div", {
          "aria-hidden": readFieldDetails,
          className: "form-feedback-item",
          id: "".concat(id !== null && id !== void 0 ? id : name, "_fieldError"),
          role: "alert"
        }, errorMessage)), hasFieldDetails && /*#__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