"use strict";

import { useCallback } from 'react';
import { useAppDispatch } from './hooks';
import { useXmppClient } from '../context/xmppProvider';
import { createRoomFromApi } from '../helpers/createRoomFromApi';
import { getRooms } from '../networking/api-requests/rooms.api';
import { addRoomViaApi, setIsLoading } from '../roomStore/roomsSlice';
import { pushSubscriptionService } from '../services/pushSubscriptionService';
const useGetNewArchRoom = () => {
  const {
    client
  } = useXmppClient();
  const dispatch = useAppDispatch();
  const syncRooms = useCallback(async (client, config) => {
    const rooms = await getRooms();
    const roomJIDs = [];
    rooms?.items?.forEach(room => {
      const createdRoom = createRoomFromApi(room, config?.xmppSettings?.conference);
      if (createdRoom) {
        dispatch(addRoomViaApi({
          room: createdRoom,
          xmpp: client
        }));
        if (createdRoom.jid) {
          roomJIDs.push(createdRoom.jid);
        }
      }
    });
    dispatch(setIsLoading({
      loading: false,
      loadingText: undefined
    }));
    if (client?.client && roomJIDs.length > 0) {
      const userNick = client.client.jid?.getLocal();
      await 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;
};
export default useGetNewArchRoom;
//# sourceMappingURL=useGetNewArchRoom.js.map