"use strict";

var logger = console;

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

workbox.setConfig({ debug: false });

const { strategies, core, routing, precaching } = workbox;

// Note: Ignore the error that Glitch raises about workbox being undefined.
core.skipWaiting();
core.clientsClaim();

routing.registerRoute(/\/api\/sessionTest/, new workbox.strategies.NetworkOnly(), 'GET');
routing.registerRoute(/\/manifest.webmanifest/, new workbox.strategies.CacheFirst(), 'GET');
routing.registerRoute(/\/vendor\/custom-component-modules\/.*/, new workbox.strategies.CacheFirst(), 'GET');
routing.registerRoute(/\/api\/loadComponent.*/, new workbox.strategies.NetworkFirst(), 'GET');
routing.registerRoute(/\/api\/loggedUser/, new workbox.strategies.NetworkFirst(), 'GET');
routing.registerRoute(/\/clientConfig\/*/, new workbox.strategies.CacheFirst(), 'GET');

routing.setCatchHandler(({event}) => {
  if (event.request.url.endsWith('sessionTest')){
    return new Response("",{ "status" : 200 , "statusText" : "ok" });
  } else{
    return fetch(event.request);
  }
});


// registering tileserver-gl route.
routing.registerRoute(
  new RegExp('.*/data/v3.json'),
  new workbox.strategies.CacheFirst()
);

routing.registerRoute(
  new RegExp('.*/styles/basic-preview/style.json'),
  new workbox.strategies.CacheFirst()
);

routing.registerRoute(
  new RegExp('.*/data/*'),
  new workbox.strategies.CacheFirst()
);

let baseUrl = new URL(location.href).searchParams.get('baseUrl');
if (baseUrl !== "") {
    baseUrl += "/";
}



self.addEventListener("fetch", function(e) {
  e.respondWith(
    caches.match(e.request).then(function(response) {
      if (response){
        return response;
      } else {
        return fetch(e.request).catch(function(e){
          if(!e.status) {
            // console.log(`Request fetched with status: ${e.status}` + e.message);
            return e;
          } else {
            return new Response("", { "status" : e.status, "statusText" : e.message });
          }
        });
      }
    })
  );
});


self.addEventListener('activate', (event) => {
    self.clients.claim();
    self.clients.matchAll().then((clients) => {
        clients.forEach((client) => client.postMessage('service-worker-activated'));
        // Manually cache clientConfig after service worker activates to prevent issue:
        // https://mantis.formbird.com/view.php?id=13163.
        // Use cacheNames.runtime to get the name of the default runtime cache that Workbox uses.
        // See for details on the cache.pen function:
        // https://developer.mozilla.org/en-US/docs/Web/API/Cache#Methods
        return caches.open(core.cacheNames.runtime);
    }).then(cache => cache.add('clientConfig'));
});


precaching.precacheAndRoute([{"revision":"4c732369e54ee14c3d884b15b441ad0f","url":"AngularJS-Shield.ab438fd42aafc0bf.svg"},{"revision":"c1228dc43b988eb9befc21ef55a46ef1","url":"client_app_modules_audit_audit_module_ts.f5ea20c5975ac3ba.js"},{"revision":"e75825dd62b9363c2e7bb619ab7fcab7","url":"client_app_modules_auth_auth_module_ts.42343da558ffa71c.js"},{"revision":"4336bb67ca073961e0088410c35b2214","url":"client_app_modules_search_search_module_ts.3bd3a7bec2a0d59e.js"},{"revision":"7699c7932ae3a1f69849346b0074b41d","url":"client_app_scripts_jsdifflib_index_js.44f295afd52b4242.js"},{"revision":"5788e801c1dbaa5cc4d1dbaaaebc518d","url":"images/btn_google_signin_light_normal_web.png"},{"revision":"a675c36589dd59d19397aefd1e656b23","url":"images/f_logo_RGB-White_58.png"},{"revision":"f92f4fae5058907f8853a5977d91c90c","url":"images/favicon.ico"},{"revision":"af3566c2a830b9eabce5aada42c5ce3d","url":"images/formbird-logo-makes-it-possible.png"},{"revision":"1d5e9d38a96f3e540d436a7c786cebea","url":"images/formbird-logo-small.png"},{"revision":"3fcf9eae67e52f1e880610b134bd45a4","url":"images/formbird-logo.png"},{"revision":"33432742a3c7e33957c4c5c87dcaac60","url":"images/ft-uploader/ft-uploader-default-32.png"},{"revision":"cbe449fd13ae4265ca015f891c3f7a25","url":"images/ft-uploader/ft-uploader-default-gallery-placeholder.png"},{"revision":"785c3ceee023530103c76876c6e7f3a1","url":"images/ft-uploader/ft-uploader-docx-32.png"},{"revision":"c56712fe34b6766179bbcebe8382b40e","url":"images/ft-uploader/ft-uploader-pdf-32.png"},{"revision":"595f74cf1b9a74dcc426178ce24c1d5e","url":"images/ft-uploader/ft-uploader-ppt-32.png"},{"revision":"4c24d55f88be3b4a102cf0341abc3797","url":"images/ft-uploader/ft-uploader-xls-32.png"},{"revision":"852dddc3261400e8d433b80636679ca5","url":"images/ft-uploader/ft-uploader-zip-32.png"},{"revision":"ef357ed7cf868e7592d7baf991b9b59a","url":"images/geolocation_marker_heading.png"},{"revision":"5f8632e84c00fe338eff47eae2b998cb","url":"images/gobble-logo.png"},{"revision":"801853c3ad68c3672e575ab7092ef49a","url":"images/loading.gif"},{"revision":"3889ca5d9fbd666da2ff4726104966fe","url":"images/logo.png"},{"revision":"94a9153db495781ee4c317420202a2be","url":"images/marker.png"},{"revision":"3a8a4582fe1dbc77086eccca317c459e","url":"images/mCSB_buttons.png"},{"revision":"52453ab65e5c6c80f53b6af21c27d7b9","url":"images/menu-background.png"},{"revision":"673c2c643a8ae95a96cc9a3f308842df","url":"images/notifications/error.png"},{"revision":"944e336648cae6a94648921fe24acd0a","url":"images/notifications/info.png"},{"revision":"d862a4397c9b1c7b46aa26d9868f49a6","url":"images/notifications/success.png"},{"revision":"5398cd0109ebf796c4acdb05c01aa905","url":"images/notifications/warning.png"},{"revision":"f99ac3b0cf5b5e0487dcac99526b61a2","url":"index.html"},{"revision":"aad8281bfe7648ff6de8409c69bdb70a","url":"main.1e3b57bdbdc70bf1.js"},{"revision":"a37b0c01c0baf1888ca812cc0508f6e2","url":"MaterialIcons-Regular.196fa4a92dd6fa73.ttf"},{"revision":"e79bfd88537def476913f3ed52f4f4b3","url":"MaterialIcons-Regular.1e50f5c2ffa6aba4.eot"},{"revision":"570eb83859dc23dd0eec423a49e147fe","url":"MaterialIcons-Regular.7ea2023eeca07427.woff2"},{"revision":"012cf6a10129e2275d79d6adac7f3b02","url":"MaterialIcons-Regular.db852539204b1a34.woff"},{"revision":"89fcc760c2fa00f120362f9220e748f3","url":"polyfills.dd176f48189b3a4f.js"},{"revision":"73f26bf98a715ecab4d2287ff3a02ad0","url":"roboto-all-400-normal.22c8c36aad9b0ba9.woff"},{"revision":"ba2c6cb0af81f8da49a960db84f26b7c","url":"roboto-cyrillic-400-normal.ba1944acf299d946.woff2"},{"revision":"2e0b8660b87034ecf56aa9f488fbc08e","url":"roboto-cyrillic-ext-400-normal.7ea3b60dca10686a.woff2"},{"revision":"22786f243202d7912399ffd10c76fe78","url":"roboto-greek-400-normal.7026b7dcbb095d18.woff2"},{"revision":"5cff07beab63ec777fc73ac0483811b0","url":"roboto-greek-ext-400-normal.771ed7af162f39a6.woff2"},{"revision":"aa23b7b4bcf2b8f0e876106bb3de69c6","url":"roboto-latin-400-normal.7b8d7718ba08bc7d.woff2"},{"revision":"718dded3393324e992b225ac61329e0c","url":"roboto-latin-ext-400-normal.cf3d77895f73a242.woff2"},{"revision":"756af8e5560d200e53b52b1ff70f2ad0","url":"roboto-vietnamese-400-normal.0f9be45784c0da1a.woff2"},{"revision":"6138ba8525bff1ad0b0b98492c41f565","url":"runtime.cf7527a869fbdabb.js"},{"revision":"8bd7a152a894823872656267efc5ab94","url":"scripts.42760417b1b0a04a.js"},{"revision":"1348c48aaead35f960d3d39c7c024514","url":"ServiceWorkerStart.js"},{"revision":"d93a6372d863154e472edb954ff70c63","url":"styles.css"}] || [], {
    "directoryIndex": `${baseUrl}/index.html`
});

const handler = precaching.createHandlerBoundToURL(`${baseUrl}/index.html`);
const navigationRoute = new routing.NavigationRoute(handler, {
  denylist: [
    /\/api\/*/,
    /\/auth\/*/,
    /\/vendor\/*/,
    /\/*.css/,
    /\/*.js/,
    /\/*.woff2/,
    /\/*.woff/,
    /\/*.svg/,
    /\/*.tff/,
    /\/*.webmanifest/,
    /\/*.eot/,
    /\/*.gif/,
    /\/*.jpg/,
    /\/images\/*/,
    /\/socket\/*/,
    /\/serviceWorker\/*/,
    /\/ServiceWorkerStart\/*/,
    /\/clientConfig\/*/,
    /\/birt-viewer\/*/
  ]
});
//routing.registerRoute(navigationRoute);
routing.registerRoute(navigationRoute, new workbox.strategies.StaleWhileRevalidate());
