import { readFileSync } from 'fs';
import { join } from 'path';
import { upperFirst } from '../jdl/core/utils/string-utils.js';
export const YO_RC_CONFIG_KEY = 'generator-begcode';
export const YO_RC_FILE = '.yo-rc.json';
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;
    }
};
