"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());
    });
};
const path = require("path");
const temp = require("temp");
const constants = require("../constants");
temp.track();
class ProjectTemplatesService {
    constructor($fs, $logger, $npmInstallationManager) {
        this.$fs = $fs;
        this.$logger = $logger;
        this.$npmInstallationManager = $npmInstallationManager;
    }
    prepareTemplate(originalTemplateName, projectDir) {
        return __awaiter(this, void 0, void 0, function* () {
            let realTemplatePath;
            let data = originalTemplateName.split("@"), name = data[0], version = data[1];
            if (constants.RESERVED_TEMPLATE_NAMES[name.toLowerCase()]) {
                realTemplatePath = yield this.prepareNativeScriptTemplate(constants.RESERVED_TEMPLATE_NAMES[name.toLowerCase()], version, projectDir);
            }
            else {
                realTemplatePath = yield this.prepareNativeScriptTemplate(originalTemplateName, version, projectDir);
            }
            this.$fs.deleteDirectory(path.join(realTemplatePath, constants.NODE_MODULES_FOLDER_NAME));
            return realTemplatePath;
        });
    }
    prepareNativeScriptTemplate(templateName, version, projectDir) {
        return __awaiter(this, void 0, void 0, function* () {
            this.$logger.trace(`Using NativeScript verified template: ${templateName} with version ${version}.`);
            return this.$npmInstallationManager.install(templateName, projectDir, { version: version, dependencyType: "save" });
        });
    }
}
exports.ProjectTemplatesService = ProjectTemplatesService;
$injector.register("projectTemplatesService", ProjectTemplatesService);
