"use strict";

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

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

const CSS_MODULE_TYPE = 'css/mini-extract';

function getFilenameFromModule(webpackModule) {
  const {
    userRequest
  } = webpackModule;

  if (webpackModule.type === CSS_MODULE_TYPE) {
    const {
      _identifier: identifier = ''
    } = webpackModule;
    return identifier.slice(identifier.lastIndexOf('!') + 1);
  }

  return userRequest;
}

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

  const isVendor = function isVendor(module) {
    const userRequest = getFilenameFromModule(module);
    const excludeList = ['script-loader', 'raw-loader'];

    if (userRequest && excludeList.some(s => userRequest.includes(s))) {
      console.log({
        userRequest
      });
    }

    const patterns = ['node_modules', '!**/node_modules/react/', '!**/node_modules/react-dom/', '!*.css', '!**/publicPathConfig.js', ...(0, _checkIsPatternsMatchFilename.modifyAsExclude)(excludeList), ...vendorPatterns];
    return userRequest && (0, _checkIsPatternsMatchFilename.checkIsPatternsMatchFilename)(patterns, userRequest);
  };

  const isReact = module => {
    const userRequest = getFilenameFromModule(module);
    const reactBundle = ['**/node_modules/react/', '**/node_modules/react-dom/'];
    return userRequest && (0, _checkIsPatternsMatchFilename.checkIsPatternsMatchFilename)(reactBundle, userRequest);
  };

  const defaultChunks = {
    'react.vendor': {
      name: 'react.vendor',
      chunks: 'all',
      minChunks: 1,
      test: isReact,
      enforce: true,
      // priority: 10
      priority: 0
    },
    vendor: {
      name: 'vendor',
      chunks: 'all',
      // chunks: 'initial',
      minChunks: 1,
      test: isVendor,
      // priority: 19,
      priority: 0
    }
  };
  const customChunksConfig = customChunks.reverse().map((obj, index) => ({
    name: obj.name,
    patterns: obj.patterns,
    minChunks: obj.minChunks || obj.size || 2,
    priority: obj.priority || 10 * (index + 2),
    enforce: obj.enforce || true,
    maxSize: obj.maxSize,
    // || 0,
    minSize: obj.minSize,
    // || 20000,
    chunks: obj.chunks || 'all'
  })).reduce((chunksConfig, {
    name,
    patterns,
    minChunks,
    priority,
    enforce,
    minSize,
    maxSize,
    chunks = 'all'
  }) => {
    const obj = {
      name,
      test: m => {
        const userRequest = getFilenameFromModule(m);
        return userRequest && (0, _checkIsPatternsMatchFilename.checkIsPatternsMatchFilename)(patterns, userRequest);
      },
      chunks,
      enforce,
      minChunks,
      priority
    };

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

    if (maxSize !== undefined) {
      obj.maxSize = maxSize;
    } // eslint-disable-next-line no-param-reassign


    chunksConfig[name] = obj;
    return chunksConfig;
  }, {});
  return {
    minSize: 12000,
    cacheGroups: {
      default: false,
      vendors: false,
      ...defaultChunks,
      ...customChunksConfig
    }
  };
}