"use strict";
var minimatch = require("minimatch");
var path = require("path");
var util = require("util");
var ProjectFilesManager = (function () {
    function ProjectFilesManager($fs, $localToDevicePathDataFactory, $logger, $mobileHelper, $projectFilesProvider) {
        this.$fs = $fs;
        this.$localToDevicePathDataFactory = $localToDevicePathDataFactory;
        this.$logger = $logger;
        this.$mobileHelper = $mobileHelper;
        this.$projectFilesProvider = $projectFilesProvider;
    }
    ProjectFilesManager.prototype.getProjectFiles = function (projectFilesPath, excludedProjectDirsAndFiles, filter, opts) {
        var _this = this;
        var projectFiles = this.$fs.enumerateFilesInDirectorySync(projectFilesPath, function (filePath, stat) {
            var isFileExcluded = _this.isFileExcluded(path.relative(projectFilesPath, filePath));
            var isFileFiltered = filter ? filter(filePath, stat).wait() : false;
            return !isFileExcluded && !isFileFiltered;
        }, opts);
        this.$logger.trace("enumerateProjectFiles: %s", util.inspect(projectFiles));
        return projectFiles;
    };
    ProjectFilesManager.prototype.isFileExcluded = function (filePath, excludedProjectDirsAndFiles) {
        var isInExcludedList = !!_.find(excludedProjectDirsAndFiles, function (pattern) { return minimatch(filePath, pattern, { nocase: true }); });
        return isInExcludedList || this.$projectFilesProvider.isFileExcluded(filePath);
    };
    ProjectFilesManager.prototype.createLocalToDevicePaths = function (deviceAppData, projectFilesPath, files, excludedProjectDirsAndFiles, projectFilesConfig) {
        var _this = this;
        files = files || this.getProjectFiles(projectFilesPath, excludedProjectDirsAndFiles, null, { enumerateDirectories: true });
        var localToDevicePaths = files
            .map(function (projectFile) { return _this.$projectFilesProvider.getProjectFileInfo(projectFile, deviceAppData.platform, projectFilesConfig); })
            .filter(function (projectFileInfo) { return projectFileInfo.shouldIncludeFile; })
            .map(function (projectFileInfo) { return _this.$localToDevicePathDataFactory.create(projectFileInfo.filePath, projectFilesPath, projectFileInfo.onDeviceFileName, deviceAppData.deviceProjectRootPath); });
        return localToDevicePaths;
    };
    ProjectFilesManager.prototype.processPlatformSpecificFiles = function (directoryPath, platform, excludedDirs) {
        var _this = this;
        return (function () {
            var contents = _this.$fs.readDirectory(directoryPath).wait();
            var files = [];
            _.each(contents, function (fileName) {
                var filePath = path.join(directoryPath, fileName);
                var fsStat = _this.$fs.getFsStats(filePath).wait();
                if (fsStat.isDirectory() && !_.includes(excludedDirs, fileName)) {
                    _this.processPlatformSpecificFilesCore(platform, _this.$fs.enumerateFilesInDirectorySync(filePath)).wait();
                }
                else if (fsStat.isFile()) {
                    files.push(filePath);
                }
            });
            _this.processPlatformSpecificFilesCore(platform, files).wait();
        }).future()();
    };
    ProjectFilesManager.prototype.processPlatformSpecificFilesCore = function (platform, files) {
        var _this = this;
        return (function () {
            _.each(files, function (filePath) {
                var projectFileInfo = _this.$projectFilesProvider.getProjectFileInfo(filePath, platform);
                if (!projectFileInfo.shouldIncludeFile) {
                    _this.$fs.deleteFile(filePath).wait();
                }
                else if (projectFileInfo.onDeviceFileName) {
                    var onDeviceFilePath = path.join(path.dirname(filePath), projectFileInfo.onDeviceFileName);
                    var extension = path.extname(projectFileInfo.onDeviceFileName);
                    if ((extension === ".js" || extension === ".map") && onDeviceFilePath !== filePath) {
                        var oldName = extension === ".map" ? _this.getFileName(filePath, extension) : path.basename(filePath);
                        var newName = extension === ".map" ? _this.getFileName(projectFileInfo.onDeviceFileName, extension) : path.basename(projectFileInfo.onDeviceFileName);
                        var fileContent = _this.$fs.readText(filePath).wait();
                        fileContent = fileContent.replace(new RegExp(oldName, 'g'), newName);
                        _this.$fs.writeFile(filePath, fileContent).wait();
                    }
                    _this.$fs.rename(filePath, onDeviceFilePath).wait();
                }
            });
        }).future()();
    };
    ProjectFilesManager.prototype.getFileName = function (filePath, extension) {
        return path.basename(filePath.replace(extension === ".map" ? ".js.map" : ".js", ""));
    };
    return ProjectFilesManager;
}());
exports.ProjectFilesManager = ProjectFilesManager;
$injector.register("projectFilesManager", ProjectFilesManager);
