'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.handleBankEntriesResponse = exports.handleShareBanksResponse = exports.handleBanksResponse = exports.handleQuizSessionsResponse = exports.handleQuizSessionResultsResponse = exports.handleResumeDataResponse = exports.handleSessionItemResultsResponse = exports.handleSessionItemsResponse = exports.handleQuizEntriesResponse = exports.handleInteractionTypesResponse = exports.handleStimuliResponse = exports.handleItemsResponse = exports.handleItemAnalysesResponse = exports.handleQuizSessionEventsResponse = exports.handleQtiImportsResponse = exports.handleQuizEntryResponse = exports.handleBankEntryResponse = exports.handleShareBankResponse = exports.handleBankResponse = exports.handleQuizSessionResponse = exports.handleQuizAnalysisResponse = exports.handleQuizResponse = undefined;

var _normalizr = require('normalizr');

var _omit = require('lodash/omit');

var _omit2 = _interopRequireDefault(_omit);

var _schema = require('./schema');

var _getPersistenceActions = require('./getPersistenceActions');

var _getPersistenceActions2 = _interopRequireDefault(_getPersistenceActions);

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

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

// ===========
//   HELPERS
// ===========

var handleSingleObject = function handleSingleObject(data, schema) {
  var normalizedData = (0, _normalizr.normalize)(data, schema);
  return (0, _getPersistenceActions2.default)(normalizedData);
}; /* eslint "no-param-reassign": "warn" */


var handleArray = function handleArray(data, schema) {
  var normalizedData = (0, _normalizr.normalize)(data, [schema]);
  return (0, _getPersistenceActions2.default)(normalizedData);
};

var setQuizSessionKeys = function setQuizSessionKeys(quizSessionId, itemToKey) {
  itemToKey.quizSessionId = quizSessionId;
  itemToKey.id = quizSessionId + '_' + itemToKey.position;
};

var setQuizSessionKeysForList = function setQuizSessionKeysForList(quizSessionId, listOfItems) {
  return listOfItems.forEach(function (itemToKey) {
    return setQuizSessionKeys(quizSessionId, itemToKey);
  });
};

var omitDeprecatedQSData = function omitDeprecatedQSData(quizSessionData) {
  return (0, _omit2.default)(quizSessionData, ['resume_data', 'session_items', 'session_item_responses']);
};

// ============
//   HANDLERS
// ============

// these handlers are called in the success callback of an api call

// - normalize the data (via normaliz & schema)
// - get actions based on that data (via getPersistenceActions)
// return an array of those actions
// - adds id attribute to objects that need a uniq key in the store

// ========= Single Object Handlers =========

var handleQuizResponse = exports.handleQuizResponse = function handleQuizResponse(quizData) {
  return handleSingleObject(quizData, _schema.quizSchema);
};

var handleQuizAnalysisResponse = exports.handleQuizAnalysisResponse = function handleQuizAnalysisResponse(quizAnalysisData) {
  return handleSingleObject(quizAnalysisData, _schema.quizAnalysisSchema);
};

var handleQuizSessionResponse = exports.handleQuizSessionResponse = function handleQuizSessionResponse(quizSessionData) {
  return handleSingleObject(omitDeprecatedQSData(quizSessionData), _schema.quizSessionSchema);
};

var handleBankResponse = exports.handleBankResponse = function handleBankResponse(bankData) {
  return handleSingleObject(bankData, _schema.bankSchema);
};

var handleShareBankResponse = exports.handleShareBankResponse = function handleShareBankResponse(shareBankData) {
  return handleSingleObject(shareBankData, _schema.shareBankSchema);
};

var handleBankEntryResponse = exports.handleBankEntryResponse = function handleBankEntryResponse(bankEntryData) {
  return handleSingleObject(bankEntryData, _schema.bankEntrySchema);
};

var handleQuizEntryResponse = exports.handleQuizEntryResponse = function handleQuizEntryResponse(quizEntryData) {
  return handleSingleObject(quizEntryData, _schema.quizEntrySchema);
};

// ========= Array Handlers =========

var handleQtiImportsResponse = exports.handleQtiImportsResponse = function handleQtiImportsResponse(importsData) {
  return handleArray(importsData, _schema.qtiImportSchema);
};

var handleQuizSessionEventsResponse = exports.handleQuizSessionEventsResponse = function handleQuizSessionEventsResponse(quizSessionEventsData) {
  return handleArray(quizSessionEventsData, _schema.quizSessionEventSchema);
};

var handleItemAnalysesResponse = exports.handleItemAnalysesResponse = function handleItemAnalysesResponse(itemAnalysisData) {
  return handleArray(itemAnalysisData, _schema.itemAnalysisSchema);
};

var handleItemsResponse = exports.handleItemsResponse = function handleItemsResponse(itemsData) {
  return handleArray(itemsData, _schema.itemSchema);
};

var handleStimuliResponse = exports.handleStimuliResponse = function handleStimuliResponse(stimuliData) {
  return handleArray(stimuliData, _schema.stimulusSchema);
};

var handleInteractionTypesResponse = exports.handleInteractionTypesResponse = function handleInteractionTypesResponse(interactionTypesData) {
  return handleArray(interactionTypesData, _schema.interactionTypeSchema);
};

var handleQuizEntriesResponse = exports.handleQuizEntriesResponse = function handleQuizEntriesResponse(quizEntriesData) {
  return handleArray(quizEntriesData, _schema.quizEntriesSchema);
};

var handleSessionItemsResponse = exports.handleSessionItemsResponse = function handleSessionItemsResponse(sessionItemsData, quizSessionId) {
  setQuizSessionKeysForList(quizSessionId, sessionItemsData);
  return handleArray(sessionItemsData, _schema.sessionItemSchema);
};

var handleSessionItemResultsResponse = exports.handleSessionItemResultsResponse = function handleSessionItemResultsResponse(sessionItemResultsData, quizSessionId) {
  setQuizSessionKeysForList(quizSessionId, sessionItemResultsData);
  return handleArray(sessionItemResultsData, _schema.sessionItemResultSchema);
};

var handleResumeDataResponse = exports.handleResumeDataResponse = function handleResumeDataResponse(resumeData, quizSessionId) {
  setQuizSessionKeysForList(quizSessionId, resumeData);
  if (resumeData.length === 0) {
    return (0, _taking.loadResumeData)(resumeData);
  }
  return handleArray(resumeData, _schema.resumeDataSchema);
};

var handleQuizSessionResultsResponse = exports.handleQuizSessionResultsResponse = function handleQuizSessionResultsResponse(quizSessionResultsData) {
  return handleArray(quizSessionResultsData, _schema.quizSessionResultSchema);
};

var handleQuizSessionsResponse = exports.handleQuizSessionsResponse = function handleQuizSessionsResponse(quizSessionsData) {
  return handleArray(quizSessionsData.map(omitDeprecatedQSData), _schema.quizSessionSchema);
};

var handleBanksResponse = exports.handleBanksResponse = function handleBanksResponse(banksData) {
  return handleArray(banksData, _schema.bankSchema);
};

var handleShareBanksResponse = exports.handleShareBanksResponse = function handleShareBanksResponse(shareBankData) {
  return handleArray(shareBankData, _schema.shareBankSchema);
};

var handleBankEntriesResponse = exports.handleBankEntriesResponse = function handleBankEntriesResponse(bankEntriesData) {
  return handleArray(bankEntriesData, _schema.bankEntrySchema);
};