"use strict";

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

var _iterateModulesInChunk = require("./iterateModulesInChunk");

class ModulesI18nKeysStore {
  constructor() {
    this.modulesI18nKeysMap = {};
    this.compilation = null;
  }

  updateCompilation(compilation) {
    this.compilation = compilation;
  }

  updateModuleI18nKeys(resource, i18nKeys) {
    this.modulesI18nKeysMap[resource] = i18nKeys;
  }

  getModuleI18nKeys(resource) {
    return this.modulesI18nKeysMap[resource];
  }

  isChunkHasI18n(chunk) {
    const {
      modulesI18nKeysMap,
      compilation
    } = this;
    let hasModuleI18n = false;
    (0, _iterateModulesInChunk.iterateModulesInChunk)({
      chunk,
      compilation,
      cb: ({
        module: mod,
        stop: stopLoop
      }) => {
        if (mod.resource) {
          const keys = modulesI18nKeysMap[mod.resource];

          if (keys && keys.length > 0) {
            hasModuleI18n = true;
            stopLoop();
          }
        }
      }
    });
    return hasModuleI18n;
  }

  getI18nKeysForChunk(chunk) {
    const {
      modulesI18nKeysMap,
      compilation
    } = this;
    const i18nKeysMap = {};

    function updateI18nKeys(mod) {
      const keys = modulesI18nKeysMap[mod.resource];

      if (keys) {
        // eslint-disable-next-line no-restricted-syntax
        for (const key of keys) {
          i18nKeysMap[key] = 1;
        }
      }
    }

    (0, _iterateModulesInChunk.iterateModulesInChunk)({
      chunk,
      compilation,
      cb: ({
        module: mod
      }) => {
        if (mod.resource) {
          updateI18nKeys(mod);
        }
      }
    });
    return Object.keys(i18nKeysMap);
  }

}

exports.ModulesI18nKeysStore = ModulesI18nKeysStore;