"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = require("path");
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 describe_1 = require("../../commandUtil/describe");
const datatableUtil_1 = require("@mablhq-internal/runtime/util/datatableUtil");
exports.command = `create <${constants_2.CommandArgInput}>`;
exports.describe = 'Create a new mabl DataTable';
exports.builder = (yargs) => {
    yargs
        .positional(constants_2.CommandArgInput, {
        alias: 'file',
        describe: 'Relative path file to create DataTable from',
    })
        .option(constants_1.CommandArgWorkspaceId, {
        alias: constants_2.CommandArgAliases.WorkspaceId,
        describe: `Workspace to create DataTable in`,
        type: 'string',
    })
        .option(constants_2.CommandArgName, {
        alias: 'table-name',
        demandOption: false,
        describe: `The name for your new DataTable`,
        type: 'string',
    })
        .option(constants_1.CommandArgFormat, {
        alias: 'fmt',
        demandOption: false,
        describe: `Specify the format of your input file (checks file extension type by default)`,
        choices: [constants_1.OutputFormats.Json, constants_1.OutputFormats.Csv],
    });
};
exports.handler = (0, cliUtil_1.failWrapper)(createDataTable);
async function createDataTable(parsed) {
    const { input } = parsed;
    const format = parsed.format ?? (0, datatableUtil_1.inferFileFormat)(input);
    const name = parsed.name ?? (0, path_1.basename)((0, path_1.resolve)(input));
    const workspaceId = await (0, util_1.getWorkspaceId)(parsed);
    let scenarios = [];
    switch (format) {
        case constants_1.OutputFormats.Json:
            scenarios = (0, datatableUtil_1.parseJsonScenarios)(input);
            break;
        case constants_1.OutputFormats.Csv:
            scenarios = await (0, datatableUtil_1.parseCsvScenarios)(input);
            break;
        default:
            throw Error(`${format} is not a valid option.`);
    }
    const dataTableCreateRequest = {
        workspace_id: workspaceId,
        data_table: { name },
        scenarios,
    };
    const apiClient = await mablApiClientFactory_1.MablApiClientFactory.createApiClient();
    const dataTable = await apiClient.createDataTable(dataTableCreateRequest);
    (0, describe_1.outputEntity)(dataTable, constants_1.OutputFormats.Json);
}
