"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const core_1 = require("@oclif/core");
const _ = require("lodash");
const YAML = require("yaml");
const render = require("../../../uilogic/render-methods");
const config_ui_utils_1 = require("../../../uilogic/config-ui-utils");
const command_base_exec_tenant_1 = require("../../../command-base-exec-tenant");
const uilogic_1 = require("../../../uilogic");
const type_safety_utils_1 = require("../../../uilogic/type-safety-utils");
class AuthRoleList extends command_base_exec_tenant_1.CommandBaseExecTenant {
    constructor() {
        super(...arguments);
        this.adminOnly = true;
    }
    captureArgs() {
        var _a, _b;
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const { args, flags } = yield this.parse(AuthRoleList);
            let partial = {};
            if (flags.output)
                partial.outputFormat = type_safety_utils_1.TypeSafetyUtils.toEnum(uilogic_1.OutputFormatsEnum, flags.output);
            // ---------------------------------------------------------------
            // TODO: move everything from here down into the base class
            // ---------------------------------------------------------------
            const interactionMode = (this.pipedInput) ? config_ui_utils_1.InteractionModes.silent : (_b = (_a = undefined !== null && undefined !== void 0 ? undefined : (flags.echo ? config_ui_utils_1.InteractionModes.echo : undefined)) !== null && _a !== void 0 ? _a : (flags["to-script"] ? config_ui_utils_1.InteractionModes.toscript : undefined)) !== null && _b !== void 0 ? _b : (flags.silent ? config_ui_utils_1.InteractionModes.silent : undefined);
            const takeDefaults = flags['take-defaults'];
            return [partial, interactionMode, takeDefaults];
        });
    }
    // /** Throws on invalid */
    // private validate(partial: Partial<IAuthRoleDef>): void {
    //   if (partial.name == undefined || _.isEmpty(partial.name)) throw new Error("Missing name");
    //   let rxName = /^[a-zA-Z][a-zA-Z0-9-]{2,50}$/;
    //   if (!rxName.test(partial.name)) throw new Error("Invalid name: must be 3- to 50-character string of alphanumerics and hyphens, starting with a letter.");
    // }
    buildCandidate_silently(partial) {
        var _a;
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            let candidate = {
                outputFormat: (_a = partial.outputFormat) !== null && _a !== void 0 ? _a : uilogic_1.OutputFormatsEnum.text
            };
            candidate.all = true;
            return candidate;
        });
    }
    buildCandidate_interactively(partial, takeDefaults) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            return yield this.buildCandidate_silently(partial);
        });
    }
    buildCommandString(candidate, cmd) {
        cmd.push("--output", candidate.outputFormat);
        cmd.push("--silent");
        return cmd;
    }
    executeTask(candidate) {
        var _a, _b, _c;
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            if ([uilogic_1.OutputFormatsEnum.json, uilogic_1.OutputFormatsEnum.yaml].includes(candidate.outputFormat) === false)
                this._ui.startSpinner("Retrieving");
            let crit = _.omit(candidate, "outputFormat");
            let result = yield this._codiacApi.AuthRole.tryReadMany(crit);
            let doneMsg = (result.error) ? "Error" : "Found ".concat(((_b = (_a = result.output) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0).toString());
            if ([uilogic_1.OutputFormatsEnum.json, uilogic_1.OutputFormatsEnum.yaml].includes(candidate.outputFormat) === false)
                this._ui.stopSpinner(doneMsg);
            if (result.error)
                throw result.error;
            let list = (_c = result.output) !== null && _c !== void 0 ? _c : [];
            // RENDER
            let output = "";
            switch (candidate.outputFormat) {
                case uilogic_1.OutputFormatsEnum.json:
                    output = JSON.stringify(list, undefined, 2);
                    this._ui.userMessage(output);
                    break;
                case uilogic_1.OutputFormatsEnum.yaml:
                    output = YAML.stringify(list, { indent: 2 });
                    this._ui.userMessage(output);
                    break;
                case uilogic_1.OutputFormatsEnum.text:
                default:
                    let listForText = list.map(r => r.name);
                    output = render.renderStringList(listForText);
                    this._ui.userMessage(output);
                    break;
            }
        });
    }
}
exports.default = AuthRoleList;
AuthRoleList.description = 'Retrieves the existing Roles within the current tenant security context.';
AuthRoleList.aliases = [];
AuthRoleList.examples = [
    '<%= config.bin %> <%= command.id %>',
    '<%= config.bin %> <%= command.id %> -o json',
    '<%= config.bin %> <%= command.id %> -o yaml > roles.yaml'
];
AuthRoleList.flags = {
    output: core_1.Flags.string({ char: 'o', default: uilogic_1.OutputFormatsEnum.text, description: "format in which to render the data.", options: [uilogic_1.OutputFormatsEnum.json, uilogic_1.OutputFormatsEnum.yaml, uilogic_1.OutputFormatsEnum.text] }),
    // TODO: Move these into the base class
    silent: core_1.Flags.boolean({ exclusive: ["echo", "to-script"], description: 'Non-Interactive mode; executes without any user interaction and fails on any missing or invalid arguments.' }),
    echo: core_1.Flags.boolean({ exclusive: ["to-script", "silent"], description: 'Echo interaction mode; renders the equivalent non-interactive cli command for future use before final execution.' }),
    "to-script": core_1.Flags.boolean({ exclusive: ["echo", "silent"], description: 'Toscript interaction mode; renders the equivalent non-interactive cli command WITHOUT any execution.' }),
    "take-defaults": core_1.Flags.boolean({ exclusive: ["silent"], description: "Setting this to true prevents prompting for confirmation on parameters that were passed in or were automatically set to default values (Irrelevant in --silent mode).  This behavior does NOT apply to scenarios that the command needs to override invalid user input; such cases will always prompt for confirmation.  eg: when no argument was provided for file or type, and only one exists, the system will render the autoselected file unless this flag is set." })
};
AuthRoleList.args = {};
//# sourceMappingURL=list.js.map