import { watch } from 'vue'
import { enqueue, flush } from '@/utils/offlineQueue'
import { useSocket } from '@/composables/useSocket'

/**
 * Hook that wires components into the global offline queue, automatically
 * processing pending mutations when the application regains connectivity.
 */
export function useOfflineSync() {
    const { isConnected } = useSocket()

    watch(isConnected, (online) => {
        if (online) {
            flush(async () => {
                // For MVP we just emit back to server via WS.
            }).catch((err) => console.error('Offline flush error', err))
        }
    }, { immediate: true })

    return {
        enqueue,
    }
} 