"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    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) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ListiOSApps = void 0;
const helpers_1 = require("../common/helpers");
const command_params_1 = require("../common/command-params");
const yok_1 = require("../common/yok");
class ListiOSApps {
    constructor($injector, $applePortalApplicationService, $applePortalSessionService, $logger, $projectData, $devicePlatformsConstants, $platformValidationService, $errors, $prompter, $options) {
        this.$injector = $injector;
        this.$applePortalApplicationService = $applePortalApplicationService;
        this.$applePortalSessionService = $applePortalSessionService;
        this.$logger = $logger;
        this.$projectData = $projectData;
        this.$devicePlatformsConstants = $devicePlatformsConstants;
        this.$platformValidationService = $platformValidationService;
        this.$errors = $errors;
        this.$prompter = $prompter;
        this.$options = $options;
        this.allowedParameters = [
            new command_params_1.StringCommandParameter(this.$injector),
            new command_params_1.StringCommandParameter(this.$injector),
        ];
        this.$projectData.initializeProjectData();
    }
    execute(args) {
        return __awaiter(this, void 0, void 0, function* () {
            if (!this.$platformValidationService.isPlatformSupportedForOS(this.$devicePlatformsConstants.iOS, this.$projectData)) {
                this.$errors.fail(`Applications for platform ${this.$devicePlatformsConstants.iOS} can not be built on this OS`);
            }
            let username = args[0];
            let password = args[1];
            if (!username) {
                username = yield this.$prompter.getString("Apple ID", {
                    allowEmpty: false,
                });
            }
            if (!password) {
                password = yield this.$prompter.getPassword("Apple ID password");
            }
            const user = yield this.$applePortalSessionService.createUserSession({ username, password }, {
                sessionBase64: this.$options.appleSessionBase64,
            });
            if (!user.areCredentialsValid) {
                this.$errors.fail(`Invalid username and password combination. Used '${username}' as the username.`);
            }
            const applications = yield this.$applePortalApplicationService.getApplications(user);
            if (!applications || !applications.length) {
                this.$logger.info("Seems you don't have any applications yet.");
            }
            else {
                const table = (0, helpers_1.createTable)(["Application Name", "Bundle Identifier", "In Flight Version"], applications.map((application) => {
                    const version = (application &&
                        application.versionSets &&
                        application.versionSets.length &&
                        application.versionSets[0].inFlightVersion &&
                        application.versionSets[0].inFlightVersion.version) ||
                        "";
                    return [application.name, application.bundleId, version];
                }));
                this.$logger.info(table.toString());
            }
        });
    }
}
exports.ListiOSApps = ListiOSApps;
yok_1.injector.registerCommand("appstore|*list", ListiOSApps);
