import { Flags } from '@oclif/core';
import BaseCommand from '../../lib/baseCommand.js';
import CloudLoginManager from '../../lib/cloudLoginManager.js';
import PasswordPrompter from '../../lib/passwordPrompter.js';
import StatusManager from '../../lib/statusManager.js';
export default class CloudLogin extends BaseCommand {
    static description = 'Log into Kyndryl Cloud Uplift';
    static flags = {
        "api-token": Flags.string({ char: 'a', description: 'API token' }),
        "api-token-stdin": Flags.boolean({ char: 'i', description: 'Read API token from stdin' }),
        username: Flags.string({ char: 'u', description: 'Username' }),
    };
    async assertPrerequisites() {
        // Login has no prerequisites
    }
    async runImpl() {
        const loginManager = new CloudLoginManager();
        const flagsProvided = Object.keys(this.parsedFlags).length > 0;
        const validApiTokenFlagProvided = (this.parsedFlags['api-token'] && !this.parsedFlags['api-token-stdin']) || (!this.parsedFlags['api-token'] && this.parsedFlags['api-token-stdin']);
        const validFlagsProvided = validApiTokenFlagProvided && this.parsedFlags.username;
        if (flagsProvided && !validFlagsProvided) {
            throw new Error('--username and either --api-token or --api-token-stdin are required');
        }
        let apiToken = this.parsedFlags["api-token"];
        if (this.parsedFlags["api-token-stdin"]) {
            apiToken = await PasswordPrompter.prompt("API token:");
        }
        const { username } = this.parsedFlags;
        if (apiToken && username) {
            await loginManager.setCredentials(apiToken, username);
        }
        await loginManager.login();
        StatusManager.finishCommand(`Login to Kyndryl Cloud Uplift as ${loginManager.getUserString()}`);
    }
}
