"use strict";

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

var _htmlWebpackPlugin = _interopRequireDefault(require("html-webpack-plugin"));

var _webpack = require("webpack");

var _nameTemplates = require("../common/nameTemplates");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

// import { Template } from 'webpack';
const pluginName = 'CdnChangePlugin'; // const MODULE_TYPE = 'css/mini-extract';

class CdnChangeRuntimePlugin extends _webpack.RuntimeModule {
  constructor(compiler, {
    variableName
  }) {
    super('cdn loading', 10);
    console.log('cdn loading', variableName);
    this.variableName = variableName;
  }

  generate() {
    console.log('changeing public pah');
    return `${_webpack.RuntimeGlobals.getChunkCssFilename} = function(chunkId) {
      const cssCdnUrl = window[${this.variableName}]/chunkId;
      console.log(chunkId);
      return cssCdnUrl;
    }`;
  }

}

class CdnChangePlugin {
  constructor(options) {
    this.enableSMap = options.enableSMap || '{{__I18N_CDN__}}';
    this.i18nTemplate = options.i18nTemplate || '{{__I18N_CDN__}}';
    this.variableName = options.variableName || '__CSS_CDN__';
    this.cssTemplate = options.cssTemplate || '{{__CSS_CDN__}}';
    this.jsTemplate = options.jsTemplate || '{{__JS_CDN__}}';
  }

  apply(compiler) {
    compiler.hooks.thisCompilation.tap(pluginName, compilation => {
      const enabledChunks = new WeakSet();

      const handler = (entryRuntimeChunk, set) => {
        if (enabledChunks.has(entryRuntimeChunk)) {
          return;
        }

        enabledChunks.add(entryRuntimeChunk);
        set.add(_webpack.RuntimeGlobals.getFullHash);
        set.add(_webpack.RuntimeGlobals.getChunkCssFilename);
        compilation.addRuntimeModule(entryRuntimeChunk, new CdnChangeRuntimePlugin(set, {
          compiler,
          variableName: this.variableName
        }));
      };

      compilation.hooks.runtimeRequirementInTree.for(_webpack.RuntimeGlobals.ensureChunkHandlers).tap(pluginName, handler);
    });
    compiler.hooks.compilation.tap(pluginName, compilation => {
      _htmlWebpackPlugin.default.getHooks(compilation).beforeAssetTagGeneration.tap(pluginName, (data, cb) => {
        const {
          enableSMap
        } = this;
        data.assets = { ...data.assets,
          css: data.assets.css.map(css => `${this.cssTemplate}${css}`),
          js: data.assets.js.map(js => {
            if (enableSMap && !(0, _nameTemplates.isI18nFile)(js)) {
              console.log('js file ==>', js);
              js = js.replace('js/', `{{--js-smap}}js/`); //eslint-disable-line
            }

            return `${(0, _nameTemplates.isI18nFile)(js) ? this.i18nTemplate : this.jsTemplate}${js}`;
          })
        };
        cb && cb(null, data);
      });
    });
  }

}

var _default = CdnChangePlugin; // Cdn Change Plugin for runtime chunks???

/**
 * the filename of the script part of the chunk
 */
// exports.getChunkScriptFilename = "__webpack_require__.u";
// /**
//  * the filename of the css part of the chunk
//  */
// exports.getChunkCssFilename = "__webpack_require__.k";

exports.default = _default;