"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());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const helpers = require("../../common/helpers");
const path = require("path");
class PlatformValidationService {
    constructor($errors, $fs, $logger, $mobileHelper, $platformsDataService) {
        this.$errors = $errors;
        this.$fs = $fs;
        this.$logger = $logger;
        this.$mobileHelper = $mobileHelper;
        this.$platformsDataService = $platformsDataService;
    }
    validatePlatform(platform, projectData) {
        if (!platform) {
            this.$errors.fail("No platform specified.");
        }
        platform = platform.split("@")[0].toLowerCase();
        if (!this.$platformsDataService.getPlatformData(platform, projectData)) {
            const platformNames = helpers.formatListOfNames(this.$mobileHelper.platformNames);
            this.$errors.fail(`Invalid platform ${platform}. Valid platforms are ${platformNames}.`);
        }
    }
    validatePlatformInstalled(platform, projectData) {
        this.validatePlatform(platform, projectData);
        const hasPlatformDirectory = this.$fs.exists(path.join(projectData.platformsDir, platform.toLowerCase()));
        if (!hasPlatformDirectory) {
            this.$errors.fail("The platform %s is not added to this project. Please use 'tns platform add <platform>'", platform);
        }
    }
    validateOptions(provision, teamId, projectData, platform, aab) {
        return __awaiter(this, void 0, void 0, function* () {
            if (platform && !this.$mobileHelper.isAndroidPlatform(platform) && aab) {
                this.$errors.failWithoutHelp("The --aab option is supported only for the Android platform.");
            }
            if (platform) {
                platform = this.$mobileHelper.normalizePlatformName(platform);
                this.$logger.trace("Validate options for platform: " + platform);
                const platformData = this.$platformsDataService.getPlatformData(platform, projectData);
                const result = yield platformData.platformProjectService.validateOptions(projectData.projectIdentifiers[platform.toLowerCase()], provision, teamId);
                return result;
            }
            else {
                let valid = true;
                const platforms = this.$mobileHelper.platformNames.map(p => p.toLowerCase());
                for (const availablePlatform of platforms) {
                    this.$logger.trace("Validate options for platform: " + availablePlatform);
                    const platformData = this.$platformsDataService.getPlatformData(availablePlatform, projectData);
                    valid = valid && (yield platformData.platformProjectService.validateOptions(projectData.projectIdentifiers[availablePlatform.toLowerCase()], provision, teamId));
                }
                return valid;
            }
        });
    }
    isPlatformSupportedForOS(platform, projectData) {
        const targetedOS = this.$platformsDataService.getPlatformData(platform, projectData).targetedOS;
        const res = !targetedOS || targetedOS.indexOf("*") >= 0 || targetedOS.indexOf(process.platform) >= 0;
        return res;
    }
}
exports.PlatformValidationService = PlatformValidationService;
$injector.register("platformValidationService", PlatformValidationService);
