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 { assocPath } from 'ramda';
import { renderHook } from '@testing-library/react-hooks';
import { Mode } from '@reltio/mdm-sdk';
import { useMdmAction, useMdmEntity, useMdmMetadata, useMdmModifiedEntity, useMdmMode } from '../../contexts/MdmModuleContext';
import { useHiddenAttributes } from './useHiddenAttributes';
jest.mock('../../contexts/MdmModuleContext', function () { return (__assign(__assign({}, jest.requireActual('../../contexts/MdmModuleContext')), { useMdmAction: jest.fn(), useMdmMetadata: jest.fn(), useMdmEntity: jest.fn().mockReturnValue({}), useMdmModifiedEntity: jest.fn(), useMdmMode: jest.fn() })); });
describe('useHiddenAttributes', function () {
    var metadata = {
        entityTypes: [
            {
                uri: 'configuration/entityTypes/HCP',
                attributes: [
                    {
                        name: 'Category1',
                        type: 'String',
                        uri: 'configuration/entityTypes/HCP/attributes/Category1'
                    },
                    {
                        name: 'Category2',
                        type: 'String',
                        uri: 'configuration/entityTypes/HCP/attributes/Category2'
                    },
                    {
                        name: 'Category3',
                        type: 'String',
                        uri: 'configuration/entityTypes/HCP/attributes/Category3'
                    },
                    {
                        name: 'Category4',
                        type: 'String',
                        uri: 'configuration/entityTypes/HCP/attributes/Category4'
                    },
                    {
                        name: 'Nested1',
                        label: 'Nested1',
                        type: 'Nested',
                        uri: 'configuration/entityTypes/HCP/attributes/Nested1',
                        attributes: [
                            {
                                name: 'NestedCategory1',
                                type: 'String',
                                uri: 'configuration/entityTypes/HCP/attributes/Nested1/attributes/NestedCategory1'
                            },
                            {
                                name: 'NestedCategory2',
                                type: 'String',
                                uri: 'configuration/entityTypes/HCP/attributes/Nested1/attributes/NestedCategory2'
                            }
                        ]
                    }
                ],
                ruleBasedAttributes: [
                    {
                        uri: 'configuration/entityTypes/HCP/ruleBasedAttributes/DynamicRule1',
                        label: 'Dynamic Rule 1',
                        description: 'Dynamic Attributes Rule 1',
                        name: 'DynamicRule1',
                        type: 'Dynamic',
                        controlFunction: {
                            expression: "listEquals(attributes.Category1.value, 'category1_value1') and listEquals(attributes.Category2.value, 'category2_value1', 'category2_value2')",
                            showAttributeURI: [
                                'configuration/entityTypes/HCP/attributes/Category3',
                                'configuration/entityTypes/HCP/attributes/Category4'
                            ]
                        }
                    },
                    {
                        uri: 'configuration/entityTypes/HCP/ruleBasedAttributes/DynamicRule2',
                        label: 'Dynamic Rule 2',
                        description: 'Dynamic Attributes Rule 2',
                        name: 'DynamicRule2',
                        type: 'Dynamic',
                        controlFunction: {
                            expression: "listEquals(attributes.Category1.value, 'category1_value1')",
                            showAttributeURI: ['configuration/entityTypes/HCP/attributes/Category2']
                        }
                    },
                    {
                        uri: 'configuration/entityTypes/HCP/ruleBasedAttributes/DynamicRule3',
                        label: 'Dynamic Rule 3',
                        description: 'Dynamic Attributes Rule 3',
                        name: 'DynamicRule3',
                        type: 'Dynamic',
                        controlFunction: {
                            expression: "listEquals(attributes.Category1.value, 'category1_value2')",
                            showAttributeURI: ['configuration/entityTypes/HCP/attributes/Category4']
                        }
                    }
                ]
            }
        ]
    };
    var updateHiddenAttributes = jest.fn();
    var resetHiddenAttributes = jest.fn();
    var setUp = function () { return renderHook(useHiddenAttributes); };
    beforeEach(function () {
        useMdmMetadata.mockReturnValue(metadata);
        useMdmEntity.mockReturnValue(null);
        useMdmModifiedEntity.mockReturnValue(null);
        useMdmMode.mockReturnValue(Mode.Viewing);
        useMdmAction.mockImplementation(function (action) {
            if (action === 'updateHiddenAttributes') {
                return updateHiddenAttributes;
            }
            if (action === 'resetHiddenAttributes') {
                return resetHiddenAttributes;
            }
            return jest.fn();
        });
    });
    afterEach(function () {
        jest.clearAllMocks();
    });
    it('should hide attributes if there is no satisfied dynamic rules', function () {
        var entity = {
            type: 'configuration/entityTypes/HCP',
            uri: 'entities/uri_e',
            attributes: {
                Category1: [
                    {
                        uri: 'entities/uri_e/attributes/uri1',
                        value: 'category1_value1_diff'
                    }
                ],
                Category2: [
                    {
                        uri: 'entities/uri_e/attributes/uri2',
                        value: 'category2_value1_diff'
                    }
                ],
                Category3: [
                    {
                        uri: 'entities/uri_e/attributes/uri3',
                        value: 'category3_value1'
                    }
                ],
                Category4: [
                    {
                        uri: 'entities/uri_e/attributes/uri4',
                        value: 'category4_value1'
                    }
                ]
            }
        };
        useMdmEntity.mockReturnValue(entity);
        setUp();
        expect(updateHiddenAttributes).toHaveBeenCalledWith(entity.uri, [
            'configuration/entityTypes/HCP/attributes/Category3',
            'configuration/entityTypes/HCP/attributes/Category4',
            'configuration/entityTypes/HCP/attributes/Category2'
        ]);
    });
    it('should not hide attributes if there is a satisfied dynamic rule', function () {
        var entity = {
            type: 'configuration/entityTypes/HCP',
            uri: 'entities/uri_e',
            attributes: {
                Category1: [
                    {
                        uri: 'entities/uri_e/attributes/uri1',
                        value: 'category1_value1'
                    }
                ],
                Category2: [
                    {
                        uri: 'entities/uri_e/attributes/uri2',
                        value: 'category2_value1'
                    }
                ],
                Category3: [
                    {
                        uri: 'entities/uri_e/attributes/uri3',
                        value: 'category3_value1'
                    }
                ],
                Category4: [
                    {
                        uri: 'entities/uri_e/attributes/uri4',
                        value: 'category4_value1'
                    }
                ]
            }
        };
        useMdmEntity.mockReturnValue(entity);
        setUp();
        expect(updateHiddenAttributes).toHaveBeenCalledWith(entity.uri, []);
    });
    it('should not hide attributes if at least one rule allows to show the attribute from showAttributeURI', function () {
        var entity = {
            type: 'configuration/entityTypes/HCP',
            uri: 'entities/uri_e',
            attributes: {
                Category1: [
                    {
                        uri: 'entities/uri_e/attributes/uri1',
                        value: 'category1_value2'
                    }
                ],
                Category4: [
                    {
                        uri: 'entities/uri_e/attributes/uri4',
                        value: 'category4_value1'
                    }
                ]
            }
        };
        var newMetadata = {
            entityTypes: [
                __assign(__assign({}, metadata.entityTypes[0]), { ruleBasedAttributes: [
                        metadata.entityTypes[0].ruleBasedAttributes[0],
                        metadata.entityTypes[0].ruleBasedAttributes[2]
                    ] })
            ]
        };
        useMdmMetadata.mockReturnValue(newMetadata);
        useMdmEntity.mockReturnValue(entity);
        setUp();
        expect(updateHiddenAttributes).toHaveBeenCalledWith(entity.uri, [
            'configuration/entityTypes/HCP/attributes/Category3'
        ]);
    });
    it('should hide nested attributes if there are no rules allow to show the attribute from showAttributeURI', function () {
        var entity = {
            type: 'configuration/entityTypes/HCP',
            uri: 'entities/uri_e',
            attributes: {
                Nested1: [
                    {
                        uri: 'entities/uri_e/attributes/nesteduri1',
                        value: {
                            NestedCategory1: [
                                {
                                    ov: true,
                                    uri: 'entities/uri_e/attributes/nesteduri1/attributes/nestedcaturi1',
                                    value: 'nested_category1_value_diff'
                                }
                            ],
                            NestedCategory2: [
                                {
                                    ov: true,
                                    uri: 'entities/uri_e/attributes/nesteduri1/attributes/nestedcaturi2',
                                    value: 'nested_category2_value1'
                                }
                            ]
                        }
                    }
                ]
            }
        };
        var nestedRuleBasedAttribute = {
            uri: 'configuration/entityTypes/HCP/ruleBasedAttributes/DynamicRuleNested',
            label: 'Dynamic Rule nested',
            description: 'Dynamic Attributes Rule nested',
            name: 'DynamicRuleNested',
            type: 'Dynamic',
            controlFunction: {
                expression: "listEquals(attributes.Nested1.value.NestedCategory1, 'nested_category1_value1')",
                showAttributeURI: ['configuration/entityTypes/HCP/attributes/Nested1/attributes/NestedCategory2']
            }
        };
        var newMetadata = {
            entityTypes: [
                __assign(__assign({}, metadata.entityTypes[0]), { ruleBasedAttributes: [nestedRuleBasedAttribute] })
            ]
        };
        useMdmMetadata.mockReturnValue(newMetadata);
        useMdmEntity.mockReturnValue(entity);
        setUp();
        expect(updateHiddenAttributes).toHaveBeenCalledWith(entity.uri, [
            'configuration/entityTypes/HCP/attributes/Nested1/attributes/NestedCategory2'
        ]);
    });
    it('should correctly show/hide attributes in edit mode while changing the entity', function () {
        var entity = {
            type: 'configuration/entityTypes/HCP',
            uri: 'entities/uri_e',
            attributes: {
                Category1: [
                    {
                        uri: 'entities/uri_e/attributes/uri1',
                        value: 'category1_value1_diff'
                    }
                ],
                Category2: [
                    {
                        uri: 'entities/uri_e/attributes/uri2',
                        value: 'category2_value1_diff'
                    }
                ],
                Category3: [
                    {
                        uri: 'entities/uri_e/attributes/uri3',
                        value: 'category3_value1'
                    }
                ],
                Category4: [
                    {
                        uri: 'entities/uri_e/attributes/uri4',
                        value: 'category4_value1'
                    }
                ]
            }
        };
        useMdmMode.mockReturnValue(Mode.Editing);
        useMdmEntity.mockReturnValue(entity);
        useMdmModifiedEntity.mockReturnValue(entity);
        var rerender = setUp().rerender;
        expect(updateHiddenAttributes).toHaveBeenCalledWith(entity.uri, [
            'configuration/entityTypes/HCP/attributes/Category3',
            'configuration/entityTypes/HCP/attributes/Category4',
            'configuration/entityTypes/HCP/attributes/Category2'
        ]);
        // change the entity to satisfy the rules
        var changedEntity1 = assocPath(['attributes', 'Category1'], [
            {
                uri: 'entities/uri_e/attributes/uri1',
                value: 'category1_value1'
            }
        ], entity);
        updateHiddenAttributes.mockClear();
        useMdmModifiedEntity.mockReturnValue(changedEntity1);
        rerender();
        expect(updateHiddenAttributes).toHaveBeenCalledWith(entity.uri, [
            'configuration/entityTypes/HCP/attributes/Category3',
            'configuration/entityTypes/HCP/attributes/Category4'
        ]);
        var changedEntity2 = assocPath(['attributes', 'Category2'], [
            {
                uri: 'entities/uri_e/attributes/uri2',
                value: 'category2_value1'
            }
        ], changedEntity1);
        updateHiddenAttributes.mockClear();
        useMdmModifiedEntity.mockReturnValue(changedEntity2);
        rerender();
        expect(updateHiddenAttributes).toHaveBeenCalledWith(entity.uri, []);
        // change the entity to not satisfy the rules
        var changedEntity3 = assocPath(['attributes', 'Category2'], [
            {
                uri: 'entities/uri_e/attributes/uri2',
                value: 'category2_value1_diff'
            }
        ], changedEntity2);
        updateHiddenAttributes.mockClear();
        useMdmModifiedEntity.mockReturnValue(changedEntity3);
        rerender();
        expect(updateHiddenAttributes).toHaveBeenCalledWith(entity.uri, [
            'configuration/entityTypes/HCP/attributes/Category3',
            'configuration/entityTypes/HCP/attributes/Category4'
        ]);
        var changedEntity4 = assocPath(['attributes', 'Category1'], [
            {
                uri: 'entities/uri_e/attributes/uri1',
                value: 'category1_value1_diff'
            }
        ], changedEntity3);
        updateHiddenAttributes.mockClear();
        useMdmModifiedEntity.mockReturnValue(changedEntity4);
        rerender();
        expect(updateHiddenAttributes).toHaveBeenCalledWith(entity.uri, [
            'configuration/entityTypes/HCP/attributes/Category3',
            'configuration/entityTypes/HCP/attributes/Category4',
            'configuration/entityTypes/HCP/attributes/Category2'
        ]);
    });
    it('does not reset hidden attributes when switching from viewing mode to editing', function () {
        var entity = {
            type: 'configuration/entityTypes/HCP',
            uri: 'entities/uri_e',
            attributes: {
                Category1: [
                    {
                        uri: 'entities/uri_e/attributes/uri1',
                        value: 'category1_value1'
                    }
                ],
                Category2: [
                    {
                        uri: 'entities/uri_e/attributes/uri2',
                        value: 'category2_value1'
                    }
                ],
                Category3: [
                    {
                        uri: 'entities/uri_e/attributes/uri3',
                        value: 'category3_value1'
                    }
                ],
                Category4: [
                    {
                        uri: 'entities/uri_e/attributes/uri4',
                        value: 'category4_value1'
                    }
                ]
            }
        };
        useMdmMode.mockReturnValue(Mode.Viewing);
        useMdmEntity.mockReturnValue(entity);
        useMdmModifiedEntity.mockReturnValue(null);
        resetHiddenAttributes.mockClear();
        var rerender = setUp().rerender;
        expect(updateHiddenAttributes).toHaveBeenCalledWith(entity.uri, []);
        useMdmMode.mockReturnValue(Mode.Editing);
        rerender();
        expect(resetHiddenAttributes).not.toHaveBeenCalled();
        resetHiddenAttributes.mockClear();
        useMdmModifiedEntity.mockReturnValue(entity);
        rerender();
        expect(resetHiddenAttributes).not.toHaveBeenCalled();
    });
    it('updates hidden attributes when saving new entity (switching from editing mode to viewing)', function () {
        var entity = {
            type: 'configuration/entityTypes/HCP',
            uri: 'entities/uri_e_temporary',
            attributes: {
                Category1: [
                    {
                        uri: 'entities/uri_e/attributes/uri1',
                        value: 'category1_value1'
                    }
                ],
                Category2: [
                    {
                        uri: 'entities/uri_e/attributes/uri2',
                        value: 'category2_value1'
                    }
                ],
                Category3: [
                    {
                        uri: 'entities/uri_e/attributes/uri3',
                        value: 'category3_value1'
                    }
                ],
                Category4: [
                    {
                        uri: 'entities/uri_e/attributes/uri4',
                        value: 'category4_value1'
                    }
                ]
            }
        };
        useMdmMode.mockReturnValue(Mode.Editing);
        useMdmEntity.mockReturnValue(null);
        useMdmModifiedEntity.mockReturnValue(entity);
        var rerender = setUp().rerender;
        expect(updateHiddenAttributes).toHaveBeenCalledWith(entity.uri, []);
        useMdmMode.mockReturnValue(Mode.Viewing);
        useMdmEntity.mockReturnValue(__assign(__assign({}, entity), { uri: 'entities/uri_e_permanent' }));
        rerender();
        expect(resetHiddenAttributes).toHaveBeenCalled();
        expect(updateHiddenAttributes).toHaveBeenCalledWith('entities/uri_e_permanent', []);
    });
    it('should not dispatch action if value is unchanged', function () {
        var entity = {
            type: 'configuration/entityTypes/HCP',
            uri: 'entities/uri_e',
            attributes: {
                Category1: [
                    {
                        uri: 'entities/uri_e/attributes/uri1',
                        value: 'category1_value1'
                    }
                ],
                Category2: [
                    {
                        uri: 'entities/uri_e/attributes/uri2',
                        value: 'category2_value1'
                    }
                ]
            }
        };
        useMdmEntity.mockReturnValue(entity);
        useMdmMode.mockReturnValue(Mode.Editing);
        var rerender = setUp().rerender;
        expect(updateHiddenAttributes).toHaveBeenCalledWith(entity.uri, []);
        updateHiddenAttributes.mockClear();
        var newEntity = assocPath(['attributes', 'Category3'], [
            {
                uri: 'entities/uri_e/attributes/uri3',
                value: 'category3_value1'
            }
        ], entity);
        useMdmEntity.mockReturnValue(newEntity);
        rerender();
        expect(updateHiddenAttributes).not.toHaveBeenCalled();
    });
});
