"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useUnreadMessagesCounter = exports.useUnread = void 0;
var _react = require("react");
var _roomStore = require("../roomStore");
const useUnreadMessagesCounter = () => {
  const subscribe = callback => {
    const unsubscribe = _roomStore.store.subscribe(() => {
      const state = _roomStore.store.getState();
      const rooms = state.rooms?.rooms;
      if (rooms) {
        Object.entries(rooms).forEach(([roomJid, room]) => {
          if (room.unreadMessages !== undefined) {
            callback();
          }
        });
      }
    });
    return unsubscribe;
  };
  const rooms = (0, _react.useSyncExternalStore)(subscribe, () => _roomStore.store.getState().rooms.rooms);
  const unreadByRoom = {};
  let totalCount = 0;
  Object.entries(rooms).forEach(([roomJid, room]) => {
    const unreadCount = room.unreadMessages || 0;
    if (unreadCount > 0) {
      unreadByRoom[roomJid] = unreadCount;
      totalCount += unreadCount;
    }
  });
  return {
    hasUnread: totalCount > 0,
    totalCount,
    unreadByRoom
  };
};
exports.useUnread = exports.useUnreadMessagesCounter = useUnreadMessagesCounter;
//# sourceMappingURL=useUnreadMessagesCounter.js.map