"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const js_yaml_1 = require("js-yaml");
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 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");
exports.command = 'list';
exports.describe = 'List mabl branches';
exports.builder = {
    [constants_2.CommandArgLimitOutput]: {
        alias: constants_2.CommandArgAliases.LimitOutput,
        describe: 'Item return limit',
        nargs: 1,
        default: commandTypes_1.DEFAULT_LISTING_RESULT_LIMIT,
    },
    [constants_2.CommandArgOutput]: {
        alias: constants_2.CommandArgAliases.OutputType,
        describe: 'Specify result output format',
        choices: constants_2.DefaultOutputFormatChoices,
        nargs: 1,
    },
    status: {
        alias: constants_2.CommandArgAliases.Status,
        describe: 'filter results by status',
        choices: ['open', 'merged'],
        nargs: 1,
    },
    [constants_1.CommandArgWorkspaceId]: {
        alias: constants_2.CommandArgAliases.WorkspaceId,
        describe: 'The workspace ID to list branches for',
        nargs: 1,
    },
};
exports.handler = (0, cliUtil_1.failWrapper)(listBranches);
async function listBranches(parsed) {
    const output = parsed.output;
    const limit = parsed.limit;
    const statusFilter = parsed.status;
    const workspaceId = await (0, util_1.getWorkspaceId)(parsed);
    const apiClient = await mablApiClientFactory_1.MablApiClientFactory.createApiClient();
    const branches = await apiClient.getBranches(workspaceId, limit, statusFilter);
    printBranches(branches, output);
    return branches.length;
}
function printBranches(branches, output) {
    switch (output) {
        case 'json':
            loggingProvider_1.logger.info(JSON.stringify(branches, null, 2));
            break;
        case 'yaml':
            loggingProvider_1.logger.info((0, js_yaml_1.dump)(branches));
            break;
        default:
            const table = new cli_table3_1.default({
                head: ['ID', 'Name', 'Status', 'Created'],
            });
            branches.forEach((branch) => {
                table.push([
                    { rowSpan: 1, content: branch.id, vAlign: 'center' },
                    { rowSpan: 1, content: branch.name, vAlign: 'center' },
                    {
                        rowSpan: 1,
                        content: branch.status,
                        vAlign: 'center',
                        hAlign: 'center',
                    },
                    {
                        rowSpan: 1,
                        content: moment.utc(branch.created_time).format(constants_1.ListTimeFormat),
                        vAlign: 'center',
                    },
                ]);
            });
            loggingProvider_1.logger.info(table.toString());
            break;
    }
}
