/**
 * 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 { readFileSync } from 'node:fs';
import { join } from 'node:path';
import { upperFirst } from 'lodash-es';
import { YO_RC_FILE } from "../constants/yeoman.js";
export const YO_RC_CONFIG_KEY = 'generator-jhipster';
export const mergeYoRcContent = (oldConfig, newConfig) => {
    const merged = { [YO_RC_CONFIG_KEY]: {} };
    for (const ns of new Set([...Object.keys(oldConfig), ...Object.keys(newConfig)])) {
        merged[ns] = { ...oldConfig[ns], ...newConfig[ns] };
    }
    if (oldConfig[YO_RC_CONFIG_KEY]?.creationTimestamp) {
        merged[YO_RC_CONFIG_KEY].creationTimestamp = oldConfig[YO_RC_CONFIG_KEY].creationTimestamp;
    }
    return merged;
};
export const readEntityFile = (applicationPath, entity) => {
    const entityFile = join(applicationPath, '.jhipster', `${upperFirst(entity)}.json`);
    try {
        return JSON.parse(readFileSync(entityFile, 'utf-8'));
    }
    catch (error) {
        throw new Error(`Error reading ${entityFile} file: ${error.message}`, { cause: error });
    }
};
export const readYoRcFile = (yoRcPath = '.') => {
    const yoRcFile = yoRcPath.endsWith(YO_RC_FILE) ? yoRcPath : join(yoRcPath, YO_RC_FILE);
    try {
        return JSON.parse(readFileSync(yoRcFile, 'utf-8'));
    }
    catch (error) {
        throw new Error(`Error reading ${yoRcFile} file: ${error.message}`, { cause: error });
    }
};
export const readCurrentPathYoRcFile = () => {
    try {
        return readYoRcFile(process.cwd());
    }
    catch {
        return undefined;
    }
};
