var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.selectReactionsForMessages = 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 _schema = require("../../schema");
var _SqliteClient = require("../../SqliteClient");
var selectReactionsForMessages = exports.selectReactionsForMessages = function () {
  var _ref = (0, _asyncToGenerator2["default"])(_regenerator["default"].mark(function _callee(messageIds) {
    var _sort$find;
    var limit,
      filters,
      sort,
      questionMarks,
      reactionsColumnNames,
      userColumnNames,
      filterValue,
      createdAtSort,
      orderByClause,
      result,
      _args = arguments;
    return _regenerator["default"].wrap(function _callee$(_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          limit = _args.length > 1 && _args[1] !== undefined ? _args[1] : 25;
          filters = _args.length > 2 ? _args[2] : undefined;
          sort = _args.length > 3 ? _args[3] : undefined;
          questionMarks = Array(messageIds.length).fill('?').join(',');
          reactionsColumnNames = Object.keys(_schema.tables.reactions.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(', ');
          filterValue = filters != null && filters.type ? [typeof filters.type === 'string' ? filters.type : filters.type.$eq] : [];
          createdAtSort = Array.isArray(sort) ? (_sort$find = sort.find(function (s) {
            return !!s.created_at;
          })) == null ? void 0 : _sort$find.created_at : sort == null ? void 0 : sort.created_at;
          orderByClause = createdAtSort ? "ORDER BY cast(strftime('%s', a.createdAt) AS INTEGER) ".concat(createdAtSort === 1 ? 'ASC' : 'DESC') : '';
          _SqliteClient.SqliteClient.logger == null || _SqliteClient.SqliteClient.logger('info', 'selectReactionsForMessages', {
            messageIds: messageIds
          });
          _context.next = 12;
          return _SqliteClient.SqliteClient.executeSql("SELECT\n      json_object(\n        'user', json_object(\n          ".concat(userColumnNames, "\n        ),\n        ").concat(reactionsColumnNames, "\n      ) as value\n    FROM reactions a\n    LEFT JOIN\n      users b\n    ON b.id = a.userId\n    WHERE a.messageId in (").concat(questionMarks, ") ").concat(filters != null && filters.type ? "AND a.type = ?" : '', "\n    ").concat(orderByClause, "\n    ").concat(limit ? 'LIMIT ?' : ''), [].concat((0, _toConsumableArray2["default"])(messageIds), filterValue, (0, _toConsumableArray2["default"])(limit ? [limit] : [])));
        case 12:
          result = _context.sent;
          return _context.abrupt("return", result.map(function (r) {
            return JSON.parse(r.value);
          }));
        case 14:
        case "end":
          return _context.stop();
      }
    }, _callee);
  }));
  return function selectReactionsForMessages(_x) {
    return _ref.apply(this, arguments);
  };
}();
//# sourceMappingURL=selectReactionsForMessages.js.map