'use strict';

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

var _serializeEvent = require('../util/serializeEvent');

var _serializeEvent2 = _interopRequireDefault(_serializeEvent);

var _QuizEventsLogger = require('../kinesis/QuizEventsLogger');

var _QuizEventsLogger2 = _interopRequireDefault(_QuizEventsLogger);

var _stateHelper = require('../util/stateHelper');

var _stateHelper2 = _interopRequireDefault(_stateHelper);

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

// ==============================================================
// this middleware queues payloads to the logger for handling
// ==============================================================

var eventLoggerMiddlewareCreator = function eventLoggerMiddlewareCreator() {
  var loggerFetch = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _QuizEventsLogger2.default;
  var serializer = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _serializeEvent2.default;
  return function (store) {
    return function (next) {
      return function (action) {
        var result = next(action);

        var eventJSON = serializer(action, store);

        if (eventJSON) {
          var quizSessionId = _stateHelper2.default.activeQuizSessionId();

          loggerFetch(quizSessionId).then(function (logger) {
            logger.enqueueEvent(eventJSON);
          });
        }

        return result;
      };
    };
  };
};

exports.default = eventLoggerMiddlewareCreator;