"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const core_1 = require("@oclif/core");
const _ = require("lodash");
const config_ui_utils_1 = require("../../../uilogic/config-ui-utils");
const command_base_exec_tenant_1 = require("../../../command-base-exec-tenant");
class AuthRoleUnassign 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(AuthRoleUnassign);
            let partial = {};
            if (flags.role)
                partial.role = flags.role;
            if (flags.user)
                partial.user = flags.user;
            // ---------------------------------------------------------------
            // 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) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            if (partial.role == undefined || _.isEmpty(partial.role))
                throw new Error("Must specify role.");
            if (partial.user == undefined || _.isEmpty(partial.user))
                throw new Error("Must specify user.");
            let candidate = {
                role: partial.role,
                user: partial.user
            };
            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("--role", candidate.role);
        cmd.push("--user", candidate.user);
        cmd.push("--silent");
        return cmd;
    }
    executeTask(candidate) {
        var _a, _b;
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            let result = yield this._codiacApi.Auth.RoleAssignment({ roleName: candidate.role, userId: candidate.user, unassign: true });
            if (result.error)
                throw result.error;
            if (result.success) {
                this._ui.userMessage(this._styler.colorGood("Success! ").concat((_a = result.message) !== null && _a !== void 0 ? _a : "Unassigned Role."));
            }
            else {
                this._ui.userMessage((_b = result.message) !== null && _b !== void 0 ? _b : "Failed to unassign role (no details given, see log for details)");
            }
            return;
        });
    }
}
exports.default = AuthRoleUnassign;
AuthRoleUnassign.description = 'Removes a Role from a user or group within the current tenant security context.';
AuthRoleUnassign.aliases = [];
AuthRoleUnassign.examples = [
    '<%= config.bin %> <%= command.id %> -r deployer -u joe.cool@yourdomain.com'
];
AuthRoleUnassign.flags = {
    role: core_1.Flags.string({ char: 'r', required: false, description: 'Name of the Role being assigned. Required in silent mode.' }),
    user: core_1.Flags.string({ char: 'u', required: false, description: 'Username getting the role assignment. Required in silent mode.' }),
    // 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." })
};
AuthRoleUnassign.args = {};
//# sourceMappingURL=unassign.js.map