"use strict";

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

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

const pluginName = 'I18nDebugPlugin'; // this plugin  only for debug  related purpose

class I18nDebugPlugin {
  constructor(jsResourceI18nKeys) {
    this.jsResourceI18nKeys = jsResourceI18nKeys;
    this.warnings = [];
  }

  apply(compiler) {
    compiler.hooks.thisCompilation.tap(pluginName, (compilation, compilationParams) => {
      // TEMP : only for debugging purpose
      this.compilation1 = compilation;
      this.compilationParams = compilationParams.normalModuleFactory;
    });
    compiler.hooks.normalModuleFactory.tap(pluginName, factory => {
      if (!this.compilationParams || this.compilationParams.normalModuleFactory === factory) {
        return;
      }

      const handler = parser => {
        parser.hooks.program.tap(pluginName, (ast, commmets) => {
          const {
            module
          } = parser.state; //

          if (!(module && /\.jsx?$/.test(module.resource))) {
            return;
          } // i18n grep proper contract regex


          let i18nKeys = (0, _utils.collectI18nKeysfromAST)(ast, this.jsResourceI18nKeys);
          i18nKeys = i18nKeys.concat( // i18n grep proper contract regex
          (0, _utils.collectI18nKeysfromComments)(commmets, this.jsResourceI18nKeys));
          i18nKeys.length && this.warnings.push({
            message: 'skiped i18n keys',
            reason: 'may be some child compiler',
            fileName: module.resource,
            i18nKeys
          });
        });
      };

      factory.hooks.parser.for('javascript/auto').tap(pluginName, handler);
      factory.hooks.parser.for('javascript/dynamic').tap(pluginName, handler); // factory.hooks.parser.for('javascript/esm').tap(pluginName, handler);
    });
  }

}

exports.default = I18nDebugPlugin;