import { addOtherRelationship } from '../../base-application/support/index.js';
import { databaseTypes } from '../../../jdl/index.js';
const { NO: NO_DATABASE, SQL, NEO4J } = databaseTypes;
export const addEntitiesOtherRelationships = (entities) => {
    const result = { warning: [] };
    for (const entity of entities.filter(entity => !entity.builtIn)) {
        for (const relationship of entity.relationships ?? []) {
            if (!relationship.otherRelationship &&
                !relationship.otherEntity.embedded &&
                (relationship.otherEntityRelationshipName ||
                    relationship.relationshipType === 'many-to-many' ||
                    (relationship.relationshipType === 'one-to-one' && entity.databaseType === SQL) ||
                    (relationship.relationshipType === 'one-to-many' && entity.databaseType !== NEO4J && entity.databaseType !== NO_DATABASE))) {
                if (relationship.otherEntity.builtIn) {
                    result.warning.push(`Ignoring '${entity.name}' definitions as it is using a built-in Entity '${relationship.otherEntityName}': 'otherEntityRelationshipName' is set with value '${relationship.otherEntityRelationshipName}' at relationship '${relationship.relationshipName}' but no back-reference was found`);
                }
                else {
                    relationship.otherRelationship = addOtherRelationship(entity, relationship.otherEntity, relationship);
                }
            }
        }
    }
    return result;
};
