"use strict";

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

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

var _global = require("aws-sdk/global");

var _kinesis = _interopRequireDefault(require("aws-sdk/clients/kinesis"));

var _config = require("./config");

var _Fetcher = _interopRequireDefault(require("../util/Fetcher"));

var buildKinesisConfiguration = function buildKinesisConfiguration(_ref) {
  var configuration = _ref.configuration,
      credentials = _ref.credentials;
  var serverTime = configuration.server_time;
  if (serverTime) _global.config.systemClockOffset = Date.parse(serverTime) - Date.now();
  return {
    client: buildKinesisClient(configuration, credentials),
    streamConfigs: (0, _config.assembleStreamConfigs)(configuration)
  };
};

exports.buildKinesisConfiguration = buildKinesisConfiguration;

var buildKinesisClient = function buildKinesisClient(configuration, credentials) {
  var kinesisConfig = Object.assign({}, (0, _config.assembleKinesisConfig)(configuration.client_configuration), {
    credentials: credentials
  });
  return new _kinesis.default(kinesisConfig);
};

exports.buildKinesisClient = buildKinesisClient;

var buildCredentials = function buildCredentials(credentialParams) {
  var creds = new _global.Credentials(credentialParams.access_key_id, credentialParams.secret_access_key, credentialParams.session_token // credentialParams
  );
  creds.expireTime = Date.parse(credentialParams.expiration);
  return creds;
};

exports.buildCredentials = buildCredentials;

var getKinesisConfig = function getKinesisConfig(quizSessionId) {
  var fetcher = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : new _Fetcher.default();
  return new Promise(function (resolve, reject) {
    var url = "/api/quiz_sessions/".concat(quizSessionId, "/kinesis_credentials");
    fetcher.post(url).then(function (configuration) {
      var config = buildKinesisConfiguration({
        configuration: configuration,
        credentials: buildCredentials(configuration.credentials)
      });
      resolve(config);
    }).then(function (err) {
      return reject(err);
    });
  });
};

var _default = getKinesisConfig;
exports.default = _default;