"use strict";

var _jshint = require("jshint");

if (typeof window !== 'undefined') {
  require('codemirror/addon/lint/lint.css');

  require('codemirror/addon/lint/lint'); // eslint-disable-next-line @typescript-eslint/no-var-requires


  var CodeMirror = require('codemirror');

  var parseErrors = function parseErrors(errors, output) {
    for (var i = 0; i < errors.length; i++) {
      var error = errors[i];

      if (error) {
        if (error.line <= 0) {
          if (window.console) {
            window.console.warn("Cannot display JSHint error (invalid line ".concat(error.line, ")"), error);
          }

          continue;
        }

        var start = error.character - 1;
        var end = start + 1;

        if (error.evidence) {
          var index = error.evidence.substring(start).search(/.\b/);

          if (index > -1) {
            end += index;
          }
        } // Convert to format expected by validation service


        var hint = {
          message: error.reason,
          severity: error.code ? error.code.startsWith('W') ? 'warning' : 'error' : 'error',
          from: CodeMirror.Pos(error.line - 1, start),
          to: CodeMirror.Pos(error.line - 1, end)
        };
        output.push(hint);
      }
    }
  };

  var validator = function validator(text, options) {
    if (!options.indent) // JSHint error.character actually is a column index, this fixes underlining on lines using tabs for indentation
      options.indent = 1; // JSHint default value is 4

    (0, _jshint.JSHINT)(text, options, options.globals);

    var _JSHINT$data = _jshint.JSHINT.data(),
        errors = _JSHINT$data.errors;

    var result = [];
    if (errors) parseErrors(errors, result);
    return result;
  };

  CodeMirror.registerHelper('lint', 'javascript', validator);
}
//# sourceMappingURL=javascript.js.map
