"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const constants_1 = require("../../constants");
const constants_2 = require("@mablhq-internal/runtime/core/constants");
const mablApiClientFactory_1 = require("@mablhq-internal/runtime/api/mablApiClientFactory");
const exportRequestProvider_1 = require("@mablhq-internal/runtime/providers/exportRequestProvider");
const util_1 = require("../../commandUtil/util");
const cliUtil_1 = require("@mablhq-internal/runtime/core/cliUtil");
const mablApi_1 = require("@mablhq-internal/runtime/mablApi");
const typesAllFlag = 'all';
exports.command = `export <id> [options]`;
exports.describe =
    'Export browser test run artifacts. Defaults to screenshots only.';
exports.builder = (yargs) => {
    yargs
        .positional(constants_2.CommandArgId, {
        describe: 'test run id',
        type: 'string',
        nargs: 1,
    })
        .option(constants_1.CommandArgOutputFilePath, {
        describe: 'export to specific file, default: local path/export name',
        nargs: 1,
        type: 'string',
    })
        .option(constants_2.CommandArgContentTypes, {
        describe: 'types to export, defaults to screenshots only',
        required: false,
        type: 'array',
        default: [mablApi_1.ExportRequest.TargetContentEnum.Screenshots.toString()],
        choices: [
            typesAllFlag,
            mablApi_1.ExportRequest.TargetContentEnum.Screenshots,
            mablApi_1.ExportRequest.TargetContentEnum.ConsoleLogs,
            mablApi_1.ExportRequest.TargetContentEnum.Doms,
            mablApi_1.ExportRequest.TargetContentEnum.Hars,
            mablApi_1.ExportRequest.TargetContentEnum.Traces,
            mablApi_1.ExportRequest.TargetContentEnum.XrayJson,
            mablApi_1.ExportRequest.TargetContentEnum.Variables,
        ],
        coerce: (arg) => arg.includes(typesAllFlag)
            ? [
                mablApi_1.ExportRequest.TargetContentEnum.ConsoleLogs,
                mablApi_1.ExportRequest.TargetContentEnum.Screenshots,
                mablApi_1.ExportRequest.TargetContentEnum.Doms,
                mablApi_1.ExportRequest.TargetContentEnum.Traces,
                mablApi_1.ExportRequest.TargetContentEnum.Hars,
                mablApi_1.ExportRequest.TargetContentEnum.Variables,
            ]
            : arg,
    })
        .example('$0 test-runs export <id>', 'export test run screenshots to file')
        .example('$0 test-runs export <id> --types screenshots doms', 'export test run screenshots and doms to file')
        .example('$0 test-runs export <id> --types all', 'export test run all types to file')
        .check((argv) => {
        (0, util_1.validateArrayInputs)(argv[constants_2.CommandArgContentTypes], 'export types must be SPACE delimited, e.g. "--types=screenshots doms"');
        return true;
    });
};
exports.handler = (0, cliUtil_1.failWrapper)(createExport);
async function createExport(parsed) {
    const targetEntityId = parsed.id;
    const targetEntityType = 'test run';
    const outputFilePath = parsed.file;
    const apiClient = await mablApiClientFactory_1.MablApiClientFactory.createApiClientFromOptionalApiKey(parsed['api-key']);
    return new exportRequestProvider_1.ExportRequestProvider(apiClient, targetEntityId, targetEntityType, parsed.types, outputFilePath).processExport();
}
