'use strict';

var _path = require('path');

var _path2 = _interopRequireDefault(_path);

var _has = require('has');

var _has2 = _interopRequireDefault(_has);

var _resolve = require('eslint-module-utils/resolve');

var _resolve2 = _interopRequireDefault(_resolve);

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

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

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

module.exports = {
  meta: {
    docs: {},

    schema: {
      anyOf: [{
        type: 'array',
        items: [enumValues],
        additionalItems: false
      }, {
        type: 'array',
        items: [patternProperties],
        additionalItems: false
      }, {
        type: 'array',
        items: [enumValues, patternProperties],
        additionalItems: false
      }]
    }
  },

  create: function (context) {
    const configuration = context.options[0] || 'never';
    const defaultConfig = typeof configuration === 'string' ? configuration : null;
    const modifiers = Object.assign({}, 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) {
      const extension = _path2.default.extname(file);
      const fileWithoutExtension = file.slice(0, -extension.length);
      const resolvedFileWithoutExtension = (0, _resolve2.default)(fileWithoutExtension, context);

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

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

      const importPath = source.value;

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

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

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

      if (!extension || !importPath.endsWith(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
    };
  }
};
//# sourceMappingURL=data:application/json;charset=utf-8;base64,[encoded data, 7840 characters]