var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.selectMessagesForChannels = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _schema = require("../../schema");
var _SqliteClient = require("../../SqliteClient");
var selectMessagesForChannels = exports.selectMessagesForChannels = function () {
  var _ref = (0, _asyncToGenerator2["default"])(_regenerator["default"].mark(function _callee(cids) {
    var questionMarks, messagesColumnNames, userColumnNames, result;
    return _regenerator["default"].wrap(function _callee$(_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          questionMarks = Array(cids.length).fill('?').join(',');
          messagesColumnNames = Object.keys(_schema.tables.messages.columns).map(function (name) {
            return "'".concat(name, "', a.").concat(name);
          }).join(', ');
          userColumnNames = Object.keys(_schema.tables.users.columns).map(function (name) {
            return "'".concat(name, "', b.").concat(name);
          }).join(', ');
          _SqliteClient.SqliteClient.logger == null || _SqliteClient.SqliteClient.logger('info', 'selectMessagesForChannels', {
            cids: cids
          });
          _context.next = 6;
          return _SqliteClient.SqliteClient.executeSql("SELECT\n      json_object(\n        'user', json_object(\n          ".concat(userColumnNames, "\n        ),\n        ").concat(messagesColumnNames, "\n      ) as value\n    FROM (\n      SELECT\n        *,\n        ROW_NUMBER() OVER (\n          PARTITION BY cid\n          ORDER BY createdAt DESC\n        ) RowNum\n      FROM messages\n      WHERE cid in (").concat(questionMarks, ")\n    ) a\n    LEFT JOIN\n      users b\n    ON b.id = a.userId\n    WHERE RowNum < 200\n    ORDER BY a.createdAt ASC"), cids);
        case 6:
          result = _context.sent;
          return _context.abrupt("return", result.map(function (r) {
            return JSON.parse(r.value);
          }));
        case 8:
        case "end":
          return _context.stop();
      }
    }, _callee);
  }));
  return function selectMessagesForChannels(_x) {
    return _ref.apply(this, arguments);
  };
}();
//# sourceMappingURL=selectMessagesForChannels.js.map