'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _regenerator;

function _load_regenerator() {
  return _regenerator = _interopRequireDefault(require('babel-runtime/regenerator'));
}

var getTopLevelDependencies = function () {
  var _ref = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee(consumer, dependencyStatusProps) {
    var driver, paths, consumerPath, tree, topLevelDependencies;
    return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            _context.next = 2;
            return consumer.driver.getDriver(false);

          case 2:
            driver = _context.sent;
            paths = dependencyStatusProps.mainFile;
            consumerPath = consumer.getPath();
            _context.next = 7;
            return driver.getDependencyTree({
              baseDir: consumerPath,
              consumerPath,
              filePaths: paths,
              bindingPrefix: (_constants || _load_constants()).DEFAULT_BINDINGS_PREFIX,
              resolveModulesConfig: consumer.bitJson.resolveModules
            });

          case 7:
            tree = _context.sent;
            topLevelDependencies = Object.keys(tree.tree).map(function (topLevelFile) {
              return topLevelFile;
            });
            return _context.abrupt('return', topLevelDependencies);

          case 10:
          case 'end':
            return _context.stop();
        }
      }
    }, _callee, this);
  }));

  return function getTopLevelDependencies(_x, _x2) {
    return _ref.apply(this, arguments);
  };
}();

var _;

function _load_() {
  return _ = require('..');
}

var _constants;

function _load_constants() {
  return _constants = require('../../constants');
}

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

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

function getComponentFiles(consumer) {
  var bitmap = consumer.bitMap;
  var componentsMaps = bitmap.getAllComponents();
  var componentFile = [];
  var values = Object.keys(componentsMaps).map(function (key) {
    return componentsMaps[key];
  });
  values.forEach(function (value) {
    if (value && value.files && Array.isArray(value.files)) {
      var currentFiles = [];
      value.files.forEach(function (file) {
        if (file && file.relativePath) currentFiles.push(file.relativePath);
      });
      componentFile = componentFile.concat(currentFiles);
    }
  });
  return componentFile;
}

exports.default = function () {
  var _ref2 = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee2(consumer, dependencyStatusProps) {
    var topLevelDependencies, componentFiles, missingDependencyFiles, results;
    return (_regenerator || _load_regenerator()).default.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            _context2.next = 2;
            return getTopLevelDependencies(consumer, dependencyStatusProps);

          case 2:
            topLevelDependencies = _context2.sent;
            componentFiles = getComponentFiles(consumer);
            missingDependencyFiles = [];

            topLevelDependencies.forEach(function (dependency) {
              if (!componentFiles.includes(dependency)) {
                missingDependencyFiles.push(dependency);
              }
            });

            results = { missingFiles: missingDependencyFiles };
            return _context2.abrupt('return', results);

          case 8:
          case 'end':
            return _context2.stop();
        }
      }
    }, _callee2, this);
  }));

  function getDependencyStatus(_x3, _x4) {
    return _ref2.apply(this, arguments);
  }

  return getDependencyStatus;
}();