import React from 'react';
import { render, screen } from '@testing-library/react';
import { MatchRulesBlock } from './MatchRulesBlock';
var mockTransitiveMatchRules = jest.fn();
var mockSimpleMatchRulesBuilder = jest.fn();
jest.mock('../TransitiveMatchBlock', function () { return ({
    TransitiveMatchBlock: function (props) {
        mockTransitiveMatchRules(props);
        return React.createElement("div", null);
    }
}); });
jest.mock('../SimpleMatchRulesBuilder', function () { return ({
    SimpleMatchRulesBuilder: function (props) {
        mockSimpleMatchRulesBuilder(props);
        return React.createElement("div", null);
    }
}); });
describe('MatchRulesBlock test', function () {
    var entity = {
        uri: 'entities/mnGj6Kn',
        type: 'configuration/entityTypes/HCP',
        label: 'Entity1',
        matchRules: [
            'configuration/entityTypes/HCP/matchGroups/HCPSameCountryCode',
            'configuration/entityTypes/HCP/matchGroups/HCPMLMatchGroup'
        ]
    };
    var metadata = {
        entityTypes: [
            {
                uri: 'configuration/entityTypes/HCP',
                label: 'HCP',
                matchGroups: [
                    {
                        uri: 'configuration/entityTypes/HCP/matchGroups/HCPSameName',
                        label: 'HCP Same Name'
                    },
                    {
                        uri: 'configuration/entityTypes/HCP/matchGroups/HCPSameCountryCode',
                        label: 'HCP Same Country Code'
                    },
                    {
                        label: 'MLmatchRule1',
                        scope: 'INTERNAL',
                        uri: 'configuration/entityTypes/HCP/matchGroups/HCPMLMatchGroup',
                        matchMethod: 'match_iq'
                    },
                    {
                        uri: 'configuration/entityTypes/HCA/matchGroups/HCAByLanguageCodeNegative',
                        label: 'HCA not match (not same LanguageCode)',
                        negativeRule: {
                            notExactSame: 'configuration/entityTypes/HCA/attributes/LanguageCode'
                        }
                    }
                ]
            }
        ]
    };
    var potentialMatches = [
        {
            matchRules: ['configuration/entityTypes/HCP/matchGroups/HCPSameCountryCode'],
            object: {
                uri: 'entities/mnGj6Kn',
                type: 'configuration/entityTypes/HCP'
            },
            shortestTransitiveMatchPath: [
                {
                    matchRules: [
                        'configuration/entityTypes/HCP/matchGroups/HCPSameCountryCode',
                        'configuration/entityTypes/HCP/matchGroups/HCPMLMatchGroup'
                    ],
                    match: 'entities/mnGg4nR'
                }
            ],
            direct: false
        },
        {
            matchRules: ['configuration/entityTypes/HCP/matchGroups/HCPMLMatchGroup'],
            object: {
                uri: 'entities/mnGg4nR',
                label: 'Entity2',
                type: 'configuration/entityTypes/HCP'
            }
        },
        {
            matchRules: ['NOT_MATCH'],
            object: {
                uri: 'entities/mnGj6K1',
                type: 'configuration/entityTypes/HCP'
            }
        },
        {
            matchRules: ['configuration/entityTypes/HCP/matchGroups/HCPSameCountryCode'],
            object: {
                uri: 'entities/mnGj6Kn',
                type: 'configuration/entityTypes/HCP'
            },
            shortestTransitiveMatchPath: [
                {
                    matchRules: [
                        'configuration/entityTypes/HCP/matchGroups/HCPSameCountryCode',
                        'configuration/entityTypes/HCP/matchGroups/HCPMLMatchGroup'
                    ],
                    match: 'entities/mnGg4nR',
                    negativeRules: ['configuration/entityTypes/HCA/matchGroups/HCAByLanguageCodeNegative']
                }
            ],
            direct: false,
            negativeRules: ['configuration/entityTypes/HCA/matchGroups/HCAByLanguageCodeNegative']
        },
        {
            matchRules: ['configuration/entityTypes/HCP/matchGroups/HCPMLMatchGroup'],
            object: {
                uri: 'entities/mnGg4nR',
                label: 'Entity2',
                type: 'configuration/entityTypes/HCP'
            },
            negativeRules: ['configuration/entityTypes/HCA/matchGroups/HCAByLanguageCodeNegative']
        }
    ];
    var entitiesMap = {
        'entities/mnGg4nR': {
            uri: 'entities/mnGg4nR',
            label: 'match entity',
            type: 'configuration/entityTypes/HCP'
        }
    };
    beforeEach(function () {
        jest.resetAllMocks();
    });
    it('should render TransitiveMatchBlock for transitive match', function () {
        render(React.createElement(MatchRulesBlock, { metadata: metadata, entity: entity, match: potentialMatches[0], entitiesMap: entitiesMap }));
        expect(mockTransitiveMatchRules).toHaveBeenCalledWith({
            transitiveMatchRules: [
                {
                    label: entitiesMap[potentialMatches[0].shortestTransitiveMatchPath[0].match].label,
                    matchRules: [metadata.entityTypes[0].matchGroups[1], metadata.entityTypes[0].matchGroups[2]]
                }
            ]
        });
        expect(mockSimpleMatchRulesBuilder).not.toHaveBeenCalled();
    });
    it('should render TransitiveMatchBlock for transitive match and work correctly with negativeRules', function () {
        render(React.createElement(MatchRulesBlock, { metadata: metadata, entity: entity, match: potentialMatches[3], entitiesMap: entitiesMap }));
        expect(mockTransitiveMatchRules).toHaveBeenCalledWith({
            transitiveMatchRules: [
                {
                    label: entitiesMap[potentialMatches[0].shortestTransitiveMatchPath[0].match].label,
                    matchRules: [
                        metadata.entityTypes[0].matchGroups[1],
                        metadata.entityTypes[0].matchGroups[2],
                        metadata.entityTypes[0].matchGroups[3]
                    ]
                }
            ]
        });
        expect(mockSimpleMatchRulesBuilder).not.toHaveBeenCalled();
    });
    it('should render SimpleMatchRulesBuilder for non transitive match', function () {
        render(React.createElement(MatchRulesBlock, { match: potentialMatches[1], metadata: metadata, entity: entity, entitiesMap: {} }));
        expect(mockTransitiveMatchRules).not.toHaveBeenCalled();
        expect(mockSimpleMatchRulesBuilder).toHaveBeenCalledWith(expect.objectContaining({
            matchRules: [metadata.entityTypes[0].matchGroups[2]]
        }));
    });
    it('should render SimpleMatchRulesBuilder for non transitive match and work correctly with negativeRules', function () {
        render(React.createElement(MatchRulesBlock, { match: potentialMatches[4], metadata: metadata, entity: entity, entitiesMap: {} }));
        expect(mockTransitiveMatchRules).not.toHaveBeenCalled();
        expect(mockSimpleMatchRulesBuilder).toHaveBeenCalledWith(expect.objectContaining({
            matchRules: [metadata.entityTypes[0].matchGroups[2], metadata.entityTypes[0].matchGroups[3]]
        }));
    });
    it('should render Not match for not matches', function () {
        render(React.createElement(MatchRulesBlock, { match: potentialMatches[2], metadata: metadata, entity: entity, entitiesMap: {} }));
        expect(mockTransitiveMatchRules).not.toHaveBeenCalled();
        expect(mockSimpleMatchRulesBuilder).not.toHaveBeenCalled();
        expect(screen.getByText('Not a match')).toBeInTheDocument();
    });
});
