"use strict";

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

var _defaultVendorPatternList = require("./defaultVendorPatternList");

var _getFilenameFromModule = require("./getFilenameFromModule");

var _checkIsPatternsMatchFilename = require("./loaderConfigs/checkIsPatternsMatchFilename");

function splitChunksConfig(options) {
  const {
    vendorPatterns,
    customChunks,
    customChunksBaseConfig
  } = options;

  const isVendor = function isVendor(webpackModule) {
    const userRequest = (0, _getFilenameFromModule.getFilenameFromModule)(webpackModule);
    const patterns = [..._defaultVendorPatternList.defaultVendorPatternList, ...vendorPatterns];
    return (0, _checkIsPatternsMatchFilename.checkIsPatternsMatchFilename)(patterns, userRequest);
  };

  const isReact = webpackModule => {
    const userRequest = (0, _getFilenameFromModule.getFilenameFromModule)(webpackModule);
    return (0, _checkIsPatternsMatchFilename.checkIsPatternsMatchFilename)(_defaultVendorPatternList.reactBundleIncludeList, userRequest);
  };

  const defaultChunks = {
    'react.vendor': {
      name: 'react.vendor',
      chunks: 'all',
      minChunks: 1,
      test: isReact,
      enforce: true,
      priority: 30
    },
    vendor: {
      name: 'vendor',
      chunks: 'all',
      minChunks: 1,
      test: isVendor,
      priority: 20
    }
  };
  const customChunksConfig = {};
  customChunks.reverse().forEach((cacheGroupObj, index) => {
    const cacheGroupName = cacheGroupObj.cacheGroupName || cacheGroupObj.name; // eslint-disable-next-line no-use-before-define

    const obj = getCacheGroup(cacheGroupObj, index);
    customChunksConfig[cacheGroupName] = obj;
  });
  const splitChunksConfig = {
    minSize: 12000,
    ...(customChunksBaseConfig || {}),
    cacheGroups: {
      default: false,
      defaultVendors: false,
      ...defaultChunks,
      ...customChunksConfig
    }
  };
  return splitChunksConfig;
}

function getCacheGroup(obj, index) {
  let {
    minChunks,
    priority,
    chunks = 'all'
  } = obj;
  const {
    name,
    patterns,
    enforce,
    minSize,
    maxSize,
    reuseExistingChunk,
    automaticNameDelimiter
  } = obj;
  chunks = obj.chunks || 'all';
  minChunks = obj.minChunks || 2;
  priority = obj.priority || 10 * (index + 2);
  const cacheGroup = {
    name,
    test: m => {
      const userRequest = (0, _getFilenameFromModule.getFilenameFromModule)(m);
      return userRequest && (0, _checkIsPatternsMatchFilename.checkIsPatternsMatchFilename)(patterns, userRequest);
    },
    chunks,
    minChunks,
    priority,
    enforce
  };

  if (minSize !== undefined) {
    cacheGroup.minSize = minSize;
  }

  if (maxSize !== undefined) {
    cacheGroup.maxSize = maxSize;
  }

  if (reuseExistingChunk !== undefined) {
    cacheGroup.reuseExistingChunk = reuseExistingChunk;
  }

  if (automaticNameDelimiter !== undefined) {
    cacheGroup.automaticNameDelimiter = automaticNameDelimiter;
  }

  return cacheGroup;
}