import { Command, flags } from '@heroku-cli/command';
import * as color from '@heroku/heroku-cli-util/color';
import { Args, ux } from '@oclif/core';
import { waitForAddonDeprovisioning, waitForAddonProvisioning } from '../../lib/addons/addons_wait.js';
import { resolveAddon } from '../../lib/addons/resolve.js';
import notify from '../../lib/notify.js';
export default class Wait extends Command {
    static args = {
        addon: Args.string({ description: 'unique identifier or globally unique name of the add-on' }),
    };
    static description = 'show provisioning status of the add-ons on the app';
    static flags = {
        app: flags.app(),
        remote: flags.remote(),
        'wait-interval': flags.string({ description: 'how frequently to poll in seconds' }),
    };
    static notifier = notify;
    static topic = 'addons';
    async run() {
        const { args, flags } = await this.parse(Wait);
        let addonsToWaitFor;
        if (args.addon) {
            addonsToWaitFor = [await resolveAddon(this.heroku, flags.app, args.addon)];
        }
        else if (flags.app) {
            const { body: addons } = await this.heroku.get(`/apps/${flags.app}/addons`);
            addonsToWaitFor = addons;
        }
        else {
            const { body: addons } = await this.heroku.get('/addons');
            addonsToWaitFor = addons;
        }
        addonsToWaitFor = addonsToWaitFor.filter((addon) => addon.state === 'provisioning' || addon.state === 'deprovisioning');
        let interval = Number.parseInt(flags['wait-interval'] || '', 10);
        if (!interval || interval < 0) {
            interval = 5;
        }
        for (const addon of addonsToWaitFor) {
            const startTime = new Date();
            const addonName = addon.name || '';
            if (addon.state === 'provisioning') {
                let addonResponse;
                try {
                    addonResponse = await waitForAddonProvisioning(this.heroku, addon, interval);
                }
                catch (error) {
                    Wait.notifier(`heroku addons:wait ${addonName}`, 'Add-on failed to provision', false);
                    throw error;
                }
                const configVars = (addonResponse.config_vars || []);
                if (configVars.length > 0) {
                    const decoratedConfigVars = configVars.map(c => color.name(c))
                        .join(', ');
                    ux.stdout(`Created ${color.addon(addonName)} as ${decoratedConfigVars}`);
                }
                if (Date.now() - startTime.valueOf() >= 1000 * 5) {
                    Wait.notifier(`heroku addons:wait ${addonName}`, 'Add-on successfully provisioned');
                }
            }
            else if (addon.state === 'deprovisioning') {
                await waitForAddonDeprovisioning(this.heroku, addon, interval);
                if (Date.now() - startTime.valueOf() >= 1000 * 5) {
                    Wait.notifier(`heroku addons:wait ${addonName}`, 'Add-on successfully deprovisioned');
                }
            }
        }
    }
}
