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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
    return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (g && (g = 0, op[0] && (_ = 0)), _) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
import { renderHook, act } from '@testing-library/react-hooks';
import { last, omit } from 'ramda';
import { useSigma, useSetSettings, useRegisterEvents } from '@react-sigma/core';
import { DASHED_EDGE_COLOR, EDGE_COLOR, INACTIVE_EDGE_COLOR, INACTIVE_LABEL_COLOR, RELATED_DASHED_EDGE_COLOR, RELATED_EDGE_COLOR, SELECTED_EDGE_COLOR, useDataModelGraphAppearance } from './useDataModelGraphAppearance';
import { DataModelGraphNodeType } from '../../types/graphDataTypes';
import { createDefaultGraph } from './defaultGraph.test-data';
jest.mock('@react-sigma/core', function () {
    var sigma = jest.requireActual('@react-sigma/core');
    return __assign(__assign({}, sigma), { useSigma: jest.fn(), useRegisterEvents: jest.fn().mockReturnValue(jest.fn()), useSetSettings: jest.fn().mockReturnValue(jest.fn()), useSigmaContext: jest.fn().mockReturnValue({
            container: {
                style: {}
            }
        }) });
});
jest.mock('../../hooks/useLoopRenderer', function () { return ({
    useLoopRenderer: jest.fn()
}); });
jest.mock('./useEdgeLabelsRenderer', function () { return ({
    useEdgeLabelsRenderer: jest.fn()
}); });
jest.mock('./useSelectedNodeRenderer', function () { return ({
    useSelectedNodeRenderer: jest.fn()
}); });
describe('useDataModelGraphAppearance tests', function () {
    var setUp = function (props) {
        var graph = createDefaultGraph();
        useSigma.mockReturnValue({
            getGraph: function () { return graph; }
        });
        return __assign({ graph: graph }, renderHook(useDataModelGraphAppearance, {
            initialProps: __assign({ graph: graph, selectedNode: null, onNodeClick: jest.fn(), selectedEdges: null, onEdgeLabelClick: jest.fn(), hideInteractionTypes: false }, props)
        }));
    };
    var setSettings = jest.fn();
    var registerEvents = jest.fn();
    beforeAll(function () {
        useSetSettings.mockReturnValue(setSettings);
        useRegisterEvents.mockReturnValue(registerEvents);
    });
    var getNodeReducer = function () { return last(setSettings.mock.calls)[0].nodeReducer; };
    var getEdgeReducer = function () { return last(setSettings.mock.calls)[0].edgeReducer; };
    var getEnterNode = function () { return last(registerEvents.mock.calls)[0].enterNode; };
    var getClickNode = function () { return last(registerEvents.mock.calls)[0].clickNode; };
    afterEach(function () {
        jest.clearAllMocks();
    });
    it('should call onNodeClick on clickNode event', function () {
        var onNodeClick = jest.fn();
        setUp({ onNodeClick: onNodeClick });
        var clickNode = getClickNode();
        act(function () {
            clickNode({ node: 'hca' });
        });
        expect(onNodeClick).toHaveBeenCalledWith('hca');
    });
    describe('nodes reducer behavior', function () {
        var getDefaultNodeAttributes = function (_a) {
            var _secondaryLabel = _a.secondaryLabel, attributes = __rest(_a, ["secondaryLabel"]);
            return (__assign(__assign({}, attributes), { type: 'image', size: 28, color: 'rgba(55, 71, 79, 1)' }));
        };
        it('should apply default node style', function () {
            var _a = setUp(), result = _a.result, graph = _a.graph;
            var nodeReducer = getNodeReducer();
            expect(nodeReducer('hcp', graph.getNodeAttributes('hcp'))).toEqual(__assign(__assign({}, getDefaultNodeAttributes(graph.getNodeAttributes('hcp'))), { image: 'HCP.svg', label: 'HCP', nodeType: DataModelGraphNodeType.entityType }));
            expect(nodeReducer('hca', graph.getNodeAttributes('hca'))).toEqual(__assign(__assign({}, getDefaultNodeAttributes(graph.getNodeAttributes('hca'))), { image: 'HCA.svg', label: 'HCA', nodeType: DataModelGraphNodeType.entityType }));
            expect(nodeReducer('gpo', graph.getNodeAttributes('gpo'))).toEqual(__assign(__assign({}, getDefaultNodeAttributes(graph.getNodeAttributes('gpo'))), { label: 'GPO', nodeType: DataModelGraphNodeType.entityType }));
            expect(nodeReducer('prescriptions', graph.getNodeAttributes('prescriptions'))).toEqual(__assign(__assign({}, getDefaultNodeAttributes(graph.getNodeAttributes('prescriptions'))), { hasBorder: true, image: expect.stringContaining('data:image/svg+xml'), label: 'Prescriptions', nodeType: DataModelGraphNodeType.interactionType }));
            expect(result.error).toBeUndefined();
        });
        it('should apply hovered node style', function () { return __awaiter(void 0, void 0, void 0, function () {
            var _a, graph, result, enterNode, nodeReducer;
            return __generator(this, function (_b) {
                _a = setUp(), graph = _a.graph, result = _a.result;
                enterNode = getEnterNode();
                act(function () {
                    enterNode({ node: 'hcp' });
                });
                nodeReducer = getNodeReducer();
                expect(nodeReducer('hcp', graph.getNodeAttributes('hcp'))).toEqual(__assign(__assign({}, getDefaultNodeAttributes(graph.getNodeAttributes('hcp'))), { image: 'HCP.svg', label: 'HCP', secondaryLabel: '12 attributes', nodeType: DataModelGraphNodeType.entityType }));
                expect(result.error).toBeUndefined();
                return [2 /*return*/];
            });
        }); });
        it('should apply correct styles when selectedNode is specified', function () {
            var _a = setUp({ selectedNode: 'hcp' }), graph = _a.graph, result = _a.result;
            var nodeReducer = getNodeReducer();
            expect(nodeReducer('hcp', graph.getNodeAttributes('hcp'))).toEqual(__assign(__assign({}, getDefaultNodeAttributes(graph.getNodeAttributes('hcp'))), { image: 'HCP.svg', label: 'HCP', secondaryLabel: '12 attributes', selected: true, zIndex: 100, nodeType: DataModelGraphNodeType.entityType }));
            expect(nodeReducer('hca', graph.getNodeAttributes('hca'))).toEqual(__assign(__assign({}, getDefaultNodeAttributes(graph.getNodeAttributes('hca'))), { image: 'HCA.svg', label: 'HCA', nodeType: DataModelGraphNodeType.entityType }));
            expect(nodeReducer('gpo', graph.getNodeAttributes('gpo'))).toEqual(__assign(__assign({}, getDefaultNodeAttributes(graph.getNodeAttributes('gpo'))), { label: 'GPO', nodeType: DataModelGraphNodeType.entityType, inactive: true, labelColor: INACTIVE_LABEL_COLOR, secondaryLabelColor: INACTIVE_LABEL_COLOR }));
            expect(nodeReducer('prescriptions', graph.getNodeAttributes('prescriptions'))).toEqual(__assign(__assign({}, getDefaultNodeAttributes(graph.getNodeAttributes('prescriptions'))), { hasBorder: true, image: expect.stringContaining('data:image/svg+xml'), label: 'Prescriptions', nodeType: DataModelGraphNodeType.interactionType }));
            expect(result.error).toBeUndefined();
        });
        it('should apply correct styles when selectedEdges is specified', function () {
            var _a = setUp({ selectedEdges: ['hcp->hca'] }), graph = _a.graph, result = _a.result;
            var nodeReducer = getNodeReducer();
            expect(nodeReducer('hcp', graph.getNodeAttributes('hcp'))).toEqual(__assign(__assign({}, getDefaultNodeAttributes(graph.getNodeAttributes('hcp'))), { image: 'HCP.svg', label: 'HCP', nodeType: DataModelGraphNodeType.entityType }));
            expect(nodeReducer('hca', graph.getNodeAttributes('hca'))).toEqual(__assign(__assign({}, getDefaultNodeAttributes(graph.getNodeAttributes('hca'))), { image: 'HCA.svg', label: 'HCA', nodeType: DataModelGraphNodeType.entityType }));
            expect(nodeReducer('gpo', graph.getNodeAttributes('gpo'))).toEqual(__assign(__assign({}, getDefaultNodeAttributes(graph.getNodeAttributes('gpo'))), { label: 'GPO', nodeType: DataModelGraphNodeType.entityType, inactive: true, labelColor: INACTIVE_LABEL_COLOR, secondaryLabelColor: INACTIVE_LABEL_COLOR }));
            expect(nodeReducer('prescriptions', graph.getNodeAttributes('prescriptions'))).toEqual(__assign(__assign({}, getDefaultNodeAttributes(graph.getNodeAttributes('prescriptions'))), { hasBorder: true, image: expect.stringContaining('data:image/svg+xml'), label: 'Prescriptions', nodeType: DataModelGraphNodeType.interactionType, inactive: true, labelColor: INACTIVE_LABEL_COLOR, secondaryLabelColor: INACTIVE_LABEL_COLOR }));
            expect(result.error).toBeUndefined();
        });
        it('should hide interaction types when hideInteractionTypes is true', function () {
            var _a = setUp({ hideInteractionTypes: true }), graph = _a.graph, result = _a.result;
            var nodeReducer = getNodeReducer();
            expect(nodeReducer('prescriptions', graph.getNodeAttributes('prescriptions'))).toEqual(__assign(__assign({}, getDefaultNodeAttributes(graph.getNodeAttributes('prescriptions'))), { hasBorder: true, image: expect.stringContaining('data:image/svg+xml'), label: 'Prescriptions', nodeType: DataModelGraphNodeType.interactionType, hidden: true }));
            expect(result.error).toBeUndefined();
        });
    });
    describe('edges reducer behavior', function () {
        var getDefaultEdgeAttributes = function (attributes) { return (__assign(__assign({}, attributes), { size: 1 })); };
        it('should apply default edge style', function () {
            var _a = setUp(), result = _a.result, graph = _a.graph;
            var edgeReducer = getEdgeReducer();
            expect(edgeReducer('hcp->hca', graph.getEdgeAttributes('hcp->hca'))).toEqual(__assign(__assign({}, getDefaultEdgeAttributes(graph.getEdgeAttributes('hcp->hca'))), { color: EDGE_COLOR }));
            expect(edgeReducer('hca->gpo', graph.getEdgeAttributes('hca->gpo'))).toEqual(__assign(__assign({}, getDefaultEdgeAttributes(graph.getEdgeAttributes('hca->gpo'))), { color: EDGE_COLOR }));
            expect(edgeReducer('hcp->prescriptions', graph.getEdgeAttributes('hcp->prescriptions'))).toEqual(__assign(__assign({}, getDefaultEdgeAttributes(graph.getEdgeAttributes('hcp->prescriptions'))), { type: 'dashed', color: DASHED_EDGE_COLOR }));
            expect(result.error).toBeUndefined();
        });
        it('should apply correct styles when selectedNode is specified', function () {
            var _a = setUp({ selectedNode: 'hcp' }), graph = _a.graph, result = _a.result;
            var edgeReducer = getEdgeReducer();
            expect(edgeReducer('hcp->hca', graph.getEdgeAttributes('hcp->hca'))).toEqual(__assign(__assign({}, getDefaultEdgeAttributes(graph.getEdgeAttributes('hcp->hca'))), { color: RELATED_EDGE_COLOR }));
            expect(edgeReducer('hca->gpo', graph.getEdgeAttributes('hca->gpo'))).toEqual(__assign(__assign({}, getDefaultEdgeAttributes(graph.getEdgeAttributes('hca->gpo'))), { color: INACTIVE_EDGE_COLOR, labelColor: INACTIVE_LABEL_COLOR }));
            expect(edgeReducer('hcp->prescriptions', graph.getEdgeAttributes('hcp->prescriptions'))).toEqual(__assign(__assign({}, getDefaultEdgeAttributes(graph.getEdgeAttributes('hcp->prescriptions'))), { type: 'dashed', color: RELATED_DASHED_EDGE_COLOR }));
            expect(result.error).toBeUndefined();
        });
        it('should apply correct styles when selectedEdges is specified', function () {
            var _a = setUp({ selectedEdges: ['hcp->hca'] }), graph = _a.graph, result = _a.result;
            var edgeReducer = getEdgeReducer();
            expect(edgeReducer('hcp->hca', graph.getEdgeAttributes('hcp->hca'))).toEqual(__assign(__assign({}, getDefaultEdgeAttributes(graph.getEdgeAttributes('hcp->hca'))), { color: SELECTED_EDGE_COLOR, selected: true }));
            expect(edgeReducer('hca->gpo', graph.getEdgeAttributes('hca->gpo'))).toEqual(__assign(__assign({}, getDefaultEdgeAttributes(graph.getEdgeAttributes('hca->gpo'))), { selected: false, color: INACTIVE_EDGE_COLOR, labelColor: INACTIVE_LABEL_COLOR }));
            expect(edgeReducer('hcp->prescriptions', graph.getEdgeAttributes('hcp->prescriptions'))).toEqual(__assign(__assign({}, getDefaultEdgeAttributes(graph.getEdgeAttributes('hcp->prescriptions'))), { type: 'dashed', selected: false, color: INACTIVE_EDGE_COLOR, labelColor: INACTIVE_LABEL_COLOR }));
            expect(result.error).toBeUndefined();
        });
        it('should remove edge label prop when relations edges amount bigger than relationEdgesLabelsAmountThreshold', function () {
            var _a = setUp({ relationEdgesLabelsAmountThreshold: 1 }), graph = _a.graph, result = _a.result;
            var edgeReducer = getEdgeReducer();
            expect(edgeReducer('hcp->hca', graph.getEdgeAttributes('hcp->hca'))).toEqual(__assign(__assign({}, omit(['label'], getDefaultEdgeAttributes(graph.getEdgeAttributes('hcp->hca')))), { color: EDGE_COLOR }));
            expect(edgeReducer('hca->gpo', graph.getEdgeAttributes('hca->gpo'))).toEqual(__assign(__assign({}, omit(['label'], getDefaultEdgeAttributes(graph.getEdgeAttributes('hca->gpo')))), { color: EDGE_COLOR }));
            expect(edgeReducer('hcp->prescriptions', graph.getEdgeAttributes('hcp->prescriptions'))).toEqual(__assign(__assign({}, getDefaultEdgeAttributes(graph.getEdgeAttributes('hcp->prescriptions'))), { type: 'dashed', color: DASHED_EDGE_COLOR }));
            expect(result.error).toBeUndefined();
        });
        it('should keep edge label prop for edge related with selected node even if relations edges amount bigger than relationEdgesLabelsAmountThreshold', function () {
            var _a = setUp({ relationEdgesLabelsAmountThreshold: 1, selectedNode: 'hcp' }), graph = _a.graph, result = _a.result;
            var edgeReducer = getEdgeReducer();
            expect(edgeReducer('hcp->hca', graph.getEdgeAttributes('hcp->hca'))).toEqual(__assign(__assign({}, getDefaultEdgeAttributes(graph.getEdgeAttributes('hcp->hca'))), { color: RELATED_EDGE_COLOR }));
            expect(edgeReducer('hca->gpo', graph.getEdgeAttributes('hca->gpo'))).toEqual(__assign(__assign({}, omit(['label'], getDefaultEdgeAttributes(graph.getEdgeAttributes('hca->gpo')))), { color: INACTIVE_EDGE_COLOR, labelColor: INACTIVE_LABEL_COLOR }));
            expect(edgeReducer('hcp->prescriptions', graph.getEdgeAttributes('hcp->prescriptions'))).toEqual(__assign(__assign({}, getDefaultEdgeAttributes(graph.getEdgeAttributes('hcp->prescriptions'))), { type: 'dashed', color: RELATED_DASHED_EDGE_COLOR }));
            expect(result.error).toBeUndefined();
        });
        it('should keep edge label prop for selected edge even if relations edges amount bigger than relationEdgesLabelsAmountThreshold', function () {
            var _a = setUp({ relationEdgesLabelsAmountThreshold: 1, selectedEdges: ['hcp->hca'] }), graph = _a.graph, result = _a.result;
            var edgeReducer = getEdgeReducer();
            expect(edgeReducer('hcp->hca', graph.getEdgeAttributes('hcp->hca'))).toEqual(__assign(__assign({}, getDefaultEdgeAttributes(graph.getEdgeAttributes('hcp->hca'))), { color: SELECTED_EDGE_COLOR, selected: true }));
            expect(edgeReducer('hca->gpo', graph.getEdgeAttributes('hca->gpo'))).toEqual(__assign(__assign({}, omit(['label'], getDefaultEdgeAttributes(graph.getEdgeAttributes('hca->gpo')))), { selected: false, color: INACTIVE_EDGE_COLOR, labelColor: INACTIVE_LABEL_COLOR }));
            expect(edgeReducer('hcp->prescriptions', graph.getEdgeAttributes('hcp->prescriptions'))).toEqual(__assign(__assign({}, getDefaultEdgeAttributes(graph.getEdgeAttributes('hcp->prescriptions'))), { type: 'dashed', selected: false, color: INACTIVE_EDGE_COLOR, labelColor: INACTIVE_LABEL_COLOR }));
            expect(result.error).toBeUndefined();
        });
    });
});
