var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
import React from 'react';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { CollaborationObjectTypes } from '@reltio/mdm-sdk';
import { MdmModuleProvider } from '../../../contexts/MdmModuleContext';
import { EntitiesMapContext } from '../../../contexts/EntitiesMapContext';
import { CommentTarget } from './CommentTarget';
var defaultMetadata = {
    entityTypes: [
        {
            uri: 'configuration/entityTypes/HCA',
            label: 'HCA',
            attributes: [
                {
                    label: 'Customer Name',
                    name: 'Name',
                    uri: 'configuration/entityTypes/HCA/attributes/Name'
                }
            ]
        }
    ],
    relationTypes: [
        {
            uri: 'configuration/relationTypes/HasAddress',
            label: 'has address'
        },
        {
            uri: 'configuration/relationTypes/HCPtoHCP',
            attributes: [
                {
                    label: 'Affiliation Status',
                    name: 'AffiliationStatus',
                    uri: 'configuration/relationTypes/HCPtoHCP/attributes/AffiliationStatus'
                }
            ]
        }
    ]
};
var defaultEntity = {
    uri: 'entities/13JKIKAz',
    type: 'configuration/entityTypes/HCA',
    attributes: {
        Name: [
            {
                type: 'configuration/entityTypes/HCA/attributes/Name',
                ov: true,
                value: 'Active',
                lookupCode: 'ACTI',
                lookupRawValue: 'ACTI',
                uri: 'entities/13JKIKAz/attributes/Name/2mlWuqDq5'
            },
            {
                type: 'configuration/entityTypes/HCA/attributes/Name',
                ov: false,
                value: 'Regression HCA entity 2',
                uri: 'entities/13JKIKAz/attributes/Name/1eoo71SIL'
            }
        ]
    },
    label: 'Active - 8 תלתן מודיעין-מכבים-רעות'
};
var defaultUiPath = 'https://reltio.com/nui';
var defaultMdmValues = {
    entity: defaultEntity,
    metadata: defaultMetadata,
    uiPath: defaultUiPath
};
var defaultEntitiesMapContextValue = {
    'entities/1N5MpHzN': {
        uri: 'entities/1N5MpHzN',
        type: 'configuration/entityTypes/HCP',
        label: 'ABCD'
    },
    'entities/13JKIKAz': {
        uri: 'entities/13JKIKAz',
        type: 'configuration/entityTypes/HCA',
        label: 'Active - 8 תלתן מודיעין-מכבים-רעות'
    },
    'entities/1Kdf7T4t': {
        uri: 'entities/1Kdf7T4t',
        type: 'configuration/entityTypes/HCA',
        label: 'DCBA'
    }
};
var setUp = function (_a) {
    var props = _a.props, _b = _a.mdmValues, mdmValues = _b === void 0 ? defaultMdmValues : _b, _c = _a.entitiesMapContextValue, entitiesMapContextValue = _c === void 0 ? defaultEntitiesMapContextValue : _c;
    var user = userEvent.setup();
    var Providers = function (_a) {
        var children = _a.children;
        return (React.createElement(EntitiesMapContext.Provider, { value: entitiesMapContextValue },
            React.createElement(MdmModuleProvider, { values: mdmValues }, children)));
    };
    return __assign(__assign({}, render(React.createElement(CommentTarget, __assign({}, props)), { wrapper: Providers })), { user: user });
};
describe('Comment target tests', function () {
    it('should render correctly if objectType is "entity"', function () {
        var comment = {
            objectId: 'entities/13JKIKAz',
            objectType: CollaborationObjectTypes.ENTITY,
            relatedObjectUris: ['entities/13JKIKAz']
        };
        setUp({ props: { comment: comment } });
        screen.getByText('Selected profile:');
        var link = screen.getByText('Active - 8 תלתן מודיעין-מכבים-רעות');
        expect(link).toHaveAttribute('href', 'https://reltio.com/nui#p~com.reltio.plugins.entity.default.DefaultPerspective_e~entities/13JKIKAz');
    });
    it('should render correctly if objectType is "entityAttribute"', function () {
        var comment = {
            objectId: 'entities/13JKIKAz/attributes/Name/2mlWuqDq5',
            objectType: CollaborationObjectTypes.ENTITY_ATTRIBUTE,
            relatedObjectUris: ['entities/13JKIKAz']
        };
        setUp({ props: { comment: comment } });
        screen.getByText('Selected attribute:');
        screen.getByText('Customer Name');
    });
    it('should render correctly if objectType is "relationAttribute"', function () {
        var comment = {
            objectId: 'relations/12rMNliU/attributes/AffiliationStatus/4CM2y5KME',
            objectType: CollaborationObjectTypes.RELATION_ATTRIBUTE,
            relatedObjectUris: [
                'relations/12rMNliU',
                'entities/1Gw3QsjR',
                'entities/1RB4jVTx',
                'configuration/relationTypes/HCPtoHCP'
            ]
        };
        setUp({ props: { comment: comment } });
        screen.getByText('Selected attribute:');
        screen.getByText('Affiliation Status');
    });
    it('should not fail when objectType is "relationAttribute" and relatedObjectUris does not contain relation type', function () {
        var comment = {
            objectId: 'relations/12rMNliU/attributes/AffiliationStatus/4CM2y5KME',
            objectType: CollaborationObjectTypes.RELATION_ATTRIBUTE,
            relatedObjectUris: ['relations/12rMNliU']
        };
        setUp({ props: { comment: comment } });
        screen.getByText('Selected attribute:');
    });
    it('should render correctly if objectType is "entityAttribute" and entityAttribute is not found', function () {
        var comment = {
            objectId: 'entities/13JKIKAAz/attributes/SomethingWrong/2mlWuqDq5',
            objectType: CollaborationObjectTypes.ENTITY_ATTRIBUTE,
            relatedObjectUris: ['entities/13JKIKAz']
        };
        setUp({ props: { comment: comment } });
        var title = screen.getByText('Selected attribute:');
        expect(title.nextElementSibling).toHaveTextContent('');
    });
    it('should render correctly if objectType is "relation" and has entitiesMap', function () {
        var comment = {
            objectId: 'relations/138xt4Lu',
            objectType: CollaborationObjectTypes.RELATION,
            relatedObjectUris: ['entities/13JKIKAz', 'entities/1Kdf7T4t', 'configuration/relationTypes/HasAddress']
        };
        setUp({ props: { comment: comment } });
        screen.getByText('Selected relation:');
        expect(screen.getByText('Active - 8 תלתן מודיעין-מכבים-רעות')).toHaveAttribute('href', 'https://reltio.com/nui#p~com.reltio.plugins.entity.default.DefaultPerspective_e~entities/13JKIKAz');
        screen.getByText('to');
        expect(screen.getByText('DCBA')).toHaveAttribute('href', 'https://reltio.com/nui#p~com.reltio.plugins.entity.default.DefaultPerspective_e~entities/1Kdf7T4t');
    });
    it('should render correctly if objectType is "relation" and entitiesMap is empty', function () {
        var comment = {
            objectId: 'relations/138xt4Lu',
            objectType: CollaborationObjectTypes.RELATION,
            relatedObjectUris: ['entities/13JKIKAz', 'entities/1Kdf7T4t', 'configuration/relationTypes/HasAddress']
        };
        setUp({ props: { comment: comment }, entitiesMapContextValue: {} });
        screen.getByText('Selected relation:');
        expect(screen.getByText('Active - 8 תלתן מודיעין-מכבים-רעות')).toHaveAttribute('href', 'https://reltio.com/nui#p~com.reltio.plugins.entity.default.DefaultPerspective_e~entities/13JKIKAz');
        screen.getByText('to');
        expect(screen.getByText('1Kdf7T4t')).toHaveAttribute('href', 'https://reltio.com/nui#p~com.reltio.plugins.entity.default.DefaultPerspective_e~entities/1Kdf7T4t');
    });
    it('should render correctly if objectType is "potentialMatch" and has entitiesMap', function () {
        var comment = {
            objectId: 'entities/1N5MpHzN-entities/13JKIKAz',
            objectType: CollaborationObjectTypes.POTENTIAL_MATCH,
            relatedObjectUris: ['entities/13JKIKAz', 'entities/1N5MpHzN']
        };
        setUp({ props: { comment: comment } });
        screen.getByText('Selected potential match:');
        expect(screen.getByText('Active - 8 תלתן מודיעין-מכבים-רעות')).toHaveAttribute('href', 'https://reltio.com/nui#p~com.reltio.plugins.entity.default.DefaultPerspective_e~entities/13JKIKAz');
        screen.getByText('and');
        expect(screen.getByText('ABCD')).toHaveAttribute('href', 'https://reltio.com/nui#p~com.reltio.plugins.entity.default.DefaultPerspective_e~entities/1N5MpHzN');
    });
    it('should render correctly if objectType is "potentialMatch" and entitesMap is empty', function () {
        var comment = {
            objectId: 'entities/1N5MpHzN-entities/13JKIKAz',
            objectType: CollaborationObjectTypes.POTENTIAL_MATCH,
            relatedObjectUris: ['entities/13JKIKAz', 'entities/1N5MpHzN']
        };
        setUp({ props: { comment: comment }, entitiesMapContextValue: {} });
        screen.getByText('Selected potential match:');
        expect(screen.getByText('Active - 8 תלתן מודיעין-מכבים-רעות')).toHaveAttribute('href', 'https://reltio.com/nui#p~com.reltio.plugins.entity.default.DefaultPerspective_e~entities/13JKIKAz');
        screen.getByText('and');
        expect(screen.getByText('1N5MpHzN')).toHaveAttribute('href', 'https://reltio.com/nui#p~com.reltio.plugins.entity.default.DefaultPerspective_e~entities/1N5MpHzN');
    });
});
