const CACHE_NAME = 'justsale-mobile-v5.0.0';
const ASSETS = [
  './',
  'index.html',
  'mobile-login.html',
  'mobile-admin.html',
  'mobile-pos.html',
  'manifest.json',
  'assets/css/mobile-pos.css',
  'assets/js/mobile-pos.js',
  'assets/pwa/icon-512.png',
  'assets/vendor/fontawesome/css/all.min.css'
];

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

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

self.addEventListener('fetch', (event) => {
  event.respondWith(
    caches.match(event.request).then((response) => {
      return response || fetch(event.request);
    })
  );
});
