'use strict';

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

var _regenerator = require('babel-runtime/regenerator');

var _regenerator2 = _interopRequireDefault(_regenerator);

var _toConsumableArray2 = require('babel-runtime/helpers/toConsumableArray');

var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2);

var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');

var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _StreamQueue = require('./StreamQueue');

var _StreamQueue2 = _interopRequireDefault(_StreamQueue);

var _client = require('./client');

var _client2 = _interopRequireDefault(_client);

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

var QuizEventsLogger = exports.QuizEventsLogger = function () {
  function QuizEventsLogger() {
    (0, _classCallCheck3.default)(this, QuizEventsLogger);

    for (var _len = arguments.length, streamQueues = Array(_len), _key = 0; _key < _len; _key++) {
      streamQueues[_key] = arguments[_key];
    }

    this.streamQueues = streamQueues;
    this.eventTypeDict = Object.create(null);
    this.builtAt = Date.now();
  }

  (0, _createClass3.default)(QuizEventsLogger, [{
    key: 'needsRefresh',
    value: function needsRefresh() {
      return Date.now() - this.builtAt > 600000; // 10 minutes
    }
  }, {
    key: 'queuesForEventType',
    value: function queuesForEventType(eventType) {
      var queues = this.eventTypeDict[eventType];
      if (queues !== void 0) return queues;

      queues = this.streamQueues.filter(function (queue) {
        return Object.keys(queue.config.eventTypes).includes(eventType);
      });

      if (!queues.length) {
        throw new Error('Unable to locate a valid StreamQueue for event type ' + eventType);
      }

      this.eventTypeDict[eventType] = queues;

      return queues;
    }
  }, {
    key: 'enqueueEvent',
    value: function enqueueEvent(data) {
      var eventType = data.eventType;

      this.queuesForEventType(eventType).forEach(function (queue) {
        queue.push(eventType, data);
      });
    }
  }]);
  return QuizEventsLogger;
}();

var loggers = {};

var getLogger = function () {
  var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee(quizSessionId) {
    var kinesisConfigGetter = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _client2.default;
    var logger, kinesisConfig, kinesisClient, streamQueues;
    return _regenerator2.default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            logger = loggers[quizSessionId];

            if (!(!logger || logger.needsRefresh())) {
              _context.next = 9;
              break;
            }

            _context.next = 4;
            return kinesisConfigGetter(quizSessionId);

          case 4:
            kinesisConfig = _context.sent;
            kinesisClient = kinesisConfig.client;

            // Set up the StreamQueues for possible events

            streamQueues = kinesisConfig.streamConfigs.map(function (config) {
              return new _StreamQueue2.default({ config: config, kinesis: kinesisClient });
            });

            logger = new (Function.prototype.bind.apply(QuizEventsLogger, [null].concat((0, _toConsumableArray3.default)(streamQueues))))();
            loggers[quizSessionId] = logger;

          case 9:
            return _context.abrupt('return', logger);

          case 10:
          case 'end':
            return _context.stop();
        }
      }
    }, _callee, this);
  }));

  return function getLogger(_x) {
    return _ref.apply(this, arguments);
  };
}();

exports.default = getLogger;