import { getFKConstraintName } from '../../server/support/index.js';
import { mutateData } from '../../base/support/index.js';
function relationshipBaseDataEquals(relationshipA, relationshipB) {
    return (relationshipA.relationshipName === relationshipB.relationshipName &&
        relationshipA.relationshipType === relationshipB.relationshipType &&
        relationshipA.entityName === relationshipB.entityName &&
        relationshipA.otherEntityName === relationshipB.otherEntityName);
}
export function relationshipEquals(relationshipA, relationshipB) {
    return (relationshipBaseDataEquals(relationshipA, relationshipB) &&
        relationshipA.options?.onDelete === relationshipB.options?.onDelete &&
        relationshipA.options?.onUpdate === relationshipB.options?.onUpdate);
}
export function relationshipNeedsForeignKeyRecreationOnly(relationshipA, relationshipB) {
    return (relationshipBaseDataEquals(relationshipA, relationshipB) &&
        (relationshipA.options?.onDelete !== relationshipB.options?.onDelete ||
            relationshipA.options?.onUpdate !== relationshipB.options?.onUpdate));
}
export function prepareRelationshipForLiquibase(entity, relationship) {
    relationship.shouldWriteRelationship =
        relationship.relationshipType === 'many-to-one' || (relationship.relationshipType === 'one-to-one' && relationship.ownerSide === true);
    if (relationship.shouldWriteJoinTable) {
        const joinTableName = relationship.joinTable.name;
        const prodDatabaseType = entity.prodDatabaseType;
        mutateData(relationship.joinTable, {
            constraintName: getFKConstraintName(joinTableName, entity.entityTableName, { prodDatabaseType }).value,
            otherConstraintName: getFKConstraintName(joinTableName, relationship.columnName, { prodDatabaseType }).value,
        });
    }
    mutateData(relationship, {
        __override__: false,
        columnDataType: data => data.otherEntity.columnType,
        columnRequired: data => data.nullable === false || data.relationshipRequired,
        liquibaseGenerateFakeData: data => data.columnRequired && data.persistableRelationship && !data.collection,
    });
    return relationship;
}
