/**
 * 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 { join } from 'node:path';
import { defaults } from 'lodash-es';
import { deploymentOptions } from "../../lib/jhipster/index.js";
import { removeFieldsWithNullishValues } from "../../lib/utils/object.js";
import BaseGenerator from "../base/index.js";
import { CONTEXT_DATA_APPLICATION_KEY } from "../base-simple-application/support/index.js";
import { CUSTOM_PRIORITIES, PRIORITY_NAMES } from "./priorities.js";
import { CONTEXT_DATA_DEPLOYMENT_KEY, CONTEXT_DATA_WORKSPACES_APPLICATIONS_KEY, CONTEXT_DATA_WORKSPACES_ROOT_KEY, } from "./support/index.js";
const { Options: DeploymentOptions } = deploymentOptions;
const { PROMPTING_WORKSPACES, CONFIGURING_WORKSPACES, LOADING_WORKSPACES, PREPARING_WORKSPACES, DEFAULT, WRITING, POST_WRITING, PRE_CONFLICTS, INSTALL, END, } = PRIORITY_NAMES;
/**
 * This is the base class for a generator that generates entities.
 */
export default class BaseWorkspacesGenerator extends BaseGenerator {
    static PROMPTING_WORKSPACES = BaseGenerator.asPriority(PROMPTING_WORKSPACES);
    static CONFIGURING_WORKSPACES = BaseGenerator.asPriority(CONFIGURING_WORKSPACES);
    static LOADING_WORKSPACES = BaseGenerator.asPriority(LOADING_WORKSPACES);
    static PREPARING_WORKSPACES = BaseGenerator.asPriority(PREPARING_WORKSPACES);
    constructor(args, options, features) {
        super(args, options, {
            skipLoadCommand: false,
            ...features,
        });
        if (this.options.help) {
            return;
        }
        this.registerPriorities(CUSTOM_PRIORITIES);
    }
    get jhipsterConfigWithDefaults() {
        return defaults({}, removeFieldsWithNullishValues(this.config.getAll()), DeploymentOptions.defaults(this.jhipsterConfig.deploymentType));
    }
    get context() {
        return this.getContextData(CONTEXT_DATA_DEPLOYMENT_KEY, { factory: () => ({}) });
    }
    get appsFolders() {
        return this.jhipsterConfigWithDefaults.appsFolders;
    }
    get directoryPath() {
        return this.jhipsterConfigWithDefaults.directoryPath;
    }
    get #applications() {
        return this.getContextData(CONTEXT_DATA_WORKSPACES_APPLICATIONS_KEY, {
            factory: () => Object.entries(this.resolveApplicationFolders()).map(([appFolder, resolvedFolder], index) => {
                const contextMap = this.env.getContextMap(resolvedFolder);
                const application = contextMap.get(CONTEXT_DATA_APPLICATION_KEY);
                if (!application) {
                    throw new Error(`No application found in ${resolvedFolder}`);
                }
                application.appFolder = appFolder;
                application.composePort = 8080 + index;
                return application;
            }),
        });
    }
    get workspacesRoot() {
        return this.getContextData(CONTEXT_DATA_WORKSPACES_ROOT_KEY);
    }
    get promptingWorkspaces() {
        return {};
    }
    get configuringWorkspaces() {
        return {};
    }
    get loadingWorkspaces() {
        return {};
    }
    get preparingWorkspaces() {
        return {};
    }
    workspacePath(...dest) {
        return join(this.workspacesRoot, ...dest);
    }
    resolveApplicationFolders({ appsFolders = this.appsFolders } = {}) {
        return Object.fromEntries(appsFolders.map(appFolder => [appFolder, this.workspacePath(appFolder)]));
    }
    setWorkspacesRoot(root) {
        const oldValue = this.getContextData(CONTEXT_DATA_WORKSPACES_ROOT_KEY, { replacement: root });
        if (oldValue) {
            throw new Error(`Workspaces root is already set to ${oldValue}. Cannot change it to ${root}.`);
        }
    }
    async bootstrapApplications() {
        const resolvedApplicationFolders = this.resolveApplicationFolders();
        for (const [_appFolder, resolvedFolder] of Object.entries(resolvedApplicationFolders)) {
            await this.composeWithJHipster('jhipster:app:bootstrap', {
                generatorOptions: { destinationRoot: resolvedFolder, reproducible: true },
            });
        }
    }
    getArgsForPriority(priorityName) {
        const args = super.getArgsForPriority(priorityName);
        if (![
            PROMPTING_WORKSPACES,
            CONFIGURING_WORKSPACES,
            LOADING_WORKSPACES,
            PREPARING_WORKSPACES,
            DEFAULT,
            WRITING,
            POST_WRITING,
            PRE_CONFLICTS,
            INSTALL,
            END,
        ].includes(priorityName)) {
            return args;
        }
        const [first, ...others] = args ?? [];
        return [
            {
                ...first,
                deployment: this.context,
                applications: this.#applications,
            },
            ...others,
        ];
    }
    /**
     * Utility method to get typed objects for autocomplete.
     */
    asPromptingWorkspacesTaskGroup(taskGroup) {
        return taskGroup;
    }
    /**
     * Utility method to get typed objects for autocomplete.
     */
    asConfiguringWorkspacesTaskGroup(taskGroup) {
        return taskGroup;
    }
    /**
     * Utility method to get typed objects for autocomplete.
     */
    asLoadingWorkspacesTaskGroup(taskGroup) {
        return taskGroup;
    }
    /**
     * Utility method to get typed objects for autocomplete.
     */
    asPreparingWorkspacesTaskGroup(taskGroup) {
        return taskGroup;
    }
}
export class CommandBaseWorkspacesGenerator extends BaseWorkspacesGenerator {
}
