"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _styledBase = _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 = _interopRequireDefault(require("react-waypoint"));

var _immutable = require("immutable");

var _netlifyCmsLibUtil = require("netlify-cms-lib-util");

var _collections = require("../../../reducers/collections");

var _EntryCard = _interopRequireDefault(require("./EntryCard"));

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

const CardsGrid = (0, _styledBase.default)("ul", {
  target: "e1i0qpjk0",
  label: "CardsGrid"
})(process.env.NODE_ENV === "production" ? {
  name: "1gscq6o",
  styles: "display:flex;flex-flow:row wrap;list-style-type:none;margin-left:-12px;"
} : {
  name: "1gscq6o",
  styles: "display:flex;flex-flow:row wrap;list-style-type:none;margin-left:-12px;",
  map: "/*# sourceMappingURL=data:application/json;charset=utf-8;base64,  */"
});

class EntryListing extends _react.default.Component {
  constructor() {
    super(...arguments);

    _defineProperty(this, "hasMore", () => {
      return _netlifyCmsLibUtil.Cursor.create(this.props.cursor).actions.has('append_next');
    });

    _defineProperty(this, "handleLoadMore", () => {
      if (this.hasMore()) {
        this.props.handleCursorActions('append_next');
      }
    });

    _defineProperty(this, "inferFields", collection => {
      const titleField = (0, _collections.selectInferedField)(collection, 'title');
      const descriptionField = (0, _collections.selectInferedField)(collection, 'description');
      const imageField = (0, _collections.selectInferedField)(collection, 'image');
      const fields = (0, _collections.selectFields)(collection);
      const inferedFields = [titleField, descriptionField, imageField];
      const remainingFields = fields && fields.filter(f => inferedFields.indexOf(f.get('name')) === -1);
      return {
        titleField,
        descriptionField,
        imageField,
        remainingFields
      };
    });

    _defineProperty(this, "renderCardsForSingleCollection", () => {
      const {
        collections,
        entries,
        publicFolder,
        viewStyle
      } = this.props;
      const inferedFields = this.inferFields(collections);
      const entryCardProps = {
        collection: collections,
        inferedFields,
        publicFolder,
        viewStyle
      };
      return entries.map((entry, idx) => _react.default.createElement(_EntryCard.default, _extends({}, entryCardProps, {
        entry: entry,
        key: idx
      })));
    });

    _defineProperty(this, "renderCardsForMultipleCollections", () => {
      const {
        collections,
        entries,
        publicFolder
      } = this.props;
      return entries.map((entry, idx) => {
        const collectionName = entry.get('collection');
        const collection = collections.find(coll => coll.get('name') === collectionName);
        const collectionLabel = collection.get('label');
        const inferedFields = this.inferFields(collection);
        const entryCardProps = {
          collection,
          entry,
          inferedFields,
          publicFolder,
          collectionLabel
        };
        return _react.default.createElement(_EntryCard.default, _extends({}, entryCardProps, {
          key: idx
        }));
      });
    });
  }

  render() {
    const {
      collections
    } = this.props;
    return _react.default.createElement("div", null, _react.default.createElement(CardsGrid, null, _immutable.Map.isMap(collections) ? this.renderCardsForSingleCollection() : this.renderCardsForMultipleCollections(), this.hasMore() && _react.default.createElement(_reactWaypoint.default, {
      onEnter: this.handleLoadMore
    })));
  }

}

exports.default = EntryListing;

_defineProperty(EntryListing, "propTypes", {
  publicFolder: _propTypes.default.string.isRequired,
  collections: _reactImmutableProptypes.default.iterable.isRequired,
  entries: _reactImmutableProptypes.default.list,
  viewStyle: _propTypes.default.string,
  cursor: _propTypes.default.any.isRequired,
  handleCursorActions: _propTypes.default.func.isRequired
});