"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);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = __importDefault(require("react"));
var InlineSimpleAttributesBlock_1 = require("./InlineSimpleAttributesBlock");
var core_1 = require("@dnd-kit/core");
var react_2 = require("@testing-library/react");
jest.mock('@dnd-kit/core', function () { return (__assign(__assign({}, jest.requireActual('@dnd-kit/core')), { useDndMonitor: jest.fn() })); });
describe('InlineSimpleAttributesBlock tests', function () {
    var attributeType = {
        uri: 'configuration/entityTypes/HCP/attributes/Simple',
        type: 'String',
        name: 'Simple',
        label: 'Simple label'
    };
    var attributeValues = [
        {
            type: attributeType.uri,
            uri: 'entities/1/attributes/Simple/1',
            value: '123',
            ov: true
        },
        {
            type: attributeType.uri,
            uri: 'entities/1/attributes/Simple/2',
            value: '789',
            ov: false
        }
    ];
    var crosswalks = [
        {
            uri: 'entities/1/crosswalks/12jf9nL05',
            type: 'configuration/sources/Reltio',
            value: 'nQpZyQt',
            reltioLoadDate: '2021-06-11T08:24:31.726Z',
            createDate: '2018-11-19T12:14:00.735Z',
            updateDate: '2018-11-19T12:14:00.735Z',
            attributes: ['entities/1/attributes/Simple/1', 'entities/1/attributes/Simple/2'],
            singleAttributeUpdateDates: {
                'entities/1/attributes/Simple/1': '2018-11-29T05:39:56.445Z'
            }
        },
        {
            uri: 'entities/1/crosswalks/1CwTinUzm',
            type: 'configuration/sources/AHA',
            value: 'AHA_LEV',
            reltioLoadDate: '2021-06-09T11:32:16.434Z',
            sourcePublishDate: '1970-01-01T00:00:00.000Z',
            createDate: '2018-11-28T13:16:18.540Z',
            updateDate: '2018-11-28T13:16:18.540Z',
            attributes: ['entities/1/attributes/Simple/2'],
            singleAttributeUpdateDates: {
                'entities/1/attributes/Simple/2': '2021-06-09T11:32:16.434Z'
            }
        }
    ];
    var crosswalksMap = {
        'entities/1/attributes/Simple/1': [crosswalks[0]],
        'entities/1/attributes/Simple/2': [crosswalks[0], crosswalks[1]]
    };
    var commonProps = {
        attributeType: attributeType,
        onPin: jest.fn(),
        onIgnore: jest.fn(),
        onEdit: jest.fn(),
        onDelete: jest.fn()
    };
    var defaultProps = __assign({ attributeValues: attributeValues, crosswalksMap: crosswalksMap, parentUri: 'entities/1', onAdd: jest.fn(), emptyTempAttributeUris: [] }, commonProps);
    var getDragEvent = function (draggingData, attributeType) { return ({
        active: {
            data: {
                current: __assign({}, draggingData)
            }
        },
        over: {
            id: attributeType.uri
        }
    }); };
    var setUp = function (props) {
        if (props === void 0) { props = defaultProps; }
        var Providers = function (_a) {
            var children = _a.children;
            return react_1.default.createElement(core_1.DndContext, null, children);
        };
        return (0, react_2.render)(react_1.default.createElement(InlineSimpleAttributesBlock_1.InlineSimpleAttributesBlock, __assign({}, props)), { wrapper: Providers });
    };
    it('should render values for each crosswalk', function () {
        setUp();
        expect(react_2.screen.getByText(attributeValues[0].value)).toBeInTheDocument();
        expect(react_2.screen.getAllByText(attributeValues[1].value)).toHaveLength(2);
    });
    describe('dnd behavior', function () {
        var onDragOver, onDragEnd;
        beforeAll(function () {
            core_1.useDndMonitor.mockImplementation(function (listeners) {
                onDragOver = listeners.onDragOver;
                onDragEnd = listeners.onDragEnd;
            });
        });
        it('should not allow drop relation crosswalk on entity attribute', function () {
            setUp();
            (0, react_2.act)(function () {
                onDragOver(getDragEvent({
                    type: 'Crosswalk',
                    crosswalk: {
                        uri: 'entities/1/crosswalks/1',
                        ownerType: 'relation',
                        type: 'configuration/sources/FB',
                        value: '123'
                    }
                }, defaultProps.attributeType));
            });
            expect(react_2.screen.queryByText('Drop here')).not.toBeInTheDocument();
        });
        it('should allow drop relation crosswalk on relation attribute', function () {
            var attributeType = {
                uri: 'configuration/relationTypes/hasAffiliations/attributes/Simple',
                type: 'String',
                name: 'Simple',
                label: 'Simple label'
            };
            var attributeValues = [
                {
                    type: attributeType.uri,
                    uri: 'entities/1/attributes/Simple/1',
                    value: '123',
                    ov: true
                },
                {
                    type: attributeType.uri,
                    uri: 'entities/1/attributes/Simple/2',
                    value: '789',
                    ov: false
                }
            ];
            setUp(__assign(__assign({}, defaultProps), { attributeType: attributeType, attributeValues: attributeValues }));
            (0, react_2.act)(function () {
                onDragOver(getDragEvent({
                    type: 'Crosswalk',
                    crosswalk: {
                        uri: 'relations/1/crosswalks/1',
                        ownerType: 'entity',
                        type: 'configuration/sources/FB',
                        value: '123'
                    }
                }, attributeType));
            });
            expect(react_2.screen.getByText('Drop here')).toBeInTheDocument();
            (0, react_2.act)(function () {
                onDragOver(getDragEvent({
                    type: 'Crosswalk',
                    crosswalk: {
                        uri: 'entities/1/crosswalks/1',
                        ownerType: 'relation',
                        type: 'configuration/sources/FB',
                        value: '123'
                    }
                }, attributeType));
            });
            expect(react_2.screen.getByText('Drop here')).toBeInTheDocument();
        });
        it('should not allow drop entity crosswalk on relation attribute', function () {
            var attributeType = {
                uri: 'configuration/relationTypes/hasAffiliations/attributes/Simple',
                type: 'String',
                name: 'Simple',
                label: 'Simple label'
            };
            var attributeValues = [
                {
                    type: attributeType.uri,
                    uri: 'entities/1/attributes/Simple/1',
                    value: '123',
                    ov: true
                },
                {
                    type: attributeType.uri,
                    uri: 'entities/1/attributes/Simple/2',
                    value: '789',
                    ov: false
                }
            ];
            setUp(__assign(__assign({}, defaultProps), { attributeType: attributeType, attributeValues: attributeValues }));
            (0, react_2.act)(function () {
                onDragOver(getDragEvent({
                    type: 'Crosswalk',
                    crosswalk: {
                        uri: 'entities/1/crosswalks/1',
                        ownerType: 'entity',
                        type: 'configuration/sources/FB',
                        value: '123'
                    }
                }, attributeType));
            });
            expect(react_2.screen.queryByText('Drop here')).not.toBeInTheDocument();
        });
        it('should not allow drop crosswalk on entity attribute without permission to create', function () {
            var readOnlyAttrType = __assign(__assign({}, attributeType), { access: ['READ'] });
            setUp(__assign(__assign({}, defaultProps), { attributeType: readOnlyAttrType, attributeValues: attributeValues }));
            (0, react_2.act)(function () {
                onDragOver(getDragEvent({
                    type: 'Crosswalk',
                    crosswalk: {
                        uri: 'entities/1/crosswalks/1',
                        ownerType: 'entity',
                        type: 'configuration/sources/FB',
                        value: '123'
                    }
                }, readOnlyAttrType));
            });
            expect(react_2.screen.queryByText('Drop here')).not.toBeInTheDocument();
        });
        it('should not allow drop crosswalk on attribute if parent attribute can not be edited', function () {
            setUp(__assign(__assign({}, defaultProps), { disableEdit: true }));
            (0, react_2.act)(function () {
                onDragOver(getDragEvent({
                    type: 'Crosswalk',
                    crosswalk: {
                        uri: 'entities/1/crosswalks/1',
                        ownerType: 'entity',
                        type: 'configuration/sources/FB',
                        value: '123'
                    }
                }, defaultProps.attributeType));
            });
            expect(react_2.screen.queryByText('Drop here')).not.toBeInTheDocument();
        });
        it('should call props.onAdd on crosswalk drop', function () {
            setUp();
            var crosswalk = {
                uri: 'entities/1/crosswalks/1',
                ownerType: 'entity',
                type: 'configuration/sources/FB',
                value: '123'
            };
            (0, react_2.act)(function () {
                onDragEnd(getDragEvent({
                    type: 'Crosswalk',
                    crosswalk: crosswalk
                }, defaultProps.attributeType));
            });
            expect(defaultProps.onAdd).toHaveBeenCalledWith({
                parentUri: defaultProps.parentUri,
                attributeType: defaultProps.attributeType,
                crosswalk: crosswalk,
                index: defaultProps.attributeValues.length
            });
        });
    });
});
