var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.upsertMessages = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _mapMessageToStorable = require("../mappers/mapMessageToStorable");
var _mapPollToStorable = require("../mappers/mapPollToStorable");
var _mapReactionToStorable = require("../mappers/mapReactionToStorable");
var _mapUserToStorable = require("../mappers/mapUserToStorable");
var _createUpsertQuery = require("../sqlite-utils/createUpsertQuery");
var _SqliteClient = require("../SqliteClient");
var upsertMessages = exports.upsertMessages = function () {
  var _ref2 = (0, _asyncToGenerator2["default"])(_regenerator["default"].mark(function _callee(_ref) {
    var _ref$execute, execute, messages, storableMessages, storableUsers, storableReactions, storablePolls, finalQueries;
    return _regenerator["default"].wrap(function _callee$(_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          _ref$execute = _ref.execute, execute = _ref$execute === void 0 ? true : _ref$execute, messages = _ref.messages;
          storableMessages = [];
          storableUsers = [];
          storableReactions = [];
          storablePolls = [];
          messages == null || messages.forEach(function (message) {
            storableMessages.push((0, _mapMessageToStorable.mapMessageToStorable)(message));
            if (message.user) {
              storableUsers.push((0, _mapUserToStorable.mapUserToStorable)(message.user));
            }
            [].concat((0, _toConsumableArray2["default"])(message.latest_reactions || []), (0, _toConsumableArray2["default"])(message.own_reactions || [])).forEach(function (r) {
              if (r.user) {
                storableUsers.push((0, _mapUserToStorable.mapUserToStorable)(r.user));
              }
              storableReactions.push((0, _mapReactionToStorable.mapReactionToStorable)(r));
            });
            if (message.poll) {
              storablePolls.push((0, _mapPollToStorable.mapPollToStorable)(message.poll));
            }
          });
          finalQueries = [].concat((0, _toConsumableArray2["default"])(storableMessages.map(function (storableMessage) {
            return (0, _createUpsertQuery.createUpsertQuery)('messages', storableMessage);
          })), (0, _toConsumableArray2["default"])(storableUsers.map(function (storableUser) {
            return (0, _createUpsertQuery.createUpsertQuery)('users', storableUser);
          })), (0, _toConsumableArray2["default"])(storableReactions.map(function (storableReaction) {
            return (0, _createUpsertQuery.createUpsertQuery)('reactions', storableReaction);
          })), (0, _toConsumableArray2["default"])(storablePolls.map(function (storablePoll) {
            return (0, _createUpsertQuery.createUpsertQuery)('poll', storablePoll);
          })));
          _SqliteClient.SqliteClient.logger == null || _SqliteClient.SqliteClient.logger('info', 'upsertMessages', {
            execute: execute,
            messages: storableMessages,
            polls: storablePolls,
            reactions: storableReactions,
            users: storableUsers
          });
          if (!execute) {
            _context.next = 11;
            break;
          }
          _context.next = 11;
          return _SqliteClient.SqliteClient.executeSqlBatch(finalQueries);
        case 11:
          return _context.abrupt("return", finalQueries);
        case 12:
        case "end":
          return _context.stop();
      }
    }, _callee);
  }));
  return function upsertMessages(_x) {
    return _ref2.apply(this, arguments);
  };
}();
//# sourceMappingURL=upsertMessages.js.map