"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 CabinetForget 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(CabinetForget);
            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) {
        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 forget cabinet [${this._styler.hilite(cab.name)}]. Its k8s namespace (if any) will be left intact.`);
            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(`Forgetting cabinet [${candidate.enterprise}/${candidate.cabinet}]...`);
            const req = { target: candidate };
            const result = yield this.genRelayClient.Call.cabinetForget(req);
            this._ui.stopSpinner(result.success ? "Done." : "Failed.");
            if (result.error) {
                this.logger.error(`Error forgetting cabinet: ${(_a = result.message) !== null && _a !== void 0 ? _a : ""}; ${result.error.message}`, result.error);
                this._ui.userMessage(`Error forgetting cabinet: ${(_b = result.message) !== null && _b !== void 0 ? _b : ""}; ${result.error.message}`);
            }
            else if (!result.success) {
                this._ui.userMessage(`Cabinet forget failed. ${(_c = result.message) !== null && _c !== void 0 ? _c : ""}`);
            }
            else {
                this._ui.userMessage(`${this._styler.colorGood("Done!")} Cabinet [${this._styler.hilite(candidate.cabinet)}] forgotten. Any k8s namespace was left untouched.`);
            }
        });
    }
}
exports.default = CabinetForget;
CabinetForget.description = 'Removes the cabinet metadata record from Codiac without touching any k8s infrastructure. The physical namespace (if any) remains on the cluster, unaware and unaffected.';
CabinetForget.aliases = ["cab:forget"];
CabinetForget.args = {
    cabinet: core_1.Args.string({ required: false, description: 'The name of the cabinet to forget.' })
};
CabinetForget.examples = [
    { description: "Forget a cabinet interactively.", command: "<%= config.bin %> <%= command.id %>" },
    { description: "Forget a cabinet silently (scripted).", command: "<%= config.bin %> <%= command.id %> my-cab --enterprise main --environment dev --silent" },
    { description: "Renders the equivalent silent command without executing.", command: "<%= config.bin %> <%= command.id %> --to-script" },
];
CabinetForget.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 forget.' }), 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=forget.js.map