"use strict";
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const core_1 = require("@oclif/core");
const command_base_exec_1 = require("../../command-base-exec");
const _ = require("lodash");
class CabinetObliterate extends command_base_exec_1.CommandBaseExec {
    captureArgs() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const { args, flags } = yield this.parse(CabinetObliterate);
            const partial = {
                enterprise: flags.enterprise,
                cabinet: args.cabinet || flags.cabinet,
                environment: flags.environment
            };
            const interactionMode = this.getInteractionMode(flags.silent, flags.echo, flags['to-script']);
            const takeDefaults = flags['take-defaults'] || false;
            return [partial, interactionMode, takeDefaults];
        });
    }
    validatePartial(partial) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            if (!partial.enterprise)
                throw new Error('Enterprise is required');
            if (!partial.cabinet)
                throw new Error('Cabinet is required');
            if (!partial.environment)
                throw new Error('Environment is required');
        });
    }
    buildCandidate_silently(partial, rootMap) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield this.validatePartial(partial);
            // partial.thisMachine = ;  // TODO: determine whether the given cabinet is a local one.
            return partial;
        });
    }
    buildCandidate_interactively(partial, rootMap, takeDefaults) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            let cab;
            let ent = rootMap.enterprises.find(e => e.name == partial.enterprise); // <-- assertion ok - we've already ensured a valid ent
            [cab] = yield this.rootMapUtils.promptForCabinet(ent.name, rootMap, partial.cabinet, false, undefined);
            if (cab.infrx == undefined || _.isEmpty(cab.infrx)) {
                // throw new Error(`Cannot connect to cabinet ${cab.name}: it has no infrastructure record.`);
                this._ui.userMessage(`Cabinet ${cab.name} has no infrastructure record, so just the cabinet record itself will be deleted.`);
            }
            if (cab.cluster == undefined || _.isEmpty(cab.cluster)) {
                throw new Error(`Cannot connect to cabinet ${cab.name}: it has no cluster assigned.`);
            }
            partial.enterprise = cab.enterprise;
            partial.environment = cab.environment;
            partial.cabinet = cab.name;
            partial.thisMachine = cab.thisMachine;
            yield this.validatePartial(partial);
            return partial;
        });
    }
    buildCommandString(candidate, cmd) {
        cmd.push('--enterprise', candidate.enterprise);
        cmd.push('--environment', candidate.environment);
        cmd.push('--cabinet', candidate.cabinet);
        cmd.push('--silent');
        return cmd;
    }
    executeTask(candidate) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const cabinetScope = {
                enterprise: candidate.enterprise,
                environment: candidate.environment,
                cabinet: candidate.cabinet
            };
            yield this._ui.startSpinner(`Obliterating cabinet [${cabinetScope.enterprise}/${cabinetScope.environment}/${this._styler.hilite(cabinetScope.cabinet)}]...`);
            const req = { target: cabinetScope, thisMachine: candidate.thisMachine, deleteIpAddress: false };
            const result = yield this.genRelayClient.Call.postExecuteCabinetKiller(req);
            yield this._ui.stopSpinner(result.success ? "SUCCESS" : (result.error ? "ERROR" : "FAILED"));
            if (result.error) {
                this.logger.error(`Error obliterating cabinet: ${result.message || ""}; ${result.error.message}`, result.error);
                this._ui.userMessage(`Error obliterating cabinet: ${result.message || ""}; ${result.error.message}`);
            }
            else if (!result.success) {
                this.logger.notice(`Cabinet obliterate failed without error. ${result.message}`);
                this._ui.userMessage(`Cabinet obliterate failed without error. ${result.message}`);
            }
            else {
                this.logger.notice(`${this._styler.colorGood("Success!")} Cabinet [${cabinetScope.cabinet}] obliterated from enterprise [${cabinetScope.enterprise}]`);
                this._ui.userMessage(`${this._styler.colorGood("Success!")} Cabinet [${cabinetScope.cabinet}] obliterated from enterprise [${cabinetScope.enterprise}]`);
            }
        });
    }
}
exports.default = CabinetObliterate;
_a = CabinetObliterate;
CabinetObliterate.description = 'Hard-deletes an entire cabinet and everything in it; makes it as if the cabinet itself never existed.';
CabinetObliterate.aliases = ["cab:obliterate"];
CabinetObliterate.args = {
    cabinet: core_1.Args.string({ required: false, description: 'The name of the cabinet' })
};
CabinetObliterate.flags = Object.assign(Object.assign({}, _a.globalFlags), { help: core_1.Flags.help({ char: 'h' }), cabinet: core_1.Flags.string({ char: 'c', required: false, description: 'REQUIRED: The name of the cabinet' }), enterprise: core_1.Flags.string({ char: 'n', required: false, description: 'REQUIRED: The name of the enterprise containing the cabinet.' }), environment: core_1.Flags.string({ char: 'e', description: 'REQUIRED: The name of the environment this cabinet will be grouped under.' }), silent: core_1.Flags.boolean({
        char: 's',
        description: 'Runs the command in silent mode. Requires --enterprise, --environment, and --cabinet flags.',
        exclusive: ['echo', 'to-script'],
        relationships: [{ type: 'all', flags: ['enterprise', 'environment', 'cabinet'] }]
    }), echo: core_1.Flags.boolean({
        char: 'x',
        description: 'Prints the command that would be executed to the console',
        exclusive: ['silent', 'to-script']
    }), 'to-script': core_1.Flags.boolean({
        char: 't',
        description: 'Prints the command that would be executed to the console and exits',
        exclusive: ['silent', 'echo']
    }), 'take-defaults': core_1.Flags.boolean({
        char: 'd',
        description: 'Take all defaults for prompts'
    }) });
//# sourceMappingURL=obliterate.js.map