"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _roomsSlice = require("../roomStore/roomsSlice");
var _updatedChatLastTimestamps = require("./updatedChatLastTimestamps");
var _initRoomsPresence = require("./initRoomsPresence");
var _roomStore = require("../roomStore");
const initXmppRooms = async (user, config, xmmpClient, rooms, roomJID) => {
  if (roomJID) {
    _roomStore.store.dispatch((0, _roomsSlice.setCurrentRoom)({
      roomJID: roomJID
    }));
  }
  try {
    if (!user.defaultWallet || !user.defaultWallet.walletAddress) {
      console.log('Error, no user');
      return;
    }
    if (!xmmpClient) {
      console.log('No xmmpClient, initializing one');
      if (rooms && Object.keys(rooms).length > 0) {
        await (0, _initRoomsPresence.initRoomsPresence)(xmmpClient, rooms);
      } else {
        if (config?.newArch) {
          // const rooms = await getRooms();
          //     rooms.items.map((room) => {
          //       dispatch(
          //         addRoomViaApi({
          //           room: createRoomFromApi(
          //             room,
          //             config?.xmppSettings?.conference
          //           ),
          //           xmpp: newClient,
          //         })
          //       );
          //     });
          return;
        }
        const res = await xmmpClient.getRoomsStanza();
        console.log(res);
      }

      //@ts-ignore
      const roomTimestampObject = await xmmpClient.getChatsPrivateStoreRequestStanza();
      (0, _updatedChatLastTimestamps.updatedChatLastTimestamps)(roomTimestampObject, _roomStore.store.dispatch);
    } else {
      //@ts-ignore
      const roomTimestampObject = await xmmpClient.getChatsPrivateStoreRequestStanza();
      (0, _updatedChatLastTimestamps.updatedChatLastTimestamps)(roomTimestampObject, _roomStore.store.dispatch);
    }
    if (config?.refreshTokens?.enabled) {
      config?.refreshTokens?.refreshFunction?.();
    }
  } catch (error) {
    console.error(error);
  } finally {
    _roomStore.store.dispatch((0, _roomsSlice.setIsLoading)({
      loading: false
    }));
  }
};
var _default = exports.default = initXmppRooms;
//# sourceMappingURL=initXmppRooms.js.map