import { formatOutput, formatSuccess } from '../output.js';
import { withMcpClient } from '../helpers.js';
export async function listResources(target, options) {
    await withMcpClient(target, options, async (client, _context) => {
        const allResources = [];
        let cursor = undefined;
        do {
            const result = await client.listResources(cursor);
            allResources.push(...result.resources);
            cursor = result.nextCursor;
        } while (cursor);
        console.log(formatOutput(allResources, options.outputMode));
    });
}
export async function listResourceTemplates(target, options) {
    await withMcpClient(target, options, async (client, _context) => {
        const allTemplates = [];
        let cursor = undefined;
        do {
            const result = await client.listResourceTemplates(cursor);
            allTemplates.push(...result.resourceTemplates);
            cursor = result.nextCursor;
        } while (cursor);
        console.log(formatOutput(allTemplates, options.outputMode));
    });
}
export async function getResource(target, uri, options) {
    await withMcpClient(target, options, async (client, _context) => {
        const result = await client.readResource(uri);
        if (options.output) {
            throw new Error('--output flag not implemented yet');
        }
        if (options.raw && result.contents.length > 0) {
            const firstContent = result.contents[0];
            if (firstContent) {
                if ('text' in firstContent && firstContent.text) {
                    console.log(firstContent.text);
                }
                else if ('blob' in firstContent && firstContent.blob) {
                    console.log(firstContent.blob);
                }
            }
            return;
        }
        console.log(formatOutput(result, options.outputMode));
    });
}
export async function subscribeResource(target, uri, options) {
    await withMcpClient(target, options, async (client, _context) => {
        await client.subscribeResource(uri);
        if (options.outputMode === 'human') {
            console.log(formatSuccess(`Subscribed to resource: ${uri}`));
        }
        else {
            console.log(formatOutput({ subscribed: true, uri }, 'json'));
        }
    });
}
export async function unsubscribeResource(target, uri, options) {
    await withMcpClient(target, options, async (client, _context) => {
        await client.unsubscribeResource(uri);
        if (options.outputMode === 'human') {
            console.log(formatSuccess(`Unsubscribed from resource: ${uri}`));
        }
        else {
            console.log(formatOutput({ unsubscribed: true, uri }, 'json'));
        }
    });
}
//# sourceMappingURL=resources.js.map