"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const core_1 = require("@oclif/core");
const cli_common_1 = require("../../cli-common");
const command_base_exec_1 = require("../../command-base-exec");
class CabinetDetach extends command_base_exec_1.CommandBaseExec {
    captureArgs() {
        var _a;
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const { args, flags } = yield this.parse(CabinetDetach);
            const partial = {};
            const explicit = {};
            if (args.cabinet) {
                partial.cabinet = args.cabinet;
                explicit.cabinet = args.cabinet;
            }
            if (flags.cabinet) {
                partial.cabinet = flags.cabinet;
                explicit.cabinet = flags.cabinet;
            }
            if (flags.environment) {
                partial.environment = flags.environment;
                explicit.environment = flags.environment;
            }
            if (flags.enterprise) {
                partial.enterprise = flags.enterprise;
                explicit.enterprise = flags.enterprise;
            }
            return [partial, (_a = flags['take-defaults']) !== null && _a !== void 0 ? _a : false, explicit];
        });
    }
    buildCandidate_silently(partial, rootMap) {
        var _a;
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            if (!partial.cabinet)
                throw new Error("Cabinet name must be provided.");
            if (!partial.environment)
                throw new Error("Environment must be provided.");
            const ent = rootMap.enterprises.find(e => e.name === partial.enterprise);
            if (!ent)
                throw new Error(`Enterprise [${partial.enterprise}] not found.`);
            const env = ent.environments.find(e => e.name === partial.environment);
            if (!env)
                throw new Error(`Environment [${partial.environment}] not found.`);
            if (!((_a = env.cabinets) === null || _a === void 0 ? void 0 : _a.find(c => c.name === partial.cabinet)))
                throw new Error(`Cabinet [${partial.cabinet}] not found.`);
            return { enterprise: partial.enterprise, environment: partial.environment, cabinet: partial.cabinet };
        });
    }
    buildCandidate_interactively(partial, rootMap, takeDefaults) {
        var _a;
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const ent = rootMap.enterprises.find(e => e.name === partial.enterprise);
            const [cab] = yield this.rootMapUtils.promptForCabinet(ent.name, rootMap, partial.cabinet, false, this.rootMapUtils.clusterLabelEmbellisher());
            this._ui.userMessage(`You are about to detach cabinet [${this._styler.hilite(cab.name)}] from cluster [${this._styler.hilite((_a = cab.cluster) !== null && _a !== void 0 ? _a : "none")}]. No k8s changes will be made.`);
            const confirm = yield this._ui.promptConfirm("confirm", "Continue? (ctrl-C to cancel)", false);
            if (!confirm)
                return undefined;
            return { enterprise: cab.enterprise, environment: cab.environment, cabinet: cab.name };
        });
    }
    buildCommandString(candidate, cmd) {
        cmd.push(candidate.cabinet);
        cmd.push("--enterprise", candidate.enterprise);
        cmd.push("--environment", candidate.environment);
        cmd.push("--silent");
        return cmd;
    }
    executeTask(candidate) {
        var _a, _b, _c;
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this._ui.startSpinner(`Detaching cabinet [${candidate.enterprise}/${candidate.cabinet}]...`);
            const req = { target: candidate };
            const result = yield this.genRelayClient.Call.cabinetDetach(req);
            this._ui.stopSpinner(result.success ? "Done." : "Failed.");
            if (result.error) {
                this.logger.error(`Error detaching cabinet: ${(_a = result.message) !== null && _a !== void 0 ? _a : ""}; ${result.error.message}`, result.error);
                this._ui.userMessage(`Error detaching cabinet: ${(_b = result.message) !== null && _b !== void 0 ? _b : ""}; ${result.error.message}`);
            }
            else if (!result.success) {
                this._ui.userMessage(`Cabinet detach failed. ${(_c = result.message) !== null && _c !== void 0 ? _c : ""}`);
            }
            else {
                this._ui.userMessage(`${this._styler.colorGood("Done!")} Cabinet [${this._styler.hilite(candidate.cabinet)}] detached. Run ${this._styler.hilite("cabinet cluster attach")} then ${this._styler.hilite("cabinet restore")} to re-home it.`);
            }
        });
    }
}
exports.default = CabinetDetach;
CabinetDetach.description = 'Clears the cluster assignment from a cabinet definition, making it available to re-attach to a different cluster. Does not touch any k8s infrastructure.';
CabinetDetach.aliases = ["cab:detach"];
CabinetDetach.args = {
    cabinet: core_1.Args.string({ required: false, description: 'The name of the cabinet to detach.' })
};
CabinetDetach.examples = [
    { description: "Detach a cabinet interactively.", command: "<%= config.bin %> <%= command.id %>" },
    { description: "Detach a cabinet silently (scripted).", command: "<%= config.bin %> <%= command.id %> my-cab --enterprise main --environment dev --silent" },
    { description: "Move a cabinet to a different cluster.", command: "<%= config.bin %> <%= command.id %> my-cab --enterprise main --environment dev --silent && cod cabinet cluster attach my-cab --enterprise main --environment dev --cluster new-cluster && cod cabinet restore --cabinet my-cab --enterprise main --environment dev" },
];
CabinetDetach.flags = Object.assign(Object.assign({}, cli_common_1.commandBase.globalFlags), { help: core_1.Flags.help({ char: 'h' }), enterprise: core_1.Flags.string({ description: 'The enterprise that owns the cabinet.' }), environment: core_1.Flags.string({ char: 'e', description: 'The environment the cabinet belongs to.' }), cabinet: core_1.Flags.string({ char: 'b', description: 'The name of the cabinet to detach.' }), silent: core_1.Flags.boolean({ exclusive: ["echo", "to-script"], description: 'Non-interactive mode; fails on any missing or invalid arguments.' }), echo: core_1.Flags.boolean({ exclusive: ["to-script", "silent"], description: 'Renders the equivalent non-interactive command before executing.' }), "to-script": core_1.Flags.boolean({ exclusive: ["echo", "silent"], description: 'Renders the equivalent non-interactive command without executing it.' }), "take-defaults": core_1.Flags.boolean({ exclusive: ["silent"], description: "Accepts default values without confirmation prompts." }) });
//# sourceMappingURL=detach.js.map