"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
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 path = require("path");
const temp = require("temp");
const constants_1 = require("../../constants");
const decorators_1 = require("../../common/decorators");
class AddPlatformService {
    constructor($fs, $pacoteService, $projectChangesService, $projectDataService, $terminalSpinnerService) {
        this.$fs = $fs;
        this.$pacoteService = $pacoteService;
        this.$projectChangesService = $projectChangesService;
        this.$projectDataService = $projectDataService;
        this.$terminalSpinnerService = $terminalSpinnerService;
    }
    addPlatformSafe(projectData, platformData, packageToInstall, nativePrepare) {
        return __awaiter(this, void 0, void 0, function* () {
            const spinner = this.$terminalSpinnerService.createSpinner();
            try {
                spinner.start();
                const frameworkDirPath = yield this.extractPackage(packageToInstall);
                const frameworkPackageJsonContent = this.$fs.readJson(path.join(frameworkDirPath, "..", "package.json"));
                const frameworkVersion = frameworkPackageJsonContent.version;
                yield this.setPlatformVersion(platformData, projectData, frameworkVersion);
                if (!nativePrepare || !nativePrepare.skipNativePrepare) {
                    yield this.addNativePlatform(platformData, projectData, frameworkDirPath, frameworkVersion);
                }
                return frameworkVersion;
            }
            catch (err) {
                const platformPath = path.join(projectData.platformsDir, platformData.platformNameLowerCase);
                this.$fs.deleteDirectory(platformPath);
                throw err;
            }
            finally {
                spinner.stop();
            }
        });
    }
    setPlatformVersion(platformData, projectData, frameworkVersion) {
        return __awaiter(this, void 0, void 0, function* () {
            const frameworkPackageNameData = { version: frameworkVersion };
            this.$projectDataService.setNSValue(projectData.projectDir, platformData.frameworkPackageName, frameworkPackageNameData);
        });
    }
    extractPackage(pkg) {
        return __awaiter(this, void 0, void 0, function* () {
            temp.track();
            const downloadedPackagePath = temp.mkdirSync("runtimeDir");
            yield this.$pacoteService.extractPackage(pkg, downloadedPackagePath);
            const frameworkDir = path.join(downloadedPackagePath, constants_1.PROJECT_FRAMEWORK_FOLDER_NAME);
            return path.resolve(frameworkDir);
        });
    }
    addNativePlatform(platformData, projectData, frameworkDirPath, frameworkVersion) {
        return __awaiter(this, void 0, void 0, function* () {
            const platformDir = path.join(projectData.platformsDir, platformData.normalizedPlatformName.toLowerCase());
            this.$fs.deleteDirectory(platformDir);
            yield platformData.platformProjectService.createProject(path.resolve(frameworkDirPath), frameworkVersion, projectData);
            platformData.platformProjectService.ensureConfigurationFileInAppResources(projectData);
            yield platformData.platformProjectService.interpolateData(projectData);
            platformData.platformProjectService.afterCreateProject(platformData.projectRoot, projectData);
            yield this.$projectChangesService.setNativePlatformStatus(platformData, projectData, { nativePlatformStatus: "2" });
        });
    }
}
__decorate([
    decorators_1.performanceLog()
], AddPlatformService.prototype, "addNativePlatform", null);
exports.AddPlatformService = AddPlatformService;
$injector.register("addPlatformService", AddPlatformService);
