"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getListBuilderOptions = getListBuilderOptions;
exports.outputEntities = outputEntities;
const commandTypes_1 = require("@mablhq-internal/runtime/core/commandTypes");
const constants_1 = require("@mablhq-internal/runtime/core/constants");
const constants_2 = require("../constants");
const loggingProvider_1 = require("@mablhq-internal/runtime/providers/logging/loggingProvider");
const js_yaml_1 = require("js-yaml");
const cli_table3_1 = __importDefault(require("cli-table3"));
const moment = require("moment");
function getListBuilderOptions(pluralEntityName) {
    return (yargs) => {
        yargs
            .option(constants_1.CommandArgWorkspaceId, {
            alias: constants_2.CommandArgAliases.WorkspaceId,
            describe: `Workspace to list ${pluralEntityName} for`,
            nargs: 1,
            type: 'string',
        })
            .option(constants_2.CommandArgLimitOutput, {
            alias: constants_2.CommandArgAliases.LimitOutput,
            describe: `The number of ${pluralEntityName} to return'`,
            default: commandTypes_1.DEFAULT_LISTING_RESULT_LIMIT,
            nargs: 1,
            type: 'string',
        })
            .option(constants_2.CommandArgOutput, {
            alias: constants_2.CommandArgAliases.OutputType,
            choices: constants_2.DefaultOutputFormatChoices,
            describe: 'Specify result output format',
            nargs: 1,
        });
    };
}
function outputEntities(entities, outputMode) {
    loggingProvider_1.logger.logNewLine();
    let content;
    switch (outputMode) {
        case constants_1.OutputFormats.Json:
            content = JSON.stringify(entities, null, 2);
            break;
        case constants_1.OutputFormats.Yaml:
            content = (0, js_yaml_1.dump)(entities);
            break;
        default:
            const table = new cli_table3_1.default({
                head: ['ID', 'Name', 'Created time'],
                wordWrap: true,
            });
            entities.forEach((entity) => {
                table.push([
                    { rowSpan: 1, content: entity.id, vAlign: 'center' },
                    { rowSpan: 1, content: entity.name, vAlign: 'center' },
                    {
                        rowSpan: 1,
                        content: moment.utc(entity.created_time).format(constants_1.ListTimeFormat),
                        vAlign: 'center',
                    },
                ]);
            });
            content = table.toString();
            break;
    }
    loggingProvider_1.logger.info(content);
    return content;
}
