"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useUnreadMessagesCounter = exports.useUnread = void 0;
var _react = require("react");
var _roomStore = require("../roomStore");
const useUnreadMessagesCounter = () => {
  // Notify React on EVERY store change and let useSyncExternalStore's
  // snapshot comparison (Object.is on the `rooms` reference) decide
  // whether to re-render. The previous implementation only fired the
  // callback while iterating rooms that already had `unreadMessages`
  // defined — so when `rooms` became empty (logout, or the last room
  // removed) the loop body never ran, the callback never fired, and the
  // hook never re-rendered: a consumer's badge stayed stuck at its last
  // non-zero count after logout. (It's also cheaper — one callback per
  // change instead of one per unread room. Redux Toolkit keeps the
  // `rooms` slice reference stable across actions that don't touch it,
  // so non-rooms dispatches still don't trigger a re-render.)
  const subscribe = callback => _roomStore.store.subscribe(callback);
  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