import { formatOutput, formatToolDetail, formatSuccess, formatWarning } from '../output.js';
import { ClientError } from '../../lib/errors.js';
import { withMcpClient } from '../helpers.js';
import { parseCommandArgs, hasStdinData, readStdinArgs } from '../parser.js';
import { loadSchemaFromFile, validateToolSchema, formatValidationError, } from '../../lib/schema-validator.js';
export async function listTools(target, options) {
    await withMcpClient(target, options, async (client, _context) => {
        const allTools = [];
        let cursor = undefined;
        do {
            const result = await client.listTools(cursor);
            allTools.push(...result.tools);
            cursor = result.nextCursor;
        } while (cursor);
        console.log(formatOutput(allTools, options.outputMode, options.full ? { full: true } : undefined));
    });
}
export async function getTool(target, name, options) {
    let expectedSchema;
    if (options.schema) {
        expectedSchema = (await loadSchemaFromFile(options.schema));
    }
    await withMcpClient(target, options, async (client, _context) => {
        const result = await client.listTools();
        const tool = result.tools.find((t) => t.name === name);
        if (!tool) {
            throw new ClientError(`Tool not found: ${name}`);
        }
        if (expectedSchema) {
            const schemaMode = options.schemaMode || 'compatible';
            const validation = validateToolSchema(tool, expectedSchema, schemaMode);
            if (!validation.valid) {
                throw new ClientError(formatValidationError(validation, `tool "${name}"`));
            }
            if (validation.warnings.length > 0 && options.outputMode === 'human') {
                for (const warning of validation.warnings) {
                    console.log(formatWarning(`Schema warning: ${warning}`));
                }
            }
        }
        if (options.outputMode === 'human') {
            console.log(formatToolDetail(tool));
        }
        else {
            console.log(formatOutput(tool, 'json'));
        }
    });
}
export async function callTool(target, name, options) {
    let parsedArgs;
    if (options.args && options.args.length > 0) {
        parsedArgs = parseCommandArgs(options.args);
    }
    else if (hasStdinData()) {
        parsedArgs = await readStdinArgs();
    }
    else {
        parsedArgs = {};
    }
    let expectedSchema;
    if (options.schema) {
        expectedSchema = (await loadSchemaFromFile(options.schema));
    }
    await withMcpClient(target, options, async (client, _context) => {
        const schemaMode = options.schemaMode || 'compatible';
        if (expectedSchema && schemaMode !== 'ignore') {
            const result = await client.listTools();
            const actualTool = result.tools.find((t) => t.name === name);
            if (!actualTool) {
                throw new ClientError(`Tool not found: ${name}`);
            }
            const validation = validateToolSchema(actualTool, expectedSchema, schemaMode, parsedArgs);
            if (!validation.valid) {
                throw new ClientError(formatValidationError(validation, `tool "${name}"`));
            }
            if (validation.warnings.length > 0 && options.outputMode === 'human') {
                for (const warning of validation.warnings) {
                    console.log(formatWarning(`Schema warning: ${warning}`));
                }
            }
        }
        const result = await client.callTool(name, parsedArgs);
        if (options.outputMode === 'human') {
            console.log(formatSuccess(`Tool ${name} executed successfully`));
            console.log(formatOutput(result, 'human'));
        }
        else {
            console.log(formatOutput(result, 'json'));
        }
    });
}
//# sourceMappingURL=tools.js.map