const CACHE_NAME = 'furnify-store-v9-customer-accounts';
const APP_SHELL = [
  './',
  './index.php',
  './cart.php',
  './custom-sofa.php',
  './reup.php',
  './login.php',
  './register.php',
  './offline.html',
  './manifest.json',
  './assets/css/styles.css',
  './assets/js/app.js',
  './assets/images/hero-sofa.webp',
  './assets/images/hero-sofa.png',
  './assets/images/sofa-luna.webp',
  './assets/images/sofa-luna.png',
  './assets/images/sofa-mika.webp',
  './assets/images/sofa-mika.png',
  './assets/images/sofa-orion.webp',
  './assets/images/sofa-orion.png',
  './assets/models/sofa-luna.glb',
  './assets/models/sofa-mika.glb',
  './assets/models/sofa-orion.glb',
  './assets/icons/icon-192.png',
  './assets/icons/icon-512.png'
];

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

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

self.addEventListener('fetch', (event) => {
  const request = event.request;

  if (request.method !== 'GET') {
    return;
  }

  const url = new URL(request.url);
  if (url.pathname.indexOf('/admin/') !== -1) {
    event.respondWith(fetch(request));
    return;
  }

  if (request.mode === 'navigate') {
    const noCachePage = url.pathname.endsWith('/checkout.php') || url.pathname.endsWith('/submit-order.php') || url.pathname.endsWith('/reup.php') || url.pathname.endsWith('/custom-sofa.php') || url.pathname.indexOf('/customer-') !== -1 || url.pathname.endsWith('/login.php') || url.pathname.endsWith('/register.php') || url.pathname.endsWith('/verify-email.php') || url.pathname.endsWith('/resend-verification.php') || url.pathname.endsWith('/logout.php') || url.pathname.endsWith('/forgot-password.php') || url.pathname.endsWith('/reset-password.php');

    if (noCachePage) {
      event.respondWith(fetch(request).catch(() => caches.match('./offline.html')));
      return;
    }

    event.respondWith(
      fetch(request)
        .then((response) => {
          const copy = response.clone();
          caches.open(CACHE_NAME).then((cache) => cache.put(request, copy));
          return response;
        })
        .catch(() => caches.match('./offline.html'))
    );
    return;
  }

  event.respondWith(
    caches.match(request).then((cached) => {
      if (cached) {
        return cached;
      }

      return fetch(request)
        .then((response) => {
          if (!response || response.status !== 200 || response.type === 'opaque') {
            return response;
          }

          const copy = response.clone();
          caches.open(CACHE_NAME).then((cache) => cache.put(request, copy));
          return response;
        })
        .catch(() => {
          if (request.destination === 'document') {
            return caches.match('./offline.html');
          }
          return Response.error();
        });
    })
  );
});
