"use strict";

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

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

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

var _stateHelper = _interopRequireDefault(require("./stateHelper"));

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

var whitelist = [_constants.QUIZ_SUBMISSION_EVENT, _constants.RESPONSE, _constants.SESSION_STARTED_OR_RESUMED_EVENT];

function serializeEvent(action) {
  var type = action.type;

  if (!whitelist.includes(type)) {
    return false;
  }

  var quizSession = _stateHelper.default.activeQuizSession();

  var defaultEventData = {
    browserSessionId: (0, _taking.getBrowserSessionId)(_stateHelper.default.state()),
    clientTimestamp: new Date()
  };
  var authoritativeData = {
    accountUuid: quizSession.accountUuid,
    eventCreationToken: quizSession.eventCreationToken,
    eventType: type,
    quizSessionId: quizSession.id
  };
  return Object.assign({
    eventData: Object.assign({}, action.payload, defaultEventData)
  }, authoritativeData);
}