import { Command } from 'commander';
import { getClient, getMessages, disconnectClient } from '../client.js';
import { formatJson } from '../formatters/json.js';
import { formatMessages } from '../formatters/plain.js';
import { formatMessagesMarkdown } from '../formatters/markdown.js';
import { getOutputFormat } from '../formatters/index.js';
import ora from 'ora';
function parseTimeOffset(offset) {
    const now = new Date();
    const match = offset.match(/^(\d+)([mhd])$/);
    if (!match) {
        throw new Error(`Invalid time offset: ${offset}. Use format like "1h", "30m", "7d"`);
    }
    const value = parseInt(match[1]);
    const unit = match[2];
    switch (unit) {
        case 'm':
            return new Date(now.getTime() - value * 60 * 1000);
        case 'h':
            return new Date(now.getTime() - value * 60 * 60 * 1000);
        case 'd':
            return new Date(now.getTime() - value * 24 * 60 * 60 * 1000);
        default:
            throw new Error(`Unknown time unit: ${unit}`);
    }
}
export const readCommand = new Command('read')
    .description('Read messages from a chat')
    .argument('<chat>', 'Chat name, username (@user), or ID')
    .option('-n, --limit <number>', 'Number of messages to fetch', '50')
    .option('--since <time>', 'Get messages since (e.g., "1h", "30m", "7d")')
    .option('--until <time>', 'Get messages until (e.g., "1h", "30m", "7d")')
    .option('--json', 'Output as JSON')
    .option('--markdown', 'Output as Markdown')
    .action(async (chat, options) => {
    const spinner = ora(`Fetching messages from "${chat}"...`).start();
    try {
        const client = await getClient();
        const fetchOptions = {
            limit: parseInt(options.limit),
        };
        if (options.since) {
            fetchOptions.minDate = parseTimeOffset(options.since);
        }
        if (options.until) {
            fetchOptions.maxDate = parseTimeOffset(options.until);
        }
        const { messages, chatTitle } = await getMessages(client, chat, fetchOptions);
        spinner.stop();
        const format = getOutputFormat(options);
        switch (format) {
            case 'json':
                console.log(formatJson({ chatTitle, messages }));
                break;
            case 'markdown':
                console.log(formatMessagesMarkdown(messages, chatTitle));
                break;
            default:
                console.log(formatMessages(messages, chatTitle));
        }
        await disconnectClient();
    }
    catch (error) {
        spinner.fail('Failed to fetch messages');
        console.error(error instanceof Error ? error.message : error);
        process.exit(1);
    }
});
//# sourceMappingURL=read.js.map