/**
 * 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 { buildMutateDataForProperty } from "../utils/derived-property.js";
export const getCommandDerivedPropertyMutations = (configs, options = {}) => {
    const scopes = options.scopes ?? ['storage', 'blueprint', 'context'];
    const scopeConfigs = Object.entries(configs).filter(([_key, def]) => scopes.includes(def.scope));
    const mutations = {
        __override__: false,
    };
    for (const [key, def] of scopeConfigs) {
        if (def.choices) {
            const array = def.internal?.type === Array;
            const anyCheck = array ? undefined : (value, choices) => (typeof value !== 'string' || value === 'no' ? false : choices.includes(value));
            const choiceValues = def.choices.map(choice => (typeof choice === 'object' ? choice.value : choice));
            Object.assign(mutations, buildMutateDataForProperty(key, choiceValues, { array, anyCheck }));
            if (def.internal?.alias) {
                Object.assign(mutations, { [def.internal.alias]: (context) => context[key] }, buildMutateDataForProperty(key, choiceValues, { array, anyCheck, prefix: def.internal.alias }));
            }
        }
    }
    return mutations;
};
export const getCommandDefaultMutations = (configs, options = {}) => {
    const scopes = options.scopes ?? ['storage', 'blueprint', 'context'];
    const scopeConfigs = Object.entries(configs).filter(([_key, def]) => scopes.includes(def.scope));
    const mutations = {
        __override__: false,
    };
    for (const [key, def] of scopeConfigs) {
        mutations[key] = (context, { delayMarker }) => {
            if (delayMarker) {
                return delayMarker;
            }
            return typeof def.default === 'function' ? def.default(context) : def.default;
        };
    }
    return mutations;
};
export const getCommandBlueprintLoadingMutations = (blueprintGenerator, configs) => {
    const blueprintScopedConfigs = Object.entries(configs).filter(([_key, def]) => def.scope === 'blueprint');
    const getValue = (key) => blueprintGenerator.blueprintStorage.get(key) ?? undefined;
    const mutations = {
        __override__: false,
    };
    for (const [key] of blueprintScopedConfigs) {
        mutations[key] = () => getValue(key);
    }
    return mutations;
};
