const fs = require('fs-extra');
const path = require('path');
const { InjectManifest } = require('workbox-webpack-plugin');

const {
    keyConfigClientServiceWorkerPathname
} = require('koot/defaults/before-build');
const defaults = require('koot/defaults/pwa');
const getSWFilename = require('koot/utils/get-sw-filename');

module.exports = (kootConfigForThisBuild, localeId) => {
    if (!kootConfigForThisBuild) throw new Error('NO_KOOT_BUILD_CONFIG');

    let { name: projectName, pwa } = kootConfigForThisBuild;
    const { distClientAssetsDirName } = kootConfigForThisBuild;

    if (pwa === true) pwa = {};
    if (pwa === false) return;

    const {
        // auto,
        // pathname,
        filename,
        template,
        // initialCache,
        initialCacheAppend = [],
        initialCacheIgonre = []
    } = Object.assign({}, defaults, pwa);

    const isDev = process.env.WEBPACK_BUILD_ENV === 'dev';

    const swDest = `${isDev ? '' : '../'}${getSWFilename(filename, localeId)}`;

    const swSrc = (() => {
        if (template) return template;

        const filename = 'new-plugin-workbox-template.js';
        const file = path.resolve(__dirname, '.tmp', filename);

        if (fs.existsSync(file)) fs.removeSync(file);
        fs.ensureDirSync(path.dirname(file));
        fs.writeFileSync(
            file,
            fs
                .readFileSync(path.resolve(__dirname, filename), 'utf-8')
                .replace(
                    /__DIST_CLIENT_ASSETS_DIRNAME__/,
                    distClientAssetsDirName
                ),
            'utf-8'
        );

        return file;
    })();

    kootConfigForThisBuild[keyConfigClientServiceWorkerPathname] = swDest;

    return new InjectManifest({
        swDest,
        swSrc,
        importWorkboxFrom: isDev ? 'cdn' : 'local',
        include: [/\.js$/, /\.css$/, ...initialCacheAppend],
        exclude: [/extract\.\d+\..+?\.css$/, ...initialCacheIgonre],
        importsDirectory: isDev ? '' : `__workbox-assets`,
        cacheId: `${projectName}-sw`
        // runtimeCaching: [
        //     {
        //         urlPattern: /(^|\/)api\//,
        //         handler: 'NetworkOnly'
        //     },
        //     {
        //         urlPattern: new RegExp(`(^|/)${distClientAssetsDirName}/`),
        //         handler: 'CacheFirst'
        //     },
        //     {
        //         urlPattern: /.+/,
        //         handler: 'NetworkFirst'
        //     }
        // ]
    });
};

@@ -464,27 +464,8 @@
 et %7B
- name: projectName,
  pwa
@@ -504,16 +504,51 @@
  const %7B
+%0A        name: projectName,%0A       
  distCli
@@ -563,16 +563,20 @@
 sDirName
+%0A   
  %7D = koo
@@ -2073,442 +2073,8 @@
 sw%60%0A
-        // runtimeCaching: %5B%0A        //     %7B%0A        //         urlPattern: /(%5E%7C%5C/)api%5C//,%0A        //         handler: 'NetworkOnly'%0A        //     %7D,%0A        //     %7B%0A        //         urlPattern: new RegExp(%60(%5E%7C/)$%7BdistClientAssetsDirName%7D/%60),%0A        //         handler: 'CacheFirst'%0A        //     %7D,%0A        //     %7B%0A        //         urlPattern: /.+/,%0A        //         handler: 'NetworkFirst'%0A        //     %7D%0A        // %5D%0A
     

remove unused code