"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const mablApiClientFactory_1 = require("@mablhq-internal/runtime/api/mablApiClientFactory");
const cliUtil_1 = require("@mablhq-internal/runtime/core/cliUtil");
const util_1 = require("../../commandUtil/util");
const describe_1 = require("../../commandUtil/describe");
const constants_1 = require("@mablhq-internal/runtime/core/constants");
const constants_2 = require("../../constants");
const constants_3 = require("@mablhq-internal/runtime/core/constants");
exports.command = 'create';
exports.describe = 'Create a new agent instruction';
exports.builder = (yargs) => {
    yargs
        .option(constants_3.CommandArgWorkspaceId, {
        alias: constants_2.CommandArgAliases.WorkspaceId,
        describe: 'Workspace to create instruction in',
        type: 'string',
    })
        .option(constants_2.CommandArgName, {
        alias: constants_2.CommandArgAliases.Name,
        demandOption: true,
        describe: 'Name for the instruction',
        type: 'string',
    })
        .option(constants_2.CommandArgInstructionText, {
        demandOption: true,
        describe: 'The instruction text (max 1000 characters)',
        type: 'string',
    })
        .option(constants_2.CommandArgCapabilities, {
        describe: 'Capabilities to scope to (authoring, recovery, results_analysis)',
        type: 'array',
    })
        .option(constants_2.CommandArgApplicationIds, {
        describe: 'Application IDs to scope the instruction to',
        type: 'array',
    })
        .option(constants_2.CommandArgEnvironmentIds, {
        describe: 'Environment IDs to scope the instruction to',
        type: 'array',
    })
        .option(constants_2.CommandArgDisabled, {
        describe: 'Create the instruction in a disabled state',
        type: 'boolean',
        default: false,
    });
};
exports.handler = (0, cliUtil_1.failWrapper)(createInstruction);
async function createInstruction(parsed) {
    const workspaceId = await (0, util_1.getWorkspaceId)(parsed);
    const capabilities = parsed[constants_2.CommandArgCapabilities];
    const applicationIds = parsed[constants_2.CommandArgApplicationIds];
    const environmentIds = parsed[constants_2.CommandArgEnvironmentIds];
    const request = {
        workspace_id: workspaceId,
        name: parsed[constants_2.CommandArgName],
        instruction_text: parsed[constants_2.CommandArgInstructionText],
        disabled: parsed[constants_2.CommandArgDisabled],
        capabilities: capabilities && capabilities.length > 0
            ? capabilities
            : undefined,
        application_ids: applicationIds && applicationIds.length > 0 ? applicationIds : undefined,
        environment_ids: environmentIds && environmentIds.length > 0 ? environmentIds : undefined,
        test_types: ['browser'],
    };
    const apiClient = await mablApiClientFactory_1.MablApiClientFactory.createApiClient();
    const instruction = await apiClient.createAgentInstruction(request);
    (0, describe_1.outputEntity)(instruction, constants_1.OutputFormats.Json);
    return instruction.instruction_id ?? '';
}
