"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useRoomUrl = void 0;
var _react = require("react");
var _reactRedux = require("react-redux");
var _roomsSlice = require("../roomStore/roomsSlice");
const useRoomUrl = (activeRoomJID, roomsList, config) => {
  const dispatch = (0, _reactRedux.useDispatch)();
  (0, _react.useEffect)(() => {
    if (config?.setRoomJidInPath && activeRoomJID && typeof activeRoomJID === 'string') {
      const chatJidUrl = activeRoomJID.split('@')[0];
      const searchParams = new URLSearchParams(window.location.search);
      searchParams.set('chatId', chatJidUrl);
      const newUrl = `${window.location.pathname}?${searchParams.toString()}`;
      window.history.pushState(null, '', newUrl);
    } else if (!activeRoomJID && Object.values(roomsList).length > 0) {
      dispatch((0, _roomsSlice.setCurrentRoom)({
        roomJID: roomsList[0]?.jid
      }));
    }
    return () => {
      if (config?.setRoomJidInPath) {
        const searchParams = new URLSearchParams(window.location.search);
        searchParams.delete('chatId');
        const newUrl = `${window.location.pathname}?${searchParams.toString()}`;
        window.history.pushState(null, '', newUrl);
      }
    };
  }, [activeRoomJID, roomsList?.length]);
};
exports.useRoomUrl = useRoomUrl;
//# sourceMappingURL=useRoomUrl.js.map