"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_exec_tenant_1 = require("../../command-base-exec-tenant");
const _ = require("lodash");
const type_safety_utils_1 = require("../../uilogic/type-safety-utils");
class CspLogout extends command_base_exec_tenant_1.CommandBaseExecTenant {
    static get allPvds() {
        if (this._allPvds == undefined) {
            this._allPvds = Object.values(contracts_1.CloudProviderEnum);
            _.remove(this._allPvds, x => x == contracts_1.CloudProviderEnum.other);
            _.remove(this._allPvds, x => x == contracts_1.CloudProviderEnum.localMachine);
        }
        return this._allPvds;
    }
    captureArgs() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const { args, flags } = yield this.parse(CspLogout);
            let partial = {};
            if (args.provider) {
                const pvdAsEnum = type_safety_utils_1.TypeSafetyUtils.toEnum(contracts_1.CloudProviderEnum, args.provider);
                if (pvdAsEnum != undefined)
                    partial.provider = pvdAsEnum;
            }
            partial.cspAcct = { subscriptionId: flags.subscriptionId, tenantId: flags.tenantId };
            return [partial, false];
        });
    }
    buildCandidate_silently(partial, rootMap) {
        var _a, _b;
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            if (!partial.provider)
                throw new Error(`You must specify a cloud provider.`);
            const pvdAsEnum = type_safety_utils_1.TypeSafetyUtils.toEnum(contracts_1.CloudProviderEnum, partial.provider);
            if (pvdAsEnum == undefined)
                throw new Error("Unrecognized cloud provider.");
            let cspAcct = (_a = partial.cspAcct) !== null && _a !== void 0 ? _a : {};
            const cspTenant = yield this.cspContextualizer.ensureCspTenant(pvdAsEnum, this._codiacApi, cspAcct, false);
            if (cspTenant) {
                cspAcct.tenantId = cspTenant.cspTenantId;
                if (cspAcct.subscriptionId && !((_b = cspTenant.subscriptionIds) === null || _b === void 0 ? void 0 : _b.includes(cspAcct.subscriptionId))) {
                    delete cspAcct.subscriptionId;
                }
            }
            return { provider: pvdAsEnum, cspAcct, suppressConfirmations: true };
        });
    }
    buildCandidate_interactively(partial, takeDefaults, rootMap) {
        var _a, _b;
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            let provider;
            if (partial.provider) {
                provider = partial.provider;
            }
            else {
                provider = yield this.cspContextualizer.promptForCloudProvider(CspLogout.allPvds, undefined);
            }
            let cspAcct = (_a = partial.cspAcct) !== null && _a !== void 0 ? _a : {};
            const cspTenant = yield this.cspContextualizer.ensureCspTenant(provider, this._codiacApi, cspAcct, false);
            if (cspTenant) {
                cspAcct.tenantId = cspTenant.cspTenantId;
                if (cspAcct.subscriptionId && !((_b = cspTenant.subscriptionIds) === null || _b === void 0 ? void 0 : _b.includes(cspAcct.subscriptionId))) {
                    delete cspAcct.subscriptionId;
                }
            }
            return { provider, cspAcct, suppressConfirmations: false };
        });
    }
    buildCommandString(candidate, cmd) {
        cmd.push(candidate.provider);
        if (candidate.cspAcct.subscriptionId)
            cmd.push("-s", candidate.cspAcct.subscriptionId);
        if (candidate.cspAcct.tenantId)
            cmd.push("-t", candidate.cspAcct.tenantId);
        cmd.push("--silent");
        return cmd;
    }
    executeTask(candidate) {
        var _a, _b;
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            try {
                yield this.cspContextualizer.promptToLogout(this.ioc, candidate.provider, candidate.cspAcct, candidate.suppressConfirmations);
                this._ui.userMessage(`${this._styler.colorGood("Success!")} Signed out from ${this._styler.hilite(candidate.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 : ""}`);
            }
        });
    }
}
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.flags = {
    help: core_1.Flags.help({ char: 'h' }),
    subscriptionId: core_1.Flags.string({ char: 's', description: '(in silent mode, either this or tenantId is required if you have more than one tenant profile on a given cloud provider) The Id of the cloud provider subscription under which to logout.' }),
    tenantId: core_1.Flags.string({ char: 't', description: '(in silent mode, either this or subscriptionId is required if you have more than one tenant profile on a given cloud provider) The Id of the cloud provider tenant under which to logout.' }),
    silent: core_1.Flags.boolean({ exclusive: ["echo", "to-script"], description: 'Executes without any user interaction; fails on missing or invalid arguments.' }),
    echo: core_1.Flags.boolean({ exclusive: ["to-script", "silent"], description: 'Renders the equivalent non-interactive command for future use before executing.' }),
    "to-script": core_1.Flags.boolean({ exclusive: ["echo", "silent"], description: 'Renders the equivalent non-interactive command without executing it.' }),
};
CspLogout.args = {
    provider: core_1.Args.string({ required: false, options: CspLogout.allPvds, description: 'Code indicating the Cloud Service Provider (CSP) being logged out of.' })
};
CspLogout.examples = [
    { description: "Log out interactively.", command: "<%= config.bin %> <%= command.id %>" },
    { description: "Log out of a specific provider.", command: "<%= config.bin %> <%= command.id %> docker" },
    { description: "Non-interactive invocation.", command: "<%= config.bin %> <%= command.id %> --silent azure" },
    { description: "Renders the equivalent silent command without executing.", command: "<%= config.bin %> <%= command.id %> azure --to-script" },
];
//# sourceMappingURL=logout.js.map