'use strict';Object.defineProperty(exports, "__esModule", { value: true });var _createClass = function () {function defineProperties(target, props) {for (var i = 0; i < props.length; i++) {var descriptor = props[i];descriptor.enumerable = descriptor.enumerable || false;descriptor.configurable = true;if ("value" in descriptor) descriptor.writable = true;Object.defineProperty(target, descriptor.key, descriptor);}}return function (Constructor, protoProps, staticProps) {if (protoProps) defineProperties(Constructor.prototype, protoProps);if (staticProps) defineProperties(Constructor, staticProps);return Constructor;};}();var _scc = require('@rtsao/scc');var _scc2 = _interopRequireDefault(_scc);
var _hash = require('eslint-module-utils/hash');
var _resolve = require('eslint-module-utils/resolve');var _resolve2 = _interopRequireDefault(_resolve);
var _builder = require('./exportMap/builder');var _builder2 = _interopRequireDefault(_builder);
var _childContext = require('./exportMap/childContext');var _childContext2 = _interopRequireDefault(_childContext);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { 'default': obj };}function _toConsumableArray(arr) {if (Array.isArray(arr)) {for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) {arr2[i] = arr[i];}return arr2;} else {return Array.from(arr);}}function _classCallCheck(instance, Constructor) {if (!(instance instanceof Constructor)) {throw new TypeError("Cannot call a class as a function");}}

var cache = new Map();var

StronglyConnectedComponentsBuilder = function () {function StronglyConnectedComponentsBuilder() {_classCallCheck(this, StronglyConnectedComponentsBuilder);}_createClass(StronglyConnectedComponentsBuilder, null, [{ key: 'clearCache', value: function () {function clearCache()
      {
        cache = new Map();
      }return clearCache;}() }, { key: 'get', value: function () {function get(

      source, context) {
        var path = (0, _resolve2['default'])(source, context);
        if (path == null) {return null;}
        return StronglyConnectedComponentsBuilder['for']((0, _childContext2['default'])(path, context));
      }return get;}() }, { key: 'for', value: function () {function _for(

      context) {
        var settingsHash = (0, _hash.hashObject)({
          settings: context.settings,
          parserOptions: context.parserOptions,
          parserPath: context.parserPath }).
        digest('hex');
        var cacheKey = context.path + settingsHash;
        if (cache.has(cacheKey)) {
          return cache.get(cacheKey);
        }
        var scc = StronglyConnectedComponentsBuilder.calculate(context);
        var visitedFiles = Object.keys(scc);
        visitedFiles.forEach(function (filePath) {return cache.set(filePath + settingsHash, scc);});
        return scc;
      }return _for;}() }, { key: 'calculate', value: function () {function calculate(

      context) {
        var exportMap = _builder2['default']['for'](context);
        var adjacencyList = this.exportMapToAdjacencyList(exportMap);
        var calculatedScc = (0, _scc2['default'])(adjacencyList);
        return StronglyConnectedComponentsBuilder.calculatedSccToPlainObject(calculatedScc);
      }return calculate;}()

    /** @returns {Map<string, Set<string>>} for each dep, what are its direct deps */ }, { key: 'exportMapToAdjacencyList', value: function () {function exportMapToAdjacencyList(
      initialExportMap) {
        var adjacencyList = new Map();
        // BFS
        function visitNode(exportMap) {
          if (!exportMap) {
            return;
          }
          exportMap.imports.forEach(function (v, importedPath) {
            var from = exportMap.path;
            var to = importedPath;

            // Ignore type-only imports, because we care only about SCCs of value imports
            var toTraverse = [].concat(_toConsumableArray(v.declarations)).filter(function (_ref) {var isOnlyImportingTypes = _ref.isOnlyImportingTypes;return !isOnlyImportingTypes;});
            if (toTraverse.length === 0) {return;}

            if (!adjacencyList.has(from)) {
              adjacencyList.set(from, new Set());
            }

            if (adjacencyList.get(from).has(to)) {
              return; // prevent endless loop
            }
            adjacencyList.get(from).add(to);
            visitNode(v.getter());
          });
        }
        visitNode(initialExportMap);
        // Fill gaps
        adjacencyList.forEach(function (values) {
          values.forEach(function (value) {
            if (!adjacencyList.has(value)) {
              adjacencyList.set(value, new Set());
            }
          });
        });
        return adjacencyList;
      }return exportMapToAdjacencyList;}()

    /** @returns {Record<string, number>} for each key, its SCC's index */ }, { key: 'calculatedSccToPlainObject', value: function () {function calculatedSccToPlainObject(
      sccs) {
        var obj = {};
        sccs.forEach(function (scc, index) {
          scc.forEach(function (node) {
            obj[node] = index;
          });
        });
        return obj;
      }return calculatedSccToPlainObject;}() }]);return StronglyConnectedComponentsBuilder;}();exports['default'] = StronglyConnectedComponentsBuilder;
//# sourceMappingURL=data:application/json;charset=utf-8;base64,[encoded data, 7836 characters]