"use strict";
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);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var ui_i18n_1 = __importDefault(require("ui-i18n"));
var ramda_1 = require("ramda");
var types_1 = require("../types");
var activities_1 = require("./activities");
describe('activities utils tests', function () {
    var activityWithDataTypes = {
        uri: 'activities/711d84f6-f96b-4b93-b06f-a165c1776914',
        user: 'egorshkov',
        timestamp: 1621847085259,
        items: [
            {
                id: '3vJgNLSN',
                user: 'egorshkov',
                method: 'POST',
                url: '/reltio/api/alenat/entities/sOoSQYm/_update',
                clientType: 'Reltio UI',
                timestamp: 1621847085259,
                objectUri: 'relations/edEnRRW',
                objectType: 'configuration/relationTypes/HasAddress',
                startObjectUri: 'entities/sOoSQYm',
                startObjectLabel: '- cBtCASwtAeEbTxe Agacati',
                endObjectUri: 'entities/1BVkicLG',
                endObjectLabel: 'cBtCASwtAeEbTxe Agacati Nash',
                data: {
                    type: 'RELATIONSHIP_CHANGED'
                },
                deltaCollection: {
                    ovChanged: true,
                    delta: [
                        {
                            type: 'ATTRIBUTE_ADDED',
                            attributeType: 'configuration/relationTypes/HasAddress/attributes/AddressType',
                            newValue: {
                                value: 'CLIN',
                                ov: true,
                                id: '3XdTr25Qx',
                                sources: ['Reltio']
                            }
                        }
                    ]
                },
                eventId: '1621847085259_00qpSyx'
            },
            {
                id: '3vJgNPid',
                user: 'egorshkov',
                method: 'POST',
                url: '/reltio/api/alenat/entities/sOoSQYm/_update',
                clientType: 'Reltio UI',
                timestamp: 1621847085259,
                objectUri: 'entities/sOoSQYm',
                objectType: 'configuration/entityTypes/HCP',
                objectLabel: '- cBtCASwtAeEbTxe Agacati',
                data: {
                    type: 'ENTITY_CHANGED'
                },
                deltaCollection: {
                    ovChanged: true,
                    delta: [
                        {
                            type: 'ATTRIBUTE_CHANGED',
                            attributeType: 'configuration/entityTypes/HCP/attributes/Address',
                            newValue: {
                                ov: true,
                                id: 'edEnRRW'
                            },
                            oldValue: {
                                ov: true,
                                id: 'edEnRRW'
                            }
                        },
                        {
                            type: 'ATTRIBUTE_ADDED',
                            attributeType: 'configuration/entityTypes/HCP/attributes/DeepNested/attributes/SecondLevelNested/attributes/FirstLevelNestedSubatr3',
                            newValue: {
                                value: '43',
                                ov: true,
                                id: '3XdTr1wuR',
                                sources: ['Reltio']
                            }
                        },
                        {
                            type: 'ATTRIBUTE_ADDED',
                            attributeType: 'configuration/entityTypes/HCP/attributes/DeepNested/attributes/SecondLevelNested/attributes/FirstLevelNestedSubDouble',
                            newValue: {
                                value: '54',
                                ov: true,
                                id: '3XdTr21Ah',
                                sources: ['Reltio']
                            }
                        },
                        {
                            type: 'ATTRIBUTE_ADDED',
                            attributeType: 'configuration/entityTypes/HCP/attributes/CountryCode',
                            newValue: {
                                value: 'PT',
                                ov: true,
                                id: '3XdTr29hD',
                                sources: ['Reltio']
                            }
                        },
                        {
                            type: 'ATTRIBUTE_ADDED',
                            attributeType: 'configuration/entityTypes/HCP/attributes/AddressNested',
                            newValue: {
                                value: {
                                    AddressLine1: [
                                        {
                                            value: '43',
                                            ov: true,
                                            id: '3XdTr2IDj',
                                            sources: ['Reltio']
                                        }
                                    ],
                                    Country: [
                                        {
                                            value: 'BM',
                                            ov: true,
                                            id: '3XdTr2MTz',
                                            sources: ['Reltio']
                                        }
                                    ]
                                },
                                ov: true,
                                id: '3XdTr2DxT',
                                sources: ['Reltio']
                            }
                        },
                        {
                            type: 'ATTRIBUTE_ADDED',
                            attributeType: 'configuration/entityTypes/HCP/attributes/CallingName',
                            newValue: {
                                value: '5454',
                                ov: true,
                                id: '3XdTr2QkF',
                                sources: ['Reltio']
                            }
                        }
                    ],
                    onSplit: false
                },
                eventId: '1621847085259_00upXzO'
            }
        ],
        itemsTotal: 2
    };
    var activityWithLabel = {
        uri: 'activities/de19-08e1-b220b95c',
        user: 'polina.arsenteva',
        label: 'USER_LOGIN',
        description: 'User polina.arsenteva was logged in',
        timestamp: 1622625987531,
        items: [
            {
                id: '3Gme7xNf',
                user: 'polina.arsenteva',
                method: 'POST',
                url: '/reltio/api/alenat/activities',
                clientType: 'Reltio UI',
                timestamp: 1622625987531
            }
        ],
        itemsTotal: 1
    };
    var activityWithNoItems = {
        uri: 'activities/a68f0fcb-d43d-4650-b5ee-484f0ffdaa99',
        user: 'collaboration-service',
        timestamp: 1621517122815,
        itemsTotal: 0
    };
    describe('getActivityType', function () {
        it('should return first item data type if present', function () {
            expect((0, activities_1.getActivityType)(activityWithDataTypes)).toBe('RELATIONSHIP_CHANGED');
        });
        it('should return label if present', function () {
            expect((0, activities_1.getActivityType)(activityWithLabel)).toBe('USER_LOGIN');
        });
        it('should return undefined otherwise', function () {
            expect((0, activities_1.getActivityType)(activityWithNoItems)).toBe(undefined);
        });
    });
    describe('isValidActivityType', function () {
        it('should return true if activity type is valid', function () {
            expect((0, activities_1.isValidActivityType)(types_1.ActivityTypes.COMMENT_ADDED)).toBe(true);
            expect((0, activities_1.isValidActivityType)(types_1.ActivityTypes.ANALYTICS_ATTRIBUTES_CHANGED)).toBe(true);
            expect((0, activities_1.isValidActivityType)('ENTITY_CHANGED')).toBe(true);
        });
        it('should return false otherwise', function () {
            expect((0, activities_1.isValidActivityType)(undefined)).toBe(false);
            expect((0, activities_1.isValidActivityType)('TestType')).toBe(false);
        });
    });
    describe('splitActivityByType', function () {
        it('should work correctly with activity with data types', function () {
            expect((0, activities_1.splitActivityByType)(activityWithDataTypes)).toEqual([
                {
                    uri: 'activities/711d84f6-f96b-4b93-b06f-a165c1776914',
                    user: 'egorshkov',
                    timestamp: 1621847085259,
                    items: [
                        {
                            id: '3vJgNLSN',
                            user: 'egorshkov',
                            method: 'POST',
                            url: '/reltio/api/alenat/entities/sOoSQYm/_update',
                            clientType: 'Reltio UI',
                            timestamp: 1621847085259,
                            objectUri: 'relations/edEnRRW',
                            objectType: 'configuration/relationTypes/HasAddress',
                            startObjectUri: 'entities/sOoSQYm',
                            startObjectLabel: '- cBtCASwtAeEbTxe Agacati',
                            endObjectUri: 'entities/1BVkicLG',
                            endObjectLabel: 'cBtCASwtAeEbTxe Agacati Nash',
                            data: {
                                type: 'RELATIONSHIP_CHANGED'
                            },
                            deltaCollection: {
                                ovChanged: true,
                                delta: [
                                    {
                                        type: 'ATTRIBUTE_ADDED',
                                        attributeType: 'configuration/relationTypes/HasAddress/attributes/AddressType',
                                        newValue: {
                                            value: 'CLIN',
                                            ov: true,
                                            id: '3XdTr25Qx',
                                            sources: ['Reltio']
                                        }
                                    }
                                ]
                            },
                            eventId: '1621847085259_00qpSyx'
                        }
                    ],
                    itemsTotal: 1
                },
                {
                    uri: 'activities/711d84f6-f96b-4b93-b06f-a165c1776914',
                    user: 'egorshkov',
                    timestamp: 1621847085259,
                    items: [
                        {
                            id: '3vJgNPid',
                            user: 'egorshkov',
                            method: 'POST',
                            url: '/reltio/api/alenat/entities/sOoSQYm/_update',
                            clientType: 'Reltio UI',
                            timestamp: 1621847085259,
                            objectUri: 'entities/sOoSQYm',
                            objectType: 'configuration/entityTypes/HCP',
                            objectLabel: '- cBtCASwtAeEbTxe Agacati',
                            data: {
                                type: 'ENTITY_CHANGED'
                            },
                            deltaCollection: {
                                ovChanged: true,
                                delta: [
                                    {
                                        type: 'ATTRIBUTE_CHANGED',
                                        attributeType: 'configuration/entityTypes/HCP/attributes/Address',
                                        newValue: {
                                            ov: true,
                                            id: 'edEnRRW'
                                        },
                                        oldValue: {
                                            ov: true,
                                            id: 'edEnRRW'
                                        }
                                    },
                                    {
                                        type: 'ATTRIBUTE_ADDED',
                                        attributeType: 'configuration/entityTypes/HCP/attributes/DeepNested/attributes/SecondLevelNested/attributes/FirstLevelNestedSubatr3',
                                        newValue: {
                                            value: '43',
                                            ov: true,
                                            id: '3XdTr1wuR',
                                            sources: ['Reltio']
                                        }
                                    },
                                    {
                                        type: 'ATTRIBUTE_ADDED',
                                        attributeType: 'configuration/entityTypes/HCP/attributes/DeepNested/attributes/SecondLevelNested/attributes/FirstLevelNestedSubDouble',
                                        newValue: {
                                            value: '54',
                                            ov: true,
                                            id: '3XdTr21Ah',
                                            sources: ['Reltio']
                                        }
                                    },
                                    {
                                        type: 'ATTRIBUTE_ADDED',
                                        attributeType: 'configuration/entityTypes/HCP/attributes/CountryCode',
                                        newValue: {
                                            value: 'PT',
                                            ov: true,
                                            id: '3XdTr29hD',
                                            sources: ['Reltio']
                                        }
                                    },
                                    {
                                        type: 'ATTRIBUTE_ADDED',
                                        attributeType: 'configuration/entityTypes/HCP/attributes/AddressNested',
                                        newValue: {
                                            value: {
                                                AddressLine1: [
                                                    {
                                                        value: '43',
                                                        ov: true,
                                                        id: '3XdTr2IDj',
                                                        sources: ['Reltio']
                                                    }
                                                ],
                                                Country: [
                                                    {
                                                        value: 'BM',
                                                        ov: true,
                                                        id: '3XdTr2MTz',
                                                        sources: ['Reltio']
                                                    }
                                                ]
                                            },
                                            ov: true,
                                            id: '3XdTr2DxT',
                                            sources: ['Reltio']
                                        }
                                    },
                                    {
                                        type: 'ATTRIBUTE_ADDED',
                                        attributeType: 'configuration/entityTypes/HCP/attributes/CallingName',
                                        newValue: {
                                            value: '5454',
                                            ov: true,
                                            id: '3XdTr2QkF',
                                            sources: ['Reltio']
                                        }
                                    }
                                ],
                                onSplit: false
                            },
                            eventId: '1621847085259_00upXzO'
                        }
                    ],
                    itemsTotal: 1
                }
            ]);
        });
        it('should work correctly with activity with label', function () {
            expect((0, activities_1.splitActivityByType)(activityWithLabel)).toEqual([activityWithLabel]);
        });
        it('should work correctly with activity with no items', function () {
            expect((0, activities_1.splitActivityByType)(activityWithNoItems)).toEqual([activityWithNoItems]);
        });
    });
    describe('stringifyDeltaValue', function () {
        var i18nDateSpy;
        beforeEach(function () {
            i18nDateSpy = jest.spyOn(ui_i18n_1.default, 'date').mockImplementation(ramda_1.identity);
        });
        afterEach(function () {
            i18nDateSpy.mockReset();
        });
        var metadata = {
            entityTypes: [
                {
                    uri: 'configuration/entityTypes/HCP',
                    attributes: [
                        {
                            uri: 'configuration/entityTypes/HCP/attributes/NestedTest',
                            name: 'NestedTest',
                            type: 'Nested',
                            attributes: [
                                {
                                    uri: 'configuration/entityTypes/HCP/attributes/NestedTest/attributes/TextField',
                                    name: 'TextField',
                                    type: 'String'
                                },
                                {
                                    uri: 'configuration/entityTypes/HCP/attributes/NestedTest/attributes/Boolean',
                                    name: 'Boolean',
                                    type: 'Boolean'
                                },
                                {
                                    uri: 'configuration/entityTypes/HCP/attributes/NestedTest/attributes/Dollar',
                                    name: 'Dollar',
                                    type: 'Dollar'
                                },
                                {
                                    uri: 'configuration/entityTypes/HCP/attributes/NestedTest/attributes/Timestamp',
                                    name: 'Timestamp',
                                    type: 'Timestamp'
                                },
                                {
                                    uri: 'configuration/entityTypes/HCP/attributes/NestedTest/attributes/NestedTestL2',
                                    name: 'NestedTestL2',
                                    type: 'Nested',
                                    attributes: [
                                        {
                                            uri: 'configuration/entityTypes/HCP/attributes/NestedTest/attributes/NestedTestL2/attributes/StringL2',
                                            name: 'StringL2',
                                            type: 'String'
                                        },
                                        {
                                            uri: 'configuration/entityTypes/HCP/attributes/NestedTest/attributes/NestedTestL2/attributes/DateL2',
                                            name: 'DateL2',
                                            type: 'Date'
                                        },
                                        {
                                            uri: 'configuration/entityTypes/HCP/attributes/NestedTest/attributes/NestedTestL2/attributes/IntL2',
                                            name: 'IntL2',
                                            type: 'Int'
                                        }
                                    ]
                                }
                            ]
                        },
                        {
                            uri: 'configuration/entityTypes/HCP/attributes/Double',
                            name: 'Double',
                            type: 'Double'
                        },
                        {
                            uri: 'configuration/entityTypes/HCP/attributes/CountryCode',
                            name: 'CountryCode',
                            type: 'String'
                        },
                        {
                            uri: 'configuration/entityTypes/HCP/attributes/Timestamp',
                            name: 'Timestamp',
                            type: 'Timestamp'
                        },
                        {
                            uri: 'configuration/entityTypes/HCP/attributes/Address',
                            name: 'Address',
                            type: 'Reference'
                        }
                    ]
                }
            ]
        };
        it('should return empty string when value is empty', function () {
            var attrTypeUri = 'configuration/entityTypes/HCP/attributes/Address';
            expect((0, activities_1.stringifyDeltaValue)(undefined, attrTypeUri, metadata)).toBe('');
            expect((0, activities_1.stringifyDeltaValue)(null, attrTypeUri, metadata)).toBe('');
        });
        it('should return same string when value is string and attribute does not exists in metadata', function () {
            var attrTypeUri = 'configuration/entityTypes/HCP/attributes/Name';
            expect((0, activities_1.stringifyDeltaValue)('some string', attrTypeUri, metadata)).toBe('some string');
            expect((0, activities_1.stringifyDeltaValue)('', attrTypeUri, metadata)).toBe('');
        });
        it('should correct format value if value is string and attribute exists in metadata', function () {
            expect((0, activities_1.stringifyDeltaValue)('2020-12-01T15:12:00.000+0000', 'configuration/entityTypes/HCP/attributes/Timestamp', metadata)).toBe('2020-12-01T15:12:00.000+0000');
            expect((0, activities_1.stringifyDeltaValue)('5000000', 'configuration/entityTypes/HCP/attributes/Double', metadata)).toBe('5,000,000');
            expect((0, activities_1.stringifyDeltaValue)('UK', 'configuration/entityTypes/HCP/attributes/CountryCode', metadata)).toBe('UK');
        });
        it('should correct format value with attribute presentation', function () {
            var attributesPresentation = {
                'configuration/entityTypes/HCP/attributes/Double': {
                    thousandSeparator: '-',
                    decimalSeparator: '^',
                    prefix: '<',
                    suffix: '>'
                }
            };
            expect((0, activities_1.stringifyDeltaValue)('1235.000001', 'configuration/entityTypes/HCP/attributes/Double', metadata, attributesPresentation)).toBe('<1-235^000001>');
            expect((0, activities_1.stringifyDeltaValue)('5000000.123', 'configuration/entityTypes/HCP/attributes/Number', metadata, attributesPresentation)).toBe('5000000.123');
        });
        it('should work correctly for nested attribute', function () {
            var attributeTypeUri = 'configuration/entityTypes/HCP/attributes/NestedTest';
            expect((0, activities_1.stringifyDeltaValue)({
                NestedTestL2: [
                    {
                        value: {
                            DateL2: [
                                {
                                    value: '2022-12-02',
                                    ov: true,
                                    id: '5hZytaP4Y',
                                    sources: ['Reltio']
                                }
                            ],
                            IntL2: [
                                {
                                    value: '12345',
                                    ov: true,
                                    id: '5hZytaKoI',
                                    sources: ['Reltio']
                                }
                            ],
                            StringL2: [
                                {
                                    value: 'String 2',
                                    ov: true,
                                    id: '5hZytaGY2',
                                    sources: ['Reltio']
                                }
                            ]
                        },
                        ov: true,
                        id: '5hZytaCHm',
                        sources: ['Reltio']
                    }
                ],
                Dollar: [
                    {
                        value: '5000',
                        ov: true,
                        id: '5hZyta3lG',
                        sources: ['Reltio']
                    }
                ],
                TextField: [
                    {
                        value: 'String',
                        ov: true,
                        id: '5hZytZvEk',
                        sources: ['Reltio']
                    }
                ],
                Boolean: [
                    {
                        value: 'true',
                        ov: true,
                        id: '5hZytZzV0',
                        sources: ['Reltio']
                    }
                ],
                Timestamp: [
                    {
                        value: '2020-12-01T15:12:00.000+0000',
                        ov: true,
                        id: '5hZyta81W',
                        sources: ['Reltio']
                    }
                ]
            }, attributeTypeUri, metadata)).toBe('2022-12-02, 12,345, String 2, $5,000, String, Yes, 2020-12-01T15:12:00.000+0000');
        });
    });
    describe('removeIgnoredItems', function () {
        var consoleWarnMock;
        beforeAll(function () {
            consoleWarnMock = jest.spyOn(console, 'warn').mockImplementationOnce(function () { });
        });
        it('should remove not implemented activities', function () {
            var activities = {
                uri: 'activities/711d84f6-f96b-4b93-b06f-a165c1776914',
                user: 'osilina',
                timestamp: 1663335811251,
                items: [
                    {
                        id: '3vJgNLSN',
                        user: 'osilina',
                        method: 'POST',
                        url: '/reltio/api/alenat/entities',
                        clientType: 'Reltio UI',
                        timestamp: 1663335811251,
                        objectUri: 'entities/1dNhxGCu',
                        objectType: 'configuration/entityTypes/HCP',
                        data: {
                            type: 'NOT_IMPLEMENTED'
                        },
                        eventId: '1663335811251_0QtJ71h'
                    },
                    {
                        id: '3vJgNPid',
                        user: 'osilina',
                        method: 'POST',
                        url: '/reltio/api/alenat/entities',
                        clientType: 'Reltio UI',
                        timestamp: 1663335811251,
                        objectUri: 'relations/191OlOId',
                        objectType: 'configuration/entityTypes/HCP',
                        objectLabel: '- cBtCASwtAeEbTxe Agacati',
                        data: {
                            type: 'RELATIONSHIP_CHANGED'
                        },
                        eventId: '1663335811251_0QhaC9C',
                        startObjectLabel: 'BulkUpdate_regression_3',
                        startObjectUri: 'entities/x3Emh0i',
                        endObjectLabel: 'new1 new1 - 123w',
                        endObjectUri: 'entities/1dNhxGCu'
                    }
                ],
                itemsTotal: 2
            };
            expect((0, activities_1.removeIgnoredItems)(activities)).toEqual(__assign(__assign({}, activities), { items: [activities.items[1]], itemsTotal: 1 }));
            expect(consoleWarnMock).toHaveBeenCalled();
        });
    });
});
