"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const core_1 = require("@oclif/core");
const _ = require("lodash");
const ops_1 = require("../../ops");
const command_base_tenantless_1 = require("../../command-base-tenantless");
class IdentityDelete extends command_base_tenantless_1.CommandBaseTenantless {
    run() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.bootstrap();
            const { args, flags } = yield this.parse(IdentityDelete);
            let set = this._projUserCfg.identitySet;
            if (set) {
                let tokenName;
                if (args.name && set[args.name]) {
                    tokenName = args.name;
                }
                else {
                    let names = [];
                    for (let tokenName in set) {
                        names.push(tokenName);
                    }
                    let result = yield this._ui.promptSelect('name', 'identity to delete:', names);
                    if (!result) {
                        this._ui.userMessage("Operation cancelled: No work was done.");
                        this.exit(0);
                    }
                    else {
                        tokenName = result;
                    }
                }
                if (tokenName && set[tokenName]) {
                    let confirm = yield this._ui.promptConfirm("removeIdentity", `Remove tokenized identity: ${tokenName}? (y/n) `);
                    if (confirm) {
                        let deleted = set[tokenName];
                        _.unset(this._projUserCfg.identitySet, tokenName);
                        this.projCfgRepo.writeProjectUserConfig(this._projUserCfg); // <--- HEY !!! This shouldnt be synchronous 
                        // Perform the identity mgmt rain dance for that identity provider.
                        let adapter = ops_1.IdentityHandlerRegistry.getAdapterFor(deleted.provider, this._projectRoot, this.logger);
                        if (adapter) {
                            yield adapter.syncIdentities(_.values(this._projUserCfg.identitySet));
                        }
                        // let npmIdentities = <TokenizedIdentity[]>_.values(_.pickBy(this._userCfg.identitySet,
                        //   x => x.provider == IdentityProvider.npm));
                        // await this._ops.npmSyncIdentitiesWithNpmrc(...npmIdentities);
                        this.logger.notice(`Removed tokenized identity: ${this._styler.hilite(tokenName)}`);
                        this._ui.userMessage(`Removed tokenized identity: ${this._styler.hilite(tokenName)}`);
                    }
                    else {
                        this._ui.userMessage("Operation cancelled: No work was done.");
                        this.exit(0);
                    }
                }
                else {
                    this.logger.error(`There is no identity token [${this._styler.hilite(tokenName !== null && tokenName !== void 0 ? tokenName : "")}] stored under the current user.`);
                }
            }
            else {
                this.logger.notice(`No identity tokens are currently being held.`);
                this._ui.userMessage(`No identity tokens are currently being held.`);
            }
        });
    }
    toArray(dictionary) {
        let output = [];
        for (let key in dictionary) {
            output.push(dictionary[key]);
        }
        return output;
    }
}
exports.default = IdentityDelete;
IdentityDelete.description = "Removes an identity token from storage for the current user.";
IdentityDelete.aliases = ['identity:remove', 'token:delete', 'token:remove'];
IdentityDelete.flags = {
    help: core_1.Flags.help({ char: 'h', description: 'Displays the help document for this command.' })
    // name: flags.string({char: 'n', required: true, description: "The name of the token to delete.  Use the 'identity:list' command to view the currently stored tokens."})
};
IdentityDelete.args = {
    name: core_1.Args.string({ description: "The name of the token to delete.  Use the 'identity:list' command to view the currently stored tokens." })
};
//# sourceMappingURL=delete.js.map