'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 && obj !== Symbol.prototype ? "symbol" : typeof obj; };

var _eslintDefaultConfig = require('./eslint-default-config.js');

var _eslintDefaultConfig2 = _interopRequireDefault(_eslintDefaultConfig);

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

/* eslint-disable */
// CodeMirror Lint addon to use ESLint, copyright (c) by Angelo ZERR and others
// Distributed under an MIT license: http://codemirror.net/LICENSE

// TODO create npm module 
var eslint = require('./eslint.js'); // Depends on eslint.js from http://eslint.org/js/app/eslint.js


(function (mod) {
  if ((typeof exports === 'undefined' ? 'undefined' : _typeof(exports)) == "object" && (typeof module === 'undefined' ? 'undefined' : _typeof(module)) == "object") // CommonJS
    mod(require("codemirror"));else if (typeof define == "function" && define.amd) // AMD
    define(["codemirror"], mod);else // Plain browser env
    mod(CodeMirror);
})(function (CodeMirror) {
  var config = Object.assign({}, _eslintDefaultConfig2.default);
  function validator(text, options) {
    var result = [];
    var errors = eslint.verify(text, config);
    for (var i = 0; i < errors.length; i++) {
      var error = errors[i];
      result.push({
        message: error.message,
        severity: getSeverity(error),
        from: getPos(error, true),
        to: getPos(error, false)
      });
    }
    return result;
  }

  CodeMirror.registerHelper("lint", "javascript", validator);

  function getPos(error, from) {
    var line = error.line - 1,
        ch = from ? error.column : error.column + 1;
    if (error.node && error.node.loc) {
      line = from ? error.node.loc.start.line - 1 : error.node.loc.end.line - 1;
      ch = from ? error.node.loc.start.column : error.node.loc.end.column;
    }
    return CodeMirror.Pos(line, ch);
  }

  function getSeverity(error) {
    switch (error.severity) {
      case 1:
        return "warning";
      case 2:
        return "error";
      default:
        return "error";
    }
  }
});