"use strict";
var fiberBootstrap = require("../../fiber-bootstrap");
exports.SYNC_WAIT_THRESHOLD = 250;
var SyncBatch = (function () {
    function SyncBatch($logger, $projectFilesManager, done) {
        this.$logger = $logger;
        this.$projectFilesManager = $projectFilesManager;
        this.done = done;
        this.timer = null;
        this.syncQueue = [];
        this.syncInProgress = false;
    }
    Object.defineProperty(SyncBatch.prototype, "filesToSync", {
        get: function () {
            var _this = this;
            var filteredFiles = this.syncQueue.filter(function (syncFilePath) { return !_this.$projectFilesManager.isFileExcluded(syncFilePath); });
            return filteredFiles;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(SyncBatch.prototype, "syncPending", {
        get: function () {
            return this.syncQueue.length > 0;
        },
        enumerable: true,
        configurable: true
    });
    SyncBatch.prototype.syncFiles = function (syncAction) {
        var _this = this;
        return (function () {
            if (_this.filesToSync.length > 0) {
                syncAction(_this.filesToSync).wait();
                _this.reset();
            }
        }).future()();
    };
    SyncBatch.prototype.addFile = function (filePath) {
        var _this = this;
        if (this.timer) {
            clearTimeout(this.timer);
            this.timer = null;
        }
        this.syncQueue.push(filePath);
        if (!this.syncInProgress) {
            this.timer = setTimeout(function () {
                if (_this.syncQueue.length > 0) {
                    _this.$logger.trace("Syncing %s", _this.syncQueue.join(", "));
                    fiberBootstrap.run(function () {
                        try {
                            _this.syncInProgress = true;
                            _this.done().wait();
                        }
                        finally {
                            _this.syncInProgress = false;
                        }
                    });
                }
                _this.timer = null;
            }, exports.SYNC_WAIT_THRESHOLD);
        }
    };
    SyncBatch.prototype.reset = function () {
        this.syncQueue = [];
    };
    return SyncBatch;
})();
exports.SyncBatch = SyncBatch;
