import { DCROperationTypes, DCRTypes, DataTypes } from '@reltio/mdm-sdk';
import { getAttributeDescription, getDCRId, getDataTypeDefinition, getOperationLabel, getOperationType, getSuggestionsCount, isOpenTask } from './common';
describe('common helpers', function () {
    it('getOperationType behaviour', function () {
        expect(getOperationType(DCRTypes.CREATE_RELATIONSHIP)).toBe(DCROperationTypes.ADDED);
        expect(getOperationType(DCRTypes.INSERT_ATTRIBUTE)).toBe(DCROperationTypes.ADDED);
        expect(getOperationType(DCRTypes.UPDATE_TAGS)).toBe(DCROperationTypes.EDITED);
        expect(getOperationType(DCRTypes.UPDATE_ROLES)).toBe(DCROperationTypes.EDITED);
        expect(getOperationType(DCRTypes.UPDATE_ATTRIBUTE)).toBe(DCROperationTypes.EDITED);
        expect(getOperationType(DCRTypes.DELETE_ATTRIBUTE)).toBe(DCROperationTypes.DELETED);
        expect(getOperationType(DCRTypes.DELETE_RELATIONSHIP)).toBe(DCROperationTypes.DELETED);
        expect(getOperationType(DCRTypes.PIN_ATTRIBUTE, true)).toBe(DCROperationTypes.PINNED);
        expect(getOperationType(DCRTypes.PIN_ATTRIBUTE, false)).toBe(DCROperationTypes.PIN_DELETED);
        expect(getOperationType(DCRTypes.IGNORE_ATTRIBUTE, true)).toBe(DCROperationTypes.IGNORED);
        expect(getOperationType(DCRTypes.IGNORE_ATTRIBUTE, false)).toBe(DCROperationTypes.IGNORE_DELETED);
    });
    it('getOperationLabel behaviour', function () {
        expect(getOperationLabel(DCROperationTypes.ADDED)).toBe('added');
        expect(getOperationLabel(DCROperationTypes.EDITED)).toBe('edited');
        expect(getOperationLabel(DCROperationTypes.DELETED)).toBe('deleted');
        expect(getOperationLabel(DCROperationTypes.PINNED)).toBe('pinned');
        expect(getOperationLabel(DCROperationTypes.PIN_DELETED)).toBe('pin deleted');
        expect(getOperationLabel(DCROperationTypes.IGNORED)).toBe('ignored');
        expect(getOperationLabel(DCROperationTypes.IGNORE_DELETED)).toBe('ignore deleted');
        expect(getOperationLabel('anyType')).toBe('');
    });
    describe('getAttributeDescription tests', function () {
        it('should return undefined parameters for empty arguments', function () {
            var attrType = {};
            var value = {};
            var entityUri = '';
            var objectsInfo = {};
            expect(getAttributeDescription(attrType, value, entityUri, objectsInfo)).toEqual({
                objectUri: undefined,
                objectLabel: undefined,
                entityTypeUri: undefined
            });
        });
        it('should return parameters from relation if it exists', function () {
            var _a;
            var entityUri = 'entityUri1';
            var relationUri = 'relations/YduMTVk';
            var attrType = { referencedEntityTypeURI: 'configuration/entityTypes/HCA' };
            var value = {
                id: 'YduMTVk',
                refObjectURI: relationUri,
                value: { Phone: [{ id: 'uri$$123', newValue: { value: 123 } }] }
            };
            var objectsInfo = (_a = {},
                _a[entityUri] = {
                    isExist: true,
                    label: '12345 - Address Line 1 City',
                    secondaryLabel: '12345',
                    type: 'configuration/entityTypes/HCA'
                },
                _a[relationUri] = {
                    endObjectLabel: 'Address Line 1 City Zip6',
                    endObjectUri: 'entities/brJPdnD',
                    isExist: true,
                    startObjectLabel: '12345 - Address Line 1 City',
                    startObjectUri: entityUri,
                    type: 'configuration/relationTypes/HasAddress'
                },
                _a);
            expect(getAttributeDescription(attrType, value, entityUri, objectsInfo)).toEqual({
                objectUri: objectsInfo[relationUri].endObjectUri,
                objectLabel: objectsInfo[relationUri].endObjectLabel
            });
        });
        it('should return parameters using refEntity', function () {
            var _a;
            var entityUri = 'entityUri1';
            var attrType = { referencedEntityTypeURI: 'configuration/entityTypes/HCA' };
            var value = {
                id: 'YduMTVk',
                refEntity: { objectURI: 'entities/brJPdnD' },
                value: { Phone: [{ newValue: { value: 123 } }] }
            };
            var objectsInfo = (_a = {},
                _a[entityUri] = {
                    isExist: true,
                    label: '12345 - Address Line 1 City',
                    secondaryLabel: '12345',
                    type: 'configuration/entityTypes/HCA'
                },
                _a['entities/brJPdnD'] = {
                    isExist: true,
                    label: 'New title',
                    secondaryLabel: '12345',
                    type: 'configuration/entityTypes/HCA'
                },
                _a);
            var expectedObjectUri = value.refEntity.objectURI;
            expect(getAttributeDescription(attrType, value, entityUri, objectsInfo)).toEqual({
                objectUri: expectedObjectUri,
                objectLabel: objectsInfo[expectedObjectUri].label
            });
        });
    });
    describe('getDataTypeDefinition behaviour', function () {
        it('should set TYPE_URL if attributeValue is url', function () {
            var attributeValue = 'https://s3.amazonaws.com/reltio/source/image.jpg';
            var attributeType = {
                label: 'Full image url (S3)',
                name: 'Url',
                type: DataTypes.TYPE_STRING,
                uri: 'configuration/entityTypes/HCO/attributes/ImageGallery/attributes/Url'
            };
            expect(getDataTypeDefinition(attributeType, attributeValue).type).toBe(DataTypes.TYPE_URL);
        });
        it('should not set TYPE_URL if attributeValue is not url', function () {
            var attributeValue = 'string value';
            var attributeType = {
                label: 'Address Line 1',
                name: 'AddressLine1',
                type: DataTypes.TYPE_STRING,
                uri: 'configuration/entityTypes/Location/attributes/AddressLine1'
            };
            expect(getDataTypeDefinition(attributeType, attributeValue).type).toBe(DataTypes.TYPE_STRING);
        });
        it('should not change type if', function () {
            var attributeValue = 'https://s3.amazonaws.com/reltio/source/image.jpg';
            var attributeType = {
                label: 'Primary',
                name: 'Primary',
                type: DataTypes.TYPE_TEXT,
                uri: 'configuration/relationTypes/HasAddress/attributes/Primary'
            };
            expect(getDataTypeDefinition(attributeType, attributeValue).type).toBe(DataTypes.TYPE_TEXT);
        });
    });
    it('getSuggestionsCount behaviour', function () {
        var changes = [{ label: '1', operation: 'added' }, { label: '2' }, { label: '3', operation: 'deleted' }];
        expect(getSuggestionsCount(changes)).toBe(2);
        expect(getSuggestionsCount([])).toBe(0);
    });
    describe('getDCRId behavior', function () {
        it('should work', function () {
            expect(getDCRId(['entities/123', 'changeRequests/456'])).toBe('456');
            expect(getDCRId(['changeRequests/123', 'relations/456'])).toBe('123');
        });
        it('should not fail if DCR uri is not found', function () {
            expect(getDCRId(['entities/123', 'relations/456'])).toBe(undefined);
        });
    });
    describe('isOpenTask behavior', function () {
        it('should return true is argument is undefined', function () {
            expect(isOpenTask(undefined)).toBe(true);
        });
        it('should return true is argument is true', function () {
            expect(isOpenTask(true)).toBe(true);
        });
        it('should return false in other cases', function () {
            expect(isOpenTask(false)).toBe(false);
        });
    });
});
