"use strict";
var path = require("path");
var shelljs = require("shelljs");
var UpdateCommand = (function () {
    function UpdateCommand($projectData, $platformService, $pluginsService, $logger, $options, $errors) {
        this.$projectData = $projectData;
        this.$platformService = $platformService;
        this.$pluginsService = $pluginsService;
        this.$logger = $logger;
        this.$options = $options;
        this.$errors = $errors;
        this.allowedParameters = [];
    }
    UpdateCommand.prototype.execute = function (args) {
        var _this = this;
        return (function () {
            var folders = ["lib", "hooks", "platforms", "node_modules"];
            var tmpDir = path.join(_this.$projectData.projectDir, ".tmp_backup");
            try {
                shelljs.rm("-fr", tmpDir);
                shelljs.mkdir(tmpDir);
                shelljs.cp(path.join(_this.$projectData.projectDir, "package.json"), tmpDir);
                for (var _i = 0, folders_1 = folders; _i < folders_1.length; _i++) {
                    var folder = folders_1[_i];
                    shelljs.cp("-rf", path.join(_this.$projectData.projectDir, folder), tmpDir);
                }
            }
            catch (error) {
                _this.$logger.error("Could not backup project folders!");
                return;
            }
            try {
                _this.executeCore(args, folders);
            }
            catch (error) {
                shelljs.cp("-f", path.join(tmpDir, "package.json"), _this.$projectData.projectDir);
                for (var _a = 0, folders_2 = folders; _a < folders_2.length; _a++) {
                    var folder = folders_2[_a];
                    shelljs.rm("-rf", path.join(_this.$projectData.projectDir, folder));
                    shelljs.cp("-fr", path.join(tmpDir, folder), _this.$projectData.projectDir);
                }
                _this.$logger.error("Could not update the project!");
            }
            finally {
                shelljs.rm("-fr", tmpDir);
            }
        }).future()();
    };
    UpdateCommand.prototype.canExecute = function (args) {
        var _this = this;
        return (function () {
            return args.length < 2 && _this.$projectData.projectDir !== "";
        }).future()();
    };
    UpdateCommand.prototype.executeCore = function (args, folders) {
        var platforms = this.$platformService.getInstalledPlatforms().wait();
        this.$platformService.removePlatforms(platforms).wait();
        this.$pluginsService.remove("tns-core-modules").wait();
        this.$pluginsService.remove("tns-core-modules-widgets").wait();
        for (var _i = 0, folders_3 = folders; _i < folders_3.length; _i++) {
            var folder = folders_3[_i];
            shelljs.rm("-fr", folder);
        }
        if (args.length === 1) {
            for (var _a = 0, platforms_1 = platforms; _a < platforms_1.length; _a++) {
                var platform = platforms_1[_a];
                this.$platformService.addPlatforms([platform + "@" + args[0]]).wait();
            }
            this.$pluginsService.add("tns-core-modules@" + args[0]).wait();
        }
        else {
            this.$platformService.addPlatforms(platforms).wait();
            this.$pluginsService.add("tns-core-modules").wait();
        }
        this.$pluginsService.ensureAllDependenciesAreInstalled().wait();
    };
    return UpdateCommand;
}());
exports.UpdateCommand = UpdateCommand;
$injector.registerCommand("update", UpdateCommand);
