"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = require("react");
var _hooks = require("./hooks");
var _xmppProvider = require("../context/xmppProvider");
var _createRoomFromApi = require("../helpers/createRoomFromApi");
var _rooms = require("../networking/api-requests/rooms.api");
var _roomsSlice = require("../roomStore/roomsSlice");
var _pushSubscriptionService = require("../services/pushSubscriptionService");
const useGetNewArchRoom = () => {
  const {
    client
  } = (0, _xmppProvider.useXmppClient)();
  const dispatch = (0, _hooks.useAppDispatch)();
  const syncRooms = (0, _react.useCallback)(async (client, config) => {
    const rooms = await (0, _rooms.getRooms)();
    const roomJIDs = [];
    rooms?.items?.forEach(room => {
      const createdRoom = (0, _createRoomFromApi.createRoomFromApi)(room, config?.xmppSettings?.conference);
      if (createdRoom) {
        dispatch((0, _roomsSlice.addRoomViaApi)({
          room: createdRoom,
          xmpp: client
        }));
        if (createdRoom.jid) {
          roomJIDs.push(createdRoom.jid);
        }
      }
    });
    dispatch((0, _roomsSlice.setIsLoading)({
      loading: false,
      loadingText: undefined
    }));
    if (client?.client && roomJIDs.length > 0) {
      const userNick = client.client.jid?.getLocal();
      await _pushSubscriptionService.pushSubscriptionService.subscribeToRooms(client.client, roomJIDs, userNick).catch(error => {
        console.error('Failed to subscribe to loaded rooms for push:', error);
      });
    }
    return rooms?.items || [];
  }, [dispatch]);
  return syncRooms;
};
var _default = exports.default = useGetNewArchRoom;
//# sourceMappingURL=useGetNewArchRoom.js.map