'use strict';

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };

var _path = require('path');

var _path2 = _interopRequireDefault(_path);

var _lodash = require('lodash.endswith');

var _lodash2 = _interopRequireDefault(_lodash);

var _has = require('has');

var _has2 = _interopRequireDefault(_has);

var _objectAssign = require('object-assign');

var _objectAssign2 = _interopRequireDefault(_objectAssign);

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

var _resolve2 = _interopRequireDefault(_resolve);

var _importType = require('../core/importType');

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

module.exports = function (context) {
  var configuration = context.options[0] || 'never';
  var defaultConfig = typeof configuration === 'string' ? configuration : null;
  var modifiers = (0, _objectAssign2.default)({}, (typeof configuration === 'undefined' ? 'undefined' : _typeof(configuration)) === 'object' ? configuration : context.options[1]);

  function isUseOfExtensionRequired(extension) {
    if (!(0, _has2.default)(modifiers, extension)) {
      modifiers[extension] = defaultConfig;
    }
    return modifiers[extension] === 'always';
  }

  function isUseOfExtensionForbidden(extension) {
    if (!(0, _has2.default)(modifiers, extension)) {
      modifiers[extension] = defaultConfig;
    }
    return modifiers[extension] === 'never';
  }

  function isResolvableWithoutExtension(file) {
    var extension = _path2.default.extname(file);
    var fileWithoutExtension = file.slice(0, -extension.length);
    var resolvedFileWithoutExtension = (0, _resolve2.default)(fileWithoutExtension, context);

    return resolvedFileWithoutExtension === (0, _resolve2.default)(file, context);
  }

  function checkFileExtension(node) {
    var source = node.source;

    var importPath = source.value;

    // don't enforce anything on builtins
    if ((0, _importType.isBuiltIn)(importPath, context.settings)) return;

    var resolvedPath = (0, _resolve2.default)(importPath, context);

    // get extension from resolved path, if possible.
    // for unresolved, use source value.
    var extension = _path2.default.extname(resolvedPath || importPath).substring(1);

    if (!extension || !(0, _lodash2.default)(importPath, extension)) {
      if (isUseOfExtensionRequired(extension) && !isUseOfExtensionForbidden(extension)) {
        context.report({
          node: source,
          message: 'Missing file extension ' + (extension ? '"' + extension + '" ' : '') + 'for "' + importPath + '"'
        });
      }
    } else if (extension) {
      if (isUseOfExtensionForbidden(extension) && isResolvableWithoutExtension(importPath)) {
        context.report({
          node: source,
          message: 'Unexpected use of file extension "' + extension + '" for "' + importPath + '"'
        });
      }
    }
  }

  return {
    ImportDeclaration: checkFileExtension
  };
};

var enumValues = { enum: ['always', 'never'] };
var patternProperties = {
  type: 'object',
  patternProperties: { '.*': enumValues }
};

module.exports.schema = {
  anyOf: [{
    type: 'array',
    items: [enumValues],
    additionalItems: false
  }, {
    type: 'array',
    items: [patternProperties],
    additionalItems: false
  }, {
    type: 'array',
    items: [enumValues, patternProperties],
    additionalItems: false
  }]
};
//# sourceMappingURL=data:application/json;base64,[encoded data, 7520 characters]