import { Command, flags } from '@heroku-cli/command';
import * as color from '@heroku/heroku-cli-util/color';
import { Args, ux } from '@oclif/core';
import { trapConfirmationRequired } from '../../lib/addons/util.js';
export default class Attach extends Command {
    static args = {
        addon_name: Args.string({ description: 'unique identifier or globally unique name of the add-on', required: true }),
    };
    static description = 'attach an existing add-on resource to an app';
    static flags = {
        app: flags.app({ required: true }),
        as: flags.string({ description: 'name for add-on attachment' }),
        confirm: flags.string({ description: 'overwrite existing add-on attachment with same name' }),
        credential: flags.string({ description: 'credential name for scoped access to Heroku Postgres' }),
        remote: flags.remote(),
    };
    static topic = 'addons';
    async run() {
        const { args, flags } = await this.parse(Attach);
        const { app, as, confirm, credential } = flags;
        const { body: addon } = await this.heroku.get(`/addons/${encodeURIComponent(args.addon_name)}`);
        const createAttachment = async (confirmed) => {
            let namespace;
            if (credential && credential !== 'default') {
                namespace = 'credential:' + credential;
            }
            const body = {
                addon: { name: addon.name }, app: { name: app }, confirm: confirmed, name: as, namespace,
            };
            try {
                ux.action.start(`Attaching ${credential ? color.name(credential) + ' of ' : ''}${color.datastore(addon.name || '')}${as ? ' as ' + color.attachment(as) : ''} to ${color.app(app)}`);
                const { body: attachment } = await this.heroku.post('/addon-attachments', { body });
                ux.action.stop();
                return attachment;
            }
            catch (error) {
                ux.action.stop(color.red('!'));
                throw error;
            }
        };
        if (credential && credential !== 'default') {
            const { body: credentialConfig } = await this.heroku.get(`/addons/${addon.name}/config/credential:${encodeURIComponent(credential)}`);
            if (credentialConfig.length === 0) {
                throw new Error(`Could not find credential ${color.name(credential)} for database ${color.datastore(addon.name || '')}`);
            }
        }
        const attachment = await trapConfirmationRequired(app, confirm, (confirmed) => createAttachment(confirmed));
        ux.action.start(`Setting ${color.attachment(attachment.name || '')} config vars and restarting ${color.app(app)}`);
        const { body: releases } = await this.heroku.get(`/apps/${app}/releases`, {
            headers: { Range: 'version ..; max=1, order=desc' }, partial: true,
        });
        ux.action.stop(`done, v${releases[0].version}`);
    }
}
