import { ux } from '@oclif/core/ux';
export async function waitForDomains(app, heroku) {
    ux.action.start('Waiting for stable domains to be created');
    for await (const apiDomains of customDomainCreationComplete(app, heroku)) {
        if (apiDomains) {
            ux.action.stop();
            return apiDomains;
        }
    }
    ux.action.stop('!');
    throw new Error('Timed out while waiting for stable domains to be created');
}
async function* customDomainCreationComplete(app, heroku) {
    let retries = 30;
    while (retries--) {
        const { body: apiDomains } = await heroku.get(`/apps/${app}/domains`);
        const someNull = apiDomains.some((domain) => domain.kind === 'custom' && !domain.cname);
        if (!someNull) {
            yield apiDomains;
            break;
        }
        await new Promise(resolve => {
            setTimeout(resolve, 1000);
        });
        yield null;
    }
}
