"use strict";

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

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

var _fs = _interopRequireDefault(require("fs"));

var _caseSensitivePathsWebpackPlugin = _interopRequireDefault(require("case-sensitive-paths-webpack-plugin"));

var _lodashWebpackPlugin = _interopRequireDefault(require("lodash-webpack-plugin"));

var _miniCssExtractPlugin = _interopRequireDefault(require("mini-css-extract-plugin"));

var _I18nSplitPlugin = _interopRequireDefault(require("../plugins/I18nSplitPlugin"));

var _RequireVariablePublicPlugin = _interopRequireDefault(require("../plugins/RequireVariablePublicPlugin"));

var _CssOrderControlPlugin = _interopRequireDefault(require("../plugins/CssOrderControlPlugin"));

var _copyWebpackPlugin = _interopRequireDefault(require("copy-webpack-plugin"));

var _webpack = _interopRequireDefault(require("webpack"));

var _plugins = require("../plugins");

var _CustomAttributePlugin = require("../plugins/CustomAttributePlugin");

var _RtlCssPlugin = require("../plugins/RtlSplitPlugin/RtlCssPlugin");

var _VariableConversionCollector = _interopRequireDefault(require("../plugins/VariableConversionCollector"));

var _SelectorPlugin = _interopRequireDefault(require("../plugins/SelectorPlugin"));

var _configHtmlWebpackPlugins = require("./configHtmlWebpackPlugins");

var _EfcResourceCleanupPlugin = _interopRequireDefault(require("../plugins/EfcResourceCleanupPlugin"));

var _EventsHandlingPlugin = require("../plugins/EventsHandlingPlugin");

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

// import { windowsModification } from '../loaderUtils/windowsModification';
const getDevPlugins = (options, publicPath) => {
  const {
    app: {
      generateHtml,
      tpFolder,
      folder,
      instrumentScript,
      customAttributes,
      devCssFileBountry,
      context,
      hasEFC: prevOptionForEnableEFC,
      hasShadowDOM,
      cssVariableReplacementConfig,
      plugins: pluginObject,
      patterns,
      exclude,
      selectorWeightConfig,
      server: {
        mode
      },
      htmlTemplate: {
        inject,
        customScriptLoadingStrategey
      },
      crossorigin
    },
    efc: {
      hasEFC: newOptionForEnableEFC,
      templateFilePath,
      nameScope: efcNameSpace,
      localeAttr: efcLocaleAttr
    },
    css: {
      enableRTLSplit,
      cssOrderControler
    },
    i18n,
    unusedFiles,
    resourceHints
  } = options;
  const hasEFC = newOptionForEnableEFC || prevOptionForEnableEFC;
  const cssLTRFileNameTempalte = enableRTLSplit ? 'css/[name].ltr.css' : 'css/[name].css';
  const cssRTLFileNameTempalte = 'css/[name].rtl.css'; // const selectorWeightLocal = windowsModification(exclude.selectorWeight);

  const pluginsArr = [new _plugins.CleanUpStatsPlugin(), new _caseSensitivePathsWebpackPlugin.default(), new _webpack.default.IgnorePlugin(/^\.\/domain$/, /moment$/), new _webpack.default.DefinePlugin({
    __CLIENT__: true,
    __TEST__: false,
    __SERVER__: false,
    __DEVELOPMENT__: true,
    __LOCAL_PRODUCTION__: false,
    __DEVTOOLS__: true,
    __DOCS__: false,
    'process.env': {
      NODE_ENV: JSON.stringify(mode === 'dev-no-warn' ? 'production' : 'development')
    }
  }), new _miniCssExtractPlugin.default({
    attributes: customAttributes.cssAttributes,
    filename: cssLTRFileNameTempalte,
    // ignoreOrder: true,
    chunkFilename: cssLTRFileNameTempalte
  }), new _plugins.ResourceHintsPlugin(resourceHints)];

  if (enableRTLSplit) {
    pluginsArr.push(new _RtlCssPlugin.RtlCssPlugin({
      templateLabel: options.css.templateLabel,
      dirVarName: options.css.dirVarName,
      disableMiniFiySelector: options.css.disableMiniFiySelector,
      // config: {},
      // sourcemap: true,
      filename: cssRTLFileNameTempalte
    }));
  }

  if (unusedFiles.enable) {
    pluginsArr.push(new _plugins.UnusedFilesFindPlugin(Object.assign(unusedFiles, {
      origin: _path.default.join(process.cwd(), folder)
    })));
  }

  const i18nManifestFileName = 'i18n-manifest.json';
  /**
   * NOTE:
   *  this file name ext .i18n.js added
   *  Because, in service worker they maintain cache as file name based
   *  So, to make differce we added .i18n.js ext.
   */

  const i18nFileNameTemplate = 'i18n-chunk/[locale]/[name].i18n.js';
  const i18nPublicPath = publicPath || '';

  if (i18n.chunkSplitEnable) {
    pluginsArr.push(new _I18nSplitPlugin.default({
      jsResource: i18n.jsResource,
      localeVarName: i18n.localeVarName,
      jsonpFunc: i18n.jsonpFunc,
      disableDefault: i18n.disableDefault,
      publicPath: i18nPublicPath,
      i18nManifestFileName,
      // template: (object, locale) => `window.loadI18n(${JSON.stringify(object)}, ${JSON.stringify(locale)})`,
      filenameTemplate: i18nFileNameTemplate,
      propertiesFolder: i18n.propertiesFolder
    }));
  } else {
    const i18nPath = _path.default.join(process.cwd(), context, 'i18n');

    if (_fs.default.existsSync(i18nPath)) {
      pluginsArr.push(new _copyWebpackPlugin.default([{
        from: `${i18nPath}/`,
        to: './i18n/[name].[ext]',
        toType: 'template'
      }]));
    }
  }

  tpFolder && pluginsArr.push(new _copyWebpackPlugin.default([{
    from: `${_path.default.join(process.cwd(), context, tpFolder)}/`,
    to: `./${tpFolder}/`,
    toType: 'dir'
  }]));
  pluginsArr.push(new _lodashWebpackPlugin.default({
    collections: true,
    shorthands: true
  }));
  generateHtml && (0, _configHtmlWebpackPlugins.configHtmlWebpackPlugins)(pluginsArr, {
    enableChunkHash: false,
    folder,
    minify: false,
    inject,
    crossorigin,
    hasEFC,
    customScriptLoadingStrategey
  });

  if (hasEFC) {
    pluginsArr.push(new _plugins.EFCPlugin({
      isDevelopment: true,
      i18nManifestFileName,
      templateFilePath,
      i18nFileNameTemplate,
      serverUrl: publicPath,
      entryPointName: 'efc',
      nameScope: efcNameSpace,
      localeAttr: efcLocaleAttr // outputFile: 'zohodesk-efc-sdk-latest.js',

    }));
  }

  pluginsArr.push(new _plugins.I18NInjectIntoIndexPlugin({
    publicPath: i18nPublicPath,
    isDevelopment: true,
    templateLabel: i18n.templateLabel || '{{--user-locale}}',
    i18nFileNameTemplate,
    i18nManifestFileName,
    mainChunkName: 'main'
  }));
  instrumentScript && pluginsArr.push(new _plugins.ScriptInstrumentPlugin());
  cssOrderControler.enable && pluginsArr.push(new _CssOrderControlPlugin.default(cssOrderControler));
  customAttributes.enable && pluginsArr.push(new _CustomAttributePlugin.CustomAttributePlugin(customAttributes));
  customAttributes.enable && pluginsArr.push(new _EfcResourceCleanupPlugin.default(Object.assign({}, customAttributes, {
    globalCacheObj: context
  })));
  hasShadowDOM && pluginsArr.push(new _plugins.ShadowDOMSupportPlugin());

  if (devCssFileBountry) {
    pluginsArr.push(new _RequireVariablePublicPlugin.default({
      requireFuncName: 'require'
    }));
  }

  if (pluginObject.cssVariableReplacement) {
    pluginsArr.push(new _VariableConversionCollector.default({
      cssVariableReplacementConfig,
      patterns
    }));
  }

  if (pluginObject.selectorWeight) {
    pluginsArr.push(new _SelectorPlugin.default({
      selectorWeightConfig,
      exclude: exclude.selectorWeight,
      patterns
    }));
  }

  pluginsArr.push(new _EventsHandlingPlugin.EventsHandlingPlugin()); // if (pluginObject.minifier) {
  //   // console.log('minifier active');
  //   pluginsArr.push(new MinifierPlugin());
  // }

  return pluginsArr.filter(Boolean);
};

var _default = getDevPlugins;
exports.default = _default;