"use strict";

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

var _postcss = _interopRequireDefault(require("postcss"));

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

var _classHandling = require("./utils/classHandling");

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

var _windowsModification = require("../loaderUtils/windowsModification");

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

function selectorWeightPrefixAdder(rootOriginal, selector, excludeStrings) {
  rootOriginal.walkRules((rule, index) => {
    const prevNode = rootOriginal.nodes[index - 1] || null;

    if ((0, _classHandling.hasPrevNodeIgnore)(index, prevNode, 'updateselector:ignore')) {
      return;
    } // console.log(rule.selector.split(','));


    const selectorArr = rule.selector.split(',');
    selectorArr.forEach((singleSelectorStr, index) => {
      if (excludeStrings.includes(singleSelectorStr.trim())) {
        return;
      } // NOTE: below logic for attrbute related


      if (singleSelectorStr.includes(']')) {
        // console.log(singleSelectorStr);
        const prefixPart = singleSelectorStr.slice(0, singleSelectorStr.lastIndexOf(']') + 1).trim();
        const selectorPart = singleSelectorStr.slice(singleSelectorStr.lastIndexOf(']') + 1).trim();

        if (excludeStrings.includes(selectorPart.trim())) {
          return;
        }

        if (/^:/gi.test(selectorPart)) {
          return;
        }

        if (selectorPart !== '') {
          selectorArr[index] = `${prefixPart} ${selector}${selectorPart}`;
        }

        return;
      }

      selectorArr[index] = `${selector}${singleSelectorStr}`;
    });
    rule.selector = selectorArr.join(',\n');
  });
  return rootOriginal;
}

class SelectorPlugin {
  constructor(options = {}) {
    this.optimize = options.optimize;
    this.selectorWeightConfig = options.selectorWeightConfig;
    this.exclude = options.exclude;
    this.patterns = options.patterns;
  }

  apply(compiler) {
    const {
      defaultSelector,
      customFileDetails,
      excludeStrings
    } = this.selectorWeightConfig;

    const rawdata = _fs.default.readFileSync(customFileDetails);

    const data = JSON.parse(rawdata);
    this.exclude = (0, _windowsModification.windowsModification)(this.exclude);
    compiler.hooks.compilation.tap('selector-weight-rewrite', compilation => {
      compilation.hooks.optimizeModules.tap('selector-weight-rewrite', modules => {
        const mods = modules.filter(x => x.type.includes('css'));
        mods.forEach(module => {
          const rootOriginal = _postcss.default.parse(module.content);

          const filename = module.issuer.resource;
          /*
              input : 
              filename : 'D:/MyWork/..../desk_client_app/supportapp/src/components/Avatar/Avatar.module.css,
            
                patterns.cssVariableReplacement: 
              // include src folder, include deskapp folder, exclude node modules
              "selectorWeight": [
                "src",
                "deskapp",
                "!node_modules"
              ]
                
              output : 
              true or false
            */

          if (!(0, _fileHandling.isFileNameMatchingPluginPattern)({
            filename,
            filterArr: this.patterns.selectorWeight
          })) {
            return;
          }
          /*
           
          filename : Deskapp.module.css ?
            data : 
          {
              "DeskApp.module.css": {"defaultSelector" : ".desk_app_ui "}
          }
              selectorWeightConfig : 
            defaultSelector : .zoho_desk_ui
          
          case filename = 'DeskApp.module.css' ? 
          data[filename].defaultSelector = '.desk_app_ui' 
            case filename is different ?
          defaultSelector = '.zoho_desk_ui'
            */


          module.content = selectorWeightPrefixAdder(rootOriginal, data[filename] ? data[filename].defaultSelector : defaultSelector, excludeStrings).toString();
        });
      });
    });
  }

}

var _default = SelectorPlugin;
exports.default = _default;