"use strict";

exports.__esModule = true;
exports.getSSRChunkHashes = getSSRChunkHashes;

function getHashes(chunkGroup, compilation, hashes = []) {
  for (const chunk of chunkGroup.chunks) {
    if (!chunk.hash) {
      throw new Error(`wuat?`);
    }

    hashes.push(chunk.hash);
  }

  for (const childChunkGroups of Object.values(chunkGroup.getChildrenByOrders(compilation.moduleGraph, compilation.chunkGraph))) {
    for (const childChunkGroup of childChunkGroups) {
      getHashes(childChunkGroup, compilation, hashes);
    }
  }

  return hashes;
}

function getSSRChunkHashes({
  stats,
  components
}) {
  const templateHashes = {};
  const componentChunkNameToTemplatePath = {};
  let renderPageHash = ``;
  components.forEach(component => {
    componentChunkNameToTemplatePath[component.componentChunkName] = component.componentPath;
  });

  for (const chunkGroup of stats.compilation.chunkGroups) {
    if (chunkGroup.name) {
      const concenatedChunksByName = getHashes(chunkGroup, stats.compilation).join(`--`);

      if (chunkGroup.name !== `render-page`) {
        const templatePath = componentChunkNameToTemplatePath[chunkGroup.name];

        if (!templatePath) {
          // additional chunk group can be created by lazy imports
          // we handle them by handling children chunk groups on top level ones
          continue;
        }

        templateHashes[templatePath] = concenatedChunksByName;
      } else {
        renderPageHash = concenatedChunksByName;
      }
    }
  }

  return {
    templateHashes,
    renderPageHash
  };
}
//# sourceMappingURL=get-ssr-chunk-hashes.js.map