import RelationshipIssue from './issues/relationship-issue.js';
import { rulesNames } from './rules.js';
import { relationshipTypes } from '../jhipster/index.mjs';
let issues;
export default {
    checkRelationships,
};
export function checkRelationships(relationshipDeclarations) {
    if (!relationshipDeclarations || relationshipDeclarations.length === 0) {
        return [];
    }
    issues = [];
    checkForCollapsibleRelationships(relationshipDeclarations);
    return issues;
}
function checkForCollapsibleRelationships(relationshipDeclarations) {
    const sortedRelationships = {
        [relationshipTypes.ONE_TO_ONE]: [],
        [relationshipTypes.ONE_TO_MANY]: [],
        [relationshipTypes.MANY_TO_ONE]: [],
        [relationshipTypes.MANY_TO_MANY]: [],
    };
    relationshipDeclarations.forEach((relationshipDeclaration) => {
        const type = relationshipDeclaration.children.relationshipType[0].children.RELATIONSHIP_TYPE[0].image;
        const from = relationshipDeclaration.children.relationshipBody[0].children.from[0].children.NAME[0].image;
        const to = relationshipDeclaration.children.relationshipBody[0].children.to[0].children.NAME[0].image;
        sortedRelationships[type].push({ from, to });
    });
    Object.keys(sortedRelationships).forEach(type => {
        if (sortedRelationships[type].length > 1) {
            sortedRelationships[type].forEach(relationship => {
                issues.push(new RelationshipIssue({
                    ruleName: rulesNames.REL_INDIVIDUAL_DECL,
                    from: relationship.from,
                    to: relationship.to,
                    type,
                }));
            });
        }
    });
}
