import BaseApplicationGenerator from '../../../base-application/index.js';
export default class FrontendPluginGenerator extends BaseApplicationGenerator {
    async beforeQueue() {
        if (!this.fromBlueprint) {
            await this.composeWithBlueprints();
        }
        if (!this.delegateToBlueprint) {
            await this.dependsOnBootstrapApplication();
            await this.dependsOnJHipster('maven');
        }
    }
    get initializing() {
        return this.asInitializingTaskGroup({
            async parseCommand() {
                await this.parseCurrentJHipsterCommand();
            },
        });
    }
    get [BaseApplicationGenerator.INITIALIZING]() {
        return this.delegateTasksToBlueprint(() => this.initializing);
    }
    get prompting() {
        return this.asPromptingTaskGroup({
            async promptCommand({ control }) {
                if (control.existingProject && this.options.askAnswered !== true)
                    return;
                await this.promptCurrentJHipsterCommand();
            },
        });
    }
    get [BaseApplicationGenerator.PROMPTING]() {
        return this.delegateTasksToBlueprint(() => this.prompting);
    }
    get loading() {
        return this.asLoadingTaskGroup({
            async loadConfig({ application }) {
                await this.loadCurrentJHipsterCommandConfig(application);
            },
        });
    }
    get [BaseApplicationGenerator.LOADING]() {
        return this.delegateTasksToBlueprint(() => this.loading);
    }
    get postWriting() {
        return this.asPostWritingTaskGroup({
            customize({ application, source }) {
                const { javaDependencies, nodeDependencies, nodeVersion, clientFrameworkAngular, clientFrameworkReact, clientFrameworkVue, microfrontend, srcMainWebapp, } = application;
                const checksumIncludedFiles = [
                    `${srcMainWebapp}**/*.*`,
                    'target/classes/static/**/*.*',
                    'package-lock.json',
                    'package.json',
                    'tsconfig.json',
                ];
                if (clientFrameworkAngular) {
                    checksumIncludedFiles.push('tsconfig.app.json', 'webpack/*.*');
                }
                else if (clientFrameworkReact) {
                    checksumIncludedFiles.push('.postcss.config.js', 'webpack/*.*');
                }
                else if (clientFrameworkVue) {
                    checksumIncludedFiles.push('.postcssrc.js', 'tsconfig.app.json');
                    if (microfrontend) {
                        checksumIncludedFiles.push('webpack/*.*');
                    }
                    else {
                        checksumIncludedFiles.push('vite.config.ts');
                    }
                }
                source.addMavenDefinition({
                    properties: [
                        { property: 'node.version', value: `v${nodeVersion}` },
                        { property: 'npm.version', value: nodeDependencies.npm },
                        { property: 'pnpm.version', value: nodeDependencies.pnpm },
                        {
                            property: 'frontend-maven-plugin.version',
                            value: javaDependencies['frontend-maven-plugin'],
                        },
                        {
                            property: 'checksum-maven-plugin.version',
                            value: javaDependencies['checksum-maven-plugin'],
                        },
                        {
                            property: 'maven-antrun-plugin.version',
                            value: javaDependencies['maven-antrun-plugin'],
                        },
                    ],
                    pluginManagement: [
                        {
                            groupId: 'com.github.eirslett',
                            artifactId: 'frontend-maven-plugin',
                            version: '${frontend-maven-plugin.version}',
                            additionalContent: `<configuration>
    <installDirectory>target</installDirectory>
    <nodeVersion>\${node.version}</nodeVersion>
    <npmVersion>\${npm.version}</npmVersion>
    <pnpmVersion>\${pnpm.version}</pnpmVersion>
</configuration>`,
                        },
                        {
                            groupId: 'net.nicoulaj.maven.plugins',
                            artifactId: 'checksum-maven-plugin',
                            version: '${checksum-maven-plugin.version}',
                        },
                        {
                            groupId: 'org.apache.maven.plugins',
                            artifactId: 'maven-antrun-plugin',
                            version: '${maven-antrun-plugin.version}',
                        },
                    ],
                    plugins: [
                        {
                            inProfile: 'webapp',
                            groupId: 'net.nicoulaj.maven.plugins',
                            artifactId: 'checksum-maven-plugin',
                            additionalContent: `
                  <executions>
                      <execution>
                          <id>create-pre-compiled-webapp-checksum</id>
                          <goals>
                              <goal>files</goal>
                          </goals>
                          <phase>generate-resources</phase>
                      </execution>
                      <execution>
                          <id>create-compiled-webapp-checksum</id>
                          <goals>
                              <goal>files</goal>
                          </goals>
                          <phase>compile</phase>
                          <configuration>
                              <csvSummaryFile>checksums.csv.old</csvSummaryFile>
                          </configuration>
                      </execution>
                  </executions>
                  <configuration>
                      <fileSets>
                          <fileSet>
                              <directory>\${project.basedir}</directory>
                              <includes>${checksumIncludedFiles
                                .map(file => `
                                  <include>${file}</include>`)
                                .join('')}
                              </includes>
                              <excludes>
                                  <exclude>**/app/**/service-worker.js</exclude>
                                  <exclude>**/app/**/vendor.css</exclude>
                              </excludes>
                          </fileSet>
                      </fileSets>
                      <failOnError>false</failOnError>
                      <failIfNoFiles>false</failIfNoFiles>
                      <individualFiles>false</individualFiles>
                      <algorithms>
                          <algorithm>SHA-1</algorithm>
                      </algorithms>
                      <includeRelativePath>true</includeRelativePath>
                      <quiet>true</quiet>
                  </configuration>`,
                        },
                        {
                            inProfile: 'webapp',
                            groupId: 'org.apache.maven.plugins',
                            artifactId: 'maven-antrun-plugin',
                            additionalContent: `
                  <executions>
                      <execution>
                          <id>eval-frontend-checksum</id>
                          <phase>generate-resources</phase>
                          <goals>
                              <goal>run</goal>
                          </goals>
                          <configuration>
                              <target>
                                  <condition property="skip.npm" value="true" else="false" >
                                      <and>
                                          <available file="checksums.csv" filepath="\${project.build.directory}" />
                                          <available file="checksums.csv.old" filepath="\${project.build.directory}" />
                                          <filesmatch file1="\${project.build.directory}/checksums.csv" file2="\${project.build.directory}/checksums.csv.old" />
                                      </and>
                                  </condition>
                              </target>
                              <exportAntProperties>true</exportAntProperties>
                          </configuration>
                      </execution>
                  </executions>
`,
                        },
                        {
                            inProfile: 'webapp',
                            groupId: 'com.github.eirslett',
                            artifactId: 'frontend-maven-plugin',
                            additionalContent: `
                  <executions>
                      <execution>
                          <id>install-node-and-pnpm</id>
                          <goals>
                              <goal>install-node-and-pnpm</goal>
                          </goals>
                      </execution>
                      <execution>
                          <id>pnpm install</id>
                          <goals>
                              <goal>pnpm</goal>
                          </goals>
                          <configuration>
                              <arguments>install</arguments>
                          </configuration>
                      </execution>
                      <execution>
                          <id>webapp build dev</id>
                          <goals>
                              <goal>pnpm</goal>
                          </goals>
                          <phase>generate-resources</phase>
                          <configuration>
                              <arguments>run webapp:build</arguments>
                              <environmentVariables>
                                  <APP_VERSION>\${project.version}</APP_VERSION>
                              </environmentVariables>
                              <pnpmInheritsProxyConfigFromMaven>false</pnpmInheritsProxyConfigFromMaven>
                          </configuration>
                      </execution>
                  </executions>`,
                        },
                        {
                            inProfile: 'prod',
                            groupId: 'com.github.eirslett',
                            artifactId: 'frontend-maven-plugin',
                            additionalContent: `
                  <executions>
                      <execution>
                          <id>install-node-and-pnpm</id>
                          <goals>
                              <goal>install-node-and-pnpm</goal>
                          </goals>
                      </execution>
                      <execution>
                          <id>pnpm install</id>
                          <goals>
                              <goal>pnpm</goal>
                          </goals>
                          <configuration>
                              <arguments>install</arguments>
                          </configuration>
                      </execution>
                      <execution>
                          <id>webapp build test</id>
                          <goals>
                              <goal>pnpm</goal>
                          </goals>
                          <phase>test</phase>
                          <configuration>
                              <arguments>run webapp:test</arguments>
                              <pnpmInheritsProxyConfigFromMaven>false</pnpmInheritsProxyConfigFromMaven>
                          </configuration>
                      </execution>
                      <execution>
                          <id>webapp build prod</id>
                          <goals>
                              <goal>pnpm</goal>
                          </goals>
                          <phase>generate-resources</phase>
                          <configuration>
                              <arguments>run webapp:prod</arguments>
                              <environmentVariables>
                                  <APP_VERSION>\${project.version}</APP_VERSION>
                              </environmentVariables>
                              <pnpmInheritsProxyConfigFromMaven>false</pnpmInheritsProxyConfigFromMaven>
                          </configuration>
                      </execution>
                  </executions>`,
                        },
                    ],
                });
            },
        });
    }
    get [BaseApplicationGenerator.POST_WRITING]() {
        return this.delegateTasksToBlueprint(() => this.postWriting);
    }
}
