const swName = "RaMemoryMM";

const addResourcesToCache = async function (resources) {
    const cache = await caches.open(swName);
    await cache.addAll(resources);
};

const putInCache = async function (request, response) {
    const cache = await caches.open(swName);
    await cache.put(request, response);
};

const enableNavigationPreload = async function () {
    if (self.registration.navigationPreload) {
        // Enable navigation preloads!
        await self.registration.navigationPreload.enable();
    }
};

self.addEventListener('activate', function (event) {
    event.waitUntil(enableNavigationPreload());
});

self.addEventListener('install', function (event) {
    event.waitUntil(
            addResourcesToCache([
                './index.html',
                './JavaScript/Css.min.js',
                './JavaScript/PWA.min.js',
                './JavaScript/Firework.min.js',
                './JavaScript/Memory.min.js',
                './JavaScript/main.min.js',
                './Css/Firework.min.css',
                './Css/main.min.css',
                './fonts/TextMeOne-Regular.ttf',
                './config/emoji-categories.min.json',
                './config/emojis.min.json',
                './config/images.json',
                './media/frog512x512.png',
                './media/altogether-logo.png',
                './media/altogether-logo-inverted.png',
                './media/light_emoji.png',
                './favicon/site.webmanifest',
                './media/green_string.png',
                './favicon/favicon.ico',
                './favicon/favicon-32x32.png',
                './favicon/favicon-16x16.png',
                './favicon/android-chrome-192x192.png',
                './favicon/android-chrome-512x512.png',
                './favicon/maskable/maskable_icon.png',
                './favicon/maskable/maskable_icon_x48.png',
                './favicon/maskable/maskable_icon_x72.png',
                './favicon/maskable/maskable_icon_x96.png',
                './favicon/maskable/maskable_icon_x128.png',
                './favicon/maskable/maskable_icon_x192.png',
                './favicon/maskable/maskable_icon_x384.png',
                './favicon/maskable/maskable_icon_x512.png'
            ])
            );
});


self.addEventListener('fetch', function (evt) {

    if (!(evt.request.url.indexOf('http') === 0)) {
        return;
    }
    evt.respondWith(
            caches
            .match(evt.request)
            .then(function (cacheRes) {
                return cacheRes || fetch(evt.request).then(function (fetchRes) {
                    cache.open(swName).then(function (cache) {
                        cache.put(evt.request.url, fetchRes.clone());
                        limitCacheSize(swName, 200);
                        return fetchRes;
                    });
                });
            }).catch(function () {
        caches.match('/fallback');
    })
            )
});

const limitCacheSize = function (name, size) {
    caches.open(name).then(function (cache) {
        cache.keys().then(function (keys) {
            if (keys.length > size) {
                cache.delete(keys[0]).then(limitCacheSize(name, size));
            }
        });
    });
};
