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 { DCRTypes, EntityAttrTypes, DCROperationTypes } from '@reltio/mdm-sdk';
import { getMetadata } from './metadata.test-data';
import { mergeChanges } from './merge';
describe('mergeChanges tests', function () {
    var entityUri = 'entities/1hRTSKE5';
    var objectInfo = {
        isExist: true,
        label: '12345 - Address Line 1 City',
        secondaryLabel: '12345',
        type: 'configuration/entityTypes/HCA',
        uri: entityUri
    };
    it('should create path to change', function () {
        var _a;
        var metadata = getMetadata();
        var changes = (_a = {},
            _a[entityUri] = [
                {
                    attributePath: 'Identifiers/45uypcuAz/Type/45uypcyRF',
                    attributeType: 'configuration/entityTypes/HCA/attributes/Identifiers/attributes/Type',
                    id: '41cb3onU',
                    newPinOrIgnoreValue: true,
                    newValue: { value: 'Business ID', lookupCode: 'BUID', lookupRawValue: 'BUID' },
                    oldValue: { value: 'Business ID', lookupCode: 'BUID', lookupRawValue: 'BUID' },
                    type: DCRTypes.IGNORE_ATTRIBUTE
                }
            ],
            _a);
        expect(mergeChanges(metadata, changes, objectInfo, [])).toEqual([
            [
                {
                    attributeType: metadata.entityTypes[0].attributes[3],
                    attributeValue: {
                        id: '45uypcuAz',
                        value: {
                            Type: [
                                {
                                    id: '45uypcyRF',
                                    value: changes[entityUri][0]
                                }
                            ]
                        }
                    },
                    label: 'Customer Identity',
                    level: 0
                },
                {
                    attributeType: metadata.entityTypes[0].attributes[3].attributes[0],
                    label: 'Identity Type',
                    level: 1,
                    newValue: changes[entityUri][0].newValue,
                    operation: 'ignored',
                    isReferenceSubAttribute: false,
                    attributePath: 'Identifiers/45uypcuAz/Type/45uypcyRF'
                }
            ],
            []
        ]);
    });
    it('should merge changes with the same paths', function () {
        var _a;
        var metadata = getMetadata();
        var changes = (_a = {},
            _a[entityUri] = [
                {
                    attributePath: 'AdditionalAttributes',
                    attributeType: 'configuration/relationTypes/HasAddress/attributes/AdditionalAttributes',
                    id: '58dpOzAR',
                    newPinOrIgnoreValue: false,
                    newValue: { value: { AttributeType: [{ value: 'testType1' }], AttributeValue: [{ value: 'testValue1' }] } },
                    refAttributeType: 'configuration/entityTypes/HCA/attributes/Address/attributes/AdditionalAttributes',
                    refObjectURI: 'relations/138xt4Lu',
                    type: DCRTypes.INSERT_ATTRIBUTE
                },
                {
                    attributePath: 'AdditionalAttributes',
                    attributeType: 'configuration/relationTypes/HasAddress/attributes/AdditionalAttributes',
                    id: '58dpP3Qh',
                    newPinOrIgnoreValue: false,
                    newValue: { value: { AttributeType: [{ value: 'testType2' }], AttributeValue: [{ value: 'testValue2' }] } },
                    refAttributeType: 'configuration/entityTypes/HCA/attributes/Address/attributes/AdditionalAttributes',
                    refObjectURI: 'relations/138xt4Lu',
                    type: DCRTypes.INSERT_ATTRIBUTE
                }
            ],
            _a);
        var mergedChanges = [
            {
                level: 0,
                label: 'Address',
                attributeType: metadata.entityTypes[0].attributes[4],
                attributeValue: {
                    id: '138xt4Lu',
                    refObjectURI: 'relations/138xt4Lu',
                    value: {
                        AdditionalAttributes: [
                            {
                                value: changes[entityUri][0]
                            },
                            {
                                value: changes[entityUri][1]
                            }
                        ]
                    }
                }
            },
            {
                level: 1,
                label: 'Additional Attributes',
                attributeType: metadata.relationTypes[0].attributes[0],
                attributeValue: { value: changes[entityUri][0] }
            },
            {
                attributeType: metadata.relationTypes[0].attributes[0].attributes[0],
                newValue: { value: 'testType1' },
                operation: 'added',
                label: 'Attribute Type',
                level: 2
            },
            {
                attributeType: metadata.relationTypes[0].attributes[0].attributes[1],
                newValue: { value: 'testValue1' },
                operation: 'added',
                label: 'Attribute Value',
                level: 2
            },
            {
                level: 1,
                label: 'Additional Attributes',
                attributeType: metadata.relationTypes[0].attributes[0],
                attributeValue: { value: changes[entityUri][1] }
            },
            {
                attributeType: metadata.relationTypes[0].attributes[0].attributes[0],
                newValue: { value: 'testType2' },
                operation: 'added',
                label: 'Attribute Type',
                level: 2
            },
            {
                attributeType: metadata.relationTypes[0].attributes[0].attributes[1],
                newValue: { value: 'testValue2' },
                operation: 'added',
                label: 'Attribute Value',
                level: 2
            }
        ];
        expect(mergeChanges(metadata, changes, objectInfo, [])).toMatchObject([mergedChanges, []]);
    });
    it('should not merge changes with the same paths, but different types', function () {
        var _a;
        var metadata = getMetadata();
        var changes = (_a = {},
            _a[entityUri] = [
                {
                    attributePath: 'Identifiers/2BNyTheu/Type/2BNyU2yC',
                    attributeType: 'configuration/entityTypes/HCA/attributes/Identifiers/attributes/Type',
                    id: '58dpOzAR',
                    newPinOrIgnoreValue: false,
                    oldValue: {
                        value: 'ValueOld'
                    },
                    newValue: {
                        value: 'ValueNew'
                    },
                    type: DCRTypes.UPDATE_ATTRIBUTE
                },
                {
                    attributePath: 'Identifiers/2BNyTheu/Type/2BNyU2yC',
                    attributeType: 'configuration/entityTypes/HCA/attributes/Identifiers/attributes/Type',
                    id: '58dpP3Qh',
                    newPinOrIgnoreValue: true,
                    oldValue: {
                        value: 'ValueOld'
                    },
                    newValue: {
                        value: 'ValueOld'
                    },
                    type: DCRTypes.IGNORE_ATTRIBUTE
                }
            ],
            _a);
        var mergedChanges = [
            {
                level: 0,
                label: 'Customer Identity',
                attributeType: metadata.entityTypes[0].attributes[3],
                attributeValue: {
                    id: '2BNyTheu',
                    value: {
                        Type: [
                            {
                                id: '2BNyU2yC',
                                value: changes[entityUri][0]
                            },
                            {
                                id: '2BNyU2yC',
                                value: changes[entityUri][1]
                            }
                        ]
                    }
                }
            },
            {
                attributeType: metadata.entityTypes[0].attributes[3].attributes[0],
                newValue: {
                    value: 'ValueNew'
                },
                oldValue: {
                    value: 'ValueOld'
                },
                operation: 'edited',
                label: 'Identity Type',
                level: 1,
                isReferenceSubAttribute: false
            },
            {
                attributeType: metadata.entityTypes[0].attributes[3].attributes[0],
                newValue: {
                    value: 'ValueOld'
                },
                operation: 'ignored',
                label: 'Identity Type',
                level: 1,
                isReferenceSubAttribute: false
            }
        ];
        expect(mergeChanges(metadata, changes, objectInfo, [])).toMatchObject([mergedChanges, []]);
    });
    it('should merge special attributes', function () {
        var _a;
        var metadata = getMetadata();
        var changes = (_a = {},
            _a[entityUri] = [
                {
                    id: 'COVLJS5X',
                    newValue: ['975783600000'],
                    oldValue: ['976474800000'],
                    type: DCRTypes.UPDATE_START_DATE
                },
                {
                    id: '3JDiqtBn',
                    newValue: ['tag1'],
                    oldValue: ['tag2', 'tag1'],
                    type: DCRTypes.UPDATE_TAGS
                }
            ],
            _a);
        var mergedChanges = [
            {
                attributeType: EntityAttrTypes.tags,
                label: EntityAttrTypes.tags.label,
                level: 0,
                newValue: { value: ['tag2'] },
                operation: DCROperationTypes.DELETED
            },
            {
                attributeType: EntityAttrTypes.startDate,
                label: EntityAttrTypes.startDate.label,
                level: 0,
                newValue: { value: parseInt(changes[entityUri][0].newValue[0]) },
                oldValue: { value: parseInt(changes[entityUri][0].oldValue[0]) },
                operation: DCROperationTypes.EDITED
            }
        ];
        expect(mergeChanges(metadata, changes, objectInfo, [])).toMatchObject([mergedChanges, []]);
    });
    it('should merge relations', function () {
        var _a;
        var metadata = getMetadata();
        var changes = {
            'relations/1EvCRmwC': [
                {
                    id: 'BpiR2Vqh',
                    type: DCRTypes.CREATE_RELATIONSHIP,
                    lineIds: ['relations/1EvCRmwC/BpiR2Vqh'],
                    newValue: {
                        uri: 'relations/1EvCRmwC',
                        type: 'configuration/relationTypes/Assistant',
                        startObject: {
                            objectURI: 'entities/1u04ksx9'
                        },
                        endObject: {
                            objectURI: 'entities/1C2v7IRp'
                        },
                        startDate: 1643742000000,
                        lineIds: ['relations/1EvCRmwC/BpiR2Vqh/newValue']
                    }
                }
            ]
        };
        var relationsInfo = [
            {
                endObjectLabel: 'End Object Label',
                endObjectUri: 'entities/1C2v7IRp',
                startObjectLabel: 'Start Object Label',
                startObjectUri: 'entities/1u04ksx9',
                isExist: false,
                type: 'configuration/relationTypes/Assistant',
                uri: 'relations/1EvCRmwC'
            }
        ];
        var mergedRelations = [
            {
                attributeType: null,
                relationType: metadata.relationTypes[1],
                attributeValue: __assign(__assign({}, relationsInfo[0]), (_a = {}, _a[EntityAttrTypes.startDate.name] = [
                    {
                        newValue: { value: 1643742000000 },
                        type: DCRTypes.INSERT_ATTRIBUTE
                    }
                ], _a)),
                label: metadata.relationTypes[1].label,
                lineIds: ['relations/1EvCRmwC/BpiR2Vqh/newValue'],
                level: 1
            },
            {
                attributeType: EntityAttrTypes.startDate,
                label: 'Start date',
                level: 2,
                newValue: {
                    value: 1643742000000,
                    lineIds: ["relations/1EvCRmwC/BpiR2Vqh/newValue/".concat(EntityAttrTypes.startDate.name)]
                },
                operation: DCROperationTypes.ADDED
            }
        ];
        expect(mergeChanges(metadata, changes, objectInfo, relationsInfo)).toMatchObject([[], mergedRelations]);
    });
    it('should merge lineIds when multiple changes have the same attributePath', function () {
        var _a;
        var _b, _c;
        var metadata = getMetadata();
        var changes = (_a = {},
            _a[entityUri] = [
                {
                    id: '0Kb5iws',
                    type: DCRTypes.UPDATE_ATTRIBUTE,
                    objectType: 'configuration/entityTypes/HCP',
                    attributePath: 'CountryCode/2DEYF4Zg',
                    oldValue: {
                        value: 'American Samoa',
                        lookupCode: 'ASM',
                        lookupRawValue: 'ASM',
                        lineIds: ['line-1']
                    },
                    newValue: {
                        value: 'Algeria',
                        lookupCode: 'DZA',
                        lookupRawValue: 'DZA',
                        lineIds: ['line-2']
                    },
                    newPinOrIgnoreValue: false,
                    attributeType: 'configuration/entityTypes/HCP/attributes/CountryCode'
                },
                {
                    id: '0Kb5nD8',
                    type: DCRTypes.UPDATE_ATTRIBUTE,
                    objectType: 'configuration/entityTypes/HCP',
                    attributePath: 'CountryCode/2DEYF4Zg',
                    oldValue: {
                        value: 'American Samoa',
                        lookupCode: 'ASM',
                        lookupRawValue: 'ASM',
                        lineIds: ['line-3']
                    },
                    newValue: {
                        value: 'Algeria',
                        lookupCode: 'DZA',
                        lookupRawValue: 'DZA',
                        lineIds: ['line-4']
                    },
                    newPinOrIgnoreValue: false,
                    attributeType: 'configuration/entityTypes/HCP/attributes/CountryCode'
                }
            ],
            _a);
        var mergedChanges = mergeChanges(metadata, changes, objectInfo, [])[0];
        expect((_c = (_b = mergedChanges[0]) === null || _b === void 0 ? void 0 : _b.newValue) === null || _c === void 0 ? void 0 : _c.lineIds).toEqual(['line-2', 'line-4']);
    });
    it('should keep role URIs in CREATE_ENTITY changes without mapping to labels', function () {
        var _a;
        var _b;
        var metadata = getMetadata();
        var roleUris = [
            'configuration/roles/DirectorofNursing',
            'configuration/roles/MedicalAssistant',
            'configuration/roles/LabDirectors'
        ];
        var changes = (_a = {},
            _a[entityUri] = [
                {
                    id: 'create1',
                    type: DCRTypes.CREATE_ENTITY,
                    objectType: 'configuration/entityTypes/HCA',
                    newValue: {
                        uri: entityUri,
                        type: 'configuration/entityTypes/HCA',
                        attributes: {
                            ActivationDate: [{ value: '2024-01-01' }]
                        },
                        roles: roleUris,
                        crosswalks: [{ type: 'configuration/sources/Reltio', value: '1hRTSKE5', dataProvider: true }],
                        analyticsAttributes: {}
                    }
                }
            ],
            _a);
        var entityChanges = mergeChanges(metadata, changes, objectInfo, [])[0];
        var rolesDiff = entityChanges.find(function (d) { var _a; return ((_a = d.attributeType) === null || _a === void 0 ? void 0 : _a.uri) === EntityAttrTypes.roles.uri; });
        expect(rolesDiff).toBeDefined();
        var rolesNewValue = (_b = rolesDiff.newValue) === null || _b === void 0 ? void 0 : _b.value;
        expect(rolesNewValue).toEqual(roleUris);
    });
});
