Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useMessageList = exports.shouldIncludeMessageInList = void 0;
var _react = require("react");
var _ChannelContext = require("../../../contexts/channelContext/ChannelContext");
var _ChatContext = require("../../../contexts/chatContext/ChatContext");
var _MessagesContext = require("../../../contexts/messagesContext/MessagesContext");
var _PaginatedMessageListContext = require("../../../contexts/paginatedMessageListContext/PaginatedMessageListContext");
var _ThreadContext = require("../../../contexts/threadContext/ThreadContext");
var _getDateSeparators = require("../utils/getDateSeparators");
var _getGroupStyles = require("../utils/getGroupStyles");
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 shouldIncludeMessageInList = exports.shouldIncludeMessageInList = function shouldIncludeMessageInList(message, options) {
  var _message$user, _message$user2;
  var deletedMessagesVisibilityType = options.deletedMessagesVisibilityType,
    userId = options.userId;
  var isMessageTypeDeleted = message.type === 'deleted';
  switch (deletedMessagesVisibilityType) {
    case 'sender':
      return !isMessageTypeDeleted || ((_message$user = message.user) == null ? void 0 : _message$user.id) === userId;
    case 'receiver':
      return !isMessageTypeDeleted || ((_message$user2 = message.user) == null ? void 0 : _message$user2.id) !== userId;
    case 'never':
      return !isMessageTypeDeleted;
    default:
      return !!message;
  }
};
var useMessageList = exports.useMessageList = function useMessageList(params) {
  var noGroupByUser = params.noGroupByUser,
    threadList = params.threadList;
  var _useChatContext = (0, _ChatContext.useChatContext)(),
    client = _useChatContext.client;
  var _useChannelContext = (0, _ChannelContext.useChannelContext)(),
    hideDateSeparators = _useChannelContext.hideDateSeparators,
    maxTimeBetweenGroupedMessages = _useChannelContext.maxTimeBetweenGroupedMessages;
  var _useMessagesContext = (0, _MessagesContext.useMessagesContext)(),
    deletedMessagesVisibilityType = _useMessagesContext.deletedMessagesVisibilityType,
    _useMessagesContext$g = _useMessagesContext.getMessagesGroupStyles,
    getMessagesGroupStyles = _useMessagesContext$g === void 0 ? _getGroupStyles.getGroupStyles : _useMessagesContext$g;
  var _usePaginatedMessageL = (0, _PaginatedMessageListContext.usePaginatedMessageListContext)(),
    messages = _usePaginatedMessageL.messages;
  var _useThreadContext = (0, _ThreadContext.useThreadContext)(),
    threadMessages = _useThreadContext.threadMessages;
  var messageList = threadList ? threadMessages : messages;
  var dateSeparators = (0, _react.useMemo)(function () {
    return (0, _getDateSeparators.getDateSeparators)({
      deletedMessagesVisibilityType: deletedMessagesVisibilityType,
      hideDateSeparators: hideDateSeparators,
      messages: messageList,
      userId: client.userID
    });
  }, [deletedMessagesVisibilityType, hideDateSeparators, messageList, client.userID]);
  var dateSeparatorsRef = (0, _react.useRef)(dateSeparators);
  dateSeparatorsRef.current = dateSeparators;
  var messageGroupStyles = (0, _react.useMemo)(function () {
    return getMessagesGroupStyles({
      dateSeparators: dateSeparatorsRef.current,
      hideDateSeparators: hideDateSeparators,
      maxTimeBetweenGroupedMessages: maxTimeBetweenGroupedMessages,
      messages: messageList,
      noGroupByUser: noGroupByUser,
      userId: client.userID
    });
  }, [dateSeparatorsRef, getMessagesGroupStyles, hideDateSeparators, maxTimeBetweenGroupedMessages, messageList, noGroupByUser, client.userID]);
  var messageGroupStylesRef = (0, _react.useRef)(messageGroupStyles);
  messageGroupStylesRef.current = messageGroupStyles;
  var processedMessageList = (0, _react.useMemo)(function () {
    var newMessageList = [];
    var _iterator = _createForOfIteratorHelper(messageList),
      _step;
    try {
      for (_iterator.s(); !(_step = _iterator.n()).done;) {
        var message = _step.value;
        if (shouldIncludeMessageInList(message, {
          deletedMessagesVisibilityType: deletedMessagesVisibilityType,
          userId: client.userID
        })) {
          newMessageList.unshift(message);
        }
      }
    } catch (err) {
      _iterator.e(err);
    } finally {
      _iterator.f();
    }
    return newMessageList;
  }, [client.userID, deletedMessagesVisibilityType, messageList]);
  return {
    dateSeparatorsRef: dateSeparatorsRef,
    messageGroupStylesRef: messageGroupStylesRef,
    processedMessageList: processedMessageList,
    rawMessageList: messageList
  };
};
//# sourceMappingURL=useMessageList.js.map