'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _isEmpty = require('lodash/isEmpty');

var _isEmpty2 = _interopRequireDefault(_isEmpty);

var _valMap2 = require('../util/valMap');

var _valMap3 = _interopRequireDefault(_valMap2);

var _camelizeHelpers = require('./camelizeHelpers');

var _camelizeHelpers2 = _interopRequireDefault(_camelizeHelpers);

var _banks = require('../actions/banks');

var _bankEntries = require('../actions/bankEntries');

var _addInteractionTypes = require('../actions/addInteractionTypes');

var _items = require('../actions/items');

var _reporting = require('../actions/reporting');

var _qtiImports = require('../actions/qtiImports');

var _quizEntries = require('../actions/quizEntries');

var _quizSessionEvents = require('../actions/quizSessionEvents');

var _quizSessionResults = require('../actions/quizSessionResults');

var _quizSessions = require('../actions/quizSessions');

var _quizzes = require('../actions/quizzes');

var _sessionItemResults = require('../actions/sessionItemResults');

var _sessionItems = require('../actions/sessionItems');

var _stimuli = require('../actions/stimuli');

var _taking = require('../actions/taking');

var _sharedBanks = require('../actions/sharedBanks');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

// getPersistenceActions is given an object that 'normalizr' returns
// is iterates over the different types of objects that can come
// back from the api, maps their type to an action, then returns
// an array of actions to dispatch (which persist the data to the store)

var getPersistenceActions = function getPersistenceActions(normalizedResp) {
  var _valMap = (0, _valMap3.default)(normalizedResp.entities, _camelizeHelpers2.default),
      banks = _valMap.banks,
      bankEntries = _valMap.bankEntries,
      interactionTypes = _valMap.interactionTypes,
      itemAnalyses = _valMap.itemAnalyses,
      items = _valMap.items,
      qtiImports = _valMap.qtiImports,
      quizAnalyses = _valMap.quizAnalyses,
      quizEntries = _valMap.quizEntries,
      quizEntry = _valMap.quizEntry,
      quizSessionEvents = _valMap.quizSessionEvents,
      quizSessionResults = _valMap.quizSessionResults,
      quizSessions = _valMap.quizSessions,
      quizzes = _valMap.quizzes,
      resumeData = _valMap.resumeData,
      sharedBanks = _valMap.sharedBanks,
      sessionItemResults = _valMap.sessionItemResults,
      sessionItems = _valMap.sessionItems,
      stimuli = _valMap.stimuli;

  var baseDataActionPairs = [{ data: banks, actionCreator: _banks.addBanks }, { data: bankEntries, actionCreator: _bankEntries.addBankEntries }, { data: interactionTypes, actionCreator: _addInteractionTypes.addInteractionTypes }, { data: items, actionCreator: _items.addItems }, { data: quizSessionEvents, actionCreator: _quizSessionEvents.addQuizSessionEvents }, { data: quizSessionResults, actionCreator: _quizSessionResults.addQuizSessionResults }, { data: quizSessions, actionCreator: _quizSessions.addQuizSessions }, { data: quizzes, actionCreator: _quizzes.addQuizzes }, { data: quizEntries, actionCreator: _quizEntries.addQuizEntries }, { data: quizEntry, actionCreator: _quizEntries.addQuizEntry }, { data: resumeData, actionCreator: _taking.loadResumeData }, { data: sessionItemResults, actionCreator: _sessionItemResults.addSessionItemResults }, { data: sessionItems, actionCreator: _sessionItems.addSessionItems }, { data: stimuli, actionCreator: _stimuli.addStimuli }, { data: itemAnalyses, actionCreator: _reporting.addItemAnalyses }, { data: quizAnalyses, actionCreator: _reporting.addQuizAnalysis }, { data: sharedBanks, actionCreator: _sharedBanks.addSharedBanks }];

  // parse only if feature is on
  var qtiImportActionPairs = [{ data: qtiImports, actionCreator: _qtiImports.addQtiImports }];
  var dataActionPairs = baseDataActionPairs.concat(qtiImportActionPairs);

  var actionsToDispatch = dataActionPairs.reduce(function (memo, _ref) {
    var data = _ref.data,
        actionCreator = _ref.actionCreator;

    return (0, _isEmpty2.default)(data) ? memo : memo.concat([actionCreator(data)]);
  }, []);

  return actionsToDispatch;
};

exports.default = getPersistenceActions;