"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.ApplePortalApplicationService = void 0;
const yok_1 = require("../../common/yok");
const _ = require("lodash");
class ApplePortalApplicationService {
    constructor($applePortalSessionService, $errors, $httpClient) {
        this.$applePortalSessionService = $applePortalSessionService;
        this.$errors = $errors;
        this.$httpClient = $httpClient;
    }
    getApplications(user) {
        return __awaiter(this, void 0, void 0, function* () {
            let result = [];
            for (const account of user.associatedAccounts) {
                const contentProviderId = account.contentProvider.contentProviderId;
                const applications = yield this.getApplicationsByProvider(contentProviderId);
                result = result.concat(applications.summaries);
            }
            return result;
        });
    }
    getApplicationsByProvider(contentProviderId) {
        return __awaiter(this, void 0, void 0, function* () {
            const webSessionCookie = yield this.$applePortalSessionService.createWebSession(contentProviderId);
            const summaries = [];
            yield this.getApplicationsByUrl(webSessionCookie, "https://appstoreconnect.apple.com/iris/v1/apps?include=appStoreVersions,prices", summaries);
            return { summaries: summaries };
        });
    }
    getApplicationsByUrl(webSessionCookie, url, summaries) {
        return __awaiter(this, void 0, void 0, function* () {
            const response = yield this.$httpClient.httpRequest({
                url,
                method: "GET",
                headers: {
                    "Content-Type": "application/json",
                    Cookie: webSessionCookie,
                },
            });
            const result = JSON.parse(response.body);
            const data = result.data;
            for (const app of data) {
                let summary;
                summary = {
                    bundleId: app.attributes.bundleId,
                    adamId: app.id,
                    name: app.attributes.name,
                    versionSets: [],
                };
                summaries.push(summary);
            }
            if (result.links.next) {
                yield this.getApplicationsByUrl(webSessionCookie, result.links.next, summaries);
            }
        });
    }
    getApplicationByBundleId(user, bundleId) {
        return __awaiter(this, void 0, void 0, function* () {
            const applications = yield this.getApplications(user);
            if (!applications || !applications.length) {
                this.$errors.fail(`Cannot find any registered applications for Apple ID ${user.userName} in iTunes Connect.`);
            }
            const application = _.find(applications, (app) => app.bundleId === bundleId);
            if (!application) {
                this.$errors.fail(`Cannot find registered applications that match the specified identifier ${bundleId} in iTunes Connect.`);
            }
            return application;
        });
    }
}
exports.ApplePortalApplicationService = ApplePortalApplicationService;
yok_1.injector.register("applePortalApplicationService", ApplePortalApplicationService);
//# sourceMappingURL=apple-portal-application-service.js.map