/* eslint-disable no-undef */
importScripts('./env-vars.js')
// custom service-worker.js
if (workbox) {
  // apply precaching. In the built version, the precacheManifest will
  // be imported using importScripts (as is workbox itself) and we can
  // precache this. This is all we need for precaching
  workbox.precaching.precacheAndRoute(self.__precacheManifest)

  // Make sure to return a specific response for all navigation requests.
  // Since we have a SPA here, this should be index.html always.
  // https://stackoverflow.com/questions/49963982/vue-router-history-mode-with-pwa-in-offline-mode
  workbox.routing.registerNavigationRoute('/index.html')

  // Setup cache strategy for Google Fonts. They consist of two parts, a static one
  // coming from fonts.gstatic.com (strategy CacheFirst) and a more ferquently updated on
  // from fonts.googleapis.com. Hence, split in two registerroutes
  workbox.routing.registerRoute(
    /^https:\/\/fonts\.googleapis\.com/,
    new workbox.strategies.StaleWhileRevalidate({
      cacheName: 'google-fonts-stylesheets',
    })
  )

  workbox.routing.registerRoute(
    /^https:\/\/fonts\.gstatic\.com/,
    new workbox.strategies.CacheFirst({
      cacheName: 'google-fonts-webfonts',
      plugins: [
        new workbox.cacheableResponse.Plugin({
          statuses: [0, 200],
        }),
        new workbox.expiration.Plugin({
          maxAgeSeconds: 60 * 60 * 24 * 365,
          maxEntries: 30,
        }),
      ],
    })
  )

  workbox.routing.registerRoute(
    /^https:\/\/stackpath\.bootstrapcdn\.com/,
    new workbox.strategies.StaleWhileRevalidate({
      cacheName: 'fontawesome',
    })
  )
  // TODO: workbox not replacing latest data to cache when network is returning response 200
  // workbox.routing.registerRoute(
  //   new RegExp(
  //     typeof VUE_APP_SHARED_SERVICE_WORKER_API_URL !== 'undefined' ? VUE_APP_SHARED_SERVICE_WORKER_API_URL : ''
  //   ),
  //   new workbox.strategies.NetworkFirst({
  //     networkTimeoutSeconds: 5,
  //     cacheName: 'nibs-apis-get',
  //     plugins: [
  //       new workbox.cacheableResponse.Plugin({
  //         statuses: [0, 200],
  //       }),
  //       new workbox.expiration.Plugin({
  //         maxEntries: 100,
  //         maxAgeSeconds:
  //           Number(
  //             typeof VUE_APP_SHARED_SERVICE_WORKER_API_CACHE_EXPIRY !== 'undefined'
  //               ? VUE_APP_SHARED_SERVICE_WORKER_API_CACHE_EXPIRY
  //               : 30
  //           ) * 60, // 30 minutes
  //       }),
  //     ],
  //   })
  // )
}

// This code listens for the user's confirmation to update the app.
self.addEventListener('message', e => {
  if (!e.data) {
    return
  }

  switch (e.data) {
    case 'skipWaiting':
      self.skipWaiting()
      break
    default:
      // NOOP
      break
  }
})

// Listen to Push
// Disabled due to not in use
// self.addEventListener('push', e => {
//   let data
//   if (e.data) {
//     data = e.data.json()
//   }
//
//   const options = {
//     body: data.body,
//     icon: '/img/icons/android-chrome-192x192.png',
//     image: '/img/autumn-forest.png',
//     vibrate: [300, 200, 300],
//     badge: '/img/icons/plint-badge-96x96.png',
//   }
//
//   e.waitUntil(self.registration.showNotification(data.title, options))
// })
