"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _fs = require("fs");

var _LocaleChunkAssetsStore = require("./I18nSplitPlugin/LocaleChunkAssetsStore");

var _emitAsset = require("./emitAsset");

var _replaceCssDirTemplate = require("./RtlSplitPlugin/replaceCssDirTemplate");

var _getInitialAssetsFuncTemplate = require("./getInitialAssetsFuncTemplate");

const pluginName = 'ServiceWorkerPlugin';

class ServiceWorkerPlugin {
  constructor(options) {
    this.templateFilePath = options.templateFilePath;
    this.outputFilePath = options.outputFilePath;
    this.publicPath = options.publicPath;
    this.i18nFileNameTemplate = options.i18nFileNameTemplate;
    this.chunkSplitEnable = options.chunkSplitEnable;
    this.enableRTLSplit = options.enableRTLSplit;
    this.entryPointName = options.entryPointName;
    this.replaceText = options.replaceText; // this.publicPaths = options.publicPaths;
  } // eslint-disable-next-line class-methods-use-this


  isInitialChunk(chunk) {
    if ('canBeInitial' in chunk) {
      return chunk.canBeInitial();
    }

    return chunk.isInitial();
  }

  apply(compiler) {
    compiler.hooks.thisCompilation.tap(pluginName, compilation => {
      // Get store for cache
      this.store = (0, _LocaleChunkAssetsStore.getLocaleChunkAssetsStore)(compilation);
    });
    compiler.hooks.thisCompilation.tap(pluginName, compilation => {
      compilation.hooks.processAssets.tap({
        name: pluginName,
        stage: compilation.PROCESS_ASSETS_STAGE_DERIVED
      }, () => {
        this.createServiceWorkerFile(compilation, compiler);
      });
    });
  }

  createServiceWorkerFile(compilation) {
    const {
      templateFilePath,
      entryPointName,
      outputFilePath,
      chunkSplitEnable,
      enableRTLSplit
    } = this;
    const templateContent = (0, _fs.readFileSync)(templateFilePath, 'utf-8');
    let allChunkUrls = [];
    const allI18nAssets = [];
    const entryPoint = compilation.entrypoints.get(entryPointName);
    const initialAssetsFuncTemplate = (0, _getInitialAssetsFuncTemplate.getInitialAssetsFuncTemplate)({
      entryPoint,
      compilation,
      enableRTLSplit,
      i18nStore: this.store,
      chunkSplitEnable,
      i18nFileNameTemplate: this.i18nFileNameTemplate,
      functionName: 'getInitialAssets'
    });
    /**
     * NOTE:
     *  if chunkSplitEnable is false means,
     *    serviceWorker will get i18n files path for html
     */
    // chunkSplitEnable &&

    [...compilation.chunks].filter(c => !this.isInitialChunk(c)).forEach(chunk => {
      allChunkUrls = [...allChunkUrls, ...chunk.files];
    });
    allChunkUrls = allChunkUrls.filter(fileName => !fileName.endsWith('.map'));

    if (enableRTLSplit) {
      allChunkUrls = (0, _replaceCssDirTemplate.replaceCssDirTemplateMapper)(allChunkUrls, _getInitialAssetsFuncTemplate.cssDirTemplate);
    }

    const mod = templateContent.replace(this.replaceText, `
      ${initialAssetsFuncTemplate}

      let initialAssetsObj = getInitialAssets({ lang: langLocale, dir: cssDir });

      let initialAssets = initialAssetsObj.i18n.concat(
        initialAssetsObj.css,
        initialAssetsObj.js
      );
      self.assets = initialAssets;
      
      self.allChunks = ${JSON.stringify(allChunkUrls.concat(allI18nAssets))};`);
    (0, _emitAsset.emitAsset)(compilation, outputFilePath, mod);
  }

}

exports.default = ServiceWorkerPlugin;