"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const core_1 = require("@oclif/core");
const contracts_1 = require("../../apis/codiac-relay/contracts");
const command_base_enterprise_1 = require("../../command-base-enterprise");
const cloud_provider_contextualizer_1 = require("../../uilogic/cloud-provider-contextualizer");
const type_safety_utils_1 = require("../../uilogic/type-safety-utils");
class CspLogout extends command_base_enterprise_1.CommandBaseEnterprise {
    get uiCloudProvider() {
        if (this.__uiCloudProvider_backing === undefined) {
            this.__uiCloudProvider_backing = this.ioc.get(cloud_provider_contextualizer_1.CloudProviderContextualizer);
        }
        return this.__uiCloudProvider_backing;
    }
    bootstrap() {
        super.bootstrap();
        cloud_provider_contextualizer_1.CloudProviderContextualizer.bootstrap(this.ioc);
    }
    run() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            return new Promise((resolve, reject) => tslib_1.__awaiter(this, void 0, void 0, function* () {
                var _a, _b, _c, _d;
                try {
                    this.bootstrap();
                    const { args, flags } = yield this.parse(CspLogout);
                    this._userCntx = yield this.establishTenantContext(this._userCntx);
                    let suppressConfirmations = Boolean(flags.silent === true);
                    // Select the CSP
                    let provider;
                    if (args.provider) {
                        const pvdAsEnum = type_safety_utils_1.TypeSafetyUtils.toEnum(contracts_1.CloudProviderEnum, args.provider);
                        if (pvdAsEnum == undefined)
                            throw new Error("Unrecognized cloud provider.");
                        provider = pvdAsEnum;
                    }
                    else if (suppressConfirmations) {
                        throw new Error(`You must specify a cloud provider.`);
                    }
                    else {
                        // Select interactively
                        let allPvds = this.cspUiFactory.getAllClusterProviders(this.ioc);
                        provider = yield this.uiCloudProvider.promptForCloudProvider(allPvds, undefined);
                    }
                    // interactive sign-out flow
                    try {
                        let username = yield this.uiCloudProvider.promptToLogout(this.ioc, provider, suppressConfirmations);
                        this._ui.userMessage(`${this._styler.colorGood("Success!")} Signed out from ${this._styler.hilite(provider)}.`);
                    }
                    catch (err) {
                        this._ui.userMessage(`${this._styler.colorCritical("FAILED")} ${(_a = err.name) !== null && _a !== void 0 ? _a : ""} ${(_b = err.message) !== null && _b !== void 0 ? _b : ""}`);
                    }
                    resolve();
                }
                catch (err) {
                    this.logger.error(err);
                    this._ui.userMessage(`${this._styler.colorCritical("ERROR")} ${(_c = err.name) !== null && _c !== void 0 ? _c : ""} ${(_d = err.message) !== null && _d !== void 0 ? _d : ""}`);
                    resolve();
                }
                finally {
                    yield this.closeConnections();
                }
            }));
        });
    }
}
exports.default = CspLogout;
CspLogout.description = 'Closes session with the given Cloud Service Provider (CSP), eg: aws, azure, docker.  Use this command to clear the local cache for your security account at that provider.';
CspLogout.examples = [
    'cod csp logout',
    'cod csp logout docker',
    'cod csp logout --silent azure',
];
// static dependsOn = ["provider"];  // this isnt the whole of the requirements
CspLogout.flags = {
    silent: core_1.Flags.boolean({ char: 'y', required: false, description: `(Optional: defaults to false) Suppresses any user-interaction.` })
};
CspLogout.args = {
    provider: core_1.Args.string({ required: false, options: Object.keys(contracts_1.CloudProviderEnum), char: 'p', description: 'Code indicating the Cloud Service Provider (CSP) being authenticated with.' })
};
//# sourceMappingURL=logout.js.map