const SERVICE_WORKER_PATH = '/service-worker.js'
const SERVICE_WORKER_SCOPE = '/karrot-push'

export async function hasServiceWorker () {
  return (await getServiceWorkers()).length > 0
}

export async function getServiceWorker () {
  return (await getServiceWorkers())[0]
}

async function getServiceWorkers () {
  if (__ENV.CORDOVA || !window.navigator.serviceWorker) return []
  const registrations = await window.navigator.serviceWorker.getRegistrations()
  return registrations.filter(worker => worker.scope.endsWith(SERVICE_WORKER_SCOPE))
}

async function getOrCreateWorker () {
  const worker = await getServiceWorker()
  if (worker) return worker
  return window.navigator.serviceWorker.register(SERVICE_WORKER_PATH, { scope: SERVICE_WORKER_SCOPE })
}

let messaging
export async function initializeMessaging () {
  const { initializeApp, messaging: initializeMessaging } = await import('./firebase.lib')
  if (messaging) return messaging

  initializeApp({ messagingSenderId: __ENV.FCM_SENDER_ID })
  messaging = await initializeMessaging()
  messaging.useServiceWorker(await getOrCreateWorker())
  return messaging
}

/*
 * When we disable push notifications we need to remove the service workers.
 * If we remove them immediately, then re-enable push notifications then
 * firebase messsaging library gets upset. So we just remember whether we want them removed
 * only do it when the page actually closes.
 */

let shouldRemoveServiceWorkersOnUnload = false
export async function removeServiceWorkersOnUnload (value) {
  shouldRemoveServiceWorkersOnUnload = value
}

async function removeServiceWorkers () {
  for (const worker of await getServiceWorkers()) {
    worker.unregister()
  }
}

window.onbeforeunload = () => {
  // Do not return anything from this function or it triggers a browser dialog
  if (shouldRemoveServiceWorkersOnUnload) removeServiceWorkers()
}

@@ -1,28 +1,71 @@
+import * as Sentry from '@sentry/browser'%0A%0A
 const SERVICE_WORKER_PATH = 
@@ -262,32 +262,62 @@
 iceWorker () %7B%0A 
+ if (await hasServiceWorker())
  return (await g
@@ -448,21 +448,45 @@
 rn %5B%5D%0A  
-const
+let registrations%0A  try %7B%0A   
  registr
@@ -550,16 +550,87 @@
 tions()%0A
+  %7D%0A  catch (err) %7B%0A    Sentry.captureException(err)%0A    return %5B%5D%0A  %7D%0A
   return

handle error when getting list of service workers