"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var fs_1 = require("fs");
var electron_1 = require("electron");
var markdownlint = require("markdownlint");
var remark = require("remark");
var remarkLintConsistent = require("remark-preset-lint-consistent");
var remarkLintMarkdownStyleGuide = require("remark-preset-lint-markdown-style-guide");
var remarkLintRecommended = require("remark-preset-lint-recommended");
var Linter = (function () {
    function Linter(sender, name, options) {
        var _this = this;
        this.sender = sender;
        this.options = options || {};
        switch (name) {
            case 'markdownlint':
                this.lint = this.markdownlint;
                this.lint_url = 'https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md';
                break;
            case 'remark-lint':
            case 'mdast-lint':
                this.lint = this.remark_lint;
                this.lint_url = 'https://github.com/wooorm/remark-lint/blob/master/doc/rules.md';
                break;
            case 'none':
                this.lint = function (_) { };
                this.lint_url = '';
                break;
            default:
                console.log("linter.js: Invalid linter name '" + name + "'");
                this.lint = function (_) { };
                this.lint_url = '';
                break;
        }
        electron_1.ipcMain.on('shiba:request-lint-rule-url', function () {
            _this.sender.send('shiba:return-lint-rule-url', _this.lint_url);
        });
    }
    Linter.prototype.sendResult = function (messages) {
        this.sender.send('shiba:notify-linter-result', messages);
    };
    Linter.prototype.markdownlint = function (filename) {
        var _this = this;
        fs_1.readFile(filename, 'utf8', function (read_err, content) {
            if (read_err) {
                console.error(read_err);
                return;
            }
            var opts = {
                strings: (_a = {},
                    _a[filename] = content,
                    _a),
                config: _this.options,
            };
            markdownlint(opts, function (err, result) {
                if (err) {
                    return;
                }
                var is_space = /\s+/;
                var messages = result.toString()
                    .split('\n')
                    .filter(function (msg) { return msg !== ''; })
                    .map(function (msg) {
                    var m = msg.match(is_space);
                    if (!m) {
                        return { header: '', body: msg };
                    }
                    return {
                        header: msg.slice(0, m.index),
                        body: msg.slice(m.index),
                    };
                });
                _this.sendResult(messages);
            });
            var _a;
        });
    };
    Linter.prototype.createRemarkProcessor = function () {
        if (this.options.plugins === undefined) {
            return remark().use(remarkLintConsistent);
        }
        var p = remark();
        if (this.options.plugins.indexOf('preset-lint-consistent') >= 0) {
            p = p.use(remarkLintConsistent);
        }
        if (this.options.plugins.indexOf('preset-lint-recommended') >= 0) {
            p = p.use(remarkLintRecommended);
        }
        if (this.options.plugins.indexOf('preset-lint-markdown-style-guide') >= 0) {
            p = p.use(remarkLintMarkdownStyleGuide);
        }
        return p;
    };
    Linter.prototype.remark_lint = function (filename) {
        var _this = this;
        fs_1.readFile(filename, 'utf8', function (read_err, content) {
            if (read_err) {
                console.error(read_err);
                return;
            }
            _this.remark = _this.remark || _this.createRemarkProcessor();
            _this.remark.process(content, function (err, file) {
                if (err) {
                    console.log('Lint failed: ', err.stack);
                    return;
                }
                _this.sendResult(file.messages.map(function (m) {
                    return {
                        header: "line:" + m.line + ", col:" + m.column,
                        body: m.reason,
                        line: m.line,
                        column: m.column,
                    };
                }));
            });
        });
    };
    return Linter;
}());
exports.default = Linter;
//# sourceMappingURL=linter.js.map