/**
 * Pusher client plugin
 *
 * Initializes Pusher connection when user is authenticated.
 * Works on all platforms (web, Capacitor, Vercel).
 */

export default defineNuxtPlugin(() => {
  if (import.meta.client) {
    const { user } = useAuth()
    const { initializePusher, disconnect } = usePusher()

    // Initialize Pusher when user is authenticated
    watch(user, async (currentUser) => {
      if (currentUser?.id) {
        await initializePusher()
      } else {
        disconnect()
      }
    }, { immediate: true })
  }
})
