"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);
};
Object.defineProperty(exports, "__esModule", { value: true });
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var metadata_test_data_1 = require("./metadata.test-data");
var attributes_1 = require("./attributes");
describe('attributes helpers tests', function () {
    it('getEntityChanges behaviour', function () {
        var metadata = (0, metadata_test_data_1.getMetadata)();
        var objectInfo = {
            isExist: true,
            label: '12345 - Address Line 1 City',
            secondaryLabel: '12345',
            type: 'configuration/entityTypes/HCA',
            uri: 'entities/1hRTSKE5'
        };
        var changes = {
            CountryCode: [
                {
                    id: '3AqlrpfGa',
                    value: {
                        attributePath: 'CountryCode/3AqlrpfGa',
                        attributeType: 'configuration/entityTypes/HCA/attributes/CountryCode',
                        id: '41cb3gGy',
                        newPinOrIgnoreValue: true,
                        newValue: {
                            value: 'Chile',
                            lookupCode: 'CL',
                            lookupRawValue: 'Chile',
                            pin: true,
                            lineIds: ['3AqlrpfGa/value/newValue']
                        },
                        oldValue: {
                            value: 'Chile',
                            lookupCode: 'CL',
                            lookupRawValue: 'Chile',
                            pin: true,
                            lineIds: ['3AqlrpfGa/value/oldValue']
                        },
                        type: mdm_sdk_1.DCRTypes.IGNORE_ATTRIBUTE
                    }
                },
                {
                    id: 'uri$$1643545710682',
                    value: {
                        attributePath: 'CountryCode',
                        attributeType: 'configuration/entityTypes/HCA/attributes/CountryCode',
                        id: '41cb3c0i',
                        newPinOrIgnoreValue: false,
                        newValue: {
                            value: 'Bahamas',
                            lookupCode: 'BS',
                            lookupRawValue: 'BS',
                            lineIds: ['uri$$1643545710682']
                        },
                        type: mdm_sdk_1.DCRTypes.INSERT_ATTRIBUTE
                    }
                }
            ],
            Identifiers: [
                {
                    id: '45uypcuAz',
                    value: {
                        Type: [
                            {
                                id: '45uypcyRF',
                                value: {
                                    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: mdm_sdk_1.DCRTypes.IGNORE_ATTRIBUTE
                                }
                            }
                        ]
                    }
                }
            ]
        };
        expect((0, attributes_1.getEntityChanges)(metadata, objectInfo, changes)).toEqual([
            {
                attributeType: metadata.entityTypes[0].attributes[1],
                label: 'Country Code',
                level: 0,
                newValue: __assign(__assign({}, changes.CountryCode[0].value.newValue), { lineIds: ['3AqlrpfGa/value/oldValue'] }),
                operation: 'ignored',
                isReferenceSubAttribute: false,
                attributePath: 'CountryCode/3AqlrpfGa'
            },
            {
                attributeType: metadata.entityTypes[0].attributes[1],
                label: 'Country Code',
                level: 0,
                newValue: changes.CountryCode[1].value.newValue,
                operation: 'added',
                isReferenceSubAttribute: false,
                attributePath: 'CountryCode'
            },
            {
                attributeType: metadata.entityTypes[0].attributes[3],
                attributeValue: changes.Identifiers[0],
                label: 'Customer Identity',
                level: 0
            },
            {
                attributeType: metadata.entityTypes[0].attributes[3].attributes[0],
                label: 'Identity Type',
                level: 1,
                newValue: changes.Identifiers[0].value.Type[0].value.newValue,
                operation: 'ignored',
                isReferenceSubAttribute: false,
                attributePath: 'Identifiers/45uypcuAz/Type/45uypcyRF'
            }
        ]);
    });
    it('should mark a nested attribute and its sub-attributes inside a reference attribute as isReferenceSubAttribute=true', function () {
        var metadata = (0, metadata_test_data_1.getMetadata)();
        var objectInfo = {
            isExist: true,
            label: 'HCA',
            secondaryLabel: '',
            type: 'configuration/entityTypes/HCA',
            uri: 'entities/1hRTSKE5'
        };
        var changes = {
            Address: [
                {
                    value: {
                        AdditionalAttributes: [
                            {
                                value: {
                                    AttributeType: [
                                        {
                                            value: {
                                                newValue: { value: 'TypeA' },
                                                type: mdm_sdk_1.DCRTypes.INSERT_ATTRIBUTE
                                            }
                                        }
                                    ]
                                }
                            }
                        ]
                    }
                }
            ]
        };
        expect((0, attributes_1.getEntityChanges)(metadata, objectInfo, changes)).toEqual([
            {
                attributeType: metadata.entityTypes[0].attributes[4],
                attributeValue: changes.Address[0],
                label: 'Address',
                level: 0
            },
            {
                attributeType: metadata.relationTypes[0].attributes[0],
                attributeValue: changes.Address[0].value.AdditionalAttributes[0],
                label: 'Additional Attributes',
                level: 1,
                isReferenceSubAttribute: true
            },
            {
                attributeType: metadata.relationTypes[0].attributes[0].attributes[0],
                label: 'Attribute Type',
                level: 2,
                newValue: changes.Address[0].value.AdditionalAttributes[0].value.AttributeType[0].value.newValue,
                operation: mdm_sdk_1.DCROperationTypes.ADDED,
                isReferenceSubAttribute: true
            }
        ]);
    });
    describe('getRelationsChanges behaviour', function () {
        var relationInfo = {
            endObjectLabel: '',
            endObjectUri: 'entities/1oUt4Zh8',
            isExist: true,
            startObjectLabel: 'New Label',
            startObjectUri: 'entities/10f6Jdz0',
            type: 'configuration/relationTypes/Assistant',
            uri: 'relations/t58vHMD'
        };
        var changes = {
            TestAttribute: [{ value: { newValue: '123', type: mdm_sdk_1.DCRTypes.INSERT_ATTRIBUTE } }],
            'activeness.startDate': [
                {
                    newValue: { value: 1644001200000, lineIds: ['relations/t58vHMD/activeness/startDate/newValue'] },
                    type: mdm_sdk_1.DCRTypes.INSERT_ATTRIBUTE
                }
            ]
        };
        it('should not show operation for relation title', function () {
            var metadata = (0, metadata_test_data_1.getMetadata)();
            expect((0, attributes_1.getRelationChanges)(metadata, relationInfo, changes, mdm_sdk_1.DCRTypes.INSERT_ATTRIBUTE, ['lineIds'])).toEqual([
                {
                    attributeType: null,
                    relationType: metadata.relationTypes[1],
                    attributeValue: __assign(__assign({}, relationInfo), changes),
                    label: metadata.relationTypes[1].label,
                    level: 1,
                    lineIds: undefined
                },
                {
                    attributeType: mdm_sdk_1.EntityAttrTypes.startDate,
                    label: mdm_sdk_1.EntityAttrTypes.startDate.label,
                    level: 2,
                    newValue: { value: 1644001200000, lineIds: ['relations/t58vHMD/activeness/startDate/newValue'] },
                    operation: mdm_sdk_1.DCROperationTypes.ADDED,
                    isReferenceSubAttribute: false
                },
                {
                    attributeType: metadata.relationTypes[1].attributes[0],
                    label: metadata.relationTypes[1].attributes[0].label,
                    level: 2,
                    newValue: '123',
                    operation: mdm_sdk_1.DCROperationTypes.ADDED,
                    isReferenceSubAttribute: false
                }
            ]);
        });
        it('should show operation for relation title if it is new', function () {
            var metadata = (0, metadata_test_data_1.getMetadata)();
            expect((0, attributes_1.getRelationChanges)(metadata, relationInfo, changes, mdm_sdk_1.DCRTypes.CREATE_RELATIONSHIP, ['lineIds'])).toEqual([
                {
                    attributeType: null,
                    relationType: metadata.relationTypes[1],
                    attributeValue: __assign(__assign({}, relationInfo), changes),
                    label: metadata.relationTypes[1].label,
                    level: 1,
                    operation: mdm_sdk_1.DCROperationTypes.ADDED,
                    lineIds: ['lineIds']
                },
                {
                    attributeType: mdm_sdk_1.EntityAttrTypes.startDate,
                    label: mdm_sdk_1.EntityAttrTypes.startDate.label,
                    level: 2,
                    newValue: { value: 1644001200000, lineIds: ['relations/t58vHMD/activeness/startDate/newValue'] },
                    operation: mdm_sdk_1.DCROperationTypes.ADDED,
                    isReferenceSubAttribute: false
                },
                {
                    attributeType: metadata.relationTypes[1].attributes[0],
                    label: metadata.relationTypes[1].attributes[0].label,
                    isReferenceSubAttribute: false,
                    level: 2,
                    newValue: '123',
                    operation: mdm_sdk_1.DCROperationTypes.ADDED
                }
            ]);
        });
        it('should show operation for relation title if it was deleted and changes are empty', function () {
            var metadata = (0, metadata_test_data_1.getMetadata)();
            expect((0, attributes_1.getRelationChanges)(metadata, relationInfo, {}, mdm_sdk_1.DCRTypes.DELETE_RELATIONSHIP, ['lineIds'])).toEqual([
                {
                    attributeType: null,
                    relationType: metadata.relationTypes[1],
                    attributeValue: relationInfo,
                    label: metadata.relationTypes[1].label,
                    level: 1,
                    operation: mdm_sdk_1.DCROperationTypes.DELETED,
                    lineIds: ['lineIds']
                }
            ]);
        });
    });
});
