"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.postQuizSessionResults = void 0;

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

var _constants = require("@instructure/quiz-common/lib/constants");

var _Fetcher = _interopRequireDefault(require("../../common/util/Fetcher"));

var _postMessage = require("../../common/actions/postMessage");

var _callHandlers = require("../../common/api/callHandlers");

var _quizSessions = require("../../common/actions/quizSessions");

var removeIdsFromModifications = function removeIdsFromModifications(modifiedResults) {
  return modifiedResults.map(function (result) {
    return (0, _omit.default)(result, 'id');
  });
};

var postQuizSessionResults = function postQuizSessionResults(sessionId, sessionItemResultsModifications, fudgePoints) {
  var fetchQuizSessionCb = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : _quizSessions.getResultInfo;
  var quizSessionUrl = "/api/quiz_sessions/".concat(sessionId);
  var url = "".concat(quizSessionUrl, "/results");
  return function (dispatch, getState) {
    var fetchLib = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : fetch;
    var modifiedResultsWithoutIds = removeIdsFromModifications(sessionItemResultsModifications);
    var params = {
      body: JSON.stringify({
        results: modifiedResultsWithoutIds,
        fudge_points: fudgePoints
      })
    };
    var fetcher = new _Fetcher.default();
    return fetcher.post(url, params).then(function (quizSessionResult) {
      return dispatch([(0, _postMessage.postToParent)({
        subject: _constants.SUBMISSION_UPDATE_SUBJECT
      }), (0, _callHandlers.handleQuizSessionResultsResponse)([quizSessionResult]), fetchQuizSessionCb(sessionId)]);
    });
  };
};

exports.postQuizSessionResults = postQuizSessionResults;