"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var electron_1 = require("electron");
var path = require("path");
var fs = require("fs");
var assertTruthy = require("assert");
var chokidar = require("chokidar");
var linter_1 = require("./linter");
var WatchDog = (function () {
    function WatchDog(config) {
        this.config = config;
    }
    WatchDog.prototype.wakeup = function (sender) {
        var _this = this;
        this.sender = sender;
        electron_1.ipcMain.on('shiba:notify-path', function (_, new_path) {
            _this.setWatchingPath(new_path);
        });
        electron_1.ipcMain.on('shiba:request-path', function () {
            _this.sender.send('shiba:return-path', _this.watching_path);
        });
        this.linter = new linter_1.default(this.sender, this.config.linter, this.config.lint_options);
    };
    WatchDog.prototype.getDocumentKindFor = function (ext) {
        for (var k in this.config.file_ext) {
            if (this.config.file_ext[k].indexOf(ext) !== -1) {
                return k;
            }
        }
        return undefined;
    };
    WatchDog.prototype.statWatchingPath = function () {
        try {
            return fs.lstatSync(this.watching_path);
        }
        catch (e) {
            return null;
        }
    };
    WatchDog.prototype.openEyes = function (pattern) {
        var _this = this;
        var followSymlinks = !!(this.config.path_watcher || {}).follow_symlinks;
        var eyes = chokidar.watch(pattern, {
            ignoreInitial: true,
            persistent: true,
            ignored: [new RegExp(this.config.ignore_path_pattern), /\.asar[\\\/]/],
            followSymlinks: followSymlinks,
        });
        eyes.on('change', function (file) {
            console.log('File changed: ' + file);
            _this.sendContentUpdated(file);
        });
        eyes.on('add', function (file) {
            console.log('File added: ' + file);
            _this.sendContentUpdated(file);
        });
        eyes.on('error', function (error) {
            console.log("Error on watching: " + error.message);
        });
        return eyes;
    };
    WatchDog.prototype.setWatchingPath = function (new_path) {
        var _this = this;
        if (new_path === this.watching_path || new_path === '') {
            return;
        }
        if (this.watching_path === undefined) {
            console.log("Start watching path: " + new_path);
        }
        else {
            console.log("Change watching path: " + this.watching_path + " -> " + new_path);
        }
        this.watching_path = new_path;
        if (this.eyes) {
            this.eyes.close();
        }
        var stats = this.statWatchingPath();
        if (stats === null) {
            return;
        }
        var path_is_file = stats.isFile();
        if (path_is_file) {
            this.sendContentUpdated(this.watching_path);
        }
        assertTruthy(path_is_file || stats.isDirectory());
        var ext_pattern = Object.
            keys(this.config.file_ext).
            map(function (k) { return _this.config.file_ext[k].join('|'); }).
            join('|');
        var watched = path_is_file ?
            this.watching_path :
            path.join(this.watching_path, '**', "*.(" + ext_pattern + ")");
        this.eyes = this.openEyes(watched);
    };
    WatchDog.prototype.sendContentUpdated = function (file) {
        var ext = path.extname(file).substr(1);
        if (ext === '') {
            return;
        }
        var kind = this.getDocumentKindFor(ext);
        switch (kind) {
            case 'markdown': {
                electron_1.app.addRecentDocument(file);
                this.sender.send('shiba:notify-content-updated', kind, file);
                this.linter.lint(file);
                break;
            }
            case 'html': {
                electron_1.app.addRecentDocument(file);
                this.sender.send('shiba:notify-content-updated', kind, file);
                break;
            }
            default: {
                break;
            }
        }
    };
    WatchDog.prototype.hasStarted = function () {
        return this.sender !== undefined;
    };
    return WatchDog;
}());
exports.default = WatchDog;
//# sourceMappingURL=watcher.js.map