"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _base = _interopRequireDefault(require("@emotion/styled/base"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _react = _interopRequireDefault(require("react"));
var _reactImmutableProptypes = _interopRequireDefault(require("react-immutable-proptypes"));
var _reactWaypoint = require("react-waypoint");
var _immutable = require("immutable");
var _collections = require("../../../reducers/collections");
var _EntryCard = _interopRequireDefault(require("./EntryCard"));
var _react2 = require("@emotion/react");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _extends() { _extends = Object.assign ? Object.assign.bind() : 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 _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _EMOTION_STRINGIFIED_CSS_ERROR__() { return "You have tried to stringify object returned from `css` function. It isn't supposed to be used directly (e.g. as value of the `className` prop), but rather handed to emotion so it can handle it (e.g. as value of `css` prop)."; }
const CardsGrid = /*#__PURE__*/(0, _base.default)("ul", {
  target: "etq0ss00",
  label: "CardsGrid"
})(process.env.NODE_ENV === "production" ? {
  name: "1dbthoi",
  styles: "display:flex;flex-flow:row wrap;list-style-type:none;margin-left:-12px;margin-top:16px;margin-bottom:16px"
} : {
  name: "1dbthoi",
  styles: "display:flex;flex-flow:row wrap;list-style-type:none;margin-left:-12px;margin-top:16px;margin-bottom:16px",
  map: "/*# sourceMappingURL=data:application/json;charset=utf-8;base64,[encoded data, 4408 characters] */",
  toString: _EMOTION_STRINGIFIED_CSS_ERROR__
});
class EntryListing extends _react.default.Component {
  constructor(...args) {
    super(...args);
    _defineProperty(this, "hasMore", () => {
      var _this$props$cursor, _this$props$cursor$ac;
      const hasMore = (_this$props$cursor = this.props.cursor) === null || _this$props$cursor === void 0 ? void 0 : (_this$props$cursor$ac = _this$props$cursor.actions) === null || _this$props$cursor$ac === void 0 ? void 0 : _this$props$cursor$ac.has('append_next');
      return hasMore;
    });
    _defineProperty(this, "handleLoadMore", () => {
      if (this.hasMore()) {
        this.props.handleCursorActions('append_next');
      }
    });
    _defineProperty(this, "inferFields", collection => {
      const titleField = (0, _collections.selectInferredField)(collection, 'title');
      const descriptionField = (0, _collections.selectInferredField)(collection, 'description');
      const imageField = (0, _collections.selectInferredField)(collection, 'image');
      const fields = (0, _collections.selectFields)(collection);
      const inferredFields = [titleField, descriptionField, imageField];
      const remainingFields = fields && fields.filter(f => inferredFields.indexOf(f.get('name')) === -1);
      return {
        titleField,
        descriptionField,
        imageField,
        remainingFields
      };
    });
    _defineProperty(this, "renderCardsForSingleCollection", () => {
      const {
        collections,
        entries,
        viewStyle
      } = this.props;
      const inferredFields = this.inferFields(collections);
      const entryCardProps = {
        collection: collections,
        inferredFields,
        viewStyle
      };
      return entries.map((entry, idx) => (0, _react2.jsx)(_EntryCard.default, _extends({}, entryCardProps, {
        entry: entry,
        key: idx
      })));
    });
    _defineProperty(this, "renderCardsForMultipleCollections", () => {
      const {
        collections,
        entries
      } = this.props;
      const isSingleCollectionInList = collections.size === 1;
      return entries.map((entry, idx) => {
        const collectionName = entry.get('collection');
        const collection = collections.find(coll => coll.get('name') === collectionName);
        const collectionLabel = !isSingleCollectionInList && collection.get('label');
        const inferredFields = this.inferFields(collection);
        const entryCardProps = {
          collection,
          entry,
          inferredFields,
          collectionLabel
        };
        return (0, _react2.jsx)(_EntryCard.default, _extends({}, entryCardProps, {
          key: idx
        }));
      });
    });
  }
  render() {
    const {
      collections,
      page
    } = this.props;
    return (0, _react2.jsx)("div", null, (0, _react2.jsx)(CardsGrid, null, _immutable.Map.isMap(collections) ? this.renderCardsForSingleCollection() : this.renderCardsForMultipleCollections(), this.hasMore() && (0, _react2.jsx)(_reactWaypoint.Waypoint, {
      key: page,
      onEnter: this.handleLoadMore
    })));
  }
}
exports.default = EntryListing;
_defineProperty(EntryListing, "propTypes", {
  collections: _reactImmutableProptypes.default.iterable.isRequired,
  entries: _reactImmutableProptypes.default.list,
  viewStyle: _propTypes.default.string,
  cursor: _propTypes.default.any.isRequired,
  handleCursorActions: _propTypes.default.func.isRequired,
  page: _propTypes.default.number
});