var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getChannels = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _getChannelMessages = require("./getChannelMessages");
var _getMembers = require("./getMembers");
var _getReads = require("./getReads");
var _selectChannels = require("./queries/selectChannels");
var _mapStorableToChannel = require("../mappers/mapStorableToChannel");
var _SqliteClient = require("../SqliteClient");
var getChannels = exports.getChannels = function () {
  var _ref2 = (0, _asyncToGenerator2["default"])(_regenerator["default"].mark(function _callee(_ref) {
    var channelIds, currentUserId, _yield$Promise$all, _yield$Promise$all2, channels, cidVsMembers, cidVsReads, cidVsMessages;
    return _regenerator["default"].wrap(function _callee$(_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          channelIds = _ref.channelIds, currentUserId = _ref.currentUserId;
          _SqliteClient.SqliteClient.logger == null || _SqliteClient.SqliteClient.logger('info', 'getChannels', {
            channelIds: channelIds,
            currentUserId: currentUserId
          });
          _context.next = 4;
          return Promise.all([(0, _selectChannels.selectChannels)({
            channelIds: channelIds
          }), (0, _getMembers.getMembers)({
            channelIds: channelIds
          }), (0, _getReads.getReads)({
            channelIds: channelIds
          }), (0, _getChannelMessages.getChannelMessages)({
            channelIds: channelIds,
            currentUserId: currentUserId
          })]);
        case 4:
          _yield$Promise$all = _context.sent;
          _yield$Promise$all2 = (0, _slicedToArray2["default"])(_yield$Promise$all, 4);
          channels = _yield$Promise$all2[0];
          cidVsMembers = _yield$Promise$all2[1];
          cidVsReads = _yield$Promise$all2[2];
          cidVsMessages = _yield$Promise$all2[3];
          return _context.abrupt("return", channels.map(function (c) {
            return Object.assign({}, (0, _mapStorableToChannel.mapStorableToChannel)(c), {
              members: cidVsMembers[c.cid] || [],
              membership: (cidVsMembers[c.cid] || []).find(function (member) {
                return member.user_id === currentUserId;
              }),
              messages: cidVsMessages[c.cid] || [],
              pinned_messages: [],
              read: cidVsReads[c.cid] || []
            });
          }));
        case 11:
        case "end":
          return _context.stop();
      }
    }, _callee);
  }));
  return function getChannels(_x) {
    return _ref2.apply(this, arguments);
  };
}();
//# sourceMappingURL=getChannels.js.map