import applicationTypes from '../../../jhipster/application-types.js';
import { readEntityFile } from '../../../utils/yo-rc.js';
let configuration = {};
export default function exportEntities(passedConfiguration) {
    init(passedConfiguration);
    if (configuration.entities.length === 0) {
        return configuration.entities;
    }
    const subFolder = passedConfiguration.application.forSeveralApplications ? configuration.application.name : '';
    configuration.entities = updateEntities(subFolder);
    if (shouldFilterOutEntitiesBasedOnMicroservice()) {
        configuration.entities = filterOutEntitiesByMicroservice();
    }
    return configuration.entities;
}
function init(passedConfiguration) {
    if (!passedConfiguration?.entities) {
        throw new Error('Entities have to be passed to be exported.');
    }
    configuration = passedConfiguration;
}
function updateEntities(applicationPath) {
    return configuration.entities.map((entity) => updateEntityToGenerateWithExistingOne(applicationPath, entity));
}
function updateEntityToGenerateWithExistingOne(applicationPath, entity) {
    try {
        const fileOnDisk = readEntityFile(applicationPath, entity.name);
        if (!entity.annotations?.changelogDate && fileOnDisk?.annotations?.changelogDate) {
            entity.annotations = entity.annotations || {};
            entity.annotations.changelogDate = fileOnDisk.annotations.changelogDate;
            return { ...fileOnDisk, ...entity };
        }
    }
    catch {
    }
    return entity;
}
function shouldFilterOutEntitiesBasedOnMicroservice() {
    return (configuration.application.type && configuration.application.type === applicationTypes.MICROSERVICE && configuration.application.name);
}
function filterOutEntitiesByMicroservice() {
    return configuration.entities.filter(entity => !(entity.microserviceName && entity.microserviceName.toLowerCase() !== configuration.application.name.toLowerCase()));
}
