"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const shelljs = require("shelljs");
const node_modules_dest_copy_1 = require("./node-modules-dest-copy");
class NodeModulesBuilder {
    constructor($fs, $injector, $options, $nodeModulesDependenciesBuilder) {
        this.$fs = $fs;
        this.$injector = $injector;
        this.$options = $options;
        this.$nodeModulesDependenciesBuilder = $nodeModulesDependenciesBuilder;
    }
    prepareNodeModules(absoluteOutputPath, platform, lastModifiedTime, projectData, projectFilesConfig) {
        return __awaiter(this, void 0, void 0, function* () {
            const productionDependencies = this.initialPrepareNodeModules(absoluteOutputPath, platform, lastModifiedTime, projectData);
            const npmPluginPrepare = this.$injector.resolve(node_modules_dest_copy_1.NpmPluginPrepare);
            yield npmPluginPrepare.preparePlugins(productionDependencies, platform, projectData, projectFilesConfig);
        });
    }
    prepareJSNodeModules(absoluteOutputPath, platform, lastModifiedTime, projectData, projectFilesConfig) {
        return __awaiter(this, void 0, void 0, function* () {
            const productionDependencies = this.initialPrepareNodeModules(absoluteOutputPath, platform, lastModifiedTime, projectData);
            const npmPluginPrepare = this.$injector.resolve(node_modules_dest_copy_1.NpmPluginPrepare);
            yield npmPluginPrepare.prepareJSPlugins(productionDependencies, platform, projectData, projectFilesConfig);
        });
    }
    cleanNodeModules(absoluteOutputPath, platform) {
        shelljs.rm("-rf", absoluteOutputPath);
    }
    initialPrepareNodeModules(absoluteOutputPath, platform, lastModifiedTime, projectData) {
        const productionDependencies = this.$nodeModulesDependenciesBuilder.getProductionDependencies(projectData.projectDir);
        if (!this.$fs.exists(absoluteOutputPath)) {
            lastModifiedTime = null;
        }
        if (!this.$options.bundle) {
            const tnsModulesCopy = this.$injector.resolve(node_modules_dest_copy_1.TnsModulesCopy, {
                outputRoot: absoluteOutputPath
            });
            tnsModulesCopy.copyModules(productionDependencies, platform);
        }
        else {
            this.cleanNodeModules(absoluteOutputPath, platform);
        }
        return productionDependencies;
    }
}
exports.NodeModulesBuilder = NodeModulesBuilder;
$injector.register("nodeModulesBuilder", NodeModulesBuilder);
