"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");
exports.command = 'list';
exports.describe = 'List flows';
exports.builder = (yargs) => {
    yargs
        .option(constants_1.CommandArgWorkspaceId, {
        alias: constants_2.CommandArgAliases.WorkspaceId,
        describe: 'Workspace to list flows for',
        nargs: 1,
        type: 'string',
    })
        .option(constants_2.CommandArgLimitOutput, {
        alias: constants_2.CommandArgAliases.LimitOutput,
        describe: 'The number of flows to return',
        default: 10,
        nargs: 1,
        type: 'string',
    });
};
exports.handler = (0, cliUtil_1.failWrapper)(listFlows);
async function listFlows(parsed) {
    const limit = parsed.limit;
    const workspaceId = await (0, util_1.getWorkspaceId)(parsed);
    const apiClient = await mablApiClientFactory_1.MablApiClientFactory.createApiClient();
    const flows = await apiClient.getFlows({
        organization_id: workspaceId,
        limit,
        reusable: true,
    });
    printFlows(flows);
    loggingProvider_1.logger.info(`${flows.length} flows returned`);
    if (flows.length === limit) {
        loggingProvider_1.logger.info(`... use the --limit flag to return a larger set`);
    }
    return flows.length;
}
function printFlows(flows) {
    const table = new cli_table3_1.default({
        head: ['ID', 'Description', 'Created time'],
        wordWrap: true,
    });
    flows.forEach((flow) => {
        table.push([
            { rowSpan: 1, content: flow.invariant_id, vAlign: 'center' },
            { rowSpan: 1, content: flow.description, vAlign: 'center' },
            {
                rowSpan: 1,
                content: moment.utc(flow.created_time).format(constants_1.ListTimeFormat),
                vAlign: 'center',
            },
        ]);
    });
    loggingProvider_1.logger.info(table.toString());
}
