import { getValidationErrors } from './validation';
import { getMetadata } from './metadata.test-data';
import { getChanges, getChangesWithBooleanPrimitiveValue, getEntitiesOrRelations, getUserChanges, getUserChangesRejectSubnestedAttributes, getUserChangesRejectSubnestedRelationAttributes, getUserChangesSubnestedEmpty, getUserChangesNewEntity } from './validation.test-data';
import { findAttributeTypeByUri } from '@reltio/mdm-sdk';
describe('getValidationErrors', function () {
    it('should validate required attributes with user changes', function () {
        var metadata = getMetadata();
        var result = getValidationErrors({
            changes: getChanges(),
            userChanges: getUserChanges(),
            entitiesOrRelations: getEntitiesOrRelations(),
            metadata: metadata
        });
        var firstNameAttributeType = findAttributeTypeByUri(metadata, 'configuration/entityTypes/HCP/attributes/FirstName');
        var prescriberIndicatorAttributeType = findAttributeTypeByUri(metadata, 'configuration/entityTypes/HCP/attributes/Boolean');
        var addressAttributeType = findAttributeTypeByUri(metadata, 'configuration/entityTypes/HCP/attributes/Address');
        var internalPostCodeAttributeType = findAttributeTypeByUri(metadata, 'configuration/relationTypes/HasAddress/attributes/InternalPostCode');
        var primarySpecialityIndAttributeType = findAttributeTypeByUri(metadata, 'configuration/entityTypes/HCP/attributes/Specialities/attributes/PrimarySpecialityInd');
        expect(result).toEqual([
            {
                error: {
                    type: 'missed',
                    attributeType: firstNameAttributeType,
                    attributeTypeUri: 'configuration/entityTypes/HCP/attributes/FirstName',
                    parentUri: 'FirstName/2QHFlPPP',
                    uri: 'entityUri1/0SBmZIW/newValue',
                    message: 'Attribute is required',
                    source: 'local',
                    path: [
                        expect.objectContaining({
                            label: '<No label>',
                            type: 'attributeValue',
                            value: expect.objectContaining({
                                uri: 'entities/entityUri1/attributes/FirstName/2QHFlPPP'
                            }),
                            valueType: firstNameAttributeType
                        })
                    ]
                },
                lineIds: ['entityUri1/0SBmZIW/newValue']
            },
            {
                error: {
                    type: 'missed',
                    attributeType: prescriberIndicatorAttributeType,
                    attributeTypeUri: 'configuration/entityTypes/HCP/attributes/Boolean',
                    parentUri: 'Boolean/2DEYFcfk',
                    uri: 'entityUri1/0SBmys4/newValue',
                    message: 'Attribute is required',
                    source: 'local',
                    path: [
                        expect.objectContaining({
                            label: '<No label>',
                            type: 'attributeValue',
                            value: expect.objectContaining({
                                uri: 'entities/entityUri1/attributes/Boolean/2DEYFcfk'
                            }),
                            valueType: prescriberIndicatorAttributeType
                        })
                    ]
                },
                lineIds: ['entityUri1/0SBmys4/newValue']
            },
            {
                error: {
                    type: 'missed',
                    attributeType: internalPostCodeAttributeType,
                    attributeTypeUri: 'configuration/entityTypes/HCP/attributes/Address/attributes/InternalPostCode',
                    parentUri: 'InternalPostCode',
                    uri: 'entityUri1/0SBmubo/newValue',
                    message: 'Attribute is required',
                    source: 'local',
                    path: [
                        expect.objectContaining({
                            label: expect.any(String),
                            type: 'attributeValue',
                            value: expect.objectContaining({
                                uri: 'entities/entityUri1/attributes/Address/0HfPK9G'
                            }),
                            valueType: addressAttributeType
                        }),
                        expect.objectContaining({
                            label: '<No label>',
                            type: 'attributeValue',
                            value: expect.objectContaining({
                                uri: 'entities/entityUri1/attributes/Address/0HfPK9G/InternalPostCode/2GMCNw4E'
                            }),
                            valueType: internalPostCodeAttributeType
                        })
                    ]
                },
                lineIds: ['entityUri1/0SBmubo/newValue']
            },
            {
                error: {
                    type: 'missed',
                    attributeType: primarySpecialityIndAttributeType,
                    attributeTypeUri: 'configuration/entityTypes/HCP/attributes/Specialities/attributes/PrimarySpecialityInd',
                    parentUri: undefined,
                    uri: 'entityUri1/0SBn7Oa/newValue/value/PrimarySpecialityInd/0',
                    message: 'Attribute is required',
                    source: 'local',
                    path: []
                },
                lineIds: ['entityUri1/0SBn7Oa/newValue/value/PrimarySpecialityInd/0']
            }
        ]);
    });
    it('should validate required attributes with subnested user changes', function () {
        var metadata = getMetadata();
        var result = getValidationErrors({
            changes: getChanges(),
            userChanges: getUserChangesRejectSubnestedAttributes(),
            entitiesOrRelations: getEntitiesOrRelations(),
            metadata: metadata
        });
        var specialitiesAttributeType = findAttributeTypeByUri(metadata, 'configuration/entityTypes/HCP/attributes/Specialities');
        var primarySpecialityIndAttributeType = findAttributeTypeByUri(metadata, 'configuration/entityTypes/HCP/attributes/Specialities/attributes/PrimarySpecialityInd');
        expect(result).toEqual([
            {
                error: {
                    type: 'missed',
                    attributeType: primarySpecialityIndAttributeType,
                    attributeTypeUri: 'configuration/entityTypes/HCP/attributes/Specialities/attributes/PrimarySpecialityInd',
                    parentUri: undefined,
                    uri: 'entityUri1/0SBn7Oa/newValue/value/PrimarySpecialityInd/0',
                    message: 'Attribute is required',
                    source: 'local',
                    path: []
                },
                lineIds: ['entityUri1/0SBn7Oa/newValue/value/PrimarySpecialityInd/0']
            },
            {
                error: {
                    type: 'missed',
                    attributeType: specialitiesAttributeType,
                    attributeTypeUri: 'configuration/entityTypes/HCP/attributes/Specialities',
                    parentUri: undefined,
                    uri: 'entityUri1/0SBn7Oa/newValue',
                    message: 'Attribute is required',
                    source: 'local',
                    path: []
                },
                lineIds: ['entityUri1/0SBn7Oa/newValue']
            }
        ]);
    });
    it('should validate required attributes with subnested relation user changes', function () {
        var metadata = getMetadata();
        var result = getValidationErrors({
            changes: getChanges(),
            userChanges: getUserChangesRejectSubnestedRelationAttributes(),
            entitiesOrRelations: getEntitiesOrRelations(),
            metadata: metadata
        });
        var refFirstLevelNestedAttributeType = findAttributeTypeByUri(metadata, 'configuration/relationTypes/HCPtoHCA/attributes/NestedAttr');
        expect(result).toEqual([
            {
                error: {
                    type: 'missed',
                    attributeType: refFirstLevelNestedAttributeType,
                    attributeTypeUri: 'configuration/relationTypes/HCPtoHCA/attributes/NestedAttr',
                    parentUri: undefined,
                    uri: '0pSTmm8/0MCLpVw/newValue/attributes/NestedAttr/0',
                    message: 'Attribute is required',
                    source: 'local',
                    path: []
                },
                lineIds: ['0pSTmm8/0MCLpVw/newValue/attributes/NestedAttr/0']
            }
        ]);
    });
    it('should validate required subnested attribute when value is empty', function () {
        var metadata = getMetadata();
        var result = getValidationErrors({
            changes: getChanges(),
            userChanges: getUserChangesSubnestedEmpty(),
            entitiesOrRelations: getEntitiesOrRelations(),
            metadata: metadata
        });
        var nestedAttributeType = findAttributeTypeByUri(metadata, 'configuration/entityTypes/HCP/attributes/NestedAttr');
        var intAttributeType = findAttributeTypeByUri(metadata, 'configuration/entityTypes/HCP/attributes/NestedAttr/attributes/Int');
        expect(result).toEqual([
            {
                error: {
                    type: 'missed',
                    attributeType: intAttributeType,
                    attributeTypeUri: 'configuration/entityTypes/HCP/attributes/NestedAttr/attributes/Int',
                    parentUri: 'NestedAttr/2BNyTheu',
                    uri: 'entityUri1/0IEmjjX/newValue',
                    message: 'Attribute is required',
                    source: 'local',
                    path: [
                        expect.objectContaining({
                            label: '<No label>',
                            type: 'attributeValue',
                            value: expect.objectContaining({
                                uri: 'entities/entityUri1/attributes/NestedAttr/2BNyTheu'
                            }),
                            valueType: nestedAttributeType
                        }),
                        expect.objectContaining({
                            label: '<No label>',
                            type: 'attributeValue',
                            value: expect.objectContaining({
                                uri: 'entities/entityUri1/attributes/NestedAttr/2BNyTheu/Int/2BNyTlvA'
                            }),
                            valueType: intAttributeType
                        })
                    ]
                },
                lineIds: ['entityUri1/0IEmjjX/newValue']
            }
        ]);
    });
    it('should not report a missing required boolean attribute when its value is primitive false', function () {
        var metadata = getMetadata();
        var result = getValidationErrors({
            changes: getChangesWithBooleanPrimitiveValue(),
            userChanges: [],
            entitiesOrRelations: getEntitiesOrRelations(),
            metadata: metadata
        });
        expect(result).toEqual([]);
    });
    it('should validate required attributes in new entity', function () {
        var metadata = getMetadata();
        var result = getValidationErrors({
            changes: getChanges(),
            userChanges: getUserChangesNewEntity(),
            entitiesOrRelations: getEntitiesOrRelations(),
            metadata: metadata
        });
        var nameAttributeType = findAttributeTypeByUri(metadata, 'configuration/entityTypes/HCO/attributes/Name');
        var emailAttributeType = findAttributeTypeByUri(metadata, 'configuration/entityTypes/HCO/attributes/ContactInfo/attributes/Email');
        expect(result).toEqual([
            {
                error: {
                    type: 'missed',
                    attributeType: nameAttributeType,
                    attributeTypeUri: 'configuration/entityTypes/HCO/attributes/Name',
                    parentUri: undefined,
                    uri: 'entityUri2/0IEiwoe/newValue/attributes/Name/0',
                    message: 'Attribute is required',
                    source: 'local',
                    path: []
                },
                lineIds: ['entityUri2/0IEiwoe/newValue/attributes/Name/0']
            },
            {
                error: {
                    type: 'missed',
                    attributeType: emailAttributeType,
                    attributeTypeUri: 'configuration/entityTypes/HCO/attributes/ContactInfo/attributes/Email',
                    parentUri: undefined,
                    uri: 'entityUri2/0IEiwoe/newValue/attributes/ContactInfo/0/value/Email/0',
                    message: 'Attribute is required',
                    source: 'local',
                    path: []
                },
                lineIds: ['entityUri2/0IEiwoe/newValue/attributes/ContactInfo/0/value/Email/0']
            }
        ]);
    });
});
