"use strict";
const path = require("path");
const minimatch = require("minimatch");
const constants = require("../constants");
const Future = require("fibers/future");
class AppFilesUpdater {
    constructor(appSourceDirectoryPath, appDestinationDirectoryPath, options, fs) {
        this.appSourceDirectoryPath = appSourceDirectoryPath;
        this.appDestinationDirectoryPath = appDestinationDirectoryPath;
        this.options = options;
        this.fs = fs;
    }
    updateApp(beforeCopyAction) {
        this.cleanDestinationApp();
        const sourceFiles = this.resolveAppSourceFiles();
        beforeCopyAction(sourceFiles);
        this.copyAppSourceFiles(sourceFiles);
    }
    cleanDestinationApp() {
        if (this.options.bundle) {
            return;
        }
        let destinationAppContents = this.readDestinationDir();
        destinationAppContents = destinationAppContents.filter((directoryName) => directoryName !== constants.TNS_MODULES_FOLDER_NAME);
        _(destinationAppContents).each((directoryItem) => {
            this.deleteDestinationItem(directoryItem);
        });
    }
    readDestinationDir() {
        if (this.fs.exists(this.appDestinationDirectoryPath).wait()) {
            return this.fs.readDirectory(this.appDestinationDirectoryPath).wait();
        }
        else {
            return [];
        }
    }
    deleteDestinationItem(directoryItem) {
        this.fs.deleteDirectory(path.join(this.appDestinationDirectoryPath, directoryItem)).wait();
    }
    readSourceDir() {
        let tnsDir = path.join(this.appSourceDirectoryPath, constants.TNS_MODULES_FOLDER_NAME);
        return this.fs.enumerateFilesInDirectorySync(this.appSourceDirectoryPath, null, { includeEmptyDirectories: true }).filter(dirName => dirName !== tnsDir);
    }
    resolveAppSourceFiles() {
        let sourceFiles = this.readSourceDir();
        if (this.options.release) {
            let testsFolderPath = path.join(this.appSourceDirectoryPath, 'tests');
            sourceFiles = sourceFiles.filter(source => source.indexOf(testsFolderPath) === -1);
        }
        if (this.options.release) {
            constants.LIVESYNC_EXCLUDED_FILE_PATTERNS.forEach(pattern => sourceFiles = sourceFiles.filter(file => !minimatch(file, pattern, { nocase: true })));
        }
        if (this.options.bundle) {
            sourceFiles = sourceFiles.filter(file => minimatch(file, "**/App_Resources/**", { nocase: true }));
        }
        return sourceFiles;
    }
    copyAppSourceFiles(sourceFiles) {
        let copyFileFutures = sourceFiles.map(source => {
            let 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);
    }
}
exports.AppFilesUpdater = AppFilesUpdater;
