/* eslint-disable-next-line no-restricted-globals */
self.addEventListener('message', (event) => {
    if (event.data.type === 'preload-images' && event.data.urls) {
        event.data.urls.forEach((image) => {
            fetch(image.pathUrl)
                .then((response) => response.blob())
                .then((imageBlob) => {
                    // Cache the preloaded image
                    caches.open('preloaded-images').then((cache) => {
                        cache.put(image.pathUrl, new Response(imageBlob));
                    });

                    postMessage({ type: 'image-preloaded', url: image.key, blob: imageBlob });
                }).catch((e) => {
                console.error(e)
            });
        });
    }
});
