"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.loadEntryHistory = exports.blockEntryHistoryForCurrentBackend = exports.addEntryHistory = exports.BLOCK_ENTRY_HISTORY = exports.DRAFT_CREATE_FROM_ENTRY_HISTORY = exports.SELECT_ENTRY_HISTORY_ITEM = exports.ADD_ENTRY_HISTORY = void 0;

var _backend = require("../backend");

const ADD_ENTRY_HISTORY = 'ADD_ENTRY_HISTORY';
exports.ADD_ENTRY_HISTORY = ADD_ENTRY_HISTORY;
const SELECT_ENTRY_HISTORY_ITEM = 'SELECT_ENTRY_HISTORY_ITEM';
exports.SELECT_ENTRY_HISTORY_ITEM = SELECT_ENTRY_HISTORY_ITEM;
const DRAFT_CREATE_FROM_ENTRY_HISTORY = 'DRAFT_CREATE_FROM_ENTRY_HISTORY';
exports.DRAFT_CREATE_FROM_ENTRY_HISTORY = DRAFT_CREATE_FROM_ENTRY_HISTORY;
const BLOCK_ENTRY_HISTORY = 'BLOCK_ENTRY_HISTORY';
exports.BLOCK_ENTRY_HISTORY = BLOCK_ENTRY_HISTORY;

const addEntryHistory = (key, commits, collection, slug) => ({
  type: ADD_ENTRY_HISTORY,
  payload: {
    key,
    commits,
    collection: collection.get('name'),
    slug
  }
});

exports.addEntryHistory = addEntryHistory;

const blockEntryHistoryForCurrentBackend = () => ({
  type: BLOCK_ENTRY_HISTORY
});

exports.blockEntryHistoryForCurrentBackend = blockEntryHistoryForCurrentBackend;

const loadEntryHistory = (collection, slug) => async (dispatch, getState) => {
  const state = getState();
  const backend = (0, _backend.currentBackend)(state.config);
  const isVersioningSupported = backend.supportsFileVersioning();
  const key = "".concat(collection.get('name'), ".").concat(slug);

  if (isVersioningSupported) {
    const commits = await backend.getFileVersions(collection, slug);
    dispatch(addEntryHistory(key, commits, collection, slug));
  } else {
    dispatch(blockEntryHistoryForCurrentBackend());
  }
};

exports.loadEntryHistory = loadEntryHistory;