"use strict";

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

var _path = _interopRequireDefault(require("path"));

var _htmlWebpackPlugin = _interopRequireDefault(require("html-webpack-plugin"));

var _htmlWebpackInjectAttributesPlugin = _interopRequireDefault(require("html-webpack-inject-attributes-plugin"));

var _common = require("../common");

var _utils = require("../utils");

var _CustomScriptLoadingStrategyPlugin = _interopRequireDefault(require("../plugins/CustomScriptLoadingStrategyPlugin"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

const defaultHTMLMiniFyOption = {
  collapseWhitespace: true,
  minifyCSS: true,
  minifyJS: true,
  keepClosingSlash: true,
  removeComments: false,
  removeRedundantAttributes: true,
  removeScriptTypeAttributes: true,
  removeStyleLinkTypeAttributes: true,
  useShortDoctype: true
};
const defaultScriptLoadingStrategy = 'defer';
const allowedScriptLoadingStrategies = ['blocking', 'defer', 'async', 'module'];

function isAllowedScriptLoadingStrategyUsed(scriptLoadingStategey) {
  return allowedScriptLoadingStrategies.includes(scriptLoadingStategey);
}

function getScriptLoadingStrategyForStringType(scriptLoadingStategey) {
  if (isAllowedScriptLoadingStrategyUsed(scriptLoadingStategey)) {
    return scriptLoadingStategey;
  }

  return defaultScriptLoadingStrategy;
}

function getScriptLoadingStrategyForObject(scriptLoadingStategey) {
  if (Object.keys(scriptLoadingStategey).length === 0) {
    return defaultScriptLoadingStrategy;
  }

  const isAllowedScriptLoadingStrategy = Object.keys(scriptLoadingStategey).every(key => isAllowedScriptLoadingStrategyUsed(key));

  if (isAllowedScriptLoadingStrategy) {
    return Object.assign({}, scriptLoadingStategey);
  }

  console.warn('un supported script loading strategy used', scriptLoadingStategey);
  return defaultScriptLoadingStrategy;
}

function getScriptLoadingStrategy(scriptLoadingStategey) {
  if ((0, _utils.getTypeOf)(scriptLoadingStategey) === 'string') {
    return {
      [getScriptLoadingStrategyForStringType(scriptLoadingStategey)]: [/.*/]
    };
  }

  if ((0, _utils.getTypeOf)(scriptLoadingStategey) === 'object') {
    return getScriptLoadingStrategyForObject(scriptLoadingStategey);
  }

  return {
    [defaultScriptLoadingStrategy]: [/.*/]
  };
}

function configHtmlWebpackPlugins(plugins, {
  enableChunkHash = false,
  folder,
  inject,
  crossorigin,
  hasEFC,
  minify: minifyHtmlOptions = false,
  customScriptLoadingStrategey = {}
}) {
  const optionsHtmlWebpack = {
    chunksSortMode: 'none',
    filename: enableChunkHash ? 'index.[templatehash:20].html' : 'index.html',
    template: _path.default.join(process.cwd(), folder, 'index.html'),
    minify: minifyHtmlOptions === true ? defaultHTMLMiniFyOption : minifyHtmlOptions === null ? false : minifyHtmlOptions,
    // : typeof minifyHtmlOptions === 'object'
    // ? minifyHtmlOptions
    // : minifyHtmlOptions,,
    templateParameters: _common.templateParameters,
    scriptLoading: defaultScriptLoadingStrategy,
    inject: inject
  };

  if (hasEFC) {
    optionsHtmlWebpack.excludeChunks = ['efc', 'widget'];
  }

  plugins.push(new _htmlWebpackPlugin.default(optionsHtmlWebpack));
  crossorigin && plugins.push(new _htmlWebpackInjectAttributesPlugin.default({
    crossorigin: 'anonymous'
  }));

  if (customScriptLoadingStrategey.enable) {
    const currentScriptLoadingStrategy = getScriptLoadingStrategy(customScriptLoadingStrategey.options);

    if ((0, _utils.getTypeOf)(currentScriptLoadingStrategy) === 'object') {
      plugins.push(new _CustomScriptLoadingStrategyPlugin.default({
        scriptLoadingStategey: currentScriptLoadingStrategy
      }));
    }
  }
}