"use strict";

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

var _path = _interopRequireDefault(require("path"));

var _webpackSources = require("webpack-sources");

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

let isValidModule = name => {
  if (name.includes('node_modules') && (name.includes(`@zohodesk${_path.default.sep}components`) || name.includes(`@zohodesk${_path.default.sep}dot`) || name.includes(`@zohodesk${_path.default.sep}icons`) || name.includes(`@zohodesk${_path.default.sep}svg`)) && !name.endsWith('docs.js')) {
    return true;
  }

  return !name.includes('index.js') && !name.endsWith('.css') && !name.endsWith('.png') && !name.includes('node_modules') && name.includes('src');
};

let getModuleName = modulePath => {
  let [name] = _path.default.parse(modulePath).name.split('.');

  if (modulePath.includes(`@zohodesk${_path.default.sep}components`)) {
    name = `@zohodesk/components/${name}`;
  } else if (modulePath.includes(`@zohodesk${_path.default.sep}dot`)) {
    name = `@zohodesk/dot/${name}`;
  }

  return name;
};

class ModuleStatsPlugin {
  constructor(options = {}) {
    this.options = options;
    this.options.filename = options.filename || 'js/moduleStats.js';
  }

  apply(compiler) {
    compiler.hooks.emit.tap('ModuleStatsPlugin', compilation => {
      let stats = compilation.getStats().toJson();
      let results = {};
      let {
        modules
      } = stats;

      for (let module of modules) {
        let {
          name,
          reasons
        } = module;

        if (isValidModule(name)) {
          let references = new Set();
          let referencedby = new Set();
          let docsName;
          let fileName;
          compilation.modules.forEach(module => {
            module.reasons.forEach(reason => {
              let exportedValue = reason.dependency;

              if (exportedValue.type === 'harmony export imported specifier') {
                if (exportedValue.module.userRequest.includes(name.replace('.', ''))) {
                  docsName = exportedValue.name;
                }
              }
            });
          });
          let currentModuleName = name;

          if (Array.isArray(reasons)) {
            for (let reason of reasons) {
              let {
                moduleName
              } = reason;

              if (isValidModule(moduleName)) {
                let reasonedModuleName = moduleName;
                referencedby.add(reasonedModuleName);
              }
            }
          }

          for (let subModule of modules) {
            let {
              name: subModuleName,
              reasons
            } = subModule;

            if (isValidModule(subModuleName)) {
              let currentModuleName = subModuleName;

              if (Array.isArray(reasons)) {
                for (let reason of reasons) {
                  let {
                    moduleName: reasonedModuleName
                  } = reason;

                  if (name === reasonedModuleName) {
                    references.add(currentModuleName);
                  }
                }
              }
            }
          }

          results[currentModuleName] = {
            name: currentModuleName,
            references: Array.from(references),
            referencedby: Array.from(referencedby),
            docsName: docsName ? docsName : '',
            fileName: name ? name : currentModuleName
          };
        }
      }

      compilation.assets[this.options.filename] = new _webpackSources.RawSource(JSON.stringify(results));
    });
  }

}

exports.default = ModuleStatsPlugin;