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 React, { useEffect } from 'react';
import G6 from '@antv/g6';
import ErrorBoundary from '../../../errorBoundary';
import useGraph from '../../hooks/useGraph';
import ChartLoading from '../../../utils/createLoading';
import { getGraphSize, getGraphId, getDefaultEdgeArrowCfg, useProps } from '../utils';
import { renderGraph } from '../utils';
import { defaultNodeAnchorPoints, defaultStateStyles, defaultNodeStyle } from '../constants';
var graphs = {};
var defaultLayout = {
    type: 'dendrogram',
    direction: 'LR',
    nodeSep: 20,
    rankSep: 100,
    radial: true,
};
var defaultProps = {
    nodeType: 'circle',
    linkCenter: true,
    edgeType: 'line',
    behaviors: ['zoom-canvas', 'drag-canvas'],
    nodeAnchorPoints: defaultNodeAnchorPoints,
    nodeSize: 30,
    layout: defaultLayout,
    animate: true,
    nodeStateStyles: defaultStateStyles,
    edgeStateStyles: defaultStateStyles,
    showArrow: false,
    arrowType: 'triangle',
    autoFit: true,
};
var RadialGraph = function (props) {
    var uProps = useProps(props, defaultProps);
    var data = uProps.data, className = uProps.className, style = uProps.style, width = uProps.width, height = uProps.height, _a = uProps.nodeType, nodeType = _a === void 0 ? 'circle' : _a, _b = uProps.linkCenter, linkCenter = _b === void 0 ? true : _b, _c = uProps.edgeType, edgeType = _c === void 0 ? 'line' : _c, _d = uProps.behaviors, behaviors = _d === void 0 ? ['zoom-canvas', 'drag-canvas'] : _d, _e = uProps.nodeAnchorPoints, nodeAnchorPoints = _e === void 0 ? defaultNodeAnchorPoints : _e, _f = uProps.nodeSize, nodeSize = _f === void 0 ? 30 : _f, layout = uProps.layout, _g = uProps.animate, animate = _g === void 0 ? true : _g, nodeCfg = uProps.nodeCfg, edgeCfg = uProps.edgeCfg, _h = uProps.nodeStateStyles, nodeStateStyles = _h === void 0 ? defaultStateStyles : _h, _j = uProps.edgeStateStyles, edgeStateStyles = _j === void 0 ? defaultStateStyles : _j, _k = uProps.showArrow, showArrow = _k === void 0 ? false : _k, _l = uProps.arrowType, arrowType = _l === void 0 ? 'triangle' : _l, _m = uProps.autoFit, autoFit = _m === void 0 ? true : _m, onReady = uProps.onReady, loading = uProps.loading, loadingTemplate = uProps.loadingTemplate, errorTemplate = uProps.errorTemplate;
    var container = React.useRef(null);
    var graph = React.useRef(null);
    var graphId = getGraphId(graph);
    useGraph(graphs[graphId], uProps, container);
    var arrowOffset = (Array.isArray(nodeSize) ? nodeSize[0] : nodeSize) / 2;
    useEffect(function () {
        var graphSize = getGraphSize(width, height, container);
        var graph = graphs[graphId];
        if (!graph) {
            graph = new G6.TreeGraph({
                container: container.current,
                width: graphSize[0],
                height: graphSize[1],
                animate: animate,
                linkCenter: linkCenter,
                modes: {
                    default: behaviors,
                },
                defaultNode: {
                    type: nodeType,
                    size: nodeSize,
                    anchorPoints: nodeAnchorPoints,
                },
                defaultEdge: {
                    type: edgeType,
                },
                nodeStateStyles: nodeStateStyles,
                edgeStateStyles: edgeStateStyles,
                layout: layout,
                fitView: autoFit,
            });
            graphs[graphId] = graph;
        }
        graph.node(function (node) {
            if (typeof nodeCfg === 'function') {
                return nodeCfg(node, graph);
            }
            return __assign(__assign({}, nodeCfg), { style: __assign(__assign({}, defaultNodeStyle), nodeCfg === null || nodeCfg === void 0 ? void 0 : nodeCfg.style) });
        });
        graph.edge(function (edge) {
            if (typeof edgeCfg === 'function') {
                return edgeCfg(edge, graph);
            }
            return {
                style: __assign(__assign({ stroke: '#ccc' }, (showArrow && getDefaultEdgeArrowCfg(arrowOffset, arrowType))), edgeCfg === null || edgeCfg === void 0 ? void 0 : edgeCfg.style),
            };
        });
        renderGraph(graph, data);
        if (onReady) {
            onReady(graph);
        }
        return function () {
            if (graphs[graphId]) {
                graphs[graphId].destroy();
                delete graphs[graphId];
            }
        };
    }, []);
    return (React.createElement(ErrorBoundary, { errorTemplate: errorTemplate },
        loading && React.createElement(ChartLoading, { loadingTemplate: loadingTemplate }),
        React.createElement("div", { className: className, style: style, ref: container })));
};
export default RadialGraph;
