import { useEffect } from 'react';
import { toast } from 'sonner';

export function PWAUpdateToast() {
    useEffect(() => {
        const handleOfflineReady = () => {
            toast.success('Aplikasi siap offline!', {
                description: 'Aplikasi dapat digunakan tanpa koneksi internet.',
                duration: 5000,
            });
        };

        const handleUpdateAvailable = () => {
            toast.info('Update tersedia!', {
                description:
                    'Versi baru aplikasi telah tersedia. Klik untuk memperbarui.',
                duration: Infinity,
                action: {
                    label: 'Update',
                    onClick: () => {
                        window.location.reload();
                    },
                },
            });
        };

        window.addEventListener('sw-offline-ready', handleOfflineReady);
        window.addEventListener('sw-update-available', handleUpdateAvailable);

        return () => {
            window.removeEventListener('sw-offline-ready', handleOfflineReady);
            window.removeEventListener(
                'sw-update-available',
                handleUpdateAvailable,
            );
        };
    }, []);

    return null;
}
