Liferay.Loader.define("layout-content-page-editor-web@2.0.162/page_editor/app/components/fragment-configuration-fields/ItemSelectorField", ['module', 'exports', 'require', '@frontend-taglib-clay$clayui/button', '@frontend-taglib-clay$clayui/form', 'frontend-js-react-web', 'frontend-js-react-web$prop-types', 'frontend-js-react-web$react', '../../../common/components/ItemSelector', '../../../prop-types/index', '../../services/InfoItemService', '../../store/index', '../../utils/useId', '../CollectionItemContext'], 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.ItemSelectorField = void 0;

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

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

    var _frontendJsReactWeb = require("frontend-js-react-web");

    var _propTypes = _interopRequireDefault(require("frontend-js-react-web$prop-types"));

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

    var _ItemSelector = _interopRequireDefault(require("../../../common/components/ItemSelector"));

    var _index = require("../../../prop-types/index");

    var _InfoItemService = _interopRequireDefault(require("../../services/InfoItemService"));

    var _index2 = require("../../store/index");

    var _useId = require("../../utils/useId");

    var _CollectionItemContext = require("../CollectionItemContext");

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

    var ItemSelectorField = function ItemSelectorField(_ref) {
      var field = _ref.field,
          onValueSelect = _ref.onValueSelect,
          value = _ref.value;
      var collectionItemContext = (0, _react.useContext)(_CollectionItemContext.CollectionItemContext);
      var _field$typeOptions = field.typeOptions,
          typeOptions = _field$typeOptions === void 0 ? {} : _field$typeOptions;
      var collectionItem = collectionItemContext.collectionItem;
      var isWithinCollection = collectionItem !== null;
      var className = isWithinCollection ? collectionItem.className : value.className;
      var classPK = isWithinCollection ? collectionItem.classPK : value.classPK;
      return (/*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_form.default.Group, {
          small: true
        }, /*#__PURE__*/_react.default.createElement(_ItemSelector.default, {
          itemSelectorURL: typeOptions.infoItemSelectorURL,
          label: field.label,
          onItemSelect: function onItemSelect(item) {
            onValueSelect(field.name, {
              className: item.className,
              classNameId: item.classNameId,
              classPK: item.classPK,
              title: item.title
            });
          },
          selectedItemTitle: isWithinCollection ? collectionItem.title || Liferay.Language.get('collection-item') : value.title,
          showAddButton: !isWithinCollection
        })), value && Object.keys(value).length > 0 && /*#__PURE__*/_react.default.createElement(_button.default, {
          className: "mt-2",
          displayType: "secondary",
          onClick: function onClick() {
            onValueSelect(field.name, {});
          }
        }, Liferay.Language.get('clear')), typeOptions.enableSelectTemplate && className && /*#__PURE__*/_react.default.createElement(_form.default.Group, {
          small: true
        }, /*#__PURE__*/_react.default.createElement(TemplateSelector, {
          className: className,
          classPK: classPK,
          onTemplateSelect: function onTemplateSelect(template) {
            onValueSelect(field.name, _objectSpread(_objectSpread({}, value), {}, {
              template: template
            }));
          },
          selectedTemplate: value.template
        })))
      );
    };

    exports.ItemSelectorField = ItemSelectorField;

    var TemplateSelector = function TemplateSelector(_ref2) {
      var className = _ref2.className,
          classPK = _ref2.classPK,
          onTemplateSelect = _ref2.onTemplateSelect,
          selectedTemplate = _ref2.selectedTemplate;
      var dispatch = (0, _index2.useDispatch)();

      var _useState = (0, _react.useState)([]),
          _useState2 = _slicedToArray(_useState, 2),
          availableTemplates = _useState2[0],
          setAvailableTemplates = _useState2[1];

      var isMounted = (0, _frontendJsReactWeb.useIsMounted)();
      var itemSelectorTemplateSelectId = (0, _useId.useId)();
      (0, _react.useEffect)(function () {
        if (isMounted()) {
          _InfoItemService.default.getAvailableTemplates({
            className: className,
            classPK: classPK,
            onNetworkStatus: dispatch
          }).then(function (response) {
            setAvailableTemplates(response);
          });
        }
      }, [className, classPK, dispatch, isMounted]);
      return (/*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_form.default.Group, {
          small: true
        }, /*#__PURE__*/_react.default.createElement("label", {
          htmlFor: itemSelectorTemplateSelectId
        }, Liferay.Language.get('template')), /*#__PURE__*/_react.default.createElement("select", {
          className: "form-control",
          id: itemSelectorTemplateSelectId,
          onChange: function onChange(event) {
            onTemplateSelect(event.target.options[event.target.selectedIndex].dataset);
          }
        }, availableTemplates.map(function (entry) {
          if (entry.templates) {
            return (/*#__PURE__*/_react.default.createElement("optgroup", {
                key: entry.label,
                label: entry.label
              }, entry.templates.map(function (template) {
                return (/*#__PURE__*/_react.default.createElement("option", {
                    "data-info-item-renderer-key": template.infoItemRendererKey,
                    "data-template-key": template.templateKey,
                    key: template.label,
                    selected: selectedTemplate && selectedTemplate.infoItemRendererKey === template.infoItemRendererKey && (!selectedTemplate.templateKey || selectedTemplate.templateKey && selectedTemplate.templateKey === template.templateKey)
                  }, template.label)
                );
              }))
            );
          } else {
            return (/*#__PURE__*/_react.default.createElement("option", {
                "data-info-item-renderer-key": entry.infoItemRendererKey,
                key: entry.label,
                selected: selectedTemplate && selectedTemplate.infoItemRendererKey === entry.infoItemRendererKey
              }, entry.label)
            );
          }
        }))))
      );
    };
    //# sourceMappingURL=ItemSelectorField.js.map
  }
});
//# sourceMappingURL=ItemSelectorField.js.map