import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import React from 'react';
import classNames from 'classnames';
import Icon from '../icon';
import Checkbox from '../checkbox';
import { ConfigContext } from '../config-provider/context';
var RightLine = Icon.RightLine,
    LoadingLine = Icon.LoadingLine;

var TreeNode = function TreeNode(props) {
  var title = props.title,
      selected = props.selected,
      checked = props.checked,
      checkable = props.checkable,
      indeterminate = props.indeterminate,
      switcherIcon = props.switcherIcon,
      isLeaf = props.isLeaf,
      loading = props.loading,
      nodeKey = props.nodeKey,
      children = props.children,
      level = props.level,
      parent = props.parent,
      icon = props.icon,
      showIcon = props.showIcon,
      className = props.className,
      onNodeSelect = props.onNodeSelect,
      onNodeCheck = props.onNodeCheck; // const nodeProps = React.useMemo(() => ({
  //   title,
  //   selected,
  //   checked,
  //   indeterminate,
  //   isLeaf,
  //   nodeKey,
  //   children: childs,
  //   parent,
  //   level
  // }), [
  //   title,
  //   selected,
  //   checked,
  //   indeterminate,
  //   isLeaf,
  //   nodeKey,
  //   children,
  //   parent,
  //   level
  // ])

  var _React$useContext = React.useContext(ConfigContext),
      getPrefixClassName = _React$useContext.getPrefixClassName;

  var treeNodePrefix = getPrefixClassName('tree-node');
  var checkboxRef = React.useRef(null);

  var onSelect = function onSelect(e) {
    var _e$persist;

    (_e$persist = e.persist) === null || _e$persist === void 0 ? void 0 : _e$persist.call(e);
    if (checkboxRef.current && e.nativeEvent.target === checkboxRef.current.input) return;
    onNodeSelect === null || onNodeSelect === void 0 ? void 0 : onNodeSelect(e, props);
  };

  var onCheck = function onCheck(e) {
    onNodeCheck === null || onNodeCheck === void 0 ? void 0 : onNodeCheck(e, props);
  };

  var renderCheckbox = function renderCheckbox() {
    if (!checkable) return null;
    return /*#__PURE__*/React.createElement("span", {
      className: "".concat(treeNodePrefix, "-checkbox")
    }, /*#__PURE__*/React.createElement(Checkbox, {
      indeterminate: indeterminate,
      checked: checked,
      onChange: onCheck,
      ref: checkboxRef
    }));
  };

  var renderSwitcherIcon = function renderSwitcherIcon() {
    if (isLeaf) return null;
    return /*#__PURE__*/React.createElement("span", {
      className: "".concat(treeNodePrefix, "-hSwitcher")
    }, loading ? /*#__PURE__*/React.createElement(LoadingLine, null) : switcherIcon || /*#__PURE__*/React.createElement(RightLine, null));
  };

  return /*#__PURE__*/React.createElement("div", {
    className: classNames(treeNodePrefix, className, _defineProperty({}, "".concat(treeNodePrefix, "-selected"), selected)),
    onClick: onSelect
  }, renderCheckbox(), /*#__PURE__*/React.createElement("span", {
    className: "".concat(treeNodePrefix, "-title")
  }, showIcon && /*#__PURE__*/React.createElement("span", {
    className: "".concat(treeNodePrefix, "-icon")
  }, icon), title), renderSwitcherIcon());
};

export default TreeNode;