'use client';

import { useEffect } from 'react';
import { SyncManager } from '@/services/sync-manager';
import { InstallPromptService } from '@/services/install-prompt-service';
import { useOfflineStore } from '@/lib/store/offline-store';

export function PWAInitializer() {
    useEffect(() => {
        if (typeof window === 'undefined') return;

        // Story 3.3: Initialize offline network listeners
        SyncManager.startNetworkListener();

        // Update online status when network changes
        const updateOnlineStatus = () => {
            const isOnline = SyncManager.isOnline();
            useOfflineStore.getState().setOnlineStatus(isOnline);
        };

        window.addEventListener('online', updateOnlineStatus);
        window.addEventListener('offline', updateOnlineStatus);

        // Initial status check
        updateOnlineStatus();

        // Story 3.4: Initialize install prompt service
        InstallPromptService.initialize();

        console.log('[PWAInitializer] Services initialized');

        return () => {
            window.removeEventListener('online', updateOnlineStatus);
            window.removeEventListener('offline', updateOnlineStatus);
        };
    }, []);

    return null;
}
