import { Command } from 'commander' import { handleError } from '@/shared/utils/error-handler' import { formatOutput } from '@/shared/utils/output' import { parseLimitOption, withInstagramClient } from './shared' async function listAction(options: { account?: string; pretty?: boolean; limit?: string }): Promise { try { const limit = parseLimitOption(options.limit, 20) const chats = await withInstagramClient(options, (client) => client.listChats(limit)) console.log(formatOutput(chats, options.pretty)) process.exit(0) } catch (error) { handleError(error as Error) } } async function searchAction( query: string, options: { account?: string; pretty?: boolean; limit?: string }, ): Promise { try { const limit = parseLimitOption(options.limit, 20) const chats = await withInstagramClient(options, (client) => client.searchChats(query, limit)) console.log(formatOutput(chats, options.pretty)) process.exit(0) } catch (error) { handleError(error as Error) } } export const chatCommand = new Command('chat') .description('Instagram chat commands') .addCommand( new Command('list') .description('List DM conversations') .option('--limit ', 'Number of chats to fetch', '20') .option('--account ', 'Use a specific Instagram account') .option('--pretty', 'Pretty print JSON output') .action(listAction), ) .addCommand( new Command('search') .description('Search DM conversations by name') .argument('', 'Search query') .option('--limit ', 'Number of results to return', '20') .option('--account ', 'Use a specific Instagram account') .option('--pretty', 'Pretty print JSON output') .action(searchAction), )