import { relationshipTypes } from '../basic-types/index.js';
import { relationshipTypeExists } from '../basic-types/relationship-types.js';
export default class JDLRelationships {
    relationships;
    constructor() {
        this.relationships = {
            OneToOne: new Map(),
            OneToMany: new Map(),
            ManyToOne: new Map(),
            ManyToMany: new Map(),
        };
    }
    add(relationship) {
        if (!relationship) {
            throw new Error('A relationship must be passed so as to be added.');
        }
        this.relationships[relationship.type].set(relationship.getId(), relationship);
    }
    getOneToOne(relationshipId) {
        return this.get(relationshipTypes.ONE_TO_ONE, relationshipId);
    }
    getOneToMany(relationshipId) {
        return this.get(relationshipTypes.ONE_TO_MANY, relationshipId);
    }
    getManyToOne(relationshipId) {
        return this.get(relationshipTypes.MANY_TO_ONE, relationshipId);
    }
    getManyToMany(relationshipId) {
        return this.get(relationshipTypes.MANY_TO_MANY, relationshipId);
    }
    get(type, relationshipId) {
        if (!relationshipTypeExists(type)) {
            throw new Error(`A valid relationship type must be passed so as to retrieve the relationship, got '${type}'.`);
        }
        if (!relationshipId) {
            throw new Error('A relationship id must be passed so as to retrieve the relationship.');
        }
        return this.relationships[type].get(relationshipId);
    }
    oneToOneQuantity() {
        return this.relationships.OneToOne.size;
    }
    oneToManyQuantity() {
        return this.relationships.OneToMany.size;
    }
    manyToOneQuantity() {
        return this.relationships.ManyToOne.size;
    }
    manyToManyQuantity() {
        return this.relationships.ManyToMany.size;
    }
    size() {
        return this.oneToOneQuantity() + this.oneToManyQuantity() + this.manyToOneQuantity() + this.manyToManyQuantity();
    }
    forEach(passedFunction) {
        if (!passedFunction) {
            return;
        }
        this.toArray().forEach(jdlRelationship => {
            passedFunction(jdlRelationship);
        });
    }
    toArray() {
        const relationships = [];
        Object.keys(this.relationships).forEach(type => {
            this.relationships[type].forEach(relationship => {
                relationships.push(relationship);
            });
        });
        return relationships;
    }
    toString() {
        if (this.size() === 0) {
            return '';
        }
        return Object.entries(this.relationships)
            .map(([key, relationships]) => (relationships.size > 0 ? relationshipTypeToString(relationships, key) : undefined))
            .filter(Boolean)
            .join('\n');
    }
}
function relationshipTypeToString(relationships, type) {
    const relationshipsLines = [...relationships.values()].map(relationship => relationship.toString().split('\n').slice(1, -1)).flat();
    return `relationship ${type} {
${relationshipsLines.join('\n')}
}`;
}
