Liferay.Loader.define("portal-search-ranking-web@2.0.50/js/components/list/List.es", ['module', 'exports', 'require', '@frontend-taglib-clay$clayui/button', '@frontend-taglib-clay$clayui/loading-indicator', 'frontend-js-react-web$prop-types', 'frontend-js-react-web$react', 'frontend-js-react-web$react-dnd', 'frontend-js-react-web$react-dnd-html5-backend', '../../utils/constants.es', '../../utils/util.es', '../shared/ClayEmptyState.es', '../shared/ErrorBoundary.es', './Item.es', './ItemDragLayer.es', './SearchBar.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 _loadingIndicator = _interopRequireDefault(require("@frontend-taglib-clay$clayui/loading-indicator"));

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

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

    var _reactDnd = require("frontend-js-react-web$react-dnd");

    var _reactDndHtml5Backend = require("frontend-js-react-web$react-dnd-html5-backend");

    var _constants = require("../../utils/constants.es");

    var _util = require("../../utils/util.es");

    var _ClayEmptyState = _interopRequireWildcard(require("../shared/ClayEmptyState.es"));

    var _ErrorBoundary = _interopRequireDefault(require("../shared/ErrorBoundary.es"));

    var _Item = _interopRequireDefault(require("./Item.es"));

    var _ItemDragLayer = _interopRequireDefault(require("./ItemDragLayer.es"));

    var _SearchBar = _interopRequireDefault(require("./SearchBar.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 _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 _classCallCheck(instance, Constructor) {
      if (!(instance instanceof Constructor)) {
        throw new TypeError("Cannot call a class as a function");
      }
    }

    function _defineProperties(target, props) {
      for (var i = 0; i < props.length; i++) {
        var descriptor = props[i];descriptor.enumerable = descriptor.enumerable || false;descriptor.configurable = true;if ("value" in descriptor) descriptor.writable = true;Object.defineProperty(target, descriptor.key, descriptor);
      }
    }

    function _createClass(Constructor, protoProps, staticProps) {
      if (protoProps) _defineProperties(Constructor.prototype, protoProps);if (staticProps) _defineProperties(Constructor, staticProps);return Constructor;
    }

    function _possibleConstructorReturn(self, call) {
      if (call && (_typeof(call) === "object" || typeof call === "function")) {
        return call;
      }return _assertThisInitialized(self);
    }

    function _getPrototypeOf(o) {
      _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
        return o.__proto__ || Object.getPrototypeOf(o);
      };return _getPrototypeOf(o);
    }

    function _assertThisInitialized(self) {
      if (self === void 0) {
        throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
      }return self;
    }

    function _inherits(subClass, superClass) {
      if (typeof superClass !== "function" && superClass !== null) {
        throw new TypeError("Super expression must either be null or a function");
      }subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } });if (superClass) _setPrototypeOf(subClass, superClass);
    }

    function _setPrototypeOf(o, p) {
      _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
        o.__proto__ = p;return o;
      };return _setPrototypeOf(o, p);
    }

    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 List = /*#__PURE__*/function (_PureComponent) {
      _inherits(List, _PureComponent);

      function List() {
        var _getPrototypeOf2;

        var _this;

        _classCallCheck(this, List);

        for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
          args[_key] = arguments[_key];
        }

        _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(List)).call.apply(_getPrototypeOf2, [this].concat(args)));

        _defineProperty(_assertThisInitialized(_this), "state", {
          focusIndex: null,
          reorder: false,
          selectedIds: []
        });

        _defineProperty(_assertThisInitialized(_this), "_handleItemBlur", function () {
          _this._handleItemFocus(null);

          _this._handleReorder(false);
        });

        _defineProperty(_assertThisInitialized(_this), "_handleItemFocus", function (index) {
          _this.setState({
            focusIndex: index
          });
        });

        _defineProperty(_assertThisInitialized(_this), "_handleKeyDown", function (event) {
          if (!(0, _util.isNull)(_this.state.focusIndex)) {
            _this._handleKeyDownFocus(event);
          }
        });

        _defineProperty(_assertThisInitialized(_this), "_handleKeyDownFocus", function (event) {
          var _this$props = _this.props,
              onMove = _this$props.onMove,
              resultIds = _this$props.resultIds,
              resultIdsPinned = _this$props.resultIdsPinned;
          var _this$state = _this.state,
              focusIndex = _this$state.focusIndex,
              reorder = _this$state.reorder;
          var pinLength = resultIdsPinned ? resultIdsPinned.length : 0;

          if (event.key === _constants.KEY_CODES.SPACE || event.key == _constants.KEY_CODES.ENTER) {
            event.preventDefault();

            _this._handleReorder(!reorder && focusIndex < pinLength);
          } else if (event.key === _constants.KEY_CODES.ARROW_DOWN) {
            event.preventDefault();

            if (focusIndex + 1 < resultIds.length) {
              if (!(reorder && focusIndex + 1 === pinLength)) {
                if (reorder && focusIndex + 1 < pinLength) {
                  onMove(focusIndex, focusIndex + 2);
                }

                _this._handleItemFocus(focusIndex + 1);
              }
            }
          } else if (event.key === _constants.KEY_CODES.ARROW_UP) {
            event.preventDefault();

            if (focusIndex > 0) {
              if (reorder) {
                onMove(focusIndex, focusIndex - 1);
              }

              _this._handleItemFocus(focusIndex - 1);
            }
          }
        });

        _defineProperty(_assertThisInitialized(_this), "_handleLoadMoreResults", function () {
          _this.props.onLoadResults();
        });

        _defineProperty(_assertThisInitialized(_this), "_handleRemoveSelect", function (ids) {
          _this.setState(function (state) {
            return {
              selectedIds: state.selectedIds.filter(function (id) {
                return !ids.includes(id);
              })
            };
          });
        });

        _defineProperty(_assertThisInitialized(_this), "_handleReorder", function (val) {
          _this.setState({
            reorder: val
          });
        });

        _defineProperty(_assertThisInitialized(_this), "_handleSelect", function (id) {
          _this.setState(function (state) {
            return {
              selectedIds: (0, _util.toggleListItem)(state.selectedIds, id)
            };
          });
        });

        _defineProperty(_assertThisInitialized(_this), "_handleSelectClear", function () {
          _this.setState({
            selectedIds: []
          });
        });

        _defineProperty(_assertThisInitialized(_this), "_handleSelectAll", function () {
          _this.setState({
            selectedIds: _this.props.resultIds
          });
        });

        _defineProperty(_assertThisInitialized(_this), "_handleTabSelect", function (index, lastIndex) {
          if (index !== lastIndex) {
            _this.setState({
              selectedIds: []
            });
          }
        });

        _defineProperty(_assertThisInitialized(_this), "_handleClickPin", function (ids, pinned) {
          _this.props.onClickPin(ids, pinned);

          if (!(0, _util.isNull)(_this.state.focusIndex)) {
            _this.setState(function (state, props) {
              var newFocusIndex = props.resultIds.indexOf(ids[0]);
              return {
                focusIndex: newFocusIndex > -1 ? newFocusIndex : null
              };
            });
          }
        });

        _defineProperty(_assertThisInitialized(_this), "_renderItem", function (id, index) {
          var _this$props2 = _this.props,
              dataMap = _this$props2.dataMap,
              onClickHide = _this$props2.onClickHide,
              onMove = _this$props2.onMove;
          var _this$state2 = _this.state,
              focusIndex = _this$state2.focusIndex,
              reorder = _this$state2.reorder,
              selectedIds = _this$state2.selectedIds;
          var item = dataMap[id];
          return item ? /*#__PURE__*/_react.default.createElement(_Item.default, {
            addedResult: item.addedResult,
            author: item.author,
            clicks: item.clicks,
            date: item.date,
            deleted: item.deleted,
            description: item.description,
            focus: index === focusIndex,
            hidden: item.hidden,
            icon: item.icon,
            id: item.id,
            index: index,
            key: item.id,
            onBlur: _this._handleItemBlur,
            onClickHide: onClickHide,
            onClickPin: _this._handleClickPin,
            onFocus: _this._handleItemFocus,
            onMove: onMove,
            onRemoveSelect: _this._handleRemoveSelect,
            onSelect: _this._handleSelect,
            pinned: item.pinned,
            reorder: index === focusIndex && reorder,
            selected: selectedIds.includes(item.id),
            title: item.title,
            type: item.type,
            viewURL: item.viewURL
          }) : null;
        });

        return _this;
      }

      _createClass(List, [{
        key: "render",
        value: function render() {
          var _this2 = this;

          var _this$props3 = this.props,
              dataLoading = _this$props3.dataLoading,
              dataMap = _this$props3.dataMap,
              displayError = _this$props3.displayError,
              fetchDocumentsSearchUrl = _this$props3.fetchDocumentsSearchUrl,
              onAddResultSubmit = _this$props3.onAddResultSubmit,
              onClickHide = _this$props3.onClickHide,
              onClickPin = _this$props3.onClickPin,
              resultIds = _this$props3.resultIds,
              showLoadMore = _this$props3.showLoadMore;
          var selectedIds = this.state.selectedIds;
          return (/*#__PURE__*/_react.default.createElement(_reactDnd.DndProvider, {
              backend: _reactDndHtml5Backend.HTML5Backend
            }, /*#__PURE__*/_react.default.createElement("div", {
              className: "result-ranking-list-root"
            }, /*#__PURE__*/_react.default.createElement(_ItemDragLayer.default, null), /*#__PURE__*/_react.default.createElement(_SearchBar.default, {
              dataMap: dataMap,
              fetchDocumentsSearchUrl: fetchDocumentsSearchUrl,
              onAddResultSubmit: onAddResultSubmit,
              onClickHide: onClickHide,
              onClickPin: onClickPin,
              onRemoveSelect: this._handleRemoveSelect,
              onSelectAll: this._handleSelectAll,
              onSelectClear: this._handleSelectClear,
              resultIds: resultIds,
              selectedIds: selectedIds
            }), /*#__PURE__*/_react.default.createElement(_ErrorBoundary.default, {
              toast: true
            }, !!resultIds.length && /*#__PURE__*/_react.default.createElement("ul", {
              className: "list-group show-quick-actions-on-hover",
              onKeyDown: this._handleKeyDown
            }, resultIds.map(function (id, index, arr) {
              return _this2._renderItem(id, index, arr);
            })), dataLoading && /*#__PURE__*/_react.default.createElement("div", {
              className: "load-more-container"
            }, /*#__PURE__*/_react.default.createElement(_loadingIndicator.default, null)), !dataLoading && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, !displayError && !resultIds.length && /*#__PURE__*/_react.default.createElement(_ClayEmptyState.default, null), displayError && /*#__PURE__*/_react.default.createElement(_ClayEmptyState.default, {
              actionLabel: Liferay.Language.get('try-again'),
              description: Liferay.Language.get('an-error-has-occurred-and-we-were-unable-to-load-the-results'),
              displayState: _ClayEmptyState.DISPLAY_STATES.EMPTY,
              onClickAction: this._handleLoadMoreResults,
              title: Liferay.Language.get('unable-to-load-content')
            }), showLoadMore && /*#__PURE__*/_react.default.createElement("div", {
              className: "load-more-container"
            }, /*#__PURE__*/_react.default.createElement(_button.default, {
              className: "load-more-button",
              displayType: "secondary",
              onClick: this._handleLoadMoreResults
            }, Liferay.Language.get('load-more-results')))))))
          );
        }
      }]);

      return List;
    }(_react.PureComponent);

    _defineProperty(List, "defaultProps", {
      dataLoading: false,
      resultIds: []
    });

    var _default = List;
    exports.default = _default;
    //# sourceMappingURL=List.es.js.map
  }
});
//# sourceMappingURL=List.es.js.map