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 { DCRChangeRequestState, DCRTypes } from '@reltio/mdm-sdk';
import { concatEntityRelationsObjects, createEntityRelationsObjectByEntity, createEntityRelationsObjectByRelation, extractObjectsFromChangeRequest, getEntityUriForChangeRequest, isCreateEntityOperation } from './dcr';
describe('dcr helpers tests', function () {
    it('extractObjectsFromChangeRequest behaviour', function () {
        var objectsInfo = { id1: {}, id2: {}, id3: {} };
        var changes = { id1: {}, id3: {} };
        expect(extractObjectsFromChangeRequest(changes, objectsInfo)).toEqual([{ uri: 'id1' }, { uri: 'id3' }]);
        expect(extractObjectsFromChangeRequest({}, {})).toEqual([]);
    });
    it('createEntityRelationsObjectByEntity behaviour', function () {
        var objectsWithChanges = [
            { uri: 'entities/1Hhdq6PW' },
            { uri: 'relations/ja6cDI2', startObjectUri: 'entities/10f6JMNk' },
            { uri: 'relations/10f6Jdz', startObjectUri: 'entities/1Hhdq6PW' }
        ];
        var entity = { uri: 'entities/1Hhdq6PW' };
        expect(createEntityRelationsObjectByEntity(objectsWithChanges, entity)).toEqual({
            entity: objectsWithChanges[0],
            relations: [objectsWithChanges[2]]
        });
    });
    it('createEntityRelationsObjectByRelation behaviour', function () {
        var objectsInfo = {
            'entities/1Hhdq6PW': { uri: 'entities/1Hhdq6PW' },
            'relations/ja6cDI2': { uri: 'relations/ja6cDI2', startObjectUri: 'entities/10f6JMNk' },
            'relations/10f6Jdz': { uri: 'relations/10f6Jdz', startObjectUri: 'entities/1Hhdq6PW' }
        };
        expect(createEntityRelationsObjectByRelation(objectsInfo, objectsInfo['relations/ja6cDI2'])).toEqual({
            relations: [objectsInfo['relations/ja6cDI2']]
        });
        expect(createEntityRelationsObjectByRelation(objectsInfo, objectsInfo['relations/10f6Jdz'])).toEqual({
            entity: objectsInfo['entities/1Hhdq6PW'],
            relations: [objectsInfo['relations/10f6Jdz']]
        });
    });
    it('concatEntityRelationsObjects behaviour', function () {
        var objects = [
            {
                entity: {
                    uri: 'entities/10f6JMNk',
                    isExist: true,
                    label: 'Test Label',
                    secondaryLabel: '',
                    type: 'configuration/entityTypes/Type'
                },
                relations: [
                    {
                        uri: 'relations/ja6cDI2',
                        type: 'configuration/relationTypes/HasAddress',
                        startObjectUri: 'entities/10f6JMNk',
                        startObjectLabel: 'label 1',
                        endObjectUri: 'entities/1MXzYxGR',
                        endObjectLabel: 'label 2',
                        isExist: true
                    }
                ]
            },
            {
                entity: {
                    uri: 'entities/1Hhdq6PW',
                    isExist: true,
                    label: 'Test Label 2',
                    secondaryLabel: '',
                    type: 'configuration/entityTypes/Type'
                },
                relations: [
                    {
                        uri: 'relations/10f6Jdz',
                        type: 'configuration/relationTypes/HasAddress',
                        startObjectUri: 'entities/1Hhdq6PW',
                        startObjectLabel: 'label 3',
                        endObjectUri: 'entities/1MXzYxGR',
                        endObjectLabel: 'label 2',
                        isExist: true
                    }
                ]
            },
            {
                entity: {
                    uri: 'entities/10f6JMNk',
                    isExist: true,
                    label: 'Test Label',
                    secondaryLabel: '',
                    type: 'configuration/entityTypes/Type'
                },
                relations: [
                    {
                        uri: 'relations/ja6caxc',
                        type: 'configuration/relationTypes/HasAddress',
                        startObjectUri: 'entities/10f6JMNk',
                        startObjectLabel: 'label 1',
                        endObjectUri: 'entities/1MXzYxGR',
                        endObjectLabel: 'label 2',
                        isExist: true
                    }
                ]
            },
            {
                entity: {
                    uri: 'entities/10f6Jabc',
                    isExist: true,
                    label: 'Test Label',
                    secondaryLabel: '',
                    type: 'configuration/entityTypes/Type'
                },
                relations: []
            }
        ];
        expect(objects.reduce(function (acc, object) { return concatEntityRelationsObjects(acc, object); }, {})).toEqual({
            'entities/10f6JMNk': {
                entity: objects[0].entity,
                relations: [objects[2].relations[0], objects[0].relations[0]]
            },
            'entities/1Hhdq6PW': objects[1],
            'entities/10f6Jabc': objects[3]
        });
    });
    it('getEntityUriForChangeRequest behaviour', function () {
        expect(getEntityUriForChangeRequest('dcrUri', 'entityUri')).toBe('dcrUri/changes/entityUri');
    });
    it('isCreateEntityOperation behaviour', function () {
        var change = {
            type: DCRTypes.CREATE_ENTITY,
            changes: {},
            uri: 'changeRequestUri',
            createdBy: 'somebody',
            objectsInfo: {},
            createdTime: 1,
            state: DCRChangeRequestState.APPLIED,
            updatedBy: 'somebody2',
            updatedTime: 2
        };
        expect(isCreateEntityOperation(change)).toBe(true);
        expect(isCreateEntityOperation(__assign(__assign({}, change), { type: DCRTypes.CREATE_RELATIONSHIP }))).toBe(false);
    });
});
