"use strict";

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

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

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

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

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

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

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

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

var _StreamQueue = _interopRequireDefault(require("./StreamQueue"));

var _client = _interopRequireDefault(require("./client"));

var QuizEventsLogger =
/*#__PURE__*/
function () {
  function QuizEventsLogger() {
    (0, _classCallCheck2.default)(this, QuizEventsLogger);

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

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

  (0, _createClass2.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 ".concat(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;
}();

exports.QuizEventsLogger = QuizEventsLogger;
var loggers = {};

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

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

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

          case 5:
            kinesisConfig = _context.sent;
            kinesisClient = kinesisConfig.client; // Set up the StreamQueues for possible events

            streamQueues = kinesisConfig.streamConfigs.map(function (config) {
              return new _StreamQueue.default({
                config: config,
                kinesis: kinesisClient
              });
            });
            logger = (0, _construct2.default)(QuizEventsLogger, (0, _toConsumableArray2.default)(streamQueues));
            loggers[quizSessionId] = logger;

          case 10:
            return _context.abrupt("return", logger);

          case 11:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  }));

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

var _default = getLogger;
exports.default = _default;