"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, $platformsData, $devicePlatformsConstants, $platformService, $bundleValidatorHelper, $logger, $workflowService) {
        super($options, $platformsData, $platformService, $projectData);
        this.$errors = $errors;
        this.$devicePlatformsConstants = $devicePlatformsConstants;
        this.$bundleValidatorHelper = $bundleValidatorHelper;
        this.$logger = $logger;
        this.$workflowService = $workflowService;
        this.$projectData.initializeProjectData();
    }
    executeCore(args) {
        return __awaiter(this, void 0, void 0, function* () {
            yield this.$workflowService.handleLegacyWorkflow({ projectDir: this.$projectData.projectDir, settings: this.$options, skipWarnings: true });
            const platform = args[0].toLowerCase();
            const appFilesUpdaterOptions = {
                bundle: !!this.$options.bundle,
                release: this.$options.release,
                useHotModuleReload: this.$options.hmr
            };
            const platformInfo = {
                platform,
                appFilesUpdaterOptions,
                platformTemplate: this.$options.platformTemplate,
                projectData: this.$projectData,
                config: this.$options,
                env: this.$options.env
            };
            yield this.$platformService.preparePlatform(platformInfo);
            const buildConfig = {
                buildForDevice: this.$options.forDevice,
                iCloudContainerEnvironment: this.$options.iCloudContainerEnvironment,
                projectDir: this.$options.path,
                clean: this.$options.clean,
                teamId: this.$options.teamId,
                device: this.$options.device,
                provision: this.$options.provision,
                release: this.$options.release,
                keyStoreAlias: this.$options.keyStoreAlias,
                keyStorePath: this.$options.keyStorePath,
                keyStoreAliasPassword: this.$options.keyStoreAliasPassword,
                keyStorePassword: this.$options.keyStorePassword,
                androidBundle: this.$options.aab
            };
            const outputPath = yield this.$platformService.buildPlatform(platform, buildConfig, this.$projectData);
            if (this.$options.copyTo) {
                this.$platformService.copyLastOutput(platform, this.$options.copyTo, buildConfig, this.$projectData);
            }
            else {
                this.$logger.info(`The build result is located at: ${outputPath}`);
            }
            return outputPath;
        });
    }
    validatePlatform(platform) {
        if (!this.$platformService.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.$platformService.validateOptions(this.$options.provision, this.$options.teamId, this.$projectData, platform);
            return result;
        });
    }
}
exports.BuildCommandBase = BuildCommandBase;
class BuildIosCommand extends BuildCommandBase {
    constructor($options, $errors, $projectData, $platformsData, $devicePlatformsConstants, $platformService, $bundleValidatorHelper, $logger, $workflowService) {
        super($options, $errors, $projectData, $platformsData, $devicePlatformsConstants, $platformService, $bundleValidatorHelper, $logger, $workflowService);
        this.$options = $options;
        this.allowedParameters = [];
    }
    execute(args) {
        return __awaiter(this, void 0, void 0, function* () {
            yield this.executeCore([this.$platformsData.availablePlatforms.iOS]);
        });
    }
    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, $platformsData, $devicePlatformsConstants, $platformService, $bundleValidatorHelper, $androidBundleValidatorHelper, $logger, $workflowService) {
        super($options, $errors, $projectData, $platformsData, $devicePlatformsConstants, $platformService, $bundleValidatorHelper, $logger, $workflowService);
        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.$platformsData.availablePlatforms.Android]);
            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);
