"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _react = _interopRequireDefault(require("react"));

var _classnames = _interopRequireDefault(require("classnames"));

var _icon = _interopRequireDefault(require("../icon"));

var _checkbox = _interopRequireDefault(require("../checkbox"));

var _context = require("../config-provider/context");

var RightLine = _icon.default.RightLine,
    LoadingLine = _icon.default.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.default.useContext(_context.ConfigContext),
      getPrefixClassName = _React$useContext.getPrefixClassName;

  var treeNodePrefix = getPrefixClassName('tree-node');

  var checkboxRef = _react.default.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.default.createElement("span", {
      className: "".concat(treeNodePrefix, "-checkbox")
    }, /*#__PURE__*/_react.default.createElement(_checkbox.default, {
      indeterminate: indeterminate,
      checked: checked,
      onChange: onCheck,
      ref: checkboxRef
    }));
  };

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

  return /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _classnames.default)(treeNodePrefix, className, (0, _defineProperty2.default)({}, "".concat(treeNodePrefix, "-selected"), selected)),
    onClick: onSelect
  }, renderCheckbox(), /*#__PURE__*/_react.default.createElement("span", {
    className: "".concat(treeNodePrefix, "-title")
  }, showIcon && /*#__PURE__*/_react.default.createElement("span", {
    className: "".concat(treeNodePrefix, "-icon")
  }, icon), title), renderSwitcherIcon());
};

var _default = TreeNode;
exports.default = _default;