var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.addReactionToLocalState = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _apis = require("../store/apis");
var addReactionToLocalState = exports.addReactionToLocalState = function () {
  var _ref2 = (0, _asyncToGenerator2["default"])(_regenerator["default"].mark(function _callee(_ref) {
    var channel, enforceUniqueReaction, messageId, reactionType, user, message, reaction, hasOwnReaction, messageWithReaction;
    return _regenerator["default"].wrap(function _callee$(_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          channel = _ref.channel, enforceUniqueReaction = _ref.enforceUniqueReaction, messageId = _ref.messageId, reactionType = _ref.reactionType, user = _ref.user;
          message = channel.state.messages.find(function (_ref3) {
            var id = _ref3.id;
            return id === messageId;
          });
          if (message) {
            _context.next = 4;
            break;
          }
          return _context.abrupt("return");
        case 4:
          reaction = {
            created_at: new Date().toISOString(),
            message_id: messageId,
            type: reactionType,
            updated_at: new Date().toISOString(),
            user: user,
            user_id: user == null ? void 0 : user.id
          };
          hasOwnReaction = message.own_reactions && message.own_reactions.length > 0;
          messageWithReaction = channel.state.addReaction(reaction, undefined, enforceUniqueReaction);
          if (messageWithReaction) {
            _context.next = 9;
            break;
          }
          return _context.abrupt("return");
        case 9:
          if (!(enforceUniqueReaction && hasOwnReaction)) {
            _context.next = 14;
            break;
          }
          _context.next = 12;
          return (0, _apis.updateReaction)({
            message: messageWithReaction,
            reaction: reaction
          });
        case 12:
          _context.next = 16;
          break;
        case 14:
          _context.next = 16;
          return (0, _apis.insertReaction)({
            message: messageWithReaction,
            reaction: reaction
          });
        case 16:
        case "end":
          return _context.stop();
      }
    }, _callee);
  }));
  return function addReactionToLocalState(_x) {
    return _ref2.apply(this, arguments);
  };
}();
//# sourceMappingURL=addReactionToLocalState.js.map