"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const core_1 = require("@oclif/core");
const stringify = require("json-stringify-safe");
const command_base_exec_tenant_1 = require("../command-base-exec-tenant");
class Whoami extends command_base_exec_tenant_1.CommandBaseExecTenant {
    captureArgs() {
        var _a;
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const { flags } = yield this.parse(Whoami);
            return [{ output: (_a = flags.output) !== null && _a !== void 0 ? _a : "text" }, false];
        });
    }
    buildCandidate_silently(partial, rootMap) {
        var _a;
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            return { output: (_a = partial.output) !== null && _a !== void 0 ? _a : "text" };
        });
    }
    buildCandidate_interactively(partial, takeDefaults, rootMap) {
        var _a;
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            return { output: (_a = partial.output) !== null && _a !== void 0 ? _a : "text" };
        });
    }
    buildCommandString(candidate, cmd) {
        if (candidate.output !== "text")
            cmd.push("-o", candidate.output);
        cmd.push("--silent");
        return cmd;
    }
    executeTask(candidate) {
        var _a;
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            if (this._userCntx) {
                if (candidate.output === "json") {
                    process.stdout.write(stringify(this._userCntx, undefined, 2));
                }
                else {
                    this._ui.userMessage(`${this._styler.hilite(this._userCntx.userName)} (tenant: ${this._styler.hilite(this._userCntx.currentTenantCode || "none")})`);
                    if (this._userCntx.roles && (this._userCntx.roles.length > 0)) {
                        this._ui.userMessage(this._styler.dim("└─ roles: ").concat((_a = this._userCntx.roles) === null || _a === void 0 ? void 0 : _a.join(", ")));
                    }
                }
            }
            else {
                if (candidate.output === "json") {
                    process.stdout.write("");
                }
                else {
                    this._ui.userMessage(`You are not currently logged in.`);
                }
            }
        });
    }
}
exports.default = Whoami;
Whoami.description = 'Renders the codiac username and its current tenant.';
Whoami.aliases = ["user:show"];
Whoami.flags = {
    help: core_1.Flags.help({ char: 'h' }),
    output: core_1.Flags.string({ char: 'o', options: ['text', 'json'], default: 'text', description: 'Format for the response data.' }),
    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.' }),
};
Whoami.args = {};
Whoami.examples = [
    { description: "Show current user.", command: "<%= config.bin %> <%= command.id %>" },
    { description: "Show current user as JSON.", command: "<%= config.bin %> <%= command.id %> -o json" },
    { description: "Non-interactive invocation.", command: "<%= config.bin %> <%= command.id %> --silent" },
    { description: "Renders the equivalent silent command without executing.", command: "<%= config.bin %> <%= command.id %> --to-script" },
];
//# sourceMappingURL=whoami.js.map