"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var moment_1 = __importDefault(require("moment"));
var filters_1 = require("./filters");
var types_1 = require("../types");
describe('filter utils tests', function () {
    describe('buildActivitiesFilterString', function () {
        it('should return empty string for empty filter', function () {
            expect((0, filters_1.buildActivitiesFilterString)(null)).toBe("(not equals(user, 'collaboration-service'))");
            expect((0, filters_1.buildActivitiesFilterString)({})).toBe("(not equals(user, 'collaboration-service'))");
            expect((0, filters_1.buildActivitiesFilterString)({
                users: [],
                activities: [],
                attributes: [],
                sources: [],
                dateRange: null,
                entityUri: null
            })).toBe("(not equals(user, 'collaboration-service'))");
        });
        it('should build users filter correctly', function () {
            var filter = {
                users: ['polina.arsenteva', 'carina.alabanza', "lev.sok'olov"]
            };
            var expectedResult = "(equals(user, 'polina.arsenteva') or equals(user, 'carina.alabanza') or equals(user, 'lev.sok\\'olov')) and (not equals(user, 'collaboration-service'))";
            expect((0, filters_1.buildActivitiesFilterString)(filter)).toEqual(expectedResult);
        });
        it('should build activities filter correctly', function () {
            var filter = {
                activities: [
                    types_1.ActivityTypes.USER_SEARCH,
                    types_1.ActivityTypes.COMMENT_ADDED,
                    types_1.ActivityTypes.SFDC_CONNECTOR_SYNCHRONIZATION_ISSUE,
                    types_1.ActivityTypes.ENTITY_CHANGED,
                    types_1.ActivityTypes.ATTRIBUTE_UNMASKED
                ]
            };
            var expectedResult = "(startsWith(label, 'USER_SEARCH')" +
                " or startsWith(label, 'COMMENT_ADDED')" +
                " or startsWith(label, 'SFDC_CONNECTOR_SYNCHRONIZATION_ISSUE')" +
                ' or equals(items.data.type, ENTITY_CHANGED)' +
                " or startsWith(label, 'ATTRIBUTE_UNMASKED'))" +
                " and (not equals(user, 'collaboration-service'))";
            expect((0, filters_1.buildActivitiesFilterString)(filter)).toEqual(expectedResult);
        });
        it('should build entityUri filter correctly', function () {
            var filter = {
                entityUri: 'entity/01234'
            };
            var expectedResult = "(equals(items.objectUri, 'entity/01234')" +
                " or equals(items.startObjectUri, 'entity/01234')" +
                " or equals(items.endObjectUri, 'entity/01234'))" +
                " and (not equals(user, 'collaboration-service'))";
            expect((0, filters_1.buildActivitiesFilterString)(filter)).toEqual(expectedResult);
        });
        it('should build attributes filter correctly', function () {
            var filter = {
                attributes: [
                    {
                        label: 'Activity',
                        value: 'configuration/entityTypes/HCP/attributes/activitywith',
                        attrType: {
                            label: 'Activity',
                            name: 'activitywith',
                            description: 'Activity',
                            type: 'Reference',
                            hidden: false,
                            important: false,
                            system: false,
                            searchable: true,
                            relationshipLabelPattern: '{Name}',
                            uri: 'configuration/entityTypes/HCP/attributes/activitywith'
                        },
                        chipLabel: 'Activity',
                        level: 0
                    },
                    {
                        label: 'ActivityTest',
                        value: 'configuration/entityTypes/HCP/attributes/activitywithTest',
                        attrType: {
                            label: 'ActivityTest',
                            name: 'activitywithTest',
                            description: 'Activity',
                            type: 'Reference',
                            hidden: false,
                            important: false,
                            system: false,
                            searchable: true,
                            relationshipLabelPattern: '{Name}',
                            uri: 'configuration/entityTypes/HCP/attributes/activitywithTest'
                        },
                        chipLabel: 'Activity',
                        level: 0
                    }
                ]
            };
            var expectedResult = "(equals(items.delta.attributeType, 'configuration/entityTypes/HCP/attributes/activitywith')" +
                " or equals(items.delta.attributeType, 'configuration/entityTypes/HCP/attributes/activitywithTest'))" +
                " and (not equals(user, 'collaboration-service'))";
            expect((0, filters_1.buildActivitiesFilterString)(filter)).toEqual(expectedResult);
        });
        it('should build entityTypes filter correctly', function () {
            var filter = {
                entityTypes: [
                    {
                        uri: 'configuration/entityTypes/HCP',
                        label: 'HCP',
                        attributes: []
                    },
                    {
                        uri: 'configuration/entityTypes/HCA',
                        label: 'HCA',
                        attributes: []
                    }
                ]
            };
            var expectedResult = "(equals(items.objectType, 'configuration/entityTypes/HCP') or equals(items.objectType, 'configuration/entityTypes/HCA')) and (not equals(user, 'collaboration-service'))";
            expect((0, filters_1.buildActivitiesFilterString)(filter)).toEqual(expectedResult);
        });
        it('should build sources filter correctly', function () {
            var filter = {
                sources: [
                    {
                        value: 'Reltio',
                        uri: 'configuration/sources/Reltio',
                        label: 'Reltio',
                        icon: 'images/source/reltio.png',
                        abbreviation: 'Reltio'
                    },
                    {
                        value: 'Test',
                        uri: 'configuration/sources/Test',
                        label: 'Test label',
                        icon: 'images/source/test.png',
                        abbreviation: 'TestAbbr'
                    }
                ]
            };
            var expectedResult = "(equals(items.delta.sources, 'Reltio') or equals(items.delta.sources, 'TestAbbr')) and (not equals(user, 'collaboration-service'))";
            expect((0, filters_1.buildActivitiesFilterString)(filter)).toEqual(expectedResult);
        });
        it('should build date range within filter correctly', function () {
            var filter = {
                dateRange: {
                    type: 'within',
                    period: [4, 'months']
                }
            };
            var expectedResult = expect.stringMatching(/^\(gte\(timestamp, [0-9]+\)\) and \(not equals\(user, 'collaboration-service'\)\)$/);
            expect((0, filters_1.buildActivitiesFilterString)(filter)).toEqual(expectedResult);
        });
        it('should build date range ago filter correctly', function () {
            var filter = {
                dateRange: {
                    type: 'ago',
                    period: [4, 'months']
                }
            };
            var expectedResult = expect.stringMatching(/^\(lte\(timestamp, [0-9]+\)\) and \(not equals\(user, 'collaboration-service'\)\)$/);
            expect((0, filters_1.buildActivitiesFilterString)(filter)).toEqual(expectedResult);
        });
        it('should build date range between filter correctly', function () {
            var filter = {
                dateRange: {
                    type: 'between',
                    period: [new Date(123), new Date(456)]
                }
            };
            var expectedResult = "(range(timestamp, 123, ".concat((0, moment_1.default)(filter.dateRange.period[1])
                .endOf('date')
                .valueOf(), ")) and (not equals(user, 'collaboration-service'))");
            expect((0, filters_1.buildActivitiesFilterString)(filter)).toEqual(expectedResult);
        });
        it('should return empty string for invalid dateRange', function () {
            var filter = {
                dateRange: {
                    type: 'between',
                    period: [null, null]
                }
            };
            expect((0, filters_1.buildActivitiesFilterString)(filter)).toBe("(not equals(user, 'collaboration-service'))");
        });
        it('should build all filters correctly', function () {
            var filter = {
                users: ['polina.arsenteva'],
                activities: [types_1.ActivityTypes.ENTITY_CHANGED],
                attributes: [
                    {
                        label: 'Activity',
                        value: 'configuration/entityTypes/HCP/attributes/activitywith',
                        attrType: {
                            label: 'Activity',
                            name: 'activitywith',
                            description: 'Activity',
                            type: 'Reference',
                            hidden: false,
                            important: false,
                            system: false,
                            searchable: true,
                            relationshipLabelPattern: '{Name}',
                            uri: 'configuration/entityTypes/HCP/attributes/activitywith'
                        },
                        chipLabel: 'Activity',
                        level: 0
                    }
                ],
                entityTypes: [
                    {
                        uri: 'configuration/entityTypes/HCP',
                        label: 'HCP',
                        attributes: []
                    }
                ],
                sources: [
                    {
                        value: 'Reltio',
                        uri: 'configuration/sources/Reltio',
                        label: 'Reltio',
                        icon: 'images/source/reltio.png',
                        abbreviation: 'Reltio'
                    }
                ],
                entityUri: 'entity/01234',
                dateRange: {
                    type: 'between',
                    period: [new Date(123), new Date(456)]
                }
            };
            var expectedResult = "(equals(user, 'polina.arsenteva'))" +
                ' and (equals(items.data.type, ENTITY_CHANGED))' +
                " and (equals(items.delta.attributeType, 'configuration/entityTypes/HCP/attributes/activitywith'))" +
                " and (equals(items.objectType, 'configuration/entityTypes/HCP'))" +
                " and (equals(items.delta.sources, 'Reltio'))" +
                " and (equals(items.objectUri, 'entity/01234')" +
                " or equals(items.startObjectUri, 'entity/01234')" +
                " or equals(items.endObjectUri, 'entity/01234'))" +
                " and (range(timestamp, 123, ".concat((0, moment_1.default)(filter.dateRange.period[1]).endOf('date').valueOf(), "))") +
                " and (not equals(user, 'collaboration-service'))";
            expect((0, filters_1.buildActivitiesFilterString)(filter)).toEqual(expectedResult);
        });
    });
});
