/**
 * Copyright 2013-2026 the original author or authors from the JHipster project.
 *
 * This file is part of the JHipster project, see https://www.jhipster.tech/
 * for more information.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { mutateData, overrideMutateDataProperty } from "../../../../lib/utils/object.js";
import BaseApplicationGenerator from "../../../base-application/index.js";
import { mutateApplicationLoading, mutateApplicationPreparing } from "../../application.js";
import { mutateEntity as languagesMutateEntity } from "../../entity.js";
import { CONTEXT_DATA_SUPPORTED_LANGUAGES } from "../../support/constants.js";
import { supportedLanguages } from "../../support/languages.js";
export default class BootstrapGenerator extends BaseApplicationGenerator {
    async beforeQueue() {
        if (!this.fromBlueprint) {
            await this.composeWithBlueprints();
        }
        supportedLanguages.forEach(lang => {
            this.supportedLanguages.set(lang.languageTag, lang);
        });
        await this.dependsOnBootstrap('base-application');
        await this.dependsOnBootstrap('javascript-simple-application');
    }
    get supportedLanguages() {
        return this.getContextData(CONTEXT_DATA_SUPPORTED_LANGUAGES, { factory: () => new Map() });
    }
    get loading() {
        return this.asLoadingTaskGroup({
            preparing({ applicationDefaults }) {
                applicationDefaults(mutateApplicationLoading, {
                    supportedLanguages: overrideMutateDataProperty(({ supportedLanguages }) => {
                        supportedLanguages.push(...this.supportedLanguages.values());
                        return supportedLanguages;
                    }),
                });
            },
        });
    }
    get [BaseApplicationGenerator.LOADING]() {
        return this.delegateTasksToBlueprint(() => this.loading);
    }
    get preparing() {
        return this.asPreparingTaskGroup({
            preparing({ applicationDefaults }) {
                applicationDefaults(mutateApplicationPreparing);
            },
        });
    }
    get [BaseApplicationGenerator.PREPARING]() {
        return this.delegateTasksToBlueprint(() => this.preparing);
    }
    get preparingEachEntity() {
        return this.asPreparingEachEntityTaskGroup({
            prepareEntity({ application, entity }) {
                mutateData(entity, languagesMutateEntity, {
                    i18nKeyPrefix: data => data.i18nKeyPrefix ?? `${application.frontendAppName}.${data.entityTranslationKey}`,
                    i18nAlertHeaderPrefix: data => (data.i18nAlertHeaderPrefix ?? data.microserviceAppName) ?
                        `${data.microserviceAppName}.${data.entityTranslationKey}`
                        : data.i18nKeyPrefix,
                });
            },
        });
    }
    get [BaseApplicationGenerator.PREPARING_EACH_ENTITY]() {
        return this.delegateTasksToBlueprint(() => this.preparingEachEntity);
    }
    get preparingEachEntityField() {
        return this.asPreparingEachEntityFieldTaskGroup({
            prepareField({ entity, field }) {
                mutateData(field, {
                    __override__: false,
                    propertyTranslationKey: ({ fieldTranslationKey, fieldName }) => fieldTranslationKey ?? `${entity.i18nKeyPrefix}.${fieldName}`,
                    fieldTranslationKey: ({ propertyTranslationKey }) => propertyTranslationKey,
                });
            },
        });
    }
    get [BaseApplicationGenerator.PREPARING_EACH_ENTITY_FIELD]() {
        return this.delegateTasksToBlueprint(() => this.preparingEachEntityField);
    }
    get preparingEachEntityRelationship() {
        return this.asPreparingEachEntityRelationshipTaskGroup({
            prepareRelationship({ entity, relationship }) {
                mutateData(relationship, {
                    __override__: false,
                    propertyTranslationKey: ({ relationshipName }) => `${entity.i18nKeyPrefix}.${relationshipName}`,
                });
            },
        });
    }
    get [BaseApplicationGenerator.PREPARING_EACH_ENTITY_RELATIONSHIP]() {
        return this.delegateTasksToBlueprint(() => this.preparingEachEntityRelationship);
    }
}
