"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) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    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) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PlatformEnvironmentRequirements = void 0;
const helpers_1 = require("../common/helpers");
const os_1 = require("os");
const yok_1 = require("../common/yok");
class PlatformEnvironmentRequirements {
    constructor($doctorService, $errors, $analyticsService, $injector) {
        this.$doctorService = $doctorService;
        this.$errors = $errors;
        this.$analyticsService = $analyticsService;
        this.$injector = $injector;
    }
    checkEnvironmentRequirements(input) {
        return __awaiter(this, void 0, void 0, function* () {
            const { platform, projectDir, runtimeVersion } = input;
            const selectedOption = null;
            if (process.env.NS_SKIP_ENV_CHECK) {
                yield this.$analyticsService.trackEventActionInGoogleAnalytics({
                    action: "Check Environment Requirements",
                    additionalData: "Skipped: NS_SKIP_ENV_CHECK is set",
                });
                return {
                    canExecute: true,
                    selectedOption,
                };
            }
            const canExecute = yield this.$doctorService.canExecuteLocalBuild({
                platform,
                projectDir,
                runtimeVersion,
                forceCheck: input.forceCheck,
            });
            if (!canExecute) {
                yield this.$analyticsService.trackEventActionInGoogleAnalytics({
                    action: "Check Environment Requirements",
                    additionalData: "Non-interactive terminal, unable to execute local builds.",
                });
                this.fail(this.getNonInteractiveConsoleMessage(platform));
            }
            return {
                canExecute,
                selectedOption,
            };
        });
    }
    fail(message) {
        this.$errors.fail({ formatStr: message, printOnStdout: true });
    }
    getNonInteractiveConsoleMessage(platform) {
        return [
            PlatformEnvironmentRequirements.MISSING_LOCAL_SETUP_MESSAGE,
            this.getEnvVerificationMessage(platform),
        ].join(os_1.EOL);
    }
    getEnvVerificationMessage(platform) {
        const os = {
            linux: "linux",
            win32: "windows",
            darwin: "macos",
        }[process.platform];
        const anchor = platform ? `#setting-up-${os}-for-${platform.toLowerCase()}` : "";
        return (`Verify that your environment is configured according to the system requirements described at\n` +
            `https://docs.nativescript.org/setup/${os}${anchor}.`);
    }
}
PlatformEnvironmentRequirements.MISSING_LOCAL_SETUP_MESSAGE = "Your environment is not configured properly and you will not be able to execute local builds.";
__decorate([
    (0, helpers_1.hook)("checkEnvironment")
], PlatformEnvironmentRequirements.prototype, "checkEnvironmentRequirements", null);
exports.PlatformEnvironmentRequirements = PlatformEnvironmentRequirements;
yok_1.injector.register("platformEnvironmentRequirements", PlatformEnvironmentRequirements);
