import { isFileStateModified } from 'mem-fs-editor/state';
import { CheckRepoActions } from 'simple-git';
import BaseApplicationGenerator from '../base-application/index.js';
import { JHIPSTER_DOCUMENTATION_ARCHIVE_PATH, JHIPSTER_DOCUMENTATION_URL, MAIN_DIR, SERVER_MAIN_RES_DIR, TEST_DIR, } from '../generator-constants.js';
import { clientFrameworkTypes } from '../../lib/jhipster/index.js';
import { GENERATOR_COMMON, GENERATOR_GIT } from '../generator-list.js';
import { createPrettierTransform } from '../bootstrap/support/prettier-support.js';
import { loadStoredAppOptions } from '../app/support/index.js';
import command from './command.js';
import { writeFiles, writeJdlsFiles } from './files.js';
const { REACT, ANGULAR } = clientFrameworkTypes;
export default class CommonGenerator extends BaseApplicationGenerator {
    command = command;
    async beforeQueue() {
        loadStoredAppOptions.call(this);
        if (!this.fromBlueprint) {
            await this.composeWithBlueprints();
        }
        if (!this.delegateToBlueprint) {
            await this.dependsOnBootstrapApplication();
            await this.dependsOnJHipster(GENERATOR_GIT);
        }
    }
    get configuring() {
        return this.asConfiguringTaskGroup({
            async configureMonorepository() {
                if (this.jhipsterConfig.monorepository)
                    return;
                const git = this.createGit();
                if ((await git.checkIsRepo()) && !(await git.checkIsRepo(CheckRepoActions.IS_REPO_ROOT))) {
                    this.jhipsterConfig.monorepository = true;
                }
            },
            configureCommitHook() {
                if (this.jhipsterConfig.monorepository) {
                    this.jhipsterConfig.skipCommitHook = true;
                }
            },
        });
    }
    get [BaseApplicationGenerator.CONFIGURING]() {
        return this.delegateTasksToBlueprint(() => this.configuring);
    }
    get composing() {
        return this.asComposingTaskGroup({
            async composing() {
                await this.composeWithJHipster('begcode:javascript:prettier');
                if (!this.jhipsterConfig.skipCommitHook) {
                    await this.composeWithJHipster('begcode:javascript:husky');
                }
            },
        });
    }
    get [BaseApplicationGenerator.COMPOSING]() {
        return this.delegateTasksToBlueprint(() => this.composing);
    }
    get configuringEachEntity() {
        return this.asConfiguringEachEntityTaskGroup({
            migrateEntity({ entityConfig, entityStorage }) {
                for (const field of entityConfig.fields) {
                    if (field.javadoc) {
                        field.documentation ??= field.javadoc;
                        delete field.javadoc;
                    }
                    if (field.fieldTypeJavadoc) {
                        field.fieldTypeDocumentation ??= field.fieldTypeJavadoc;
                        delete field.fieldTypeJavadoc;
                    }
                }
                for (const relationship of entityConfig.relationships) {
                    if (relationship.javadoc) {
                        relationship.documentation = relationship.javadoc;
                        delete relationship.javadoc;
                    }
                }
                if (entityConfig.javadoc) {
                    entityConfig.documentation = entityConfig.javadoc;
                    delete entityConfig.javadoc;
                }
                else {
                    entityStorage.save();
                }
            },
        });
    }
    get [BaseApplicationGenerator.CONFIGURING_EACH_ENTITY]() {
        return this.delegateTasksToBlueprint(() => this.configuringEachEntity);
    }
    get loading() {
        return this.asLoadingTaskGroup({
            loadPackageJson({ application }) {
                this.loadNodeDependenciesFromPackageJson(application.nodeDependencies, this.fetchFromInstalledJHipster(GENERATOR_COMMON, 'resources', 'package.json'));
            },
            loadConfig({ applicationDefaults }) {
                applicationDefaults({
                    prettierTabWidth: this.jhipsterConfig.prettierTabWidth ?? 2,
                });
            },
        });
    }
    get [BaseApplicationGenerator.LOADING]() {
        return this.delegateTasksToBlueprint(() => this.loading);
    }
    get preparing() {
        return this.asPreparingTaskGroup({
            checkSuffix({ application }) {
                if (application.entitySuffix === application.dtoSuffix) {
                    throw new Error('Entities cannot be generated as the entity suffix and DTO suffix are equals!');
                }
            },
            setupConstants({ applicationDefaults }) {
                applicationDefaults({
                    MAIN_DIR,
                    TEST_DIR,
                    SERVER_MAIN_RES_DIR,
                    ANGULAR,
                    REACT,
                    DOCUMENTATION_URL: JHIPSTER_DOCUMENTATION_URL,
                    DOCUMENTATION_ARCHIVE_PATH: JHIPSTER_DOCUMENTATION_ARCHIVE_PATH,
                });
            },
        });
    }
    get [BaseApplicationGenerator.PREPARING]() {
        return this.delegateTasksToBlueprint(() => this.preparing);
    }
    get default() {
        return this.asDefaultTaskGroup({
            async formatSonarProperties() {
                this.queueTransformStream({
                    name: 'prettifying sonar-project.properties',
                    filter: file => isFileStateModified(file) && file.path.startsWith(this.destinationPath()) && file.path.endsWith('sonar-project.properties'),
                    refresh: false,
                }, await createPrettierTransform.call(this, { extensions: 'properties', prettierProperties: true }));
            },
        });
    }
    get [BaseApplicationGenerator.DEFAULT]() {
        return this.delegateTasksToBlueprint(() => this.default);
    }
    get writing() {
        return this.asWritingTaskGroup({
            async cleanup({ application, control }) {
                if (application.onlyLowcode)
                    return;
                await control.cleanupFiles({
                    '7.1.1': [[!application.skipCommitHook, '.huskyrc']],
                    '7.6.1': [[application.skipClient, 'npmw', 'npmw.cmd']],
                    '8.0.0-rc.2': [[!application.skipCommitHook, '.lintstagedrc.js']],
                });
            },
            ...writeFiles(),
            ...writeJdlsFiles(),
        });
    }
    get [BaseApplicationGenerator.WRITING]() {
        return this.delegateTasksToBlueprint(() => this.writing);
    }
    get postWriting() {
        return this.asPostWritingTaskGroup({
            setConfig({ application }) {
                if (application.onlyLowcode)
                    return;
                const packageJsonConfigStorage = this.packageJson.createStorage('config').createProxy();
                if (application.defaultEnvironment) {
                    packageJsonConfigStorage.default_environment = application.defaultEnvironment;
                }
            },
            addJHipsterDependencies({ application }) {
                if (application.skipJhipsterDependencies || application.onlyLowcode)
                    return;
                this.packageJson.merge({
                    devDependencies: {
                        'generator-begcode': application.jhipsterVersion,
                        ...Object.fromEntries(application.blueprints.map(blueprint => [blueprint.name, blueprint.version])),
                    },
                });
            },
        });
    }
    get [BaseApplicationGenerator.POST_WRITING]() {
        return this.delegateTasksToBlueprint(() => this.postWriting);
    }
}
