var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.updateMessage = 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 _mapReactionToStorable = require("../mappers/mapReactionToStorable");
var _mapUserToStorable = require("../mappers/mapUserToStorable");
var _createSelectQuery = require("../sqlite-utils/createSelectQuery");
var _createUpdateQuery = require("../sqlite-utils/createUpdateQuery");
var _createUpsertQuery = require("../sqlite-utils/createUpsertQuery");
var _SqliteClient = require("../SqliteClient");
var updateMessage = exports.updateMessage = function () {
  var _ref2 = (0, _asyncToGenerator2["default"])(_regenerator["default"].mark(function _callee(_ref) {
    var _ref$execute, execute, message, queries, messages, storableMessage, storableUsers, storableUser, latestReactions, ownReactions, storableReactions;
    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, message = _ref.message;
          queries = [];
          _context.next = 4;
          return _SqliteClient.SqliteClient.executeSql.apply(null, (0, _createSelectQuery.createSelectQuery)('messages', ['*'], {
            id: message.id
          }));
        case 4:
          messages = _context.sent;
          if (!(messages.length === 0)) {
            _context.next = 7;
            break;
          }
          return _context.abrupt("return", queries);
        case 7:
          storableMessage = (0, _mapMessageToStorable.mapMessageToStorable)(Object.assign({}, message));
          queries.push((0, _createUpdateQuery.createUpdateQuery)('messages', storableMessage, {
            id: message.id
          }));
          storableUsers = [];
          if (message.user) {
            storableUser = (0, _mapUserToStorable.mapUserToStorable)(message.user);
            storableUsers.push(storableUser);
            queries.push((0, _createUpsertQuery.createUpsertQuery)('users', storableUser));
          }
          latestReactions = message.latest_reactions || [];
          ownReactions = message.own_reactions || [];
          storableReactions = [];
          [].concat((0, _toConsumableArray2["default"])(latestReactions), (0, _toConsumableArray2["default"])(ownReactions)).forEach(function (r) {
            if (r.user) {
              var _storableUser = (0, _mapUserToStorable.mapUserToStorable)(r.user);
              storableUsers.push(_storableUser);
              queries.push((0, _createUpsertQuery.createUpsertQuery)('users', _storableUser));
            }
            var storableReaction = (0, _mapReactionToStorable.mapReactionToStorable)(r);
            storableReactions.push(storableReaction);
            queries.push((0, _createUpsertQuery.createUpsertQuery)('reactions', storableReaction));
          });
          _SqliteClient.SqliteClient.logger == null || _SqliteClient.SqliteClient.logger('info', 'updateMessage', {
            message: storableMessage,
            reactions: storableReactions,
            users: storableUsers
          });
          if (!execute) {
            _context.next = 19;
            break;
          }
          _context.next = 19;
          return _SqliteClient.SqliteClient.executeSqlBatch(queries);
        case 19:
          return _context.abrupt("return", queries);
        case 20:
        case "end":
          return _context.stop();
      }
    }, _callee);
  }));
  return function updateMessage(_x) {
    return _ref2.apply(this, arguments);
  };
}();
//# sourceMappingURL=updateMessage.js.map