"use strict";
const command_params_1 = require("../common/command-params");
const path = require("path");
class PublishIOS {
    constructor($errors, $fs, $hostInfo, $injector, $itmsTransporterService, $logger, $options, $prompter, $stringParameterBuilder, $devicePlatformsConstants) {
        this.$errors = $errors;
        this.$fs = $fs;
        this.$hostInfo = $hostInfo;
        this.$injector = $injector;
        this.$itmsTransporterService = $itmsTransporterService;
        this.$logger = $logger;
        this.$options = $options;
        this.$prompter = $prompter;
        this.$stringParameterBuilder = $stringParameterBuilder;
        this.$devicePlatformsConstants = $devicePlatformsConstants;
        this.allowedParameters = [new command_params_1.StringCommandParameter(this.$injector), new command_params_1.StringCommandParameter(this.$injector),
            new command_params_1.StringCommandParameter(this.$injector), new command_params_1.StringCommandParameter(this.$injector)];
    }
    get $platformsData() {
        return this.$injector.resolve("platformsData");
    }
    get $platformService() {
        return this.$injector.resolve("platformService");
    }
    execute(args) {
        return (() => {
            let username = args[0], password = args[1], mobileProvisionIdentifier = args[2], codeSignIdentity = args[3], teamID = this.$options.teamId, ipaFilePath = this.$options.ipa ? path.resolve(this.$options.ipa) : null;
            if (!username) {
                username = this.$prompter.getString("Apple ID", { allowEmpty: false }).wait();
            }
            if (!password) {
                password = this.$prompter.getPassword("Apple ID password").wait();
            }
            if (!mobileProvisionIdentifier && !ipaFilePath) {
                this.$logger.warn("No mobile provision identifier set. A default mobile provision will be used. You can set one in app/App_Resources/iOS/build.xcconfig");
            }
            if (!codeSignIdentity && !ipaFilePath) {
                this.$logger.warn("No code sign identity set. A default code sign identity will be used. You can set one in app/App_Resources/iOS/build.xcconfig");
            }
            this.$options.release = true;
            if (!ipaFilePath) {
                let platform = this.$devicePlatformsConstants.iOS;
                if (mobileProvisionIdentifier || codeSignIdentity) {
                    let iOSBuildConfig = {
                        buildForDevice: true,
                        mobileProvisionIdentifier,
                        codeSignIdentity
                    };
                    this.$logger.info("Building .ipa with the selected mobile provision and/or certificate.");
                    this.$platformService.prepareAndBuild(platform, iOSBuildConfig, true).wait();
                    ipaFilePath = this.$platformService.lastOutputPath(platform, { isForDevice: iOSBuildConfig.buildForDevice });
                }
                else {
                    this.$logger.info("No .ipa, mobile provision or certificate set. Perfect! Now we'll build .xcarchive and let Xcode pick the distribution certificate and provisioning profile for you when exporting .ipa for AppStore submission.");
                    this.$platformService.preparePlatform(platform, true).wait();
                    let platformData = this.$platformsData.getPlatformData(platform);
                    let iOSProjectService = platformData.platformProjectService;
                    let archivePath = iOSProjectService.archive(platformData.projectRoot).wait();
                    this.$logger.info("Archive at: " + archivePath);
                    let exportPath = iOSProjectService.exportArchive({ archivePath, teamID }).wait();
                    this.$logger.info("Export at: " + exportPath);
                    ipaFilePath = exportPath;
                }
            }
            this.$itmsTransporterService.upload({
                username,
                password,
                ipaFilePath,
                verboseLogging: this.$logger.getLevel() === "TRACE"
            }).wait();
        }).future()();
    }
    canExecute(args) {
        return (() => {
            if (!this.$hostInfo.isDarwin) {
                this.$errors.failWithoutHelp("This command is only available on Mac OS X.");
            }
            return true;
        }).future()();
    }
}
exports.PublishIOS = PublishIOS;
$injector.registerCommand(["publish|ios", "appstore|upload"], PublishIOS);
