import { Command } from '@oclif/core';
import Conf from 'conf';
import api from '../aws/index.js';
import prompts from '../prompts/index.js';
import log from '../util/log.js';
const DEFAULT_NAME = 'campfire';
const config = new Conf({ projectName: DEFAULT_NAME });
const clientsObj = api.clients;
const destroyAllClusters = () => {
    const appNames = JSON.parse(config.get('APP_NAMES', '[]'));
    appNames.forEach((name) => {
        api.deleteCluster({ cluster: name });
    });
};
class Destroy extends Command {
    static description = 'Initialize cluster for a new Campfire deploy preview environment';
    async run() {
        const { closedAllPrs } = await prompts.confirmPRsClosed();
        if (closedAllPrs === 'No') {
            log.info('Oh no! You will need to visit all Campfire repository on GitHub and close any open pull requests before proceeding.');
            process.exit(0);
        }
        const { iAmSure } = await prompts.confirmAreYouSure();
        if (iAmSure === 'No') {
            log.error('Destroy operation cancelled.');
            process.exit(0);
        }
        const { deleteSecrets } = await prompts.confirmDeleteSecrets();
        if (deleteSecrets === "Yes") {
            await api.destroySecrets();
        }
        clientsObj.cloudFormation = await api.initializeCfClient(config.get('AWS_REGION'));
        clientsObj.ecs = await api.initializeEcsClient(config.get('AWS_REGION'));
        await api.deleteStack({ StackName: DEFAULT_NAME });
        destroyAllClusters();
        log.header("Done :D Later skater!");
        config.clear();
    }
}
export default Destroy;
