"use strict";

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

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

var _serializeEvent = _interopRequireDefault(require("../util/serializeEvent"));

var _QuizEventsLogger = _interopRequireDefault(require("../kinesis/QuizEventsLogger"));

var _quizSessions = require("../selectors/quizSessions");

// ==============================================================
// this middleware queues payloads to the logger for handling
// ==============================================================
var eventLoggerMiddlewareCreator = function eventLoggerMiddlewareCreator() {
  var loggerFetch = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _QuizEventsLogger.default;
  var serializer = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _serializeEvent.default;
  return function (store) {
    return function (next) {
      return function (action) {
        var result = next(action);
        var eventJSON = serializer(action, store);

        if (eventJSON) {
          var quizSessionId = (0, _quizSessions.getActiveQuizSessionId)(store.getState());
          loggerFetch(quizSessionId).then(function (logger) {
            logger.enqueueEvent(eventJSON);
          });
        }

        return result;
      };
    };
  };
};

var _default = eventLoggerMiddlewareCreator;
exports.default = _default;