"use strict";

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

var _immutable = require("immutable");

var _entries = require("../actions/entries");

var _entryHistory = require("../actions/entryHistory");

var _search = require("../actions/search");

let collection;
let loadedEntries;
let append;
let page;
let slug;

const entries = function entries() {
  let state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : (0, _immutable.Map)({
    entities: (0, _immutable.Map)(),
    pages: (0, _immutable.Map)()
  });
  let action = arguments.length > 1 ? arguments[1] : undefined;

  switch (action.type) {
    case _entries.ENTRY_REQUEST:
      return state.setIn(['entities', "".concat(action.payload.collection, ".").concat(action.payload.slug), 'isFetching'], true);

    case _entries.ENTRY_PERSIST_SUCCESS:
      {
        const {
          slug,
          newRef
        } = action.payload;
        return state.withMutations(map => {
          map.setIn(['entities', "".concat(collection, ".").concat(slug), 'ref'], newRef);
          map.setIn(['entities', "".concat(collection, ".").concat(slug), 'contentSame'], true);
        });
      }

    case _entryHistory.ADD_ENTRY_HISTORY:
      {
        const {
          collection,
          slug,
          commits
        } = action.payload;
        const lastCommit = commits[commits.length - 1];
        const {
          ref
        } = lastCommit;
        const newState = state.withMutations(map => {
          map.setIn(['entities', "".concat(collection, ".").concat(slug), 'ref'], ref);
          map.setIn(['entities', "".concat(collection, ".").concat(slug), 'contentSame'], false);
        });
        return newState;
      }

    case _entries.ENTRY_SUCCESS:
      {
        collection = action.payload.collection;
        slug = action.payload.entry.slug;
        const newState = state.withMutations(map => {
          const existing = map.getIn(['entities', "".concat(collection, ".").concat(slug)]) || (0, _immutable.Map)();
          const newValue = existing.merge((0, _immutable.fromJS)(action.payload.entry)).set('isFetching', false).set('contentSame', false);
          map.setIn(['entities', "".concat(collection, ".").concat(slug)], newValue);
          const ids = map.getIn(['pages', collection, 'ids'], (0, _immutable.List)());

          if (!ids.includes(slug)) {
            map.setIn(['pages', collection, 'ids'], ids.unshift(slug));
          }
        });
        return newState;
      }

    case _entries.ENTRIES_REQUEST:
      return state.setIn(['pages', action.payload.collection, 'isFetching'], true);

    case _entries.ENTRIES_SUCCESS:
      collection = action.payload.collection;
      loadedEntries = action.payload.entries;
      append = action.payload.append;
      page = action.payload.page;
      return state.withMutations(map => {
        loadedEntries.forEach(entry => {
          const existing = map.getIn(['entities', "".concat(collection, ".").concat(entry.slug)]);
          const newValue = (0, _immutable.fromJS)(entry).merge(existing).set('isFetching', false);
          map.setIn(['entities', "".concat(collection, ".").concat(entry.slug)], newValue);
        });
        const ids = (0, _immutable.List)(loadedEntries.map(entry => entry.slug));
        map.setIn(['pages', collection], (0, _immutable.Map)({
          page,
          ids: append ? map.getIn(['pages', collection, 'ids'], (0, _immutable.List)()).concat(ids) : ids
        }));
      });

    case _entries.ENTRIES_FAILURE:
      return state.setIn(['pages', action.meta.collection, 'isFetching'], false);

    case _entries.ENTRY_FAILURE:
      return state.withMutations(map => {
        map.setIn(['entities', "".concat(action.payload.collection, ".").concat(action.payload.slug), 'isFetching'], false);
        map.setIn(['entities', "".concat(action.payload.collection, ".").concat(action.payload.slug), 'error'], action.payload.error.message);
      });

    case _search.SEARCH_ENTRIES_SUCCESS:
      loadedEntries = action.payload.entries;
      return state.withMutations(map => {
        loadedEntries.forEach(entry => map.setIn(['entities', "".concat(entry.collection, ".").concat(entry.slug)], (0, _immutable.fromJS)(entry).set('isFetching', false)));
      });

    case _entries.ENTRY_DELETE_SUCCESS:
      return state.withMutations(map => {
        map.deleteIn(['entities', "".concat(action.payload.collectionName, ".").concat(action.payload.entrySlug)]);
        map.updateIn(['pages', action.payload.collectionName, 'ids'], ids => ids.filter(id => id !== action.payload.entrySlug));
      });

    default:
      return state;
  }
};

const selectEntry = (state, collection, slug) => state.getIn(['entities', "".concat(collection, ".").concat(slug)]);

exports.selectEntry = selectEntry;

const selectPublishedSlugs = (state, collection) => state.getIn(['pages', collection, 'ids'], (0, _immutable.List)());

exports.selectPublishedSlugs = selectPublishedSlugs;

const selectEntries = (state, collection) => {
  const slugs = selectPublishedSlugs(state, collection);
  return slugs && slugs.map(slug => selectEntry(state, collection, slug));
};

exports.selectEntries = selectEntries;
var _default = entries;
exports.default = _default;