"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 createLoading_1 = __importDefault(require("../../../utils/createLoading"));
var errorBoundary_1 = __importDefault(require("../../../errorBoundary"));
var useGraph_1 = __importDefault(require("../../hooks/useGraph"));
var customItems_1 = require("../customItems");
var constants_1 = require("../constants");
var utils_1 = require("../utils");
var defaultNodeStyle = {
    fill: '#91d5ff',
    stroke: '#40a9ff',
    radius: 2,
};
var defaultLayout = {
    type: 'compactBox',
    direction: 'TB',
    getId: function getId(d) {
        return d.id;
    },
    getHeight: function getHeight() {
        return 16;
    },
    getWidth: function getWidth() {
        return 16;
    },
    getVGap: function getVGap() {
        return 40;
    },
    getHGap: function getHGap() {
        return 70;
    },
};
var defaultProps = {
    animate: true,
    nodeType: 'rect',
    edgeType: 'polyline',
    nodeSize: constants_1.defaultNodeSize,
    behaviors: ['drag-canvas', 'zoom-canvas'],
    nodeLabelCfg: {
        style: constants_1.defaultLabelStyle,
    },
    layout: defaultLayout,
    showMarker: false,
    showArrow: true,
    arrowType: 'triangle',
    nodeStateStyles: constants_1.defaultStateStyles,
    edgeStateStyles: constants_1.defaultStateStyles,
    autoFit: true,
    style: {
        height: 'inherit',
    },
};
var graphs = {};
var OrganizationalGraph = 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.animate, animate = _a === void 0 ? true : _a, _b = uProps.nodeType, nodeType = _b === void 0 ? 'rect' : _b, _c = uProps.edgeType, edgeType = _c === void 0 ? 'polyline' : _c, _d = uProps.nodeSize, nodeSize = _d === void 0 ? constants_1.defaultNodeSize : _d, _e = uProps.behaviors, behaviors = _e === void 0 ? ['drag-canvas', 'zoom-canvas'] : _e, nodeLabelCfg = uProps.nodeLabelCfg, nodeCfg = uProps.nodeCfg, _f = uProps.layout, layout = _f === void 0 ? defaultLayout : _f, _g = uProps.showMarker, showMarker = _g === void 0 ? false : _g, _h = uProps.showArrow, showArrow = _h === void 0 ? true : _h, _j = uProps.arrowType, arrowType = _j === void 0 ? 'triangle' : _j, minimapCfg = uProps.minimapCfg, edgeCfg = uProps.edgeCfg, markerStyle = uProps.markerStyle, _k = uProps.nodeStateStyles, nodeStateStyles = _k === void 0 ? constants_1.defaultStateStyles : _k, _l = uProps.edgeStateStyles, edgeStateStyles = _l === void 0 ? constants_1.defaultStateStyles : _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[1] : nodeSize) / 2;
    (0, react_1.useEffect)(function () {
        var graphSize = (0, utils_1.getGraphSize)(width, height, container);
        if (nodeType === 'icon-node') {
            (0, customItems_1.registerIconNode)();
        }
        var graph = graphs[graphId];
        if (!graph) {
            graph = new g6_1.default.TreeGraph({
                container: container.current,
                width: graphSize[0],
                height: graphSize[1],
                linkCenter: true,
                animate: animate,
                modes: {
                    default: behaviors,
                },
                defaultNode: {
                    type: nodeType,
                    size: nodeSize,
                    labelCfg: nodeLabelCfg,
                    markerStyle: markerStyle,
                    showMarker: showMarker,
                },
                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 {
                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 __assign(__assign({}, edgeCfg), { style: __assign(__assign({ stroke: '#91d5ff' }, (showArrow && (0, utils_1.getDefaultEdgeArrowCfg)(arrowOffset, arrowType, '#91d5ff'))), edgeCfg === null || edgeCfg === void 0 ? void 0 : edgeCfg.style) });
        });
        (0, utils_1.processMinimap)(minimapCfg, graph);
        (0, utils_1.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 = OrganizationalGraph;
