import chalk from 'chalk';
import BaseGenerator from '../base/index.js';
import { files } from './files.js';
export default class InitGenerator extends BaseGenerator {
    gitInstalled;
    gitInitialized;
    skipGit;
    async beforeQueue() {
        if (!this.fromBlueprint) {
            await this.composeWithBlueprints();
        }
    }
    get initializing() {
        return this.asInitializingTaskGroup({
            async parseCommand() {
                await this.parseCurrentJHipsterCommand();
            },
            async checkGit() {
                if (!this.skipGit) {
                    this.gitInstalled = (await this.createGit().version()).installed;
                    if (!this.gitInstalled) {
                        this.log.warn('Git repository will not be created, as Git is not installed on your system');
                        this.skipGit = true;
                    }
                }
            },
            async initializeMonorepository() {
                if (!this.skipGit && this.jhipsterConfig.monorepository) {
                    await this.initializeGitRepository();
                }
            },
        });
    }
    get [BaseGenerator.INITIALIZING]() {
        return this.delegateTasksToBlueprint(() => this.initializing);
    }
    get writing() {
        return this.asWritingTaskGroup({
            async writeFiles() {
                if (this.skipGit)
                    return undefined;
                await this.writeFiles({ sections: files, context: {} });
            },
        });
    }
    get [BaseGenerator.WRITING]() {
        return this.delegateTasksToBlueprint(() => this.writing);
    }
    get postWriting() {
        return this.asPostWritingTaskGroup({
            async initGitRepo() {
                if (!this.skipGit && !this.jhipsterConfig.monorepository) {
                    await this.initializeGitRepository();
                }
            },
        });
    }
    get [BaseGenerator.POST_WRITING]() {
        return this.delegateTasksToBlueprint(() => this.postWriting);
    }
    get end() {
        return this.asEndTaskGroup({
            async gitCommit() {
                if (this.skipGit)
                    return;
                if (!this.gitInitialized) {
                    this.log.warn('The generated application could not be committed to Git, as a Git repository could not be initialized.');
                    return;
                }
                const commitFiles = async () => {
                    this.debug('Committing files to git');
                    const git = this.createGit();
                    const repositoryRoot = await git.revparse(['--show-toplevel']);
                    const result = await git.log(['-n', '1', '--', '.yo-rc.json']).catch(() => { });
                    if (result && result.total > 0) {
                        this.log.info(`Found .yo-rc.json in Git from ${repositoryRoot}. So we assume this is application regeneration. Therefore automatic Git commit is not done. You can do Git commit manually.`);
                        return;
                    }
                    try {
                        const statusResult = await git.status();
                        if (statusResult.staged.length > 0) {
                            this.log.verboseInfo(`The repository ${repositoryRoot} has staged files, skipping commit.`);
                            return;
                        }
                        let commitMsg = `Initial version of ${this.jhipsterConfig.baseName} generated by generator-begcode@${this.jhipsterConfig.jhipsterVersion}`;
                        if (this.jhipsterConfig.blueprints && this.jhipsterConfig.blueprints.length > 0) {
                            const bpInfo = this.jhipsterConfig.blueprints.map(bp => `${bp.name}@${bp.version}`).join(', ');
                            commitMsg += ` with blueprints ${bpInfo}`;
                        }
                        this.log.ok(`committing to Git from ${repositoryRoot}...`);
                        await git.add(['.']).commit(commitMsg);
                        this.log.ok(`Application successfully committed to Git from ${repositoryRoot}.`);
                    }
                    catch (e) {
                        this.log.warn(chalk.red.bold(`Application commit to Git failed from ${repositoryRoot}. Try to commit manually. (${e.message})`));
                    }
                };
                if (this.env.adapter.queue) {
                    await this.env.adapter.queue(commitFiles);
                }
                else {
                    await commitFiles();
                }
            },
        });
    }
    get [BaseGenerator.END]() {
        return this.delegateTasksToBlueprint(() => this.end);
    }
    async initializeGitRepository() {
        try {
            const git = this.createGit();
            if (await git.checkIsRepo('root')) {
                this.log.info('Using existing git repository.');
            }
            else if (await git.checkIsRepo()) {
                this.log.info('Using existing git repository at parent folder.');
            }
            else if (await git.init()) {
                this.log.ok('Git repository initialized.');
            }
            this.gitInitialized = true;
        }
        catch (error) {
            this.log.warn(`Failed to initialize Git repository.\n ${error}`);
        }
    }
}
