import { flags } from '@heroku-cli/command';
import { AppCompletion, PipelineCompletion, SpaceCompletion, TeamCompletion, } from '@heroku-cli/command/lib/completions.js';
import * as color from '@heroku/heroku-cli-util/color';
import { Args, ux } from '@oclif/core';
import * as path from 'path';
import { AutocompleteBase } from '../../lib/autocomplete/base.js';
import { updateCache } from '../../lib/autocomplete/cache.js';
import Create from './create.js';
export default class Index extends AutocompleteBase {
    static args = {
        shell: Args.string({ description: 'shell type', required: false }),
    };
    static description = 'display autocomplete installation instructions';
    static examples = [
        color.command('heroku autocomplete'),
        color.command('heroku autocomplete bash'),
        color.command('heroku autocomplete zsh'),
        color.command('heroku autocomplete --refresh-cache'),
    ];
    static flags = {
        'refresh-cache': flags.boolean({ char: 'r', description: 'refresh cache only (ignores displaying instructions)' }),
    };
    async run() {
        const { args, flags } = await this.parse(Index);
        const shell = args.shell || this.config.shell;
        this.errorIfNotSupportedShell(shell);
        ux.action.start('Building the autocomplete cache');
        const create = new Create([], this.config);
        await create.run();
        await this.updateCache(AppCompletion, 'app');
        await this.updateCache(PipelineCompletion, 'pipeline');
        await this.updateCache(SpaceCompletion, 'space');
        await this.updateCache(TeamCompletion, 'team');
        ux.action.stop();
        if (!flags['refresh-cache']) {
            const { bin } = this.config;
            const bashNote = 'If your terminal starts as a login shell you may need to print the init script into ~/.bash_profile or ~/.profile.';
            const zshNote = `After sourcing, you can run \`${color.command('compaudit -D')}\` to ensure no permissions conflicts are present`;
            const note = shell === 'zsh' ? zshNote : bashNote;
            const tabStr = shell === 'bash' ? '<TAB><TAB>' : '<TAB>';
            this.log(`
${color.label(`Setup Instructions for ${bin.toUpperCase()} CLI Autocomplete ---`)}

1) Add the autocomplete env var to your ${shell} profile and source it
${color.command(`printf "$(${bin} autocomplete:script ${shell})" >> ~/.${shell}rc; source ~/.${shell}rc`)}

NOTE: ${note}

2) Test it out, e.g.:
${color.command(`${bin} ${tabStr}`)}                 # Command completion
${color.command(`${bin} apps:info --${tabStr}`)}     # Flag completion
${color.command(`${bin} apps:info --app=${tabStr}`)} # Flag option completion

Visit the autocomplete Dev Center doc at ${color.info('https://devcenter.heroku.com/articles/heroku-cli-autocomplete')}

Enjoy!
`);
        }
    }
    async updateCache(completion, cacheKey) {
        const cachePath = path.join(this.completionsCacheDir, cacheKey);
        const options = await completion.options({ config: this.config });
        await updateCache(cachePath, options);
    }
}
