import { __assign, __spreadArrays } from "tslib";
import React, { useContext, useRef, useState } from 'react';
import classNames from 'classnames';
import { ConfigContext } from '../config-provider/config-context';
import { getPrefixCls } from '../_utils/general';
import { TreeInstance } from './tree-instance';
import TreeNode from './tree-node';
var Tree = React.forwardRef(function (props, ref) {
    var _a = props.data, data = _a === void 0 ? [] : _a, _b = props.defaultCheckedKeys, defaultCheckedKeys = _b === void 0 ? [] : _b, _c = props.defaultExpandedKeys, defaultExpandedKeys = _c === void 0 ? [] : _c, _d = props.defaultExpandAll, defaultExpandAll = _d === void 0 ? false : _d, _e = props.indent, indent = _e === void 0 ? 20 : _e, _f = props.blockNode, blockNode = _f === void 0 ? true : _f, _g = props.checkable, checkable = _g === void 0 ? false : _g, _h = props.disabled, disabled = _h === void 0 ? false : _h, onCheck = props.onCheck, onExpand = props.onExpand, className = props.className, customisedCls = props.prefixCls;
    var configContext = useContext(ConfigContext);
    var prefixCls = getPrefixCls('tree', configContext.prefixCls, customisedCls);
    var cls = classNames(prefixCls, className);
    var treeInstance = useRef(new TreeInstance(data, defaultCheckedKeys, defaultExpandedKeys, defaultExpandAll));
    var _j = useState(treeInstance.current.nodes), treeNodes = _j[0], setTreeNodes = _j[1];
    var onCheckboxChange = function (key, e) {
        var tree = treeInstance.current;
        tree.setNodeChecked(key, e.currentTarget.checked);
        setTreeNodes(__spreadArrays(treeInstance.current.nodes));
        onCheck && onCheck(tree.getCheckedKeys(), e);
    };
    var onExpandChange = function (key, isExpanded, e) {
        var tree = treeInstance.current;
        tree.setNodeExpanded(key, isExpanded);
        setTreeNodes(__spreadArrays(treeInstance.current.nodes));
        onExpand && onExpand(tree.getExpandedKeys(), e);
    };
    return (React.createElement("ul", { className: cls, ref: ref, "aria-level": 0 }, treeNodes.map(function (item) { return (React.createElement(TreeNode, __assign({}, props, { key: item.uniqueKey, node: item, level: 0, indent: indent, blockNode: blockNode, checkable: checkable, disabled: disabled, treeClassName: cls, onCheckboxChange: onCheckboxChange, onExpandChange: onExpandChange }))); })));
});
Tree.displayName = 'Tree';
export default Tree;
//# sourceMappingURL=tree.js.map