export function hasPivotRelationship(property) {
    return (property.relationship?.type === 'many-to-many' &&
        property.relationship.pivot !== undefined);
}
export function extractPivotFromProperty(property) {
    if (!hasPivotRelationship(property)) {
        return null;
    }
    const relationship = property.relationship;
    const pivotName = relationship.pivot.moduleName;
    return {
        pivotName,
        relationship,
    };
}
export function separatePropertiesAndPivots(properties) {
    const regularProperties = [];
    const pivotProperties = [];
    for (const prop of properties) {
        if (hasPivotRelationship(prop)) {
            pivotProperties.push(prop);
        }
        else {
            regularProperties.push(prop);
        }
    }
    return { pivotProperties, regularProperties };
}
/**
 * Strips pivot data from a many-to-many property so it can appear in the main
 * properties section without duplicating the pivot definition.
 */
export function createPropertyWithoutPivot(property) {
    if (!hasPivotRelationship(property)) {
        return property;
    }
    const simplified = {
        ...property,
    };
    if (simplified.relationship) {
        simplified.relationship = {
            aggregateName: simplified.relationship.aggregateName,
            avoidConstraint: simplified.relationship.avoidConstraint,
            modulePath: simplified.relationship.modulePath,
            singularName: simplified.relationship.singularName,
            type: simplified.relationship.type,
        };
    }
    return simplified;
}
export function formatRelationshipType(type) {
    if (!type)
        return '';
    const typeMap = {
        'many-to-many': 'N:M',
        'many-to-one': 'N:1',
        'one-to-many': '1:N',
        'one-to-one': '1:1',
    };
    return typeMap[type] || type;
}
export function parseRelationshipType(display) {
    const typeMap = {
        '1:1': 'one-to-one',
        '1:N': 'one-to-many',
        'many-to-many': 'many-to-many',
        'many-to-one': 'many-to-one',
        'N:1': 'many-to-one',
        'N:M': 'many-to-many',
        'one-to-many': 'one-to-many',
        'one-to-one': 'one-to-one',
    };
    return typeMap[display];
}
export function collectUniquePivots(properties) {
    const pivots = new Map();
    for (const prop of properties) {
        if (hasPivotRelationship(prop)) {
            const pivotName = prop.relationship.pivot.moduleName;
            if (!pivots.has(pivotName)) {
                pivots.set(pivotName, prop);
            }
        }
    }
    return pivots;
}
