/*
 * @flow
 */

import type {RawStats, Module} from '../types/Stats';

import type {Child} from './getEntryHeirarchy';

import getModulesByChunk from './getModulesByChunk';

export default function getChunkModules(
  stats: RawStats,
  parentChunks: ?Array<Child>,
): ?Array<Module> {
  if (!parentChunks) {
    return null;
  }

  const modulesByChunk = getModulesByChunk(
    stats,
    parentChunks.map((chunk) => chunk.id),
  );

  return parentChunks.reduce(
    (modules, chunk) => modules.concat(modulesByChunk[chunk.id].modules),
    [],
  );
}

/*
 * @flow
 */

import type {RawStats, Module} from '../types/Stats';

import type {Child} from './getEntryHeirarchy';

import getModulesByChunk from './getModulesByChunk';

export default function getChunkModules(
  stats: RawStats,
  parentChunks: ?Array<Child>,
): ?Array<Module> {
  if (!parentChunks) {
    return null;
  }

  const modulesByChunk = getModulesByChunk(
    stats,
    parentChunks.map((chunk) => chunk.id),
  );

  return parentChunks.reduce(
    (modules, chunk) => {
      const chunkWithModules = modulesByChunk[chunk.id] || {modules: []};
      return modules.concat(chunkWithModules.modules);
    },
    [],
  );
}

Check for undefined when fetching a chunkWithModules by chunk.id
Check for undefined when fetching a chunkWithModules by chunk.id
