"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.AppleLogin = void 0;
const command_params_1 = require("../common/command-params");
const yok_1 = require("../common/yok");
class AppleLogin {
    constructor($applePortalSessionService, $errors, $injector, $logger, $prompter) {
        this.$applePortalSessionService = $applePortalSessionService;
        this.$errors = $errors;
        this.$injector = $injector;
        this.$logger = $logger;
        this.$prompter = $prompter;
        this.allowedParameters = [
            new command_params_1.StringCommandParameter(this.$injector),
            new command_params_1.StringCommandParameter(this.$injector),
        ];
    }
    execute(args) {
        return __awaiter(this, void 0, void 0, function* () {
            let username = args[0];
            if (!username) {
                username = yield this.$prompter.getString("Apple ID", {
                    allowEmpty: false,
                });
            }
            let password = args[1];
            if (!password) {
                password = yield this.$prompter.getPassword("Apple ID password");
            }
            const user = yield this.$applePortalSessionService.createUserSession({
                username,
                password,
            });
            if (!user.areCredentialsValid) {
                this.$errors.fail(`Invalid username and password combination. Used '${username}' as the username.`);
            }
            const output = Buffer.from(user.userSessionCookie).toString("base64");
            this.$logger.info(output);
        });
    }
}
exports.AppleLogin = AppleLogin;
yok_1.injector.registerCommand("apple-login", AppleLogin);
//# sourceMappingURL=apple-login.js.map