"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);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useLoopRenderer = void 0;
var react_1 = require("react");
var core_1 = require("@react-sigma/core");
var SigmaCustomRenderersContext_1 = require("../contexts/SigmaCustomRenderersContext");
var selfRelationLoop_1 = require("../rendering/canvas/selfRelationLoop");
var useLoopRenderer = function (graph) {
    var sigma = (0, core_1.useSigma)();
    var _a = (0, SigmaCustomRenderersContext_1.useSigmaCustomRenderers)(), addCustomRenderers = _a.addRenderers, canvases = _a.canvases;
    var selfRelationsPathsMap = (0, react_1.useRef)({});
    var selfRelationLoopsCanvas = canvases.selfRelationLoops;
    (0, react_1.useEffect)(function () {
        addCustomRenderers({
            selfRelationLoops: {
                renderer: function (context) {
                    graph.forEachEdge(function (edgeId, edgeData, sourceId) {
                        if (edgeData.loop) {
                            delete selfRelationsPathsMap.current[edgeId];
                            var edgeDisplayData = sigma.getEdgeDisplayData(edgeId);
                            var sourceDisplayData = sigma.getNodeDisplayData(sourceId);
                            var isHidden = edgeDisplayData.hidden || sourceDisplayData.hidden;
                            if (isHidden)
                                return;
                            var path = (0, selfRelationLoop_1.drawSelfRelationLoop)(context, __assign(__assign({}, edgeDisplayData), { size: sigma.scaleSize(edgeDisplayData.size) }), __assign(__assign(__assign({}, sourceDisplayData), sigma.framedGraphToViewport(sourceDisplayData)), { size: sigma.scaleSize(sourceDisplayData.size) }));
                            selfRelationsPathsMap.current[edgeId] = path;
                        }
                    });
                },
                insertBefore: 'edges'
            }
        });
    }, [addCustomRenderers, graph, sigma]);
    return { selfRelationsPathsMap: selfRelationsPathsMap.current, selfRelationLoopsCanvas: selfRelationLoopsCanvas };
};
exports.useLoopRenderer = useLoopRenderer;
