import {
  getRoomListSubscriptionConfig
} from "./chunk-QSPF5JY5.mjs";
import {
  useChatRoom_default
} from "./chunk-U2XGYM5M.mjs";

// tmp-babel/messages/native/graphql/subscriptions/useRoomListSubscription.js
import { useCallback, useMemo, useRef } from "react";
import { useAppStateSubscription } from "@baseapp-frontend/utils/hooks/useAppStateSubscription";
import { useFocusEffect } from "expo-router";
import { requestSubscription } from "react-relay";
var useRoomListSubscription = function useRoomListSubscription2(_ref) {
  var connections = _ref.connections, profileId = _ref.profileId, onRemoval = _ref.onRemoval, environment = _ref.environment;
  var _useChatRoom = useChatRoom_default(), selectedRoom = _useChatRoom.id, resetChatRoom = _useChatRoom.resetChatRoom;
  var disposableRef = useRef(null);
  var config = useMemo(function() {
    return getRoomListSubscriptionConfig(profileId, connections, selectedRoom, resetChatRoom, onRemoval);
  }, [profileId, connections, onRemoval, selectedRoom, resetChatRoom]);
  var subscribe = useCallback(function() {
    var _disposableRef$curren, _disposableRef$curren2;
    if (!profileId) return;
    (_disposableRef$curren = disposableRef.current) === null || _disposableRef$curren === void 0 || (_disposableRef$curren2 = _disposableRef$curren.dispose) === null || _disposableRef$curren2 === void 0 || _disposableRef$curren2.call(_disposableRef$curren);
    disposableRef.current = requestSubscription(environment, config);
  }, [profileId, environment, config]);
  var unsubscribe = useCallback(function() {
    var _disposableRef$curren3, _disposableRef$curren4;
    (_disposableRef$curren3 = disposableRef.current) === null || _disposableRef$curren3 === void 0 || (_disposableRef$curren4 = _disposableRef$curren3.dispose) === null || _disposableRef$curren4 === void 0 || _disposableRef$curren4.call(_disposableRef$curren3);
    disposableRef.current = null;
  }, []);
  useFocusEffect(useCallback(function() {
    subscribe();
    return function() {
      unsubscribe();
    };
  }, [subscribe, unsubscribe]));
  useAppStateSubscription(function() {
    subscribe();
  });
};

export {
  useRoomListSubscription
};
//# sourceMappingURL=chunk-5RSR72KN.mjs.map