"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");
class EnterpriseList extends command_base_enterprise_1.CommandBaseEnterprise {
    run() {
        var _a, _b, _c, _d, _e;
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.bootstrap();
            const { args, flags } = yield this.parse(EnterpriseList);
            try {
                this._userCntx = yield this.establishTenantContext(this._userCntx);
                // RETRIEVE 
                let outputFormat = (_a = type_safety_utils_1.TypeSafetyUtils.toEnum(render_methods_1.OutputFormatsEnum, flags.output)) !== null && _a !== void 0 ? _a : render_methods_1.OutputFormatsEnum.tree;
                if ([render_methods_1.OutputFormatsEnum.json, render_methods_1.OutputFormatsEnum.yaml].includes(outputFormat) === false)
                    this._ui.startSpinner("Retrieving");
                let enterprises = yield this._codiacApi.Enterprise.readMany({ all: true });
                let entCount = enterprises.length;
                if ([render_methods_1.OutputFormatsEnum.json, render_methods_1.OutputFormatsEnum.yaml].includes(outputFormat) === false)
                    this._ui.stopSpinner(`found ${entCount}`);
                // RENDER
                let output = "";
                switch (outputFormat) {
                    case render_methods_1.OutputFormatsEnum.json:
                        let listForJson = (_b = enterprises.map(ent => { return { name: ent.code }; })) !== null && _b !== void 0 ? _b : [];
                        listForJson = _.sortBy(listForJson, e => e.name);
                        output = JSON.stringify(listForJson, undefined, 2);
                        this._ui.userMessage(output);
                        break;
                    case render_methods_1.OutputFormatsEnum.yaml:
                        let listForYaml = (_c = enterprises.map(ent => { return { name: ent.code }; })) !== null && _c !== void 0 ? _c : [];
                        listForYaml = _.sortBy(listForYaml, c => c.name);
                        output = YAML.stringify(listForYaml, { indent: 2 });
                        this._ui.userMessage(output);
                        break;
                    case render_methods_1.OutputFormatsEnum.text:
                        let flatList = (_d = enterprises.map(ent => { return { name: ent.code }; })) !== null && _d !== void 0 ? _d : [];
                        flatList = _.sortBy(flatList, c => c.name);
                        let listForText = flatList.map(ent => ent.name);
                        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 entTree = (_e = enterprises.map(ent => { return { text: ent.code }; })) !== null && _e !== void 0 ? _e : [];
                        let out = render.renderStringTree(entTree); //, this._ui.userMessage);
                        this._ui.userMessage(out);
                        break;
                }
                // if ([OutputFormatsEnum.tree].includes(outputFormat)) {
                //   let cabTree = this.buildCabinetTree(rootMap);
                //   let out = this.renderStringTree(cabTree); //, this._ui.userMessage);
                //   this._ui.userMessage(out);
                // } else {
                //   let flatList: FlatCabinet[] = this.FlattenCabinets(rootMap);
                //   flatList = _.sortBy(flatList, c => c.enterprise, c => c.env, c => c.name)
                //   this.renderCabinets(flatList, outputFormat);
                // }
                return;
            }
            catch (err) {
                this.handleError(err);
                return;
            }
            finally {
                yield this.closeConnections();
            }
        });
    }
}
exports.default = EnterpriseList;
EnterpriseList.description = 'Shows the list of enterprises.';
EnterpriseList.aliases = ["ent:list"];
EnterpriseList.flags = {
    output: core_1.Flags.string({ char: 'o', default: render_methods_1.OutputFormatsEnum.tree, description: "format in which to render the data.", options: [render_methods_1.OutputFormatsEnum.json, render_methods_1.OutputFormatsEnum.yaml, render_methods_1.OutputFormatsEnum.tree, render_methods_1.OutputFormatsEnum.text] }),
    help: core_1.Flags.help({ char: 'h', description: 'Displays the help document for this command.' })
};
EnterpriseList.args = {};
//# sourceMappingURL=list.js.map