"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const cli_table3_1 = __importDefault(require("cli-table3"));
const moment = require("moment");
const mablApiClientFactory_1 = require("@mablhq-internal/runtime/api/mablApiClientFactory");
const util_1 = require("../../commandUtil/util");
const cliUtil_1 = require("@mablhq-internal/runtime/core/cliUtil");
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");
exports.command = 'list';
exports.describe = 'List tests';
exports.builder = (yargs) => {
    yargs
        .option(constants_1.CommandArgWorkspaceId, {
        alias: constants_2.CommandArgAliases.WorkspaceId,
        describe: 'Workspace to list tests for',
        nargs: 1,
        type: 'string',
    })
        .option(constants_1.CommandArgLabels, {
        describe: 'Space delimited test labels to list tests for',
        type: 'array',
    })
        .option(constants_2.CommandArgOutput, {
        alias: constants_2.CommandArgAliases.OutputType,
        default: constants_1.OutputFormats.Table,
        describe: 'Specify result output format',
        nargs: 1,
        choices: [...constants_2.DefaultOutputFormatChoices, constants_1.OutputFormats.Table],
    })
        .option(constants_2.CommandArgLimitOutput, {
        alias: constants_2.CommandArgAliases.LimitOutput,
        describe: 'The number of tests to return',
        default: 10,
        nargs: 1,
        type: 'string',
    });
};
exports.handler = (0, cliUtil_1.failWrapper)(listTests);
async function listTests(parsed) {
    const limit = parsed.limit;
    const workspaceId = await (0, util_1.getWorkspaceId)(parsed);
    const labels = parsed[constants_1.CommandArgLabels];
    const output = parsed[constants_2.CommandArgOutput];
    const apiClient = await mablApiClientFactory_1.MablApiClientFactory.createApiClient();
    const journeys = await apiClient.getJourneys({
        organization_id: workspaceId,
        limit,
        include_labels: labels,
    });
    const outputJourneys = journeys.map((journey) => ({
        created_time: journey.created_time,
        id: journey.invariant_id,
        name: journey.name,
    }));
    switch (output) {
        case constants_1.OutputFormats.Json:
            loggingProvider_1.logger.info(JSON.stringify(outputJourneys, null, 2));
            break;
        case constants_1.OutputFormats.Yaml:
            loggingProvider_1.logger.info((0, js_yaml_1.dump)(outputJourneys));
            break;
        case constants_1.OutputFormats.Table:
        default:
            printTestsAsTable(journeys);
            loggingProvider_1.logger.info(`${journeys.length} tests returned`);
            if (journeys.length === limit) {
                loggingProvider_1.logger.info(`... use the --limit flag to return a larger set`);
            }
            break;
    }
    return journeys.length;
}
function printTestsAsTable(tests) {
    const table = new cli_table3_1.default({
        head: ['ID', 'Name', 'Created time'],
        wordWrap: true,
    });
    tests.forEach((test) => {
        table.push([
            { rowSpan: 1, content: test.invariant_id, vAlign: 'center' },
            { rowSpan: 1, content: test.name, vAlign: 'center' },
            {
                rowSpan: 1,
                content: moment.utc(test.created_time).format(constants_1.ListTimeFormat),
                vAlign: 'center',
            },
        ]);
    });
    loggingProvider_1.logger.info(table.toString());
}
