export function merge(object1, object2) {
    if (!object1 || Object.keys(object1).length === 0) {
        return object2;
    }
    if (!object2 || Object.keys(object2).length === 0) {
        return object1;
    }
    return {
        ...object1,
        ...object2,
    };
}
function removeEntriesWithUndefinedValue(entity) {
    Object.keys(entity).forEach(key => {
        if (entity[key] === undefined) {
            delete entity[key];
        }
    });
}
export function areEntitiesEqual(firstEntity, secondEntity) {
    removeEntriesWithUndefinedValue(firstEntity);
    removeEntriesWithUndefinedValue(secondEntity);
    if (firstEntity.fields.length !== secondEntity.fields.length ||
        firstEntity.relationships.length !== secondEntity.relationships.length ||
        firstEntity.documentation !== secondEntity.documentation ||
        firstEntity.entityTableName !== secondEntity.entityTableName ||
        Object.keys(firstEntity).length !== Object.keys(secondEntity).length) {
        return false;
    }
    return (areFieldsEqual(firstEntity.fields, secondEntity.fields) &&
        areRelationshipsEqual(firstEntity.relationships, secondEntity.relationships) &&
        areOptionsTheSame(firstEntity, secondEntity));
}
function areFieldsEqual(firstFields, secondFields) {
    return firstFields.every((field, index) => {
        if (Object.keys(field).length !== Object.keys(secondFields[index]).length) {
            return false;
        }
        const secondEntityField = secondFields[index];
        return Object.keys(field).every(key => {
            if (field[key].constructor === Array) {
                return field[key].toString() === secondEntityField[key].toString();
            }
            return field[key] === secondEntityField[key];
        });
    });
}
function areRelationshipsEqual(firstRelationships, secondRelationships) {
    return firstRelationships.every((relationship, index) => {
        if (Object.keys(relationship).length !== Object.keys(secondRelationships[index]).length) {
            return false;
        }
        const secondEntityRelationship = secondRelationships[index];
        return Object.keys(relationship).every(key => relationship[key] === secondEntityRelationship[key]);
    });
}
function areOptionsTheSame(firstEntity, secondEntity) {
    if (firstEntity.jpaMetamodelFiltering === 'no') {
        firstEntity.jpaMetamodelFiltering = false;
    }
    if (secondEntity.jpaMetamodelFiltering === 'no') {
        secondEntity.jpaMetamodelFiltering = false;
    }
    return (firstEntity.dto === secondEntity.dto &&
        firstEntity.pagination === secondEntity.pagination &&
        firstEntity.service === secondEntity.service &&
        firstEntity.searchEngine === secondEntity.searchEngine &&
        firstEntity.jpaMetamodelFiltering === secondEntity.jpaMetamodelFiltering);
}
