"use strict";
var command_params_1 = require("../common/command-params");
var path = require("path");
var PublishIOS = (function () {
    function PublishIOS($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)];
    }
    Object.defineProperty(PublishIOS.prototype, "$platformsData", {
        get: function () {
            return this.$injector.resolve("platformsData");
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(PublishIOS.prototype, "$platformService", {
        get: function () {
            return this.$injector.resolve("platformService");
        },
        enumerable: true,
        configurable: true
    });
    PublishIOS.prototype.execute = function (args) {
        var _this = this;
        return (function () {
            var 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");
            }
            if (!ipaFilePath) {
                var platform = _this.$devicePlatformsConstants.iOS;
                if (mobileProvisionIdentifier || codeSignIdentity) {
                    var iOSBuildConfig = {
                        buildForDevice: true,
                        mobileProvisionIdentifier: mobileProvisionIdentifier,
                        codeSignIdentity: 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();
                    var platformData = _this.$platformsData.getPlatformData(platform);
                    var iOSProjectService = platformData.platformProjectService;
                    var archivePath = iOSProjectService.archive(platformData.projectRoot).wait();
                    _this.$logger.info("Archive at: " + archivePath);
                    var exportPath = iOSProjectService.exportArchive({ archivePath: archivePath, teamID: teamID }).wait();
                    _this.$logger.info("Export at: " + exportPath);
                    ipaFilePath = exportPath;
                }
            }
            _this.$options.release = true;
            _this.$itmsTransporterService.upload({
                username: username,
                password: password,
                ipaFilePath: ipaFilePath,
                verboseLogging: _this.$logger.getLevel() === "TRACE"
            }).wait();
        }).future()();
    };
    PublishIOS.prototype.canExecute = function (args) {
        var _this = this;
        return (function () {
            if (!_this.$hostInfo.isDarwin) {
                _this.$errors.failWithoutHelp("This command is only available on Mac OS X.");
            }
            return true;
        }).future()();
    };
    return PublishIOS;
}());
exports.PublishIOS = PublishIOS;
$injector.registerCommand(["publish|ios", "appstore|upload"], PublishIOS);
