"use strict";
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 constants_1 = require("./constants");
var utils_1 = require("./utils");
var customItems_1 = require("./customItems");
(0, customItems_1.registerCustomItems)();
var defaultNodeStyle = {
    stroke: '#40a9ff',
};
var defaultNodeAnchorPoints = [
    [0, 0.5],
    [1, 0.5],
];
var defaultEdgeStyle = {
    stroke: '#ccc',
    endArrow: {
        path: g6_1.default.Arrow.vee(10, 10),
        fill: '#ccc',
    },
};
var defaultLayout = {
    type: 'indented',
    direction: 'LR',
    dropCap: false,
    indent: 250,
    getHeight: function () {
        return 60;
    },
    getWidth: function () {
        return 100;
    },
};
var defaultProps = {
    nodeType: 'card-node',
    edgeType: 'cubic-horizontal',
    behaviors: ['zoom-canvas', 'drag-canvas'],
    nodeSize: constants_1.defaultNodeSize,
    nodeLabelCfg: constants_1.defaultLabelCfg,
    nodeAnchorPoints: defaultNodeAnchorPoints,
    layout: defaultLayout,
    nodeStyle: defaultNodeStyle,
    edgeStyle: defaultEdgeStyle,
    nodeStateStyles: constants_1.defaultStateStyles,
    edgeStateStyles: constants_1.defaultStateStyles,
    collapseExpand: true,
    autoFit: true,
};
var graphs = {};
var IndentedTree = 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 ? 'card-node' : _a, _b = uProps.edgeType, edgeType = _b === void 0 ? 'cubic-horizontal' : _b, _c = uProps.behaviors, behaviors = _c === void 0 ? ['zoom-canvas', 'drag-canvas'] : _c, _d = uProps.nodeSize, nodeSize = _d === void 0 ? constants_1.defaultNodeSize : _d, _e = uProps.nodeLabelCfg, nodeLabelCfg = _e === void 0 ? constants_1.defaultLabelCfg : _e, _f = uProps.nodeAnchorPoints, nodeAnchorPoints = _f === void 0 ? defaultNodeAnchorPoints : _f, _g = uProps.layout, layout = _g === void 0 ? defaultLayout : _g, minimapCfg = uProps.minimapCfg, _h = uProps.nodeStyle, nodeStyle = _h === void 0 ? defaultNodeStyle : _h, _j = uProps.edgeStyle, edgeStyle = _j === void 0 ? defaultEdgeStyle : _j, _k = uProps.nodeStateStyles, nodeStateStyles = _k === void 0 ? constants_1.defaultStateStyles : _k, _l = uProps.edgeStateStyles, edgeStateStyles = _l === void 0 ? constants_1.defaultStateStyles : _l, _m = uProps.collapseExpand, collapseExpand = _m === void 0 ? true : _m, _o = uProps.autoFit, autoFit = _o === void 0 ? true : _o, handleNodeClick = uProps.handleNodeClick, graphRef = uProps.graphRef, onReady = uProps.onReady, loading = uProps.loading, loadingTemplate = uProps.loadingTemplate, errorTemplate = uProps.errorTemplate;
    var graph = react_1.default.useRef(null);
    var graphId = (0, utils_1.getGraphId)(graph);
    var container = react_1.default.useRef(null);
    (0, useGraph_1.default)(graphs[graphId], uProps, container);
    (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],
                modes: {
                    default: behaviors,
                },
                defaultNode: {
                    type: nodeType,
                    size: nodeSize,
                    style: nodeStyle,
                    anchorPoints: nodeAnchorPoints,
                    labelCfg: nodeLabelCfg,
                },
                defaultEdge: {
                    type: edgeType,
                    style: edgeStyle,
                },
                nodeStateStyles: nodeStateStyles,
                edgeStateStyles: edgeStateStyles,
                layout: layout,
                fitView: autoFit,
            });
            graphs[graphId] = graph;
        }
        if (graphRef) {
            graphRef.current = graph;
        }
        (0, utils_1.processMinimap)(minimapCfg, graph);
        (0, utils_1.renderGraph)(graph, data);
        if (onReady) {
            onReady(graph);
        }
        if (collapseExpand) {
            var onClick_1 = function (e) {
                var item = e.item;
                if (e.target.get('name') === 'collapse-icon') {
                    graph.updateItem(item, {
                        collapsed: !item.getModel().collapsed,
                    });
                    graph.layout();
                }
                else if (handleNodeClick) {
                    handleNodeClick(item, graph);
                }
            };
            graph.on('node:click', function (e) {
                onClick_1(e);
            });
            graph.on('node:touchstart', function (e) {
                onClick_1(e);
            });
        }
        (0, utils_1.bindEvents)(graph, props);
        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 = IndentedTree;
