const CACHE_NAME = 'badminton-scoreboard-v2'
const APP_SHELL = [
  '/manifest.webmanifest',
  '/favicon.png',
  '/icon.png',
  '/apple-touch-icon.png',
  '/pwa-192.png',
  '/pwa-512.png',
]

self.addEventListener('install', (event) => {
  event.waitUntil(
    caches.open(CACHE_NAME).then((cache) => cache.addAll(APP_SHELL)),
  )
  self.skipWaiting()
})

self.addEventListener('activate', (event) => {
  event.waitUntil(
    caches.keys().then((keys) =>
      Promise.all(
        keys.map((key) => {
          if (key !== CACHE_NAME) {
            return caches.delete(key)
          }

          return Promise.resolve(false)
        }),
      ),
    ),
  )
  self.clients.claim()
})

self.addEventListener('message', (event) => {
  if (event.data?.type === 'SKIP_WAITING') {
    self.skipWaiting()
  }
})

self.addEventListener('fetch', (event) => {
  if (event.request.method !== 'GET') {
    return
  }

  const requestUrl = new URL(event.request.url)

  if (requestUrl.origin !== self.location.origin) {
    return
  }

  if (requestUrl.pathname.startsWith('/api/')) {
    event.respondWith(fetch(event.request))
    return
  }

  const isNavigationRequest =
    event.request.mode === 'navigate' || event.request.destination === 'document'

  if (isNavigationRequest) {
    event.respondWith(
      fetch(event.request)
        .then(async (networkResponse) => {
          if (networkResponse.ok) {
            const cache = await caches.open(CACHE_NAME)
            cache.put('/index.html', networkResponse.clone())
          }

          return networkResponse
        })
        .catch(async () => {
          const fallback = await caches.match('/index.html')
          if (fallback) {
            return fallback
          }

          throw new Error('Navigation request failed')
        }),
    )
    return
  }

  event.respondWith(
    caches.match(event.request).then(async (cachedResponse) => {
      try {
        const networkResponse = await fetch(event.request)

        if (
          networkResponse.ok &&
          (event.request.destination === 'script' ||
            event.request.destination === 'style' ||
            event.request.destination === 'image' ||
            requestUrl.pathname.startsWith('/assets/'))
        ) {
          const cache = await caches.open(CACHE_NAME)
          cache.put(event.request, networkResponse.clone())
        }

        return networkResponse
      } catch (error) {
        if (cachedResponse) {
          return cachedResponse
        }

        throw error
      }
    }),
  )
})
