"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 constants_1 = require("../constants");
const command_base_1 = require("./command-base");
class BuildCommandBase extends command_base_1.ValidatePlatformCommandBase {
    constructor($options, $errors, $projectData, $platformsDataService, $devicePlatformsConstants, $buildController, $platformValidationService, $bundleValidatorHelper, $buildDataService, $logger) {
        super($options, $platformsDataService, $platformValidationService, $projectData);
        this.$errors = $errors;
        this.$devicePlatformsConstants = $devicePlatformsConstants;
        this.$buildController = $buildController;
        this.$bundleValidatorHelper = $bundleValidatorHelper;
        this.$buildDataService = $buildDataService;
        this.$logger = $logger;
        this.dashedOptions = {
            watch: { type: "boolean", default: false, hasSensitiveValue: false },
        };
        this.$projectData.initializeProjectData();
    }
    executeCore(args) {
        return __awaiter(this, void 0, void 0, function* () {
            const platform = args[0].toLowerCase();
            const buildData = this.$buildDataService.getBuildData(this.$projectData.projectDir, platform, this.$options);
            const outputPath = yield this.$buildController.prepareAndBuild(buildData);
            return outputPath;
        });
    }
    validatePlatform(platform) {
        if (!this.$platformValidationService.isPlatformSupportedForOS(platform, this.$projectData)) {
            this.$errors.fail(`Applications for platform ${platform} can not be built on this OS`);
        }
        this.$bundleValidatorHelper.validate(this.$projectData);
    }
    validateArgs(args, platform) {
        return __awaiter(this, void 0, void 0, function* () {
            const canExecute = yield this.validateArgsCore(args, platform);
            return {
                canExecute,
                suppressCommandHelp: false
            };
        });
    }
    validateArgsCore(args, platform) {
        return __awaiter(this, void 0, void 0, function* () {
            if (args.length !== 0) {
                return false;
            }
            const result = yield this.$platformValidationService.validateOptions(this.$options.provision, this.$options.teamId, this.$projectData, platform);
            return result;
        });
    }
}
exports.BuildCommandBase = BuildCommandBase;
class BuildIosCommand extends BuildCommandBase {
    constructor($options, $errors, $projectData, $platformsDataService, $devicePlatformsConstants, $buildController, $platformValidationService, $bundleValidatorHelper, $logger, $buildDataService) {
        super($options, $errors, $projectData, $platformsDataService, $devicePlatformsConstants, $buildController, $platformValidationService, $bundleValidatorHelper, $buildDataService, $logger);
        this.$options = $options;
        this.allowedParameters = [];
    }
    execute(args) {
        return __awaiter(this, void 0, void 0, function* () {
            yield this.executeCore([this.$devicePlatformsConstants.iOS.toLowerCase()]);
        });
    }
    canExecute(args) {
        const _super = Object.create(null, {
            validatePlatform: { get: () => super.validatePlatform },
            canExecuteCommandBase: { get: () => super.canExecuteCommandBase },
            validateArgs: { get: () => super.validateArgs }
        });
        return __awaiter(this, void 0, void 0, function* () {
            const platform = this.$devicePlatformsConstants.iOS;
            _super.validatePlatform.call(this, platform);
            let result = yield _super.canExecuteCommandBase.call(this, platform, { notConfiguredEnvOptions: { hideSyncToPreviewAppOption: true } });
            if (result.canExecute) {
                result = yield _super.validateArgs.call(this, args, platform);
            }
            return result;
        });
    }
}
exports.BuildIosCommand = BuildIosCommand;
$injector.registerCommand("build|ios", BuildIosCommand);
class BuildAndroidCommand extends BuildCommandBase {
    constructor($options, $errors, $projectData, platformsDataService, $devicePlatformsConstants, $buildController, $platformValidationService, $bundleValidatorHelper, $androidBundleValidatorHelper, $buildDataService, $logger) {
        super($options, $errors, $projectData, platformsDataService, $devicePlatformsConstants, $buildController, $platformValidationService, $bundleValidatorHelper, $buildDataService, $logger);
        this.$options = $options;
        this.$errors = $errors;
        this.$androidBundleValidatorHelper = $androidBundleValidatorHelper;
        this.$logger = $logger;
        this.allowedParameters = [];
    }
    execute(args) {
        return __awaiter(this, void 0, void 0, function* () {
            yield this.executeCore([this.$devicePlatformsConstants.Android.toLowerCase()]);
            if (this.$options.aab) {
                this.$logger.info(constants_1.AndroidAppBundleMessages.ANDROID_APP_BUNDLE_DOCS_MESSAGE);
                if (this.$options.release) {
                    this.$logger.info(constants_1.AndroidAppBundleMessages.ANDROID_APP_BUNDLE_PUBLISH_DOCS_MESSAGE);
                }
            }
        });
    }
    canExecute(args) {
        const _super = Object.create(null, {
            validatePlatform: { get: () => super.validatePlatform },
            canExecuteCommandBase: { get: () => super.canExecuteCommandBase },
            validateArgs: { get: () => super.validateArgs }
        });
        return __awaiter(this, void 0, void 0, function* () {
            const platform = this.$devicePlatformsConstants.Android;
            _super.validatePlatform.call(this, platform);
            this.$androidBundleValidatorHelper.validateRuntimeVersion(this.$projectData);
            let result = yield _super.canExecuteCommandBase.call(this, platform, { notConfiguredEnvOptions: { hideSyncToPreviewAppOption: true } });
            if (result.canExecute) {
                if (this.$options.release && (!this.$options.keyStorePath || !this.$options.keyStorePassword || !this.$options.keyStoreAlias || !this.$options.keyStoreAliasPassword)) {
                    this.$errors.fail(constants_1.ANDROID_RELEASE_BUILD_ERROR_MESSAGE);
                }
                result = yield _super.validateArgs.call(this, args, platform);
            }
            return result;
        });
    }
}
exports.BuildAndroidCommand = BuildAndroidCommand;
$injector.registerCommand("build|android", BuildAndroidCommand);
