"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 });
class ApplePortalApplicationService {
    constructor($applePortalSessionService, $errors, $httpClient) {
        this.$applePortalSessionService = $applePortalSessionService;
        this.$errors = $errors;
        this.$httpClient = $httpClient;
    }
    getApplications(credentials) {
        return __awaiter(this, void 0, void 0, function* () {
            let result = [];
            const user = yield this.$applePortalSessionService.createUserSession(credentials);
            for (const account of user.associatedAccounts) {
                const contentProviderId = account.contentProvider.contentProviderId;
                const dsId = user.sessionToken.dsId;
                const applications = yield this.getApplicationsByProvider(contentProviderId, dsId);
                result = result.concat(applications.summaries);
            }
            return result;
        });
    }
    getApplicationsByProvider(contentProviderId, dsId) {
        return __awaiter(this, void 0, void 0, function* () {
            const webSessionCookie = yield this.$applePortalSessionService.createWebSession(contentProviderId, dsId);
            const response = yield this.$httpClient.httpRequest({
                url: "https://appstoreconnect.apple.com/WebObjects/iTunesConnect.woa/ra/apps/manageyourapps/summary/v2",
                method: "GET",
                body: JSON.stringify({
                    contentProviderId
                }),
                headers: {
                    'Content-Type': 'application/json',
                    'Cookie': webSessionCookie
                }
            });
            return JSON.parse(response.body).data;
        });
    }
    getApplicationByBundleId(credentials, bundleId) {
        return __awaiter(this, void 0, void 0, function* () {
            const applications = yield this.getApplications(credentials);
            if (!applications || !applications.length) {
                this.$errors.failWithoutHelp(`Cannot find any registered applications for Apple ID ${credentials.username} in iTunes Connect.`);
            }
            const application = _.find(applications, app => app.bundleId === bundleId);
            if (!application) {
                this.$errors.failWithoutHelp(`Cannot find registered applications that match the specified identifier ${bundleId} in iTunes Connect.`);
            }
            return application;
        });
    }
}
exports.ApplePortalApplicationService = ApplePortalApplicationService;
$injector.register("applePortalApplicationService", ApplePortalApplicationService);
