Liferay.Loader.define("dynamic-data-mapping-form-field-type@5.0.68/LocalizableText/LocalizableText.es", ['module', 'exports', 'require', '@frontend-taglib-clay$clayui/button', '@frontend-taglib-clay$clayui/drop-down', '@frontend-taglib-clay$clayui/form', '@frontend-taglib-clay$clayui/icon', '@frontend-taglib-clay$clayui/label', '@frontend-taglib-clay$clayui/layout', 'frontend-js-react-web$classnames', 'frontend-js-react-web$react', '../FieldBase/ReactFieldBase.es', './InputComponent.es', './transform.es'], 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 _button = _interopRequireDefault(require("@frontend-taglib-clay$clayui/button"));

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

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

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

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

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

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

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

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

    var _InputComponent = _interopRequireDefault(require("./InputComponent.es"));

    var _transform = require("./transform.es");

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

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

    var INITIAL_DEFAULT_LOCALE = {
      icon: themeDisplay.getDefaultLanguageId(),
      localeId: themeDisplay.getDefaultLanguageId()
    };
    var INITIAL_EDITING_LOCALE = {
      icon: (0, _transform.normalizeLocaleId)(themeDisplay.getDefaultLanguageId()),
      localeId: themeDisplay.getDefaultLanguageId()
    };
    var DropdownTrigger = /*#__PURE__*/(0, _react.forwardRef)(function (_ref, ref) {
      var editingLocale = _ref.editingLocale,
          otherProps = _objectWithoutProperties(_ref, ["editingLocale"]);

      return (/*#__PURE__*/_react.default.createElement(_button.default, _extends({
          "aria-expanded": "false",
          "aria-haspopup": "true",
          className: "dropdown-toggle",
          "data-testid": "triggerButton",
          displayType: "secondary",
          monospaced: true,
          ref: ref
        }, otherProps), /*#__PURE__*/_react.default.createElement("span", {
          className: "inline-item"
        }, /*#__PURE__*/_react.default.createElement(_icon.default, {
          symbol: editingLocale.icon
        })), /*#__PURE__*/_react.default.createElement("span", {
          className: "btn-section",
          "data-testid": "triggerText"
        }, editingLocale.icon))
      );
    });

    var AvailableLocaleLabel = function AvailableLocaleLabel(_ref2) {
      var isDefault = _ref2.isDefault,
          isTranslated = _ref2.isTranslated;
      var labelText = isDefault ? 'default' : isTranslated ? 'translated' : 'not-translated';
      return (/*#__PURE__*/_react.default.createElement(_label.default, {
          displayType: (0, _classnames.default)({
            info: isDefault,
            success: isTranslated,
            warning: !isDefault && !isTranslated
          })
        }, Liferay.Language.get(labelText))
      );
    };

    var LocalesDropdown = function LocalesDropdown(_ref3) {
      var availableLocales = _ref3.availableLocales,
          editingLocale = _ref3.editingLocale,
          _ref3$onLanguageClick = _ref3.onLanguageClicked,
          onLanguageClicked = _ref3$onLanguageClick === void 0 ? function () {} : _ref3$onLanguageClick;

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

      return (/*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement(_dropDown.default, {
          active: dropdownActive,
          onActiveChange: setDropdownActive,
          trigger: /*#__PURE__*/_react.default.createElement(DropdownTrigger, {
            editingLocale: editingLocale
          })
        }, /*#__PURE__*/_react.default.createElement(_dropDown.default.ItemList, null, availableLocales.map(function (_ref4) {
          var displayName = _ref4.displayName,
              icon = _ref4.icon,
              isDefault = _ref4.isDefault,
              isTranslated = _ref4.isTranslated,
              localeId = _ref4.localeId;
          return (/*#__PURE__*/_react.default.createElement(_dropDown.default.Item, {
              className: "custom-dropdown-item-row",
              "data-testid": "availableLocalesDropdown".concat(localeId),
              key: localeId,
              onClick: function onClick(event) {
                onLanguageClicked({
                  event: event,
                  localeId: localeId
                });
                setDropdownActive(false);
              }
            }, /*#__PURE__*/_react.default.createElement(_layout.default.ContentRow, {
              containerElement: "span"
            }, /*#__PURE__*/_react.default.createElement(_layout.default.ContentCol, {
              containerElement: "span",
              expand: true
            }, /*#__PURE__*/_react.default.createElement(_layout.default.ContentSection, {
              containerElement: "span"
            }, /*#__PURE__*/_react.default.createElement("span", {
              className: "inline-item inline-item-before"
            }, /*#__PURE__*/_react.default.createElement(_icon.default, {
              symbol: icon
            })), displayName)), /*#__PURE__*/_react.default.createElement(_layout.default.ContentCol, {
              containerElement: "span"
            }, /*#__PURE__*/_react.default.createElement(AvailableLocaleLabel, {
              isDefault: isDefault,
              isTranslated: isTranslated
            }))))
          );
        }))))
      );
    };

    var LocalizableText = function LocalizableText(_ref5) {
      var _ref5$availableLocale = _ref5.availableLocales,
          availableLocales = _ref5$availableLocale === void 0 ? [] : _ref5$availableLocale,
          _ref5$defaultLocale = _ref5.defaultLocale,
          defaultLocale = _ref5$defaultLocale === void 0 ? INITIAL_DEFAULT_LOCALE : _ref5$defaultLocale,
          _ref5$displayStyle = _ref5.displayStyle,
          displayStyle = _ref5$displayStyle === void 0 ? 'singleline' : _ref5$displayStyle,
          _ref5$editingLocale = _ref5.editingLocale,
          editingLocale = _ref5$editingLocale === void 0 ? INITIAL_EDITING_LOCALE : _ref5$editingLocale,
          id = _ref5.id,
          name = _ref5.name,
          onFieldBlurred = _ref5.onFieldBlurred,
          _ref5$onFieldChanged = _ref5.onFieldChanged,
          _onFieldChanged = _ref5$onFieldChanged === void 0 ? function () {} : _ref5$onFieldChanged,
          onFieldFocused = _ref5.onFieldFocused,
          _ref5$placeholder = _ref5.placeholder,
          placeholder = _ref5$placeholder === void 0 ? '' : _ref5$placeholder,
          _ref5$predefinedValue = _ref5.predefinedValue,
          predefinedValue = _ref5$predefinedValue === void 0 ? '' : _ref5$predefinedValue,
          readOnly = _ref5.readOnly,
          value = _ref5.value;

      var _useState3 = (0, _react.useState)(editingLocale),
          _useState4 = _slicedToArray(_useState3, 2),
          currentEditingLocale = _useState4[0],
          setCurrentEditingLocale = _useState4[1];

      var _useState5 = (0, _react.useState)(value),
          _useState6 = _slicedToArray(_useState5, 2),
          currentValue = _useState6[0],
          setCurrentValue = _useState6[1];

      var _useState7 = (0, _react.useState)((0, _transform.getInitialInternalValue)({
        editingLocale: currentEditingLocale,
        value: value
      })),
          _useState8 = _slicedToArray(_useState7, 2),
          currentInternalValue = _useState8[0],
          setCurrentInternalValue = _useState8[1];

      var inputValue = currentInternalValue ? currentInternalValue : predefinedValue;
      return (/*#__PURE__*/_react.default.createElement(_form.ClayInput.Group, null, /*#__PURE__*/_react.default.createElement(_InputComponent.default, {
          displayStyle: displayStyle,
          id: id,
          inputValue: inputValue,
          name: name,
          onFieldBlurred: onFieldBlurred,
          onFieldChanged: function onFieldChanged(event) {
            var target = event.target;
            var valueJSON = (0, _transform.convertValueToJSON)(currentValue);
            var newValue = JSON.stringify(_objectSpread(_objectSpread({}, valueJSON), {}, _defineProperty({}, currentEditingLocale.localeId, target.value)));
            setCurrentValue(newValue);
            setCurrentInternalValue(target.value);

            _onFieldChanged({
              event: event,
              value: newValue
            });
          },
          onFieldFocused: onFieldFocused,
          placeholder: placeholder,
          readOnly: readOnly
        }), /*#__PURE__*/_react.default.createElement("input", {
          id: id,
          name: name,
          type: "hidden",
          value: currentValue || ''
        }), /*#__PURE__*/_react.default.createElement(_form.ClayInput.GroupItem, {
          className: "liferay-ddm-form-field-localizable-text",
          shrink: true
        }, /*#__PURE__*/_react.default.createElement(LocalesDropdown, {
          availableLocales: availableLocales,
          editingLocale: currentEditingLocale,
          onLanguageClicked: function onLanguageClicked(_ref6) {
            var localeId = _ref6.localeId;
            var newEditingLocale = availableLocales.find(function (availableLocale) {
              return availableLocale.localeId === localeId;
            });
            setCurrentEditingLocale(_objectSpread(_objectSpread({}, newEditingLocale), {}, {
              icon: (0, _transform.normalizeLocaleId)(newEditingLocale.localeId)
            }));
            setCurrentInternalValue((0, _transform.getEditingValue)({
              defaultLocale: defaultLocale,
              editingLocale: newEditingLocale,
              value: currentValue
            }));
          }
        })))
      );
    };

    var Main = function Main(_ref7) {
      var availableLocales = _ref7.availableLocales,
          defaultLocale = _ref7.defaultLocale,
          displayStyle = _ref7.displayStyle,
          editingLocale = _ref7.editingLocale,
          id = _ref7.id,
          name = _ref7.name,
          onBlur = _ref7.onBlur,
          onChange = _ref7.onChange,
          onFocus = _ref7.onFocus,
          placeholder = _ref7.placeholder,
          predefinedValue = _ref7.predefinedValue,
          readOnly = _ref7.readOnly,
          _ref7$value = _ref7.value,
          value = _ref7$value === void 0 ? {} : _ref7$value,
          otherProps = _objectWithoutProperties(_ref7, ["availableLocales", "defaultLocale", "displayStyle", "editingLocale", "id", "name", "onBlur", "onChange", "onFocus", "placeholder", "predefinedValue", "readOnly", "value"]);

      return (/*#__PURE__*/_react.default.createElement(_ReactFieldBase.FieldBase, _extends({}, otherProps, {
          id: id,
          name: name,
          readOnly: readOnly
        }), /*#__PURE__*/_react.default.createElement(LocalizableText, _extends({}, (0, _transform.transformAvailableLocalesAndValue)({
          availableLocales: availableLocales,
          defaultLocale: defaultLocale,
          value: value
        }), {
          displayStyle: displayStyle,
          editingLocale: editingLocale,
          id: id,
          name: name,
          onFieldBlurred: onBlur,
          onFieldChanged: function onFieldChanged(_ref8) {
            var event = _ref8.event,
                value = _ref8.value;
            return onChange(event, value);
          },
          onFieldFocused: onFocus,
          placeholder: placeholder,
          predefinedValue: predefinedValue,
          readOnly: readOnly
        })))
      );
    };

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