"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 _utils = require("./utils/_utils");

var ArrowRightFill = _icon.default.ArrowRightFill,
    LoadingLine = _icon.default.LoadingLine,
    NavigationLine = _icon.default.NavigationLine;

var TreeNodeRenderFunction = function TreeNodeRenderFunction(props, ref) {
  var _classNames5, _handleExportNodes;

  var hover = props.hover,
      level = props.level,
      title = props.title,
      isLeaf = props.isLeaf,
      checked = props.checked,
      loading = props.loading,
      icon = props.icon,
      childrenLen = props.childrenLen,
      showIcon = props.showIcon,
      showLine = props.showLine,
      switcherIcon = props.switcherIcon,
      searchValue = props.searchValue,
      expanded = props.expanded,
      indexAtLevel = props.indexAtLevel,
      checkable = props.checkable,
      draggable = props.draggable,
      allLineClickable = props.allLineClickable,
      lineClickable = props.lineClickable,
      nodeKey = props.nodeKey,
      numberKeysSet = props.numberKeysSet,
      selected = props.selected,
      fieldNames = props.fieldNames,
      dragoverInfo = props.dragoverInfo,
      indeterminate = props.indeterminate,
      disabled = props.disabled,
      disableCheckbox = props.disableCheckbox,
      renderExtra = props.renderExtra,
      renderSearchedTitle = props.renderSearchedTitle,
      onNodeDragStart = props.onNodeDragStart,
      onNodeDragEnd = props.onNodeDragEnd,
      onNodeDragEnter = props.onNodeDragEnter,
      onNodeDragOver = props.onNodeDragOver,
      onNodeDragLeave = props.onNodeDragLeave,
      onNodeDrop = props.onNodeDrop,
      onNodeCheck = props.onNodeCheck,
      onNodeSelect = props.onNodeSelect,
      onNodeExpand = props.onNodeExpand;

  var checkboxRef = _react.default.useRef(null);

  var titleRef = _react.default.useRef(null);

  var _React$useContext = _react.default.useContext(_context.ConfigContext),
      getPrefixClassName = _React$useContext.getPrefixClassName;

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

  var onNodeAllLineClick = function onNodeAllLineClick() {
    if (typeof lineClickable === 'boolean' && !lineClickable) return;
    if (!allLineClickable) return;

    if (checkable) {
      var _checkboxRef$current, _checkboxRef$current$;

      (_checkboxRef$current = checkboxRef.current) === null || _checkboxRef$current === void 0 ? void 0 : (_checkboxRef$current$ = _checkboxRef$current.input) === null || _checkboxRef$current$ === void 0 ? void 0 : _checkboxRef$current$.click(); // checkboxRef.current?.
    } else {
      var _titleRef$current;

      (_titleRef$current = titleRef.current) === null || _titleRef$current === void 0 ? void 0 : _titleRef$current.click();
    }
  };

  var renderPlaceholder = function renderPlaceholder() {
    if (!level) return null;
    return new Array(level).fill(1).map(function (item, index) {
      return /*#__PURE__*/_react.default.createElement("div", {
        className: "".concat(treeNodePrefix, "-placeholder"),
        key: index
      }, showLine && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement("span", {
        className: "".concat(treeNodePrefix, "-line-top")
      }), (indexAtLevel !== childrenLen - 1 || index !== level - 1) && /*#__PURE__*/_react.default.createElement("span", {
        className: "".concat(treeNodePrefix, "-line-bottom")
      }), index === level - 1 && /*#__PURE__*/_react.default.createElement("span", {
        className: "".concat(treeNodePrefix, "-line-horizontal")
      })));
    });
  };

  var renderExpandIcon = function renderExpandIcon() {
    var _classNames;

    if (isLeaf) return /*#__PURE__*/_react.default.createElement("div", {
      className: "".concat(treeNodePrefix, "-switcher-placeholder")
    });
    var iconNode = null;

    if (loading) {
      iconNode = /*#__PURE__*/_react.default.createElement(LoadingLine, null);
    } else if (Array.isArray(switcherIcon)) {
      iconNode = expanded ? switcherIcon[1] : switcherIcon[0];
    } else {
      iconNode = switcherIcon || /*#__PURE__*/_react.default.createElement(ArrowRightFill, null);
    }

    return /*#__PURE__*/_react.default.createElement("div", {
      className: (0, _classnames.default)("".concat(treeNodePrefix, "-switcher"), (_classNames = {}, (0, _defineProperty2.default)(_classNames, "".concat(treeNodePrefix, "-switcher-close"), !expanded), (0, _defineProperty2.default)(_classNames, "".concat(treeNodePrefix, "-switcher-open"), expanded), _classNames)),
      onClick: function onClick(e) {
        e.stopPropagation();
        onNodeExpand(props);
      }
    }, iconNode);
  };

  var renderDragIcon = function renderDragIcon() {
    if (!draggable) return null;
    return /*#__PURE__*/_react.default.createElement("div", {
      className: "".concat(treeNodePrefix, "-drag")
    }, /*#__PURE__*/_react.default.createElement(NavigationLine, null));
  }; // checkbox + title + underline + extra


  var renderContent = function renderContent() {
    var _classNames2;

    var dragBottom = dragoverInfo.dragBottom,
        dragTop = dragoverInfo.dragTop,
        targetKey = dragoverInfo.targetKey;
    var underline = draggable && targetKey === nodeKey ? /*#__PURE__*/_react.default.createElement("div", {
      className: (0, _classnames.default)("".concat(treeNodePrefix, "-drag"), (_classNames2 = {}, (0, _defineProperty2.default)(_classNames2, "".concat(treeNodePrefix, "-drag-offset"), dragBottom && !dragTop), (0, _defineProperty2.default)(_classNames2, "".concat(treeNodePrefix, "-drag-bottom"), !dragTop), (0, _defineProperty2.default)(_classNames2, "".concat(treeNodePrefix, "-drag-top"), dragTop), _classNames2))
    }, /*#__PURE__*/_react.default.createElement("span", {
      className: "".concat(treeNodePrefix, "-drag-underline")
    }), /*#__PURE__*/_react.default.createElement("span", {
      className: "".concat(treeNodePrefix, "-drag-underline-circle")
    }), /*#__PURE__*/_react.default.createElement("span", {
      className: "".concat(treeNodePrefix, "-drag-underline-innerCircle")
    })) : null;

    var nodeTitle = /*#__PURE__*/_react.default.createElement("span", {
      ref: titleRef,
      className: (0, _classnames.default)((0, _defineProperty2.default)({}, "".concat(treeNodePrefix, "-title-selected"), selected)),
      onClick: function onClick(e) {
        e.stopPropagation();

        if (!disabled) {
          onNodeSelect === null || onNodeSelect === void 0 ? void 0 : onNodeSelect(e, props);
        }
      }
    }, showIcon && /*#__PURE__*/_react.default.createElement("span", {
      className: "".concat(treeNodePrefix, "-customizeIcon")
    }, icon), renderSearchedTitle(title, searchValue));

    var nodeCheckBox = checkable ? /*#__PURE__*/_react.default.createElement("span", {
      className: "".concat(treeNodePrefix, "-checkbox")
    }, /*#__PURE__*/_react.default.createElement(_checkbox.default, {
      ref: checkboxRef,
      checked: checked,
      indeterminate: indeterminate,
      disabled: typeof disableCheckbox !== 'undefined' ? disableCheckbox : disabled,
      // @ts-ignore
      onClick: function onClick(e) {
        e.stopPropagation();
      },
      onChange: function onChange(e) {
        e.stopPropagation();
        onNodeCheck === null || onNodeCheck === void 0 ? void 0 : onNodeCheck(e, props);
      }
    })) : null;
    return /*#__PURE__*/_react.default.createElement("div", {
      className: (0, _classnames.default)("".concat(treeNodePrefix, "-title"), (0, _defineProperty2.default)({}, "".concat(treeNodePrefix, "-hover-").concat(hover), hover !== 'longtest'))
    }, nodeCheckBox, nodeTitle, underline);
  };

  return /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _classnames.default)(treeNodePrefix, (_classNames5 = {}, (0, _defineProperty2.default)(_classNames5, "".concat(treeNodePrefix, "-hover-longtest"), hover === 'longtest'), (0, _defineProperty2.default)(_classNames5, "".concat(treeNodePrefix, "-disabled"), disabled), _classNames5)),
    title: title,
    draggable: draggable,
    // onDrag={e => onNodeDrag(e, props)}
    onClick: onNodeAllLineClick,
    onDragStart: function onDragStart(e) {
      return onNodeDragStart(e, props);
    },
    onDragEnd: function onDragEnd(e) {
      return onNodeDragEnd(e, props);
    },
    onDragEnter: function onDragEnter(e) {
      return onNodeDragEnter(e, props);
    },
    onDragOver: function onDragOver(e) {
      return onNodeDragOver(e, props);
    },
    onDragLeave: function onDragLeave(e) {
      return onNodeDragLeave(e, props);
    },
    onDrop: function onDrop(e) {
      return onNodeDrop(e, props);
    }
  }, renderDragIcon(), renderPlaceholder(), renderExpandIcon(), renderContent(), renderExtra ? /*#__PURE__*/_react.default.createElement("span", {
    className: "".concat(treeNodePrefix, "-extra")
  }, renderExtra((_handleExportNodes = (0, _utils.handleExportNodes)([], fieldNames, props, numberKeysSet)) === null || _handleExportNodes === void 0 ? void 0 : _handleExportNodes[1])) : null);
};

var VerticalTreeNode = /*#__PURE__*/_react.default.forwardRef(TreeNodeRenderFunction);

var _default = VerticalTreeNode;
exports.default = _default;