"use strict";

import { useSyncExternalStore } from 'react';
import { store } from '../roomStore';
export const useUnreadMessagesCounter = () => {
  const subscribe = callback => {
    const unsubscribe = store.subscribe(() => {
      const state = store.getState();
      const rooms = state.rooms?.rooms;
      if (rooms) {
        Object.entries(rooms).forEach(([roomJid, room]) => {
          if (room.unreadMessages !== undefined) {
            callback();
          }
        });
      }
    });
    return unsubscribe;
  };
  const rooms = useSyncExternalStore(subscribe, () => 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
  };
};
export { useUnreadMessagesCounter as useUnread };
//# sourceMappingURL=useUnreadMessagesCounter.js.map