"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var path = require("path");
var chokidar_1 = require("chokidar");
var promise_utils_1 = require("./promise-utils");
var api_1 = require("./api");
var local_fs_crud_only_1 = require("./local-fs-crud-only");
var LocalFileSystem = /** @class */ (function (_super) {
    tslib_1.__extends(LocalFileSystem, _super);
    function LocalFileSystem(baseUrl, ignore, retryOptions) {
        if (retryOptions === void 0) { retryOptions = {
            interval: 100,
            retries: 3
        }; }
        var _this = _super.call(this, baseUrl, ignore) || this;
        _this.baseUrl = baseUrl;
        _this.retryOptions = retryOptions;
        return _this;
    }
    LocalFileSystem.prototype.init = function () {
        var _this = this;
        this.watcher = chokidar_1.watch([this.baseUrl], {
            //  usePolling:true,
            //  interval:100,
            ignored: function (path) { return _this.isIgnored(path); },
            //    atomic: false, //todo 50?
            cwd: this.baseUrl
        });
        this.watcher.on('error', function (err) { return console.log('Error in LocalFileSystem watch', err); });
        return new Promise(function (resolve) {
            _this.watcher.once('ready', function () {
                _this.watcher.on('addDir', function (relPath) {
                    if (relPath) {
                        _this.events.emit('directoryCreated', {
                            type: 'directoryCreated',
                            fullPath: relPath.split(path.sep).join(api_1.pathSeparator)
                        });
                    }
                });
                _this.watcher.on('add', function (relPath) {
                    promise_utils_1.retryPromise(function () { return _this.loadTextFile(relPath)
                        .then(function (content) { return _this.events.emit('fileCreated', {
                        type: 'fileCreated',
                        fullPath: relPath.split(path.sep).join(api_1.pathSeparator),
                        newContent: content
                    }); }); }, _this.retryOptions).catch(function () { return _this.events.emit('unexpectedError', { type: 'unexpectedError' }); });
                });
                _this.watcher.on('change', function (relPath) {
                    promise_utils_1.retryPromise(function () { return _this.loadTextFile(relPath)
                        .then(function (content) { return _this.events.emit('fileChanged', {
                        type: 'fileChanged',
                        fullPath: relPath.split(path.sep).join(api_1.pathSeparator),
                        newContent: content
                    }); }); }, _this.retryOptions).catch(function () { return _this.events.emit('unexpectedError', { type: 'unexpectedError' }); });
                });
                _this.watcher.on('unlinkDir', function (relPath) {
                    return _this.events.emit('directoryDeleted', {
                        type: 'directoryDeleted',
                        fullPath: relPath.split(path.sep).join(api_1.pathSeparator)
                    });
                });
                _this.watcher.on('unlink', function (relPath) {
                    return _this.events.emit('fileDeleted', {
                        type: 'fileDeleted',
                        fullPath: relPath.split(path.sep).join(api_1.pathSeparator)
                    });
                });
                resolve(_this);
            });
        });
    };
    LocalFileSystem.prototype.dispose = function () {
        this.watcher.close();
    };
    return LocalFileSystem;
}(local_fs_crud_only_1.LocalFileSystemCrudOnly));
exports.LocalFileSystem = LocalFileSystem;
//# sourceMappingURL=local-fs.js.map