"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 __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = __importStar(require("react"));
var g6_1 = __importDefault(require("@antv/g6"));
var errorBoundary_1 = __importDefault(require("../../../errorBoundary"));
var useGraph_1 = __importDefault(require("../../hooks/useGraph"));
var createLoading_1 = __importDefault(require("../../../utils/createLoading"));
var utils_1 = require("../utils");
var utils_2 = require("../utils");
var constants_1 = require("../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: constants_1.defaultNodeAnchorPoints,
    nodeSize: 30,
    layout: defaultLayout,
    animate: true,
    nodeStateStyles: constants_1.defaultStateStyles,
    edgeStateStyles: constants_1.defaultStateStyles,
    showArrow: false,
    arrowType: 'triangle',
    autoFit: true,
};
var RadialGraph = function (props) {
    var uProps = (0, utils_1.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 ? constants_1.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 ? constants_1.defaultStateStyles : _h, _j = uProps.edgeStateStyles, edgeStateStyles = _j === void 0 ? constants_1.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_1.default.useRef(null);
    var graph = react_1.default.useRef(null);
    var graphId = (0, utils_1.getGraphId)(graph);
    (0, useGraph_1.default)(graphs[graphId], uProps, container);
    var arrowOffset = (Array.isArray(nodeSize) ? nodeSize[0] : nodeSize) / 2;
    (0, react_1.useEffect)(function () {
        var graphSize = (0, utils_1.getGraphSize)(width, height, container);
        var graph = graphs[graphId];
        if (!graph) {
            graph = new g6_1.default.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({}, constants_1.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 && (0, utils_1.getDefaultEdgeArrowCfg)(arrowOffset, arrowType))), edgeCfg === null || edgeCfg === void 0 ? void 0 : edgeCfg.style),
            };
        });
        (0, utils_2.renderGraph)(graph, data);
        if (onReady) {
            onReady(graph);
        }
        return function () {
            if (graphs[graphId]) {
                graphs[graphId].destroy();
                delete graphs[graphId];
            }
        };
    }, []);
    return (react_1.default.createElement(errorBoundary_1.default, { errorTemplate: errorTemplate },
        loading && react_1.default.createElement(createLoading_1.default, { loadingTemplate: loadingTemplate }),
        react_1.default.createElement("div", { className: className, style: style, ref: container })));
};
exports.default = RadialGraph;
