"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 cliUtil_1 = require("@mablhq-internal/runtime/core/cliUtil");
const util_1 = require("../../commandUtil/util");
const cli_table3_1 = __importDefault(require("cli-table3"));
const list_1 = require("../../commandUtil/list");
const loggingProvider_1 = require("@mablhq-internal/runtime/providers/logging/loggingProvider");
exports.command = 'list';
exports.describe = 'List agent instructions';
exports.builder = (0, list_1.getListBuilderOptions)('agent instructions');
exports.handler = (0, cliUtil_1.failWrapper)(listInstructions);
async function listInstructions(parsed) {
    const output = parsed.output;
    const workspaceId = await (0, util_1.getWorkspaceId)(parsed);
    const limit = parsed.limit;
    const apiClient = await mablApiClientFactory_1.MablApiClientFactory.createApiClient();
    const instructions = await apiClient.getAgentInstructions(workspaceId, limit);
    printInstructions(instructions, output);
    return instructions.length;
}
function printInstructions(instructions, output) {
    switch (output) {
        case 'json':
            loggingProvider_1.logger.info(JSON.stringify(instructions, null, 2));
            break;
        case 'yaml':
            loggingProvider_1.logger.info((0, js_yaml_1.dump)(instructions));
            break;
        default: {
            const table = new cli_table3_1.default({
                head: ['ID', 'Name', 'Enabled', 'Capabilities'],
                wordWrap: true,
            });
            instructions.forEach((instruction) => {
                table.push([
                    instruction.instruction_id ?? '',
                    instruction.name ?? '',
                    instruction.disabled ? 'No' : 'Yes',
                    instruction.capabilities?.join(', ') ?? 'All',
                ]);
            });
            loggingProvider_1.logger.info(table.toString());
            break;
        }
    }
}
