import JDLRelationship from '../../core/models/jdl-relationship.js';
import { lowerFirst } from '../../core/utils/string-utils.js';
import { formatComment } from '../../core/utils/format-utils.js';
import { asJdlRelationshipType } from '../../core/basic-types/relationship-types.js';
export default { convertRelationships };
export function convertRelationships(parsedRelationships, annotationToOptionConverter) {
    if (!parsedRelationships) {
        throw new Error('Relationships have to be passed so as to be converted.');
    }
    return parsedRelationships.map(parsedRelationship => {
        const relationshipConfiguration = {
            from: parsedRelationship.from.name,
            to: parsedRelationship.to.name,
            type: asJdlRelationshipType(parsedRelationship.cardinality),
            injectedFieldInFrom: parsedRelationship.from.injectedField,
            injectedFieldInTo: parsedRelationship.to.injectedField,
            isInjectedFieldInFromRequired: parsedRelationship.from.required,
            isInjectedFieldInToRequired: parsedRelationship.to.required,
            commentInFrom: formatComment(parsedRelationship.from.documentation),
            commentInTo: formatComment(parsedRelationship.to.documentation),
            options: {
                global: annotationToOptionConverter.call(undefined, parsedRelationship.options.global),
                source: annotationToOptionConverter.call(undefined, parsedRelationship.options.source),
                destination: annotationToOptionConverter.call(undefined, parsedRelationship.options.destination),
            },
        };
        if (!relationshipConfiguration.injectedFieldInFrom && !relationshipConfiguration.injectedFieldInTo) {
            relationshipConfiguration.injectedFieldInFrom = lowerFirst(relationshipConfiguration.to);
            relationshipConfiguration.injectedFieldInTo = lowerFirst(relationshipConfiguration.from);
        }
        return new JDLRelationship(relationshipConfiguration);
    });
}
