"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 mablApi_1 = require("@mablhq-internal/runtime/mablApi");
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 constants_2 = require("../../constants");
exports.command = 'list';
exports.describe = 'List your mobile build files';
exports.builder = (yargs) => {
    (0, list_1.getListBuilderOptions)('mobile build files')(yargs);
    yargs.option(constants_2.CommandArgMobilePlatform, {
        describe: 'Filter results to a single mobile platform',
        type: 'string',
        choices: [
            mablApi_1.MobilePlatformEnum.Android.toString().toLowerCase(),
            mablApi_1.MobilePlatformEnum.Ios.toString().toLowerCase(),
        ],
    });
};
exports.handler = (0, cliUtil_1.failWrapper)(listMobileAppFiles);
async function listMobileAppFiles(parsed) {
    const output = parsed.output;
    const workspaceId = await (0, util_1.getWorkspaceId)(parsed);
    const limit = parsed.limit;
    const platform = parsed.platform;
    const apiClient = await mablApiClientFactory_1.MablApiClientFactory.createApiClient();
    const mobileAppFiles = await apiClient.getMobileAppFiles(workspaceId, limit, platform);
    printMobileAppFiles(mobileAppFiles, output);
    return mobileAppFiles.length;
}
function printMobileAppFiles(mobileAppFiles, output) {
    switch (output) {
        case 'json':
            loggingProvider_1.logger.info(JSON.stringify(mobileAppFiles, undefined, 2));
            break;
        case 'yaml':
            loggingProvider_1.logger.info((0, js_yaml_1.dump)(mobileAppFiles));
            break;
        default:
            const table = new cli_table3_1.default({
                head: [
                    'ID',
                    'Platform',
                    'Name',
                    'Version',
                    'Branch',
                    'Labels',
                    'Created time',
                ],
                wordWrap: true,
            });
            mobileAppFiles.forEach((mobileAppFile) => {
                table.push([
                    { rowSpan: 1, content: mobileAppFile.id, vAlign: 'center' },
                    { rowSpan: 1, content: mobileAppFile.platform, vAlign: 'center' },
                    { rowSpan: 1, content: mobileAppFile.name, vAlign: 'center' },
                    { rowSpan: 1, content: mobileAppFile.version, vAlign: 'center' },
                    { rowSpan: 1, content: mobileAppFile.branch, vAlign: 'center' },
                    {
                        rowSpan: 1,
                        content: JSON.stringify(mobileAppFile.labels, undefined, 2),
                        vAlign: 'center',
                    },
                    {
                        rowSpan: 1,
                        content: moment
                            .utc(mobileAppFile.created_time)
                            .format(constants_1.ListTimeFormat),
                        vAlign: 'center',
                    },
                ]);
            });
            loggingProvider_1.logger.info(table.toString());
            break;
    }
}
