"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 describe_1 = require("../../commandUtil/describe");
const constants_1 = require("@mablhq-internal/runtime/core/constants");
const constants_2 = require("@mablhq-internal/runtime/core/constants");
const constants_3 = require("../../constants");
exports.command = `update <${constants_2.CommandArgId}>`;
exports.describe = 'Update an agent instruction';
exports.builder = (yargs) => {
    yargs
        .positional(constants_2.CommandArgId, {
        describe: 'The instruction ID',
        type: 'string',
    })
        .option(constants_3.CommandArgName, {
        describe: 'Updated name',
        type: 'string',
    })
        .option(constants_3.CommandArgInstructionText, {
        describe: 'Updated instruction text',
        type: 'string',
    })
        .option(constants_3.CommandArgCapabilities, {
        describe: 'Updated capabilities (authoring, recovery, results_analysis)',
        type: 'array',
    })
        .option(constants_3.CommandArgApplicationIds, {
        describe: 'Updated application IDs',
        type: 'array',
    })
        .option(constants_3.CommandArgEnvironmentIds, {
        describe: 'Updated environment IDs',
        type: 'array',
    })
        .option(constants_3.CommandArgDisabled, {
        describe: 'Disable the instruction',
        type: 'boolean',
    })
        .option(constants_3.CommandArgEnabled, {
        describe: 'Enable the instruction',
        type: 'boolean',
    })
        .conflicts(constants_3.CommandArgDisabled, constants_3.CommandArgEnabled);
};
exports.handler = (0, cliUtil_1.failWrapper)(updateInstruction);
async function updateInstruction(parsed) {
    const id = parsed[constants_2.CommandArgId];
    const update = {};
    if (parsed[constants_3.CommandArgName] !== undefined) {
        update.name = parsed[constants_3.CommandArgName];
    }
    if (parsed[constants_3.CommandArgInstructionText] !== undefined) {
        update.instruction_text = parsed[constants_3.CommandArgInstructionText];
    }
    if (parsed[constants_3.CommandArgCapabilities] !== undefined) {
        update.capabilities = parsed[constants_3.CommandArgCapabilities];
    }
    if (parsed[constants_3.CommandArgApplicationIds] !== undefined) {
        update.application_ids = parsed[constants_3.CommandArgApplicationIds];
    }
    if (parsed[constants_3.CommandArgEnvironmentIds] !== undefined) {
        update.environment_ids = parsed[constants_3.CommandArgEnvironmentIds];
    }
    if (parsed[constants_3.CommandArgDisabled] !== undefined) {
        update.disabled = parsed[constants_3.CommandArgDisabled];
    }
    if (parsed[constants_3.CommandArgEnabled] !== undefined) {
        update.disabled = !parsed[constants_3.CommandArgEnabled];
    }
    const apiClient = await mablApiClientFactory_1.MablApiClientFactory.createApiClient();
    const instruction = await apiClient.updateAgentInstruction(id, update);
    (0, describe_1.outputEntity)(instruction, constants_1.OutputFormats.Json);
    return instruction.instruction_id ?? id;
}
