"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const core_1 = require("@oclif/core");
const YAML = require("yaml");
const command_base_enterprise_1 = require("../../../command-base-enterprise");
const _ = require("lodash");
const render = require("../../../uilogic/render-methods");
const render_methods_1 = require("../../../uilogic/render-methods");
const type_safety_utils_1 = require("../../../uilogic/type-safety-utils");
const codiac_constants_1 = require("../../../codiac-constants");
class NocClusterList extends command_base_enterprise_1.CommandBaseEnterprise {
    run() {
        var _a, _b, _c, _d, _e, _f;
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.bootstrap();
            const { args, flags } = yield this.parse(NocClusterList);
            try {
                this._userCntx = yield this.establishTenantContext(this._userCntx);
                let rootMap = yield this.getRootMap();
                let currentEnterprise = (yield this.establishEnterpriseContext(rootMap)).enterprise; // We only need this bc the RootMap requires it.
                _.remove(rootMap.enterprises, e => e.name != currentEnterprise.name); // trim down to match legacy conditions
                let outputFormat = (flags.output) ? type_safety_utils_1.TypeSafetyUtils.toEnum(render_methods_1.OutputFormatsEnum, flags.output) : render_methods_1.OutputFormatsEnum.tree;
                if (outputFormat == undefined)
                    throw new Error("Unrecognized output format.");
                // RETRIEVE 
                if ([render_methods_1.OutputFormatsEnum.json, render_methods_1.OutputFormatsEnum.yaml].includes(outputFormat) === false)
                    this._ui.startSpinner("Retrieving");
                let clusterCount = (_b = (_a = rootMap.clusters) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0;
                if ([render_methods_1.OutputFormatsEnum.json, render_methods_1.OutputFormatsEnum.yaml].includes(outputFormat) === false)
                    this._ui.stopSpinner(`found ${clusterCount}`);
                // RENDER
                let output = "";
                switch (outputFormat) {
                    case render_methods_1.OutputFormatsEnum.json:
                        let listForJson = (_c = rootMap.clusters) !== null && _c !== void 0 ? _c : [];
                        listForJson = _.sortBy(listForJson, x => x.name);
                        output = JSON.stringify(listForJson, undefined, 2);
                        this._ui.userMessage(output);
                        break;
                    case render_methods_1.OutputFormatsEnum.yaml:
                        let listForYaml = (_d = rootMap.clusters) !== null && _d !== void 0 ? _d : [];
                        listForYaml = _.sortBy(listForYaml, x => x.name);
                        output = YAML.stringify(listForYaml, { indent: 2 });
                        this._ui.userMessage(output);
                        break;
                    case render_methods_1.OutputFormatsEnum.text:
                        let flatList = (_e = rootMap.clusters) !== null && _e !== void 0 ? _e : [];
                        flatList = _.sortBy(flatList, c => c.name);
                        let listForText = flatList.map(c => this.clusterToString(c));
                        ;
                        output = render.renderStringList(listForText);
                        this._ui.userMessage(output);
                        break;
                    // TODO:
                    // case OutputFormatsEnum.table:
                    // case OutputFormatsEnum.tsv:
                    //   break;
                    case render_methods_1.OutputFormatsEnum.tree:
                    default:
                        let listForTree = (_f = rootMap.clusters) !== null && _f !== void 0 ? _f : [];
                        listForTree = _.sortBy(listForTree, c => c.name);
                        let clusterTree = listForTree.map(c => {
                            return { text: this.clusterToString(c) };
                        });
                        let out = render.renderStringTree(clusterTree); //, this._ui.userMessage);
                        this._ui.userMessage(out);
                        break;
                }
            }
            catch (err) {
                this.handleError(err);
            }
            finally {
                yield this.closeConnections();
                return;
            }
        });
    }
    clusterToString(c) {
        var _a;
        const isLocal = type_safety_utils_1.TypeSafetyUtils.toBoolean(c.thisMachine);
        let localIcon = isLocal ? codiac_constants_1.CliConstants.UTF8_LOCAL.concat(" ") : undefined;
        let dim1 = isLocal ? `(${c.provider})` : `(${c.provider}, ${c.locationName})`;
        let dim2 = ` (${new Date((_a = c.modified) !== null && _a !== void 0 ? _a : "").toLocaleDateString()}, by ${c.modifiedBy})`;
        return `${this._styler.hilite(c.name)}  ${localIcon !== null && localIcon !== void 0 ? localIcon : ""}${this._styler.dim(dim1)} ${this._styler.dim(dim2)}`;
    }
}
exports.default = NocClusterList;
NocClusterList.description = 'Shows the list of clusters.';
NocClusterList.aliases = ["cluster:list"];
NocClusterList.flags = {
    output: core_1.Flags.string({ char: 'o', default: render_methods_1.OutputFormatsEnum.tree, description: "format in which to render the data.", options: Object.keys(render_methods_1.OutputFormatsEnum) }),
    help: core_1.Flags.help({ char: 'h', description: 'Displays the help document for this command.' })
};
NocClusterList.args = {};
//# sourceMappingURL=list.js.map