try {
    // Give the service worker access to Firebase Messaging.
    // Note that you can only use Firebase Messaging here. Other Firebase libraries
    // are not available in the service worker.
    // Replace 10.13.2 with latest version of the Firebase JS SDK.
    importScripts('./firebase-scripts/firebase-app-compat.js');
    importScripts('./firebase-scripts/firebase-messaging-compat.js');
    // Initialize the Firebase app in the service worker by passing in
    // your app's Firebase config object.
    // https://firebase.google.com/docs/web/setup#config-object
    firebase.initializeApp({
        apiKey: 'AIzaSyA4tONYizVYn2gtmar48bQfv8YTTuzRJU4',
        authDomain: 'salona-8e174.firebaseapp.com',
        projectId: 'salona-8e174',
        storageBucket: 'salona-8e174.firebasestorage.app',
        messagingSenderId: '336749753638',
        appId: '1:336749753638:web:dcf886e1289f39ef267de7',
        measurementId: 'G-ZFYBE6DDDH',
    });
    const messaging = firebase.messaging();

    messaging.onBackgroundMessage(async (payload) => {
        // console.log('Background message:: ', payload);
        self.registration.showNotification(payload.notification?.title ?? 'سالونا', {
            body: payload.notification?.body ?? "پیام جدید",
            icon: '/pwa-96x96.png',
            badge: '/notif-96x96.png',
            data: payload.data,
            tag: payload.collapseKey,
            renotify: true,
            requireInteraction: true,
        });
    });
    self.addEventListener('notificationclick', (event) => {
        event.notification.close();
        if (event.notification?.data?.url) {
            event.waitUntil(
                clients.openWindow(event.notification.data.url)
            );
        }
    });
} catch (e) {
    console.error('Error in service worker:', e);
}