"use strict";

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

var _immutable = require("immutable");

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

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

const defaultState = (0, _immutable.Map)([['history', (0, _immutable.Map)()], ['blockHistory', false]]);

const defaultHandler = state => state;

const newHistoryForEntry = (state, _ref) => {
  let {
    payload
  } = _ref;
  const {
    key,
    commits
  } = payload;
  return state.setIn(['history', key], commits);
};

const entryPersistSuccess = (state, _ref2) => {
  let {
    payload
  } = _ref2;
  const {
    collectionName,
    slug,
    newRef,
    author,
    commitDate,
    oldRef,
    revert
  } = payload;
  const key = "".concat(collectionName, ".").concat(slug);
  const newHistoryObject = {
    ref: newRef,
    date: commitDate,
    author,
    file: {
      // TODO REVISIT THIS - THIS MIGHT BE VERY WRONG. (Even if it isn't really being used I think -> in that case remove);
      path: "_".concat(collectionName, "/").concat(slug, ".md")
    }
  };
  return state.withMutations(mutatingState => {
    return mutatingState.updateIn(['history', key], history => {
      const newHistory = history ? history.concat([newHistoryObject]) : [newHistoryObject];
      return revert ? newHistory.filter((_ref3) => {
        let {
          ref
        } = _ref3;
        return ref !== oldRef;
      }) : newHistory;
    });
  });
};

const entryDeleteSuccess = (state, _ref4) => {
  let {
    payload
  } = _ref4;
  const {
    collectionName,
    entrySlug
  } = payload;
  const key = "".concat(collectionName, ".").concat(entrySlug);
  return state.withMutations(mutatingState => {
    mutatingState.deleteIn(['history', key]);
  });
};

const blockEntryHistory = state => {
  return state.set('blockHistory', true);
};

const actionHandlers = {
  [_entryHistory.ADD_ENTRY_HISTORY]: newHistoryForEntry,
  [_entries.ENTRY_PERSIST_SUCCESS]: entryPersistSuccess,
  [_entries.ENTRY_DELETE_SUCCESS]: entryDeleteSuccess,
  [_entryHistory.BLOCK_ENTRY_HISTORY]: blockEntryHistory
};

const entryHistory = function entryHistory() {
  let state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultState;
  let action = arguments.length > 1 ? arguments[1] : undefined;

  if (state.get('blockHistory')) {
    return state;
  }

  const {
    type
  } = action;
  const handler = actionHandlers[type] || defaultHandler;
  const newState = handler(state, action);
  return newState;
};

const selectEntryHistory = (state, collectionName, slug) => state.getIn(['history', "".concat(collectionName, ".").concat(slug)]) || [];

exports.selectEntryHistory = selectEntryHistory;

const selectIsRefLatestCommit = (state, collection, slug, ref) => {
  const history = selectEntryHistory(state, collection, slug);
  const lastItem = history[history.length - 1];

  if (!lastItem) {
    return true;
  }

  return lastItem && lastItem.ref === ref;
};

exports.selectIsRefLatestCommit = selectIsRefLatestCommit;
var _default = entryHistory;
exports.default = _default;