"use strict";

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

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

var _I18nDependency = require("./I18nDependency");

var _NullFactory = _interopRequireDefault(require("webpack/lib/NullFactory"));

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

const pluginName = 'I18nKeysIdentifer';
/**
 * this plugin search the i18 keys and make it as I18nDependency.
 * how it works?
 *  this plugin get jsResourceI18nKeys in constructor.
 *  we walk throught the js files AST
 *      if any string literal in that AST is in jsResourceI18nKeys then  we consider that string as I18n key
 *  after the walk of AST we get some I18n keys
 *  then we make it as I18nDependency.
 */

class I18nKeysIdentifer {
  constructor(jsResourceI18nKeys) {
    this.jsResourceI18nKeys = jsResourceI18nKeys;
  }

  defineConfigarationForI18nDependency(compilation) {
    compilation.dependencyFactories.set(_I18nDependency.I18nDependency, new _NullFactory.default());
    compilation.dependencyTemplates.set(_I18nDependency.I18nDependency, new _I18nDependency.I18nDependencyTemplate());
  }

  addI18nDependency(module, i18nKeys) {
    const identifier = `i18n ${module.resource}`;
    const dep = new _I18nDependency.I18nDependency({
      identifier,
      i18nKeys
    }, module.context, 0);
    module.addDependency(dep);
  }

  apply(compiler) {
    compiler.hooks.thisCompilation.tap(pluginName, (compilation, params) => {
      // this below  hook was tapped for  we use I18nDependency
      // so we must define module factory for I18nDependency
      // and we must define how  it  import statement look like I18nDependencyTemplate
      this.defineConfigarationForI18nDependency(compilation); // handler for parser

      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));

          if (i18nKeys.length) {
            this.addI18nDependency(module, i18nKeys);
          }
        });
      }; // this below  hook was tapped for collect I18n Key from files and add as Deppenensy


      const factory = params.normalModuleFactory;
      factory.hooks.parser.for('javascript/auto').tap(pluginName, handler);
      factory.hooks.parser.for('javascript/dynamic').tap(pluginName, handler);
    });
  }

}

exports.default = I18nKeysIdentifer;

I18nKeysIdentifer.isChunkHasI18n = function (chunk) {
  for (const module of chunk.modulesIterable) {
    for (const dep of module.dependencies) {
      if (dep instanceof _I18nDependency.I18nDependency) {
        return true;
      }
    }
  }

  return false;
};