"use strict";
var path = require("path");
var minimatch = require("minimatch");
var constants = require("../constants");
var Future = require("fibers/future");
var AppFilesUpdater = (function () {
    function AppFilesUpdater(appSourceDirectoryPath, appDestinationDirectoryPath, options, fs) {
        this.appSourceDirectoryPath = appSourceDirectoryPath;
        this.appDestinationDirectoryPath = appDestinationDirectoryPath;
        this.options = options;
        this.fs = fs;
    }
    AppFilesUpdater.prototype.updateApp = function (beforeCopyAction) {
        this.cleanDestinationApp();
        var sourceFiles = this.resolveAppSourceFiles();
        beforeCopyAction(sourceFiles);
        this.copyAppSourceFiles(sourceFiles);
    };
    AppFilesUpdater.prototype.cleanDestinationApp = function () {
        var _this = this;
        if (this.options.bundle) {
            return;
        }
        var destinationAppContents = this.readDestinationDir();
        destinationAppContents = destinationAppContents.filter(function (directoryName) { return directoryName !== constants.TNS_MODULES_FOLDER_NAME; });
        _(destinationAppContents).each(function (directoryItem) {
            _this.deleteDestinationItem(directoryItem);
        });
    };
    AppFilesUpdater.prototype.readDestinationDir = function () {
        if (this.fs.exists(this.appDestinationDirectoryPath).wait()) {
            return this.fs.readDirectory(this.appDestinationDirectoryPath).wait();
        }
        else {
            return [];
        }
    };
    AppFilesUpdater.prototype.deleteDestinationItem = function (directoryItem) {
        this.fs.deleteDirectory(path.join(this.appDestinationDirectoryPath, directoryItem)).wait();
    };
    AppFilesUpdater.prototype.readSourceDir = function () {
        return this.fs.enumerateFilesInDirectorySync(this.appSourceDirectoryPath, null, { includeEmptyDirectories: true });
    };
    AppFilesUpdater.prototype.resolveAppSourceFiles = function () {
        var sourceFiles = this.readSourceDir();
        if (this.options.release) {
            var testsFolderPath_1 = path.join(this.appSourceDirectoryPath, 'tests');
            sourceFiles = sourceFiles.filter(function (source) { return source.indexOf(testsFolderPath_1) === -1; });
        }
        if (this.options.release) {
            constants.LIVESYNC_EXCLUDED_FILE_PATTERNS.forEach(function (pattern) { return sourceFiles = sourceFiles.filter(function (file) { return !minimatch(file, pattern, { nocase: true }); }); });
        }
        if (this.options.bundle) {
            sourceFiles = sourceFiles.filter(function (file) { return minimatch(file, "**/App_Resources/**", { nocase: true }); });
        }
        return sourceFiles;
    };
    AppFilesUpdater.prototype.copyAppSourceFiles = function (sourceFiles) {
        var _this = this;
        var copyFileFutures = sourceFiles.map(function (source) {
            var destinationPath = path.join(_this.appDestinationDirectoryPath, path.relative(_this.appSourceDirectoryPath, source));
            if (_this.fs.getFsStats(source).wait().isDirectory()) {
                return _this.fs.createDirectory(destinationPath);
            }
            return _this.fs.copyFile(source, destinationPath);
        });
        Future.wait(copyFileFutures);
    };
    return AppFilesUpdater;
}());
exports.AppFilesUpdater = AppFilesUpdater;
