var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.upsertChannels = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _upsertMembers = require("./upsertMembers");
var _upsertMessages = require("./upsertMessages");
var _upsertReads = require("./upsertReads");
var _mapChannelDataToStorable = require("../mappers/mapChannelDataToStorable");
var _createUpsertQuery = require("../sqlite-utils/createUpsertQuery");
var _SqliteClient = require("../SqliteClient");
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
var upsertChannels = exports.upsertChannels = function () {
  var _ref2 = (0, _asyncToGenerator2["default"])(_regenerator["default"].mark(function _callee(_ref) {
    var channels, _ref$execute, execute, isLatestMessagesSet, queries, channelIds, _loop, _iterator, _step, channel;
    return _regenerator["default"].wrap(function _callee$(_context2) {
      while (1) switch (_context2.prev = _context2.next) {
        case 0:
          channels = _ref.channels, _ref$execute = _ref.execute, execute = _ref$execute === void 0 ? true : _ref$execute, isLatestMessagesSet = _ref.isLatestMessagesSet;
          queries = [];
          channelIds = channels.map(function (channel) {
            return channel.channel.cid;
          });
          _SqliteClient.SqliteClient.logger == null || _SqliteClient.SqliteClient.logger('info', 'upsertChannels', {
            channelIds: channelIds
          });
          _loop = _regenerator["default"].mark(function _loop() {
            var members, membership, messages, read, _membership$user2;
            return _regenerator["default"].wrap(function _loop$(_context) {
              while (1) switch (_context.prev = _context.next) {
                case 0:
                  queries.push((0, _createUpsertQuery.createUpsertQuery)('channels', (0, _mapChannelDataToStorable.mapChannelDataToStorable)(channel.channel)));
                  members = channel.members, membership = channel.membership, messages = channel.messages, read = channel.read;
                  if (membership && !members.includes(function (m) {
                    var _m$user, _membership$user;
                    return ((_m$user = m.user) == null ? void 0 : _m$user.id) === ((_membership$user = membership.user) == null ? void 0 : _membership$user.id);
                  })) {
                    members.push(Object.assign({}, membership, {
                      user_id: (_membership$user2 = membership.user) == null ? void 0 : _membership$user2.id
                    }));
                  }
                  _context.t0 = queries;
                  _context.next = 6;
                  return (0, _upsertMembers.upsertMembers)({
                    cid: channel.channel.cid,
                    execute: false,
                    members: members
                  });
                case 6:
                  _context.t1 = _context.sent;
                  queries = _context.t0.concat.call(_context.t0, _context.t1);
                  if (!read) {
                    _context.next = 14;
                    break;
                  }
                  _context.t2 = queries;
                  _context.next = 12;
                  return (0, _upsertReads.upsertReads)({
                    cid: channel.channel.cid,
                    execute: false,
                    reads: read
                  });
                case 12:
                  _context.t3 = _context.sent;
                  queries = _context.t2.concat.call(_context.t2, _context.t3);
                case 14:
                  if (!isLatestMessagesSet) {
                    _context.next = 20;
                    break;
                  }
                  _context.t4 = queries;
                  _context.next = 18;
                  return (0, _upsertMessages.upsertMessages)({
                    execute: false,
                    messages: messages
                  });
                case 18:
                  _context.t5 = _context.sent;
                  queries = _context.t4.concat.call(_context.t4, _context.t5);
                case 20:
                case "end":
                  return _context.stop();
              }
            }, _loop);
          });
          _iterator = _createForOfIteratorHelper(channels);
          _context2.prev = 6;
          _iterator.s();
        case 8:
          if ((_step = _iterator.n()).done) {
            _context2.next = 13;
            break;
          }
          channel = _step.value;
          return _context2.delegateYield(_loop(), "t0", 11);
        case 11:
          _context2.next = 8;
          break;
        case 13:
          _context2.next = 18;
          break;
        case 15:
          _context2.prev = 15;
          _context2.t1 = _context2["catch"](6);
          _iterator.e(_context2.t1);
        case 18:
          _context2.prev = 18;
          _iterator.f();
          return _context2.finish(18);
        case 21:
          if (!execute) {
            _context2.next = 24;
            break;
          }
          _context2.next = 24;
          return _SqliteClient.SqliteClient.executeSqlBatch(queries);
        case 24:
          return _context2.abrupt("return", queries);
        case 25:
        case "end":
          return _context2.stop();
      }
    }, _callee, null, [[6, 15, 18, 21]]);
  }));
  return function upsertChannels(_x) {
    return _ref2.apply(this, arguments);
  };
}();
//# sourceMappingURL=upsertChannels.js.map