'use strict';

var _resolve = require('../core/resolve');

var _resolve2 = _interopRequireDefault(_resolve);

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

module.exports = function (context) {

  var ignoreRegExps = [];
  if (context.options[0] != null && context.options[0].ignore != null) {
    ignoreRegExps = context.options[0].ignore.map(function (p) {
      return new RegExp(p);
    });
  }

  function checkSourceValue(source) {
    if (source == null) return;

    if (ignoreRegExps.some(function (re) {
      return re.test(source.value);
    })) return;

    if ((0, _resolve2.default)(source.value, context) === undefined) {
      context.report(source, 'Unable to resolve path to module \'' + source.value + '\'.');
    }
  }

  // for import-y declarations
  function checkSource(node) {
    checkSourceValue(node.source);
  }

  // for CommonJS `require` calls
  // adapted from @mctep: http://git.io/v4rAu
  function checkCommon(call) {
    if (call.callee.type !== 'Identifier') return;
    if (call.callee.name !== 'require') return;
    if (call.arguments.length !== 1) return;

    var modulePath = call.arguments[0];
    if (modulePath.type !== 'Literal') return;
    if (typeof modulePath.value !== 'string') return;

    checkSourceValue(modulePath);
  }

  function checkAMD(call) {
    if (call.callee.type !== 'Identifier') return;
    if (call.callee.name !== 'require' && call.callee.name !== 'define') return;
    if (call.arguments.length !== 2) return;

    var modules = call.arguments[0];
    if (modules.type !== 'ArrayExpression') return;

    modules.elements.forEach(function (element) {
      if (element.type === 'Literal' && typeof element.value === 'string') {

        // magic modules: http://git.io/vByan
        if (element.value !== 'require' && element.value !== 'exports') {
          checkSourceValue(element);
        }
      }
    });
  }

  var visitors = {
    'ImportDeclaration': checkSource,
    'ExportNamedDeclaration': checkSource,
    'ExportAllDeclaration': checkSource
  };

  if (context.options[0] != null) {
    (function () {
      var _context$options$ = context.options[0];
      var commonjs = _context$options$.commonjs;
      var amd = _context$options$.amd;


      if (commonjs || amd) {
        visitors['CallExpression'] = function (call) {
          if (commonjs) checkCommon(call);
          if (amd) checkAMD(call);
        };
      }
    })();
  }

  return visitors;
}; /**
    * @fileOverview Ensures that an imported path exists, given resolution rules.
    * @author Ben Mosher
    */

module.exports.schema = [{
  'type': 'object',
  'properties': {
    'commonjs': { 'type': 'boolean' },
    'amd': { 'type': 'boolean' },
    'ignore': {
      'type': 'array',
      'minItems': 1,
      'items': { 'type': 'string' },
      'uniqueItems': true
    }
  },
  'additionalProperties': false
}];
//# sourceMappingURL=data:application/json;base64,[encoded data, 7332 characters]