import { Logger } from './core/logger.js';

export function registerServiceWorker() {
    if ('serviceWorker' in navigator) {
        navigator.serviceWorker.register('/sw.js')
            .then(reg => {
                console.log('✅ Service Worker registriert:', reg.scope);

                // Update found?
                reg.onupdatefound = () => {
                    const installing = reg.installing;
                    installing.onstatechange = () => {
                        if (installing.state === 'installed') {
                            if (navigator.serviceWorker.controller) {
                                console.log('🔄 Neue Version verfügbar – Seite neu laden?');
                            } else {
                                console.log('✅ Inhalte jetzt offline verfügbar');
                            }
                        }
                    };
                };
            })
            .catch(err => {
                Logger.error('Service Worker Fehler:', err);
            });
    }
}
