import { flags } from '@heroku-cli/command';
import { color, hux } from '@heroku/heroku-cli-util';
import BaseCommand from '../../../lib/webhooks/base.js';
export default class Deliveries extends BaseCommand {
    static description = 'list webhook deliveries on an app';
    static examples = [
        `${color.command('heroku webhooks:deliveries')}`,
    ];
    static flags = {
        app: flags.app(),
        remote: flags.remote(),
        status: flags.string({ char: 's', description: 'filter deliveries by status' }),
        pipeline: flags.pipeline({ char: 'p', description: 'pipeline on which to list', hidden: true }),
    };
    async run() {
        const { flags } = await this.parse(Deliveries);
        const webhookType = this.webhookType(flags);
        let { path } = webhookType;
        const { display } = webhookType;
        const max = 1000;
        path = `${path}/webhook-deliveries`;
        if (flags.status) {
            path += `?eq[status]=${encodeURIComponent(flags.status)}`;
        }
        const { body: deliveries } = await this.webhooksClient.get(path, {
            headers: {
                Range: `seq ..; order=desc,max=${max}`,
            },
            partial: true,
        });
        if (deliveries.length === 0) {
            this.log(`${display} has no deliveries`);
        }
        else {
            const code = (w) => (w.last_attempt && w.last_attempt.code && String(w.last_attempt.code)) || '';
            deliveries.reverse();
            if (deliveries.length === max) {
                this.warn(`Only showing the ${max} most recent deliveries`);
                this.warn('It is possible to filter deliveries by using the --status flag');
            }
            const printLine = (...args) => this.log(...args);
            hux.table(deliveries, {
                id: {
                    header: 'Delivery ID',
                },
                created_at: {
                    header: 'Created', get: (w) => w.created_at,
                },
                status: {
                    header: 'Status', get: (w) => w.status,
                },
                include: {
                    header: 'Include', get: (w) => w.event.include,
                },
                level: {
                    header: 'Level', get: (w) => w.webhook.level,
                },
                num_attempts: {
                    header: 'Attempts', get: (w) => String(w.num_attempts),
                },
                last_code: {
                    header: 'Code', get: code,
                },
                last_error: {
                    header: 'Error', get: (w) => (w.last_attempt && w.last_attempt.error_class) || '',
                },
                next_attempt_at: {
                    header: 'Next Attempt', get: (w) => w.next_attempt_at || '',
                },
            }, {
                printLine,
            });
        }
    }
}
