/* eslint-disable no-console */

import { register } from 'register-service-worker'

if (process.env.NODE_ENV === 'production') {
  register(`${process.env.BASE_URL}service-worker.js`, {
    ready(registration) {
      // Interval checking on new service worker existence
      window.setInterval(() => {
        registration.update()
      }, 10000)
      console.log('iBS Member Portal - App is being served from cache by a service worker')
    },
    cached() {
      console.log('iBS Member Portal - Content has been cached for offline use.')
    },
    updated(registration) {
      console.log('iBS Member Portal - New content is available!')
      document.dispatchEvent(new CustomEvent('swUpdated', { detail: registration }))
    },
    offline() {
      console.log('iBS Member Portal - No internet connection found. App is running in offline mode.')
    },
    error(error) {
      console.error('iBS Member Portal - Error during service worker registration:', error)
    },
  })
}
