"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 __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useDataModelGraphAppearance = exports.INACTIVE_LABEL_COLOR = exports.RELATED_DASHED_EDGE_COLOR = exports.RELATED_EDGE_COLOR = exports.SELECTED_EDGE_COLOR = exports.INACTIVE_EDGE_COLOR = exports.DASHED_EDGE_COLOR = exports.EDGE_COLOR = void 0;
var react_1 = require("react");
var core_1 = require("@react-sigma/core");
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var graphDataTypes_1 = require("../../types/graphDataTypes");
var useLoopRenderer_1 = require("../../hooks/useLoopRenderer");
var useEdgeLabelsRenderer_1 = require("./useEdgeLabelsRenderer");
var useSelectedNodeRenderer_1 = require("./useSelectedNodeRenderer");
var interaction_inline_svg_1 = require("../icons/interaction.inline.svg");
var DEFAULT_NODE_STYLE = {
    type: 'image',
    size: 28,
    color: 'rgba(55, 71, 79, 1)'
};
var DEFAULT_EDGE_STYLE = {
    size: 1
};
exports.EDGE_COLOR = 'rgba(0, 0, 0, 0.2)';
exports.DASHED_EDGE_COLOR = 'rgba(0, 0, 0, 0.2)';
exports.INACTIVE_EDGE_COLOR = 'rgba(224, 224, 224, 1)';
exports.SELECTED_EDGE_COLOR = 'rgba(79, 79, 79, 1)';
exports.RELATED_EDGE_COLOR = 'rgba(130, 130, 130, 1)';
exports.RELATED_DASHED_EDGE_COLOR = 'rgba(79, 79, 79, 1)';
exports.INACTIVE_LABEL_COLOR = 'rgba(0, 0, 0, 0.38)';
var RELATION_EDGES_LABELS_AMOUNT_THRESHOLD = 29;
var useDataModelGraphAppearance = function (_a) {
    var graph = _a.graph, selectedNode = _a.selectedNode, selectedEdges = _a.selectedEdges, onNodeClick = _a.onNodeClick, onEdgeLabelClick = _a.onEdgeLabelClick, hideInteractionTypes = _a.hideInteractionTypes, _b = _a.relationEdgesLabelsAmountThreshold, relationEdgesLabelsAmountThreshold = _b === void 0 ? RELATION_EDGES_LABELS_AMOUNT_THRESHOLD : _b;
    var sigma = (0, core_1.useSigma)();
    var container = (0, core_1.useSigmaContext)().container;
    var _c = (0, react_1.useState)(null), hoveredNode = _c[0], setHoveredNode = _c[1];
    var registerEvents = (0, core_1.useRegisterEvents)();
    var setSettings = (0, core_1.useSetSettings)();
    (0, useLoopRenderer_1.useLoopRenderer)(graph);
    var hoveredEdge = (0, useEdgeLabelsRenderer_1.useEdgeLabelsRenderer)({ graph: graph, onEdgeLabelClick: onEdgeLabelClick });
    (0, useSelectedNodeRenderer_1.useSelectedNodeRenderer)(graph);
    var relationsEdgesAmount = (0, react_1.useMemo)(function () {
        return graph.reduceEdges(function (count, _edgeId, edgeData) {
            var _a;
            if (((_a = edgeData.relationTypes) === null || _a === void 0 ? void 0 : _a.length) > 0) {
                return count + 1;
            }
            return count;
        }, 0);
    }, [graph]);
    (0, react_1.useEffect)(function () {
        registerEvents({
            enterNode: function (_a) {
                var node = _a.node;
                setHoveredNode(node);
            },
            leaveNode: function () {
                setHoveredNode(null);
            },
            clickNode: function (_a) {
                var node = _a.node;
                onNodeClick(node);
            }
        });
    }, [sigma, registerEvents, onNodeClick]);
    (0, react_1.useEffect)(function () {
        container.style.cursor = hoveredNode || hoveredEdge ? 'pointer' : 'default';
    }, [container, hoveredNode, hoveredEdge]);
    (0, react_1.useEffect)(function () {
        var graph = sigma.getGraph();
        var selectedNodeNeighbours;
        try {
            selectedNodeNeighbours = graph.neighbors(selectedNode);
        }
        catch (_a) {
            //do nothing
        }
        setSettings({
            nodeReducer: function (node, data) {
                var isSelectedNode = node === selectedNode;
                var isInactiveNode = (selectedNode &&
                    selectedNodeNeighbours &&
                    __spreadArray(__spreadArray([], selectedNodeNeighbours, true), [selectedNode], false).every(function (n) { return n !== node; })) ||
                    ((selectedEdges === null || selectedEdges === void 0 ? void 0 : selectedEdges.length) > 0 &&
                        selectedEdges.every(function (selectedEdge) { return !graph.hasExtremity(selectedEdge, node); })) ||
                    false;
                var newData = __assign(__assign({}, DEFAULT_NODE_STYLE), data);
                if (data.nodeType === graphDataTypes_1.DataModelGraphNodeType.interactionType) {
                    newData.hasBorder = true;
                    newData.image = (0, mdm_sdk_1.svg2Url)(interaction_inline_svg_1.interactionIcon);
                }
                if (isInactiveNode) {
                    newData.inactive = true;
                    newData.labelColor = exports.INACTIVE_LABEL_COLOR;
                    newData.secondaryLabelColor = exports.INACTIVE_LABEL_COLOR;
                }
                if (isSelectedNode) {
                    newData.selected = true;
                    newData.zIndex = 100;
                }
                if (hoveredNode !== node && !isSelectedNode) {
                    delete newData.secondaryLabel;
                }
                if (data.nodeType === graphDataTypes_1.DataModelGraphNodeType.interactionType && hideInteractionTypes) {
                    newData.hidden = true;
                }
                return newData;
            },
            edgeReducer: function (edge, data) {
                var isDashedLine = data.type === 'dashed';
                var newData = __assign(__assign({}, DEFAULT_EDGE_STYLE), data);
                var isSelected = selectedEdges === null || selectedEdges === void 0 ? void 0 : selectedEdges.includes(edge);
                newData.selected = isSelected;
                var showOnlyRelatedEdgesLabels = relationsEdgesAmount > relationEdgesLabelsAmountThreshold;
                if (selectedNode) {
                    if (graph.hasExtremity(edge, selectedNode)) {
                        newData.color = isDashedLine ? exports.RELATED_DASHED_EDGE_COLOR : exports.RELATED_EDGE_COLOR;
                    }
                    else {
                        newData.color = exports.INACTIVE_EDGE_COLOR;
                        newData.labelColor = exports.INACTIVE_LABEL_COLOR;
                        if (showOnlyRelatedEdgesLabels) {
                            delete newData.label;
                        }
                    }
                }
                else if (selectedEdges === null || selectedEdges === void 0 ? void 0 : selectedEdges.length) {
                    newData.color = isSelected ? exports.SELECTED_EDGE_COLOR : exports.INACTIVE_EDGE_COLOR;
                    newData.labelColor = isSelected ? undefined : exports.INACTIVE_LABEL_COLOR;
                    if (showOnlyRelatedEdgesLabels && !isSelected) {
                        delete newData.label;
                    }
                }
                else {
                    newData.color = isDashedLine ? exports.DASHED_EDGE_COLOR : exports.EDGE_COLOR;
                    if (showOnlyRelatedEdgesLabels) {
                        delete newData.label;
                    }
                }
                return newData;
            }
        });
    }, [
        sigma,
        setSettings,
        selectedNode,
        selectedEdges,
        hoveredEdge,
        hoveredNode,
        relationsEdgesAmount,
        relationEdgesLabelsAmountThreshold,
        hideInteractionTypes
    ]);
};
exports.useDataModelGraphAppearance = useDataModelGraphAppearance;
