"use strict";
var path = require("path");
var shelljs = require("shelljs");
var constants = require("../../constants");
var minimatch = require("minimatch");
var Future = require("fibers/future");
var TnsModulesCopy = (function () {
    function TnsModulesCopy(outputRoot, $fs) {
        this.outputRoot = outputRoot;
        this.$fs = $fs;
    }
    TnsModulesCopy.prototype.copyModules = function (dependencies, platform) {
        var _this = this;
        for (var entry in dependencies) {
            var dependency = dependencies[entry];
            this.copyDependencyDir(dependency);
            if (dependency.name === constants.TNS_CORE_MODULES_NAME) {
                var tnsCoreModulesResourcePath = path.join(this.outputRoot, constants.TNS_CORE_MODULES_NAME);
                var allFiles = this.$fs.enumerateFilesInDirectorySync(tnsCoreModulesResourcePath);
                var deleteFilesFutures = allFiles.filter(function (file) { return minimatch(file, "**/*.ts", { nocase: true }); }).map(function (file) { return _this.$fs.deleteFile(file); });
                Future.wait(deleteFilesFutures);
                shelljs.rm("-rf", path.join(tnsCoreModulesResourcePath, "node_modules"));
                shelljs.cp("-Rf", path.join(tnsCoreModulesResourcePath, "*"), this.outputRoot);
                shelljs.rm("-rf", tnsCoreModulesResourcePath);
            }
        }
    };
    TnsModulesCopy.prototype.copyDependencyDir = function (dependency) {
        if (dependency.depth === 0) {
            var isScoped = dependency.name.indexOf("@") === 0;
            var targetDir = this.outputRoot;
            if (isScoped) {
                targetDir = path.join(this.outputRoot, dependency.name.substring(0, dependency.name.indexOf("/")));
            }
            shelljs.mkdir("-p", targetDir);
            shelljs.cp("-Rf", dependency.directory, targetDir);
        }
    };
    return TnsModulesCopy;
}());
exports.TnsModulesCopy = TnsModulesCopy;
var NpmPluginPrepare = (function () {
    function NpmPluginPrepare($fs, $pluginsService, $platformsData) {
        this.$fs = $fs;
        this.$pluginsService = $pluginsService;
        this.$platformsData = $platformsData;
    }
    NpmPluginPrepare.prototype.preparePlugins = function (dependencies, platform) {
        var _this = this;
        if (_.isEmpty(dependencies)) {
            return;
        }
        this.$platformsData.getPlatformData(platform).platformProjectService.beforePrepareAllPlugins(dependencies).wait();
        _.each(dependencies, function (dependency) {
            var isPlugin = !!dependency.nativescript;
            if (isPlugin) {
                console.log("preparing: " + dependency.name);
                _this.$pluginsService.prepare(dependency, platform).wait();
            }
        });
        this.$platformsData.getPlatformData(platform).platformProjectService.afterPrepareAllPlugins().wait();
    };
    return NpmPluginPrepare;
}());
exports.NpmPluginPrepare = NpmPluginPrepare;
