"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

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 = void 0;

var _normalizr = require("normalizr");

var _omit = _interopRequireDefault(require("lodash/omit"));

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

var _getPersistenceActions = _interopRequireDefault(require("./getPersistenceActions"));

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

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

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

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

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

var omitDeprecatedQSData = function omitDeprecatedQSData(quizSessionData) {
  return (0, _omit.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 = function handleQuizResponse(quizData) {
  return handleSingleObject(quizData, _schema.quizSchema);
};

exports.handleQuizResponse = handleQuizResponse;

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

exports.handleQuizAnalysisResponse = handleQuizAnalysisResponse;

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

exports.handleQuizSessionResponse = handleQuizSessionResponse;

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

exports.handleBankResponse = handleBankResponse;

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

exports.handleShareBankResponse = handleShareBankResponse;

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

exports.handleBankEntryResponse = handleBankEntryResponse;

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


exports.handleQuizEntryResponse = handleQuizEntryResponse;

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

exports.handleQtiImportsResponse = handleQtiImportsResponse;

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

exports.handleQuizSessionEventsResponse = handleQuizSessionEventsResponse;

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

exports.handleItemAnalysesResponse = handleItemAnalysesResponse;

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

exports.handleItemsResponse = handleItemsResponse;

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

exports.handleStimuliResponse = handleStimuliResponse;

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

exports.handleInteractionTypesResponse = handleInteractionTypesResponse;

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

exports.handleQuizEntriesResponse = handleQuizEntriesResponse;

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

exports.handleSessionItemsResponse = handleSessionItemsResponse;

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

exports.handleSessionItemResultsResponse = handleSessionItemResultsResponse;

var handleResumeDataResponse = function handleResumeDataResponse(resumeData, quizSessionId) {
  setQuizSessionKeysForList(quizSessionId, resumeData);

  if (resumeData.length === 0) {
    return (0, _taking.loadResumeData)(resumeData);
  }

  return handleArray(resumeData, _schema.resumeDataSchema);
};

exports.handleResumeDataResponse = handleResumeDataResponse;

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

exports.handleQuizSessionResultsResponse = handleQuizSessionResultsResponse;

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

exports.handleQuizSessionsResponse = handleQuizSessionsResponse;

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

exports.handleBanksResponse = handleBanksResponse;

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

exports.handleShareBanksResponse = handleShareBanksResponse;

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

exports.handleBankEntriesResponse = handleBankEntriesResponse;