"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 mablApiClientFactory_1 = require("@mablhq-internal/runtime/api/mablApiClientFactory");
const util_1 = require("../../commandUtil/util");
const cliUtil_1 = require("@mablhq-internal/runtime/core/cliUtil");
const cli_table3_1 = __importDefault(require("cli-table3"));
const moment = require("moment");
const list_1 = require("../../commandUtil/list");
const loggingProvider_1 = require("@mablhq-internal/runtime/providers/logging/loggingProvider");
const constants_1 = require("@mablhq-internal/runtime/core/constants");
const chalk = require('chalk');
exports.command = 'list';
exports.describe = 'List your credentials';
exports.builder = (0, list_1.getListBuilderOptions)('credentials');
exports.handler = (0, cliUtil_1.failWrapper)(listCredentials);
async function listCredentials(parsed) {
    const output = parsed.output;
    const limit = parsed.limit;
    const workspaceId = await (0, util_1.getWorkspaceId)(parsed);
    const apiClient = await mablApiClientFactory_1.MablApiClientFactory.createApiClient();
    const credentials = await apiClient.getCredentials(workspaceId, limit);
    printCredentials(credentials, output);
    return credentials.length;
}
function printCredentials(credentials, output) {
    switch (output) {
        case 'json':
            loggingProvider_1.logger.info(JSON.stringify(credentials, null, 2));
            break;
        case 'yaml':
            loggingProvider_1.logger.info((0, js_yaml_1.dump)(credentials));
            break;
        default:
            const table = new cli_table3_1.default({
                head: ['ID', 'Name', 'Type', 'Description', 'Created time'],
                wordWrap: true,
            });
            credentials.forEach((credential) => {
                table.push([
                    { rowSpan: 1, content: credential.id, vAlign: 'center' },
                    { rowSpan: 1, content: credential.name, vAlign: 'center' },
                    {
                        rowSpan: 1,
                        content: (0, util_1.getCredentialType)(credential),
                        vAlign: 'center',
                    },
                    {
                        rowSpan: 1,
                        content: credential.description || '---',
                        vAlign: 'center',
                    },
                    {
                        rowSpan: 1,
                        content: moment.utc(credential.created_time).format(constants_1.ListTimeFormat),
                        vAlign: 'center',
                    },
                ]);
            });
            loggingProvider_1.logger.info(table.toString());
            break;
    }
    loggingProvider_1.logger.info(chalk.bgBlack(chalk.yellow.bold('\nCloud credentials are not available for local runs.')));
}
