"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.store = exports.persistorReady = exports.getActiveRoom = void 0;
var _toolkit = require("@reduxjs/toolkit");
var _chatSettingsSlice = _interopRequireWildcard(require("./chatSettingsSlice"));
var _roomsSlice = _interopRequireWildcard(require("./roomsSlice"));
var _roomHeapSlice = require("./roomHeapSlice");
var _unreadMidlleware = require("./Middleware/unreadMidlleware");
var _logoutMiddleware = require("./Middleware/logoutMiddleware");
var _newMessageMidlleware = require("./Middleware/newMessageMidlleware");
var _reactionsMiddleware = require("./Middleware/reactionsMiddleware");
var _persistence = require("./persistence");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
const rootReducer = (0, _toolkit.combineReducers)({
  chatSettingStore: _chatSettingsSlice.default,
  rooms: _roomsSlice.default,
  roomHeapSlice: _roomHeapSlice.roomHeapSlice.reducer
});
const store = exports.store = (0, _toolkit.configureStore)({
  reducer: rootReducer,
  middleware: getDefaultMiddleware => getDefaultMiddleware({
    serializableCheck: {
      // Slice names: chatSlice→'chat', roomsStore→'roomMessages'.
      ignoredActions: ['chat/addMessage', 'chat/setStoreClient', 'chat/setConfig', 'roomMessages/addRoom'],
      ignoredActionPaths: ['payload.client', 'payload.config'],
      ignoredPaths: ['chat.messages.timestamp', 'chatSettingStore.client', 'chatSettingStore.config']
    }
  }).concat(_unreadMidlleware.unreadMiddleware).concat(_newMessageMidlleware.newMessageMidlleware).concat(_reactionsMiddleware.reactionsMiddleware).concat(_logoutMiddleware.logoutMiddleware).concat(_persistence.persistenceMiddleware)
});
const getActiveRoom = state => {
  const roomMessagesState = state.rooms;
  return roomMessagesState.activeRoomJID ? roomMessagesState.rooms[roomMessagesState.activeRoomJID] : null;
};

// Async rehydrate — read persisted slices and replay them as standard
// actions. Idempotent: only rehydrates when a slice has data.
exports.getActiveRoom = getActiveRoom;
const persistorReady = exports.persistorReady = (async () => {
  const {
    chat,
    rooms
  } = await (0, _persistence.readPersistedState)();
  if (chat?.user && chat.user.walletAddress) {
    store.dispatch((0, _chatSettingsSlice.setUser)(chat.user));
  }
  if (rooms?.rooms) {
    for (const [jid, room] of Object.entries(rooms.rooms)) {
      if (!jid || !room) {
        continue;
      }
      store.dispatch((0, _roomsSlice.addRoom)({
        roomData: room
      }));
    }
  }
})();
//# sourceMappingURL=index.js.map