// This is the "Offline page" service worker

importScripts('https://storage.googleapis.com/workbox-cdn/releases/5.1.2/workbox-sw.js');

const CACHE = "pwabuilder-page";

// TODO: replace the following with the correct offline fallback page i.e.: const offlineFallbackPage = "offline.html";
const offlineFallbackPage = "offline.html";

self.addEventListener("message", (event) => {
  if (event.data && event.data.type === "SKIP_WAITING") {
    self.skipWaiting();
  }
});

self.addEventListener('install', async (event) => {
  event.waitUntil(
    caches.open(CACHE)
      .then((cache) => cache.add(offlineFallbackPage))
  );
});

if (workbox.navigationPreload.isSupported()) {
  workbox.navigationPreload.enable();
}

self.addEventListener('fetch', (event) => {
  if (event.request.mode === 'navigate') {
    event.respondWith((async () => {
      try {
        const preloadResp = await event.preloadResponse;

        if (preloadResp) {
          return preloadResp;
        }

        const networkResp = await fetch(event.request);
        return networkResp;
      } catch (error) {

        const cache = await caches.open(CACHE);
        const cachedResp = await cache.match(offlineFallbackPage);
        return cachedResp;
      }
    })());
  }
});


self.addEventListener('notificationclick', function(event){
  var notification = event.notification;
  var action = event.action;

  if (action === 'confirm') {
      notification.close();
  }else{
      event.waitUntil(
          clients.matchAll()
          .then(function(clis){
              var client = clis.find(function(c){
                  return c.visibilityState === 'visible';
              });
              if (client !== undefined) {
                  client.navigate(notification.data.url);
                  client.focus();

              }else{
                  clients.openWindow(notification.data.url);
              }
              notification.close();
          })
      );
  }
});

self.addEventListener('push', function(e){

  var data = {};
    console.log(e.data.json());
    if (e.data) {
    data = e.data.json();
    }  
    var title = data.title || 'HULA CAPITAL';
    var message = data.message || 'New investment opportunity available!';
            var action = data.action || 'https://dashboard.hulacapital.com';
var img = data.image || 'https://res.cloudinary.com/dqbbuntc6/image/upload/v1751818518/hulacapital-appicon_qfzgqw.png';

  var options = {
      body: message,
      image: img,
      icon: 'https://res.cloudinary.com/dqbbuntc6/image/upload/v1751818518/hulacapital-appicon_qfzgqw.png',
      dir: 'ltr',
      lang: 'en-Us',
      vibrate: [100, 50, 200],
      badge: 'https://res.cloudinary.com/dqbbuntc6/image/upload/v1751818518/hulacapital-appicon_qfzgqw.png',
      data: {
          url: action
      },
      actions: [
          {action: 'visit', title: 'View'}
      ],
      requireInteraction: false,
      silent: false,
      tag: 'hulacapital-notification'
  }

  registration.showNotification(title, options);
});