"use strict";

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

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

var _cssClassNameGenerate = _interopRequireDefault(require("../utils/cssClassNameGenerate"));

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

var _fileHandling = require("../plugins/utils/fileHandling");

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

const fs = require('fs');

const options = (0, _utils.getOptions)();
const defaultPostCssPluginOrder = ['valueReplacer', 'selectorReplace', 'hasRTL', 'hoverActive', 'combinerMediaQuery', 'cssVariableReplacement', 'selectorWeight', 'minifier', 'composeMinification', 'autoPrefixer'];

function calculatePostCssPluginOrder(postCssPluginOrder, pluginOrder) {
  // if(typeof postCssPluginOrder === Boolean)
  if (Array.isArray(postCssPluginOrder)) {
    return postCssPluginOrder;
  }

  if (postCssPluginOrder) {
    return pluginOrder;
  }

  return defaultPostCssPluginOrder.filter(value => pluginOrder.includes(value));
}

const getCSSLoaders = optionsObj => {
  const {
    plugins,
    patterns,
    mediaQueryHoverActiveString,
    cssVariableReplacementConfig,
    prefixConfig,
    classNameBlob,
    cssUniqueness,
    selectorReplace,
    cssHashSelectors,
    classNamePrefix,
    postCssPluginOrder,
    customClassNamePrefix
  } = optionsObj;
  const {
    devCssFileBountry
  } = options.app;
  const {
    valueReplacer
  } = options.css;
  const {
    cssSelectorZipPath
  } = options.impactService;
  const cssLoaderOptions = {
    // importLoaders: hasRTL||hoverActive ? 1 : 0,
    importLoaders: 1,
    modules: {},
    sourceMap: true
  };

  if (classNameBlob) {
    cssLoaderOptions.modules.localIdentName = classNameBlob;
  } else {
    cssLoaderOptions.modules.getLocalIdent = (0, _cssClassNameGenerate.default)(cssUniqueness, cssHashSelectors, classNamePrefix, customClassNamePrefix, patterns);
  }

  const pluginOrder = calculatePostCssPluginOrder(postCssPluginOrder, Object.keys(plugins).filter(x => plugins[x] === true)); // console.log(require('autoprefixer'));
  // console.log(require('autoprefixer')());
  // console.log('selector weight config : ', selectorWeightConfig);

  const postcssPlugins = [plugins.valueReplacer && require('../postcss-plugins/ValueReplacer')(valueReplacer), plugins.hasRTL && require('@zohodesk/postcss-rtl')({
    addPrefixToSelector: function addPrefixToSelector(selector, prefix) {
      if (prefix === '[dir]') {
        return selector;
      }

      return `${prefix} ${selector}`; // Make selectors like [dir=rtl] > .selector
    }
  }), plugins.selectorReplace && require('../postcss-plugins/SelectorReplace')(selectorReplace), plugins.combinerMediaQuery && require('postcss-combine-media-query')(), plugins.hoverActive && require('../postcss-plugins/hoverActivePlugin')(mediaQueryHoverActiveString), plugins.cssVariableReplacement && fs.existsSync(cssVariableReplacementConfig) && require('../postcss-plugins/variableModificationPlugin/index').plugin(cssVariableReplacementConfig), plugins.autoPrefixer && require('../postcss-plugins/AutoPrefixer')(prefixConfig) // require('autoprefixer')({ flexbox: true, grid: false })
  // ,
  // plugins.composeMinification &&
  //   require('../postcss-plugins/composePlugin')()
  ].filter(Boolean);
  return [cssSelectorZipPath && {
    loader: require.resolve('../loaders/selectorMappingLoader')
  }, {
    loader: _miniCssExtractPlugin.default.loader,
    options: {
      publicPath: '../'
    }
  }, {
    loader: 'css-loader',
    options: cssLoaderOptions
  }, devCssFileBountry && {
    loader: require.resolve('../loaders/fileBountryLoader'),
    options: {
      devCssFileBountry
    }
  }, postcssPlugins.length > 0 ? {
    loader: 'postcss-loader',
    options: {
      ident: 'postcss',
      plugins: function (params) {
        // console.log('check here : ', params.resourcePath);
        // getCSSLoaders for all postcss filtering

        /*
        input : 
        params.resourcePath : 'D:/MyWork/..../desk_client_app/supportapp/src/components/Avatar/Avatar.module.css,
        patterns : [
            // include all files
           "valueReplacer": [ 
              "*"
            ],
            // include all files
            "selectorReplace": [
              "*"
            ],
            // include src folder, include deskapp folder, exclude node modules
            "hoverActive": [
              "src",
              "deskapp",
              "!node_modules"
            ],
            // include src folder, include deskapp folder, exclude node modules
            "combinerMediaQuery": [
              "src",
              "deskapp",
              "!node_modules"
            ],
            // include src folder, include deskapp folder, exclude node modules
            "hasRTL": [
              "src",
              "deskapp",
              "!node_modules"
            ],
            // include src folder, include deskapp folder, exclude node modules
            "cssVariableReplacement": [
              "src",
              "deskapp",
              "!node_modules"
            ],
            // include src folder, include deskapp folder, exclude node modules
            "selectorWeight": [
              "src",
              "deskapp",
              "!node_modules"
            ],
            // include all files
            "cssUniqueness": [
              "*"
            ]
        ],
        
        [ postcssPlugins that are implemented ]
            */
        //  console.log(params.resourcePath);
        //  console.log(postcssPlugins);
        const finalPostcssPlugins = (0, _fileHandling.isFileNameMatchingPattern)({
          filename: params.resourcePath,
          filterObject: patterns,
          plugins: postcssPlugins,
          order: pluginOrder
        }); // postcssPlugins that are allowed

        return finalPostcssPlugins.length > 0 ? finalPostcssPlugins : [require('../postcss-plugins/EmptyPlugin')()];
      }
    }
  } : null, plugins.composeMinification ? {
    loader: require.resolve('../loaders/composeLoader')
  } : null].filter(Boolean);
};

var _default = getCSSLoaders;
exports.default = _default;