Liferay.Loader.define("dynamic-data-mapping-form-field-type@5.0.73/ImagePicker/ImagePicker.es", ['module', 'exports', 'require', '@frontend-taglib-clay$clayui/button', '@frontend-taglib-clay$clayui/form', '@frontend-taglib-clay$clayui/modal', 'frontend-js-web', 'frontend-js-react-web$react', '../FieldBase/ReactFieldBase.es', '../hooks/useSyncValue.es'], function (module, exports, require) {
  var define = undefined;
  var global = window;
  {
    Object.defineProperty(exports, "__esModule", {
      value: true
    });
    exports.default = void 0;

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

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

    var _modal = _interopRequireWildcard(require("@frontend-taglib-clay$clayui/modal"));

    var _frontendJsWeb = require("frontend-js-web");

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

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

    var _useSyncValue3 = require("../hooks/useSyncValue.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 _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 _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 _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 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;
    }

    var defaultValue = {
      description: '',
      title: '',
      url: ''
    };

    var ImagePicker = function ImagePicker(_ref) {
      var id = _ref.id,
          inputValue = _ref.inputValue,
          itemSelectorURL = _ref.itemSelectorURL,
          name = _ref.name,
          onClearClick = _ref.onClearClick,
          onDescriptionChange = _ref.onDescriptionChange,
          onFieldChanged = _ref.onFieldChanged,
          portletNamespace = _ref.portletNamespace,
          readOnly = _ref.readOnly;

      var _useSyncValue = (0, _useSyncValue3.useSyncValue)(inputValue),
          _useSyncValue2 = _slicedToArray(_useSyncValue, 2),
          imageValues = _useSyncValue2[0],
          setImageValues = _useSyncValue2[1];

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

      var _useModal = (0, _modal.useModal)({
        onClose: function onClose() {
          return setModalVisible(false);
        }
      }),
          observer = _useModal.observer,
          onClose = _useModal.onClose;

      var dispatchValue = function dispatchValue(_ref2) {
        var clear = _ref2.clear,
            value = _ref2.value;
        var callback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function () {};
        return setImageValues(function (oldValues) {
          var mergedValues = _objectSpread(_objectSpread({}, oldValues), value);

          mergedValues = clear ? {} : mergedValues;
          mergedValues.alt = mergedValues.description || '';
          callback(mergedValues);
          return mergedValues;
        });
      };

      var handleFieldChanged = function handleFieldChanged(event) {
        var selectedItem = event.selectedItem;

        if (selectedItem && selectedItem.value) {
          var img = new Image();
          var item = JSON.parse(selectedItem.value);
          img.addEventListener('load', function (event) {
            var _event$target = event.target,
                height = _event$target.height,
                width = _event$target.width;

            var imageData = _objectSpread(_objectSpread({}, {
              description: '',
              event: event,
              height: height,
              title: '',
              url: '',
              width: width
            }), item);

            dispatchValue({
              value: imageData
            }, function (mergedValues) {
              return onFieldChanged(mergedValues);
            });
          });
          img.src = item.url;
        }
      };

      var placeholder = readOnly ? '' : Liferay.Language.get('add-image-description');
      return (/*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_form.default.Group, {
          style: {
            marginBottom: '0.5rem'
          }
        }, /*#__PURE__*/_react.default.createElement("input", {
          name: name,
          type: "hidden",
          value: JSON.stringify(imageValues)
        }), /*#__PURE__*/_react.default.createElement(_form.ClayInput.Group, null, /*#__PURE__*/_react.default.createElement(_form.ClayInput.GroupItem, {
          className: "d-none d-sm-block",
          prepend: true
        }, /*#__PURE__*/_react.default.createElement(_form.ClayInput, {
          className: "field",
          disabled: true,
          id: id,
          type: "text",
          value: imageValues.title
        })), /*#__PURE__*/_react.default.createElement(_form.ClayInput.GroupItem, {
          append: true,
          shrink: true
        }, /*#__PURE__*/_react.default.createElement(_button.default, {
          disabled: readOnly,
          displayType: "secondary",
          onClick: function handleItemSelectorTriggerClick(event) {
            event.preventDefault();
            var itemSelectorDialog = new _frontendJsWeb.ItemSelectorDialog({
              eventName: "".concat(portletNamespace, "selectDocumentLibrary"),
              singleSelect: true,
              url: itemSelectorURL
            });
            itemSelectorDialog.on('selectedItemChange', handleFieldChanged);
            itemSelectorDialog.open();
          },
          type: "button"
        }, Liferay.Language.get('select'))), imageValues.url && /*#__PURE__*/_react.default.createElement(_form.ClayInput.GroupItem, {
          shrink: true
        }, /*#__PURE__*/_react.default.createElement(_button.default, {
          disabled: readOnly,
          displayType: "secondary",
          onClick: function onClick(event) {
            return dispatchValue({
              clear: true,
              value: {
                description: '',
                event: event,
                title: '',
                url: ''
              }
            }, function (mergedValues) {
              return onClearClick(mergedValues);
            });
          },
          type: "button"
        }, Liferay.Language.get('clear'))))), imageValues.url && modalVisible ? /*#__PURE__*/_react.default.createElement(_modal.default, {
          className: "image-picker-preview-modal",
          observer: observer,
          size: "full-screen"
        }, /*#__PURE__*/_react.default.createElement(_modal.default.Header, null), /*#__PURE__*/_react.default.createElement(_modal.default.Body, null, /*#__PURE__*/_react.default.createElement("img", {
          alt: imageValues.description,
          className: "d-block img-fluid mb-2 mx-auto rounded",
          onClick: onClose,
          src: imageValues.url,
          style: {
            cursor: 'zoom-out',
            maxHeight: '95%'
          }
        }), /*#__PURE__*/_react.default.createElement("p", {
          className: "font-weight-light text-center",
          style: {
            color: '#FFFFFF'
          }
        }, imageValues.description))) : imageValues.url && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement("div", {
          className: "image-picker-preview"
        }, /*#__PURE__*/_react.default.createElement("img", {
          alt: imageValues.description,
          className: "d-block img-fluid mb-2 rounded",
          onClick: function onClick() {
            return setModalVisible(true);
          },
          src: imageValues.url,
          style: {
            cursor: 'pointer'
          }
        })), /*#__PURE__*/_react.default.createElement(_form.default.Group, null, /*#__PURE__*/_react.default.createElement(_form.ClayInput, {
          disabled: readOnly,
          name: "".concat(name, "-description"),
          onChange: function onChange(_ref3) {
            var event = _ref3.event,
                value = _ref3.target.value;
            return dispatchValue({
              value: {
                description: value,
                event: event
              }
            }, function (mergedValues) {
              return onDescriptionChange(mergedValues);
            });
          },
          placeholder: placeholder,
          type: "text",
          value: imageValues.description
        }))))
      );
    };

    var Main = function Main(_ref4) {
      var _ref5, _transformValue;

      var displayErrors = _ref4.displayErrors,
          errorMessage = _ref4.errorMessage,
          id = _ref4.id,
          inputValue = _ref4.inputValue,
          itemSelectorURL = _ref4.itemSelectorURL,
          name = _ref4.name,
          onChange = _ref4.onChange,
          portletNamespace = _ref4.portletNamespace,
          readOnly = _ref4.readOnly,
          valid = _ref4.valid,
          value = _ref4.value,
          otherProps = _objectWithoutProperties(_ref4, ["displayErrors", "errorMessage", "id", "inputValue", "itemSelectorURL", "name", "onChange", "portletNamespace", "readOnly", "valid", "value"]);

      var isSignedIn = Liferay.ThemeDisplay.isSignedIn();

      var transformValue = function transformValue(sourceValue) {
        if (sourceValue) {
          if (typeof sourceValue === 'string') {
            return JSON.parse(sourceValue);
          } else if (_typeof(sourceValue) === 'object') {
            return sourceValue;
          }
        }

        return null;
      };

      return (/*#__PURE__*/_react.default.createElement(_ReactFieldBase.FieldBase, _extends({}, otherProps, {
          displayErrors: isSignedIn ? displayErrors : true,
          errorMessage: function getErrorMessages(errorMessage, isSignedIn) {
            var errorMessages = [errorMessage];

            if (!isSignedIn) {
              errorMessages.push(Liferay.Language.get('you-need-to-be-signed-in-to-edit-this-field'));
            }

            return errorMessages.join(' ');
          }(errorMessage, isSignedIn),
          id: id,
          name: name,
          readOnly: isSignedIn ? readOnly : true,
          valid: isSignedIn ? valid : false
        }), /*#__PURE__*/_react.default.createElement(ImagePicker, {
          id: id,
          inputValue: (_ref5 = (_transformValue = transformValue(inputValue)) !== null && _transformValue !== void 0 ? _transformValue : transformValue(value)) !== null && _ref5 !== void 0 ? _ref5 : defaultValue,
          itemSelectorURL: itemSelectorURL,
          name: name,
          onClearClick: function onClearClick(_ref6) {
            var event = _ref6.event,
                data = _objectWithoutProperties(_ref6, ["event"]);

            return onChange(event, data);
          },
          onDescriptionChange: function onDescriptionChange(_ref7) {
            var event = _ref7.event,
                data = _objectWithoutProperties(_ref7, ["event"]);

            return onChange(event, data);
          },
          onFieldChanged: function onFieldChanged(_ref8) {
            var event = _ref8.event,
                data = _objectWithoutProperties(_ref8, ["event"]);

            return onChange(event, data);
          },
          portletNamespace: portletNamespace,
          readOnly: isSignedIn ? readOnly : true
        }))
      );
    };

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