import React from 'react';
import { renderHook } from '@testing-library/react-hooks';
import { HistoryMode } from '@reltio/mdm-sdk';
import { MdmModuleProvider } from '../../../contexts/MdmModuleContext';
import { DEFAULT_HISTORY_DIFF } from '../constants';
import { useHistoryDiff } from './useHistoryDiff';
describe('useHistoryDiff behaviour', function () {
    var historySlice, historyDiff, historyMode;
    var setHistoryDiff = jest.fn();
    var clearHistoryDiff = jest.fn();
    var entity = { uri: 'entityUri', type: 'entityType' };
    var initialProps = { entity: entity, historySlice: historySlice, historyDiff: historyDiff, historyMode: historyMode, setHistoryDiff: setHistoryDiff };
    var setUp = function () {
        var Providers = function (_a) {
            var children = _a.children;
            return (React.createElement(MdmModuleProvider, { values: { entity: entity, historySlice: historySlice, historyDiff: historyDiff, historyMode: historyMode }, actions: { setHistoryDiff: setHistoryDiff, clearHistoryDiff: clearHistoryDiff } }, children));
        };
        return renderHook(function () { return useHistoryDiff(); }, { initialProps: initialProps, wrapper: Providers });
    };
    afterEach(function () {
        jest.clearAllMocks();
    });
    it('should return default history diff if historySlice is empty', function () {
        var result = setUp().result;
        expect(result.current).toBe(DEFAULT_HISTORY_DIFF);
    });
    it('should set historyDiff if historySlice is not empty', function () {
        historySlice = {
            aEntity: {
                attributes: {
                    FirstName: [
                        {
                            type: 'configuration/entityTypes/HCP/attributes/FirstName',
                            ov: true,
                            value: 'Jhon',
                            uri: 'entities/Cpk0VAs/attributes/FirstName/IbvtOvWg'
                        }
                    ],
                    LastName: [
                        {
                            type: 'configuration/entityTypes/HCP/attributes/LastName',
                            ov: true,
                            value: 'Snow1',
                            uri: 'entities/Cpk0VAs/attributes/LastName/IbvtOzmw'
                        }
                    ]
                },
                tags: ['new tag', 'tag1'],
                roles: ['new role', 'role1']
            },
            bEntity: {
                attributes: {
                    FirstName: [
                        {
                            type: 'configuration/entityTypes/HCP/attributes/FirstName',
                            ov: true,
                            value: 'Jhon',
                            uri: 'entities/Cpk0VAs/attributes/FirstName/IbvtOvWg'
                        }
                    ]
                },
                tags: ['old tag', 'tag1'],
                roles: ['old role', 'role1']
            }
        };
        var expectedHistoryDiff = {
            appearance: {
                'entities/Cpk0VAs/attributes/LastName/IbvtOzmw': 'INSERT_ATTRIBUTE',
                roles: {
                    'new role': 'INSERT_ATTRIBUTE',
                    'old role': 'DELETE_ATTRIBUTE'
                },
                tags: {
                    'new tag': 'INSERT_ATTRIBUTE',
                    'old tag': 'DELETE_ATTRIBUTE'
                }
            },
            attributes: {
                FirstName: [
                    {
                        type: 'configuration/entityTypes/HCP/attributes/FirstName',
                        ov: true,
                        value: 'Jhon',
                        uri: 'entities/Cpk0VAs/attributes/FirstName/IbvtOvWg'
                    }
                ],
                LastName: [
                    {
                        type: 'configuration/entityTypes/HCP/attributes/LastName',
                        ov: true,
                        value: 'Snow1',
                        uri: 'entities/Cpk0VAs/attributes/LastName/IbvtOzmw'
                    }
                ]
            },
            roles: ['old role', 'new role', 'role1'],
            tags: ['old tag', 'new tag', 'tag1']
        };
        setUp();
        expect(setHistoryDiff).toHaveBeenCalledWith(expectedHistoryDiff);
    });
    it('should return historyDiff between historySlice.aEntity and entity if historyMode is HistoryMode.Current', function () {
        historySlice = {
            aEntity: {
                attributes: {
                    FirstName: [
                        {
                            type: 'configuration/entityTypes/HCP/attributes/FirstName',
                            ov: true,
                            value: 'Jhon',
                            uri: 'entities/Cpk0VAs/attributes/FirstName/IbvtOvWg'
                        }
                    ],
                    LastName: [
                        {
                            type: 'configuration/entityTypes/HCP/attributes/LastName',
                            ov: true,
                            value: 'Snow1',
                            uri: 'entities/Cpk0VAs/attributes/LastName/IbvtOzmw'
                        }
                    ]
                },
                tags: ['new tag', 'tag1'],
                roles: ['new role', 'role1']
            },
            bEntity: {
                attributes: {
                    FirstName: [
                        {
                            type: 'configuration/entityTypes/HCP/attributes/FirstName',
                            ov: true,
                            value: 'Jhon',
                            uri: 'entities/Cpk0VAs/attributes/FirstName/IbvtOvWg'
                        }
                    ]
                },
                tags: ['old tag', 'tag1'],
                roles: ['old role', 'role1']
            }
        };
        historyMode = HistoryMode.Current;
        var expectedHistoryDiff = {
            appearance: {
                'entities/Cpk0VAs/attributes/FirstName/IbvtOvWg': 'INSERT_ATTRIBUTE',
                'entities/Cpk0VAs/attributes/LastName/IbvtOzmw': 'INSERT_ATTRIBUTE',
                roles: {
                    'new role': 'INSERT_ATTRIBUTE',
                    role1: 'INSERT_ATTRIBUTE'
                },
                tags: {
                    'new tag': 'INSERT_ATTRIBUTE',
                    tag1: 'INSERT_ATTRIBUTE'
                }
            },
            attributes: {
                FirstName: [
                    {
                        type: 'configuration/entityTypes/HCP/attributes/FirstName',
                        ov: true,
                        value: 'Jhon',
                        uri: 'entities/Cpk0VAs/attributes/FirstName/IbvtOvWg'
                    }
                ],
                LastName: [
                    {
                        type: 'configuration/entityTypes/HCP/attributes/LastName',
                        ov: true,
                        value: 'Snow1',
                        uri: 'entities/Cpk0VAs/attributes/LastName/IbvtOzmw'
                    }
                ]
            },
            roles: ['new role', 'role1'],
            tags: ['new tag', 'tag1']
        };
        setUp();
        expect(setHistoryDiff).toHaveBeenCalledWith(expectedHistoryDiff);
    });
    it('should fix refEntity and refRelation', function () {
        historySlice = {
            aEntity: {
                attributes: {
                    Address: [
                        {
                            uri: 'referencedUri',
                            refEntity: {
                                type: 'refEntityType',
                                objectURI: 'entities/uri'
                            },
                            refRelation: {
                                type: 'relationType',
                                objectURI: 'relations/uri'
                            }
                        }
                    ]
                }
            },
            bEntity: null
        };
        historyMode = HistoryMode.Current;
        var expectedHistoryDiff = {
            appearance: {
                referencedUri: 'INSERT_ATTRIBUTE',
                roles: {},
                tags: {}
            },
            attributes: {
                Address: [
                    {
                        refEntity: [
                            {
                                objectURI: 'entities/uri',
                                type: 'refEntityType'
                            }
                        ],
                        refRelation: [
                            {
                                objectURI: 'relations/uri',
                                type: 'relationType'
                            }
                        ],
                        uri: 'referencedUri'
                    }
                ]
            },
            roles: [],
            tags: []
        };
        setUp();
        expect(setHistoryDiff).toHaveBeenCalledWith(expectedHistoryDiff);
    });
    it('should clear historyDiff if historySlice is empty', function () {
        historySlice = null;
        setUp();
        expect(setHistoryDiff).not.toHaveBeenCalled();
        expect(clearHistoryDiff).toHaveBeenCalled();
    });
});
