'use strict';

var _path = require('path');

var path = _interopRequireWildcard(_path);

var _getExports = require('../core/getExports');

var _getExports2 = _interopRequireDefault(_getExports);

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

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

module.exports = function (context) {
  function checkSpecifiers(key, type, node) {
    if (node.source == null) return; // local export, ignore

    if (!node.specifiers.some(function (im) {
      return im.type === type;
    })) {
      return; // no named imports/exports
    }

    var imports = _getExports2.default.get(node.source.value, context);
    if (imports == null) return;

    if (imports.errors.length) {
      imports.reportErrors(context, node);
      return;
    }

    node.specifiers.forEach(function (im) {
      if (im.type !== type) return;

      var deepLookup = imports.hasDeep(im[key].name);

      if (!deepLookup.found) {
        if (deepLookup.path.length > 1) {
          var deepPath = deepLookup.path.map(function (i) {
            return path.relative(path.dirname(context.getFilename()), i.path);
          }).join(' -> ');

          context.report(im[key], im[key].name + ' not found via ' + deepPath);
        } else {
          context.report(im[key], im[key].name + ' not found in \'' + node.source.value + '\'');
        }
      }
    });
  }

  return {
    'ImportDeclaration': checkSpecifiers.bind(null, 'imported', 'ImportSpecifier'),

    'ExportNamedDeclaration': checkSpecifiers.bind(null, 'local', 'ExportSpecifier')
  };
};
//# sourceMappingURL=data:application/json;base64,[encoded data, 4096 characters]