import { Command, flags } from '@heroku-cli/command';
import { Args, ux } from '@oclif/core';
import { stream } from '../../lib/releases/output.js';
import { findByLatestOrId } from '../../lib/releases/releases.js';
export default class Output extends Command {
    static topic = 'releases';
    static description = 'View the release command output';
    static flags = {
        remote: flags.remote(),
        app: flags.app({ required: true }),
    };
    static args = {
        release: Args.string({ description: 'ID of the release. If omitted, we use the last release ID.' }),
    };
    async run() {
        const { flags, args } = await this.parse(Output);
        const { app } = flags;
        const release = await findByLatestOrId(this.heroku, app, args.release);
        const streamUrl = release.output_stream_url;
        if (!streamUrl) {
            ux.warn(`Release v${release.version} has no release output available.`);
            return;
        }
        await stream(streamUrl)
            .catch(error => {
            if (error.statusCode === 404 || error.response?.statusCode === 404) {
                ux.warn('Release command not started yet. Please try again in a few seconds.');
                return;
            }
            throw error;
        });
    }
}
