---

---

<script>
  import { registerSW } from 'virtual:pwa-register'
  import { unsubscribeFromPushNotifications } from '../lib/client/clientPushNotifications'

  const NO_AUTO_RELOAD_ROUTES = ['/account/welcome', '/500', '/404'] as const satisfies `/${string}`[]

  let hasPendingUpdate = false

  const updateSW = registerSW({
    immediate: true,
    onRegisteredSW: (_swScriptUrl, registration) => {
      if (registration) window.__SW_REGISTRATION__ = registration

      document.addEventListener('astro:after-swap', checkAndApplyPendingUpdate, { passive: true })
      window.addEventListener('popstate', checkAndApplyPendingUpdate, { passive: true })
    },
    onNeedRefresh: () => {
      if (shouldSkipAutoReload()) {
        void updateSW(false)
        hasPendingUpdate = true
        return
      }

      void updateSW(true)
    },
    onRegisterError: (error) => {
      console.error('Service Worker registration error', error)
    },
  })

  function shouldSkipAutoReload() {
    const currentPath = window.location.pathname
    const isErrorPage = document.body.hasAttribute('data-is-error-page')

    return isErrorPage || NO_AUTO_RELOAD_ROUTES.some((route) => currentPath === route)
  }

  function checkAndApplyPendingUpdate() {
    if (hasPendingUpdate && !shouldSkipAutoReload()) {
      hasPendingUpdate = false
      void updateSW(true)
    }
  }

  window.addEventListener('beforeinstallprompt', (event) => {
    event.preventDefault()
  })

  document.addEventListener('astro:page-load', async () => {
    if (!document.body.hasAttribute('data-is-logged-in')) {
      await unsubscribeFromPushNotifications()
      window.__SW_REGISTRATION__?.unregister()
    }
  })
</script>
