"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;
};
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;
    }
    async checkEnvironmentRequirements(input) {
        const { platform, projectDir, runtimeVersion } = input;
        const selectedOption = null;
        if (process.env.NS_SKIP_ENV_CHECK) {
            await this.$analyticsService.trackEventActionInGoogleAnalytics({
                action: "Check Environment Requirements",
                additionalData: "Skipped: NS_SKIP_ENV_CHECK is set",
            });
            return {
                canExecute: true,
                selectedOption,
            };
        }
        const canExecute = await this.$doctorService.canExecuteLocalBuild({
            platform,
            projectDir,
            runtimeVersion,
            forceCheck: input.forceCheck,
        });
        if (!canExecute) {
            await 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}.`);
    }
}
exports.PlatformEnvironmentRequirements = PlatformEnvironmentRequirements;
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);
yok_1.injector.register("platformEnvironmentRequirements", PlatformEnvironmentRequirements);
