"use strict";

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

const {
  Template
} = require('webpack');

const pluginName = 'prefetch-preload-chunk-plugin';

class ResourceHintsPlugin {
  constructor(options) {
    this.options = options;
  }

  apply(compiler) {
    compiler.hooks.thisCompilation.tap(pluginName, ({
      mainTemplate
    }) => {
      mainTemplate.hooks.requireExtensions.tap(pluginName, (source, chunk, hash) => {
        const {
          allowPrefetchingMultipleChunks
        } = this.options;
        const idNameMap = chunk.getChunkMaps().name;
        const nameIdMap = {};
        let needsMap = allowPrefetchingMultipleChunks;

        for (const key in idNameMap) {
          if (Object.prototype.hasOwnProperty.call(idNameMap, key)) {
            const value = idNameMap[key];
            nameIdMap[value] = key;

            if (key !== value) {
              needsMap = true;
            }
          }
        }

        return Template.asString([source, Template.indent([`const nameToChunkIdMap = ${JSON.stringify(needsMap ? nameIdMap : {})};`, 'const chunkNames = Object.keys(nameToChunkIdMap);']), `${mainTemplate.requireFn}.getChunkIds = function getChunkIds(chunkId) {`, Template.indent([allowPrefetchingMultipleChunks ? `const isRegExAsChunkId = chunkId instanceof RegExp; 
                return isRegExAsChunkId ? chunkNames.filter(chunkName => chunkId.test(chunkName)).map(chunkName => nameToChunkIdMap[chunkName]) : [nameToChunkIdMap[chunkId] || chunkId];` : needsMap ? 'return [nameToChunkIdMap[chunkId] || chunkId];' : 'return [chunkId];']), '}', `// Prefetch a chunk (${pluginName})`, `${mainTemplate.requireFn}.pfc = function prefetchChunk(chunkId) {`, Template.indent([`let chunkIds = ${mainTemplate.requireFn}.getChunkIds(chunkId)`, `chunkIds.forEach(idOfAChunk => {
                ${mainTemplate.requireFn}.e(idOfAChunk);
              })`]), '};',
        /*
          (needsMap
            ? [`chunkId = ${JSON.stringify(nameIdMap)}[chunkId]||chunkId;`]
            : []
          ).concat([
            'var promises = [];',
            'if(installedChunks[chunkId] === undefined) {',
            Template.indent([
              'installedChunks[chunkId] = null;',
              /* mainTemplate.hooks.linkPrefetch.call('', chunk, hash),
              'document.head.appendChild(link);', */

        /*
              mainTemplate.hooks.requireEnsure.call('', chunk, hash)
            ]),
            '}'
          ])
        ),
        '}',
        */
        `// Preload a chunk (${pluginName})`, `${mainTemplate.requireFn}.plc = function preloadChunk(chunkId) {`, Template.indent([`chunkId = ${mainTemplate.requireFn}.getChunkIds(chunkId)[0]`, 'if(installedChunks[chunkId] === undefined) {', Template.indent(['installedChunks[chunkId] = null;', mainTemplate.hooks.linkPreload.call('', chunk, hash), 'document.head.appendChild(link);', `${mainTemplate.requireFn}.e(chunkId);` // 'var head = document.getElementsByTagName(\'head\')[0];',
        // mainTemplate.hooks.jsonpScript.call('', chunk, hash),
        // 'head.appendChild(script);'
        ]), '}']), '};']);
      });
    });
  }

}

var _default = ResourceHintsPlugin;
exports.default = _default;