import {
  newMessageSubscription
} from "./chunk-DPQXJIIV.mjs";

// tmp-babel/messages/native/graphql/subscriptions/useMessagesListSubscription.js
import { useCallback, useMemo, useRef } from "react";
import { useAppStateSubscription } from "@baseapp-frontend/utils/hooks/useAppStateSubscription";
import { useFocusEffect } from "expo-router";
import { ConnectionHandler, requestSubscription } from "react-relay";
var useMessagesListSubscription = function useMessagesListSubscription2(roomId, profileId, environment) {
  var disposableRef = useRef(null);
  var connectionID = useMemo(function() {
    return ConnectionHandler.getConnectionID(roomId, "chatRoom_allMessages");
  }, [roomId]);
  var config = useMemo(function() {
    return {
      subscription: newMessageSubscription,
      variables: {
        roomId,
        profileId,
        connections: [connectionID]
      },
      onError: console.error
    };
  }, [roomId, profileId, connectionID]);
  var subscribe = useCallback(function() {
    var _disposableRef$curren, _disposableRef$curren2;
    if (!roomId || !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);
  }, [roomId, 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 {
  useMessagesListSubscription
};
//# sourceMappingURL=chunk-74UQV4X3.mjs.map