"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.loadEntryHistory = exports.addEntryHistory = 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 addEntryHistory = (key, commits, collection, slug) => ({
  type: ADD_ENTRY_HISTORY,
  payload: {
    key,
    commits,
    collection: collection.get('name'),
    slug
  }
});

exports.addEntryHistory = addEntryHistory;

const loadEntryHistory = (collection, slug) => async (dispatch, getState) => {
  const state = getState();
  const backend = (0, _backend.currentBackend)(state.config);
  const commits = await backend.getFileVersions(collection, slug);
  const key = "".concat(collection.get('name'), ".").concat(slug);
  dispatch(addEntryHistory(key, commits, collection, slug));
};

exports.loadEntryHistory = loadEntryHistory;