"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useSessionSocket = useSessionSocket;
var _react = require("react");
var _socket = _interopRequireDefault(require("socket.io-client"));
var _sonner = require("../../lib/sonner");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function useSessionSocket({
  userId,
  activeSessionId,
  refreshSessions,
  logout,
  baseURL,
  onRemoteSignOut
}) {
  const socketRef = (0, _react.useRef)(null);
  (0, _react.useEffect)(() => {
    if (!userId || !baseURL) return;
    if (!socketRef.current) {
      socketRef.current = (0, _socket.default)(baseURL, {
        transports: ['websocket']
      });
    }
    const socket = socketRef.current;
    socket.on('connect', () => {
      console.log('Socket connected:', socket.id);
    });
    socket.emit('join', {
      userId: `user:${userId}`
    });
    console.log('Emitting join for room:', `user:${userId}`);
    socket.on('session_update', data => {
      console.log('Received session_update:', data);

      // Always refresh sessions to get the latest state
      refreshSessions();

      // If the current session was logged out, handle it specially
      if (data.sessionId === activeSessionId) {
        if (onRemoteSignOut) onRemoteSignOut();else _sonner.toast.info('You have been signed out remotely.');
        logout();
      }
    });
    return () => {
      socket.emit('leave', {
        userId: `user:${userId}`
      });
      socket.off('session_update');
    };
  }, [userId, baseURL, activeSessionId, refreshSessions, logout, onRemoteSignOut]);
}
//# sourceMappingURL=useSessionSocket.js.map