import B from "../../@babel/runtime/helpers/esm/extends.mjs";
import d from "../../@babel/runtime/helpers/esm/defineProperty.mjs";
import Y from "../../@babel/runtime/helpers/esm/objectWithoutProperties.mjs";
import H from "../../@babel/runtime/helpers/esm/objectSpread2.mjs";
import Z from "../../@babel/runtime/helpers/esm/classCallCheck.mjs";
import ee from "../../@babel/runtime/helpers/esm/createClass.mjs";
import S from "../../@babel/runtime/helpers/esm/assertThisInitialized.mjs";
import oe from "../../@babel/runtime/helpers/esm/inherits.mjs";
import te from "../../@babel/runtime/helpers/esm/createSuper.mjs";
import D from "../../classnames/index.mjs";
import re from "../../rc-util/es/pickAttrs.mjs";
import * as l from "react";
import { TreeContext as ne } from "./contextTypes.mjs";
import ae from "./Indent.mjs";
import R from "./utils/keyUtil.mjs";
import { convertNodePropsToEventData as u } from "./utils/treeUtil.mjs";
var ie = ["eventKey", "className", "style", "dragOver", "dragOverGapTop", "dragOverGapBottom", "isLeaf", "isStart", "isEnd", "expanded", "selected", "checked", "halfChecked", "loading", "domRef", "active", "data", "onMouseMove", "selectable"], G = "open", A = "close", ce = "---", de = /* @__PURE__ */ function(w) {
  oe(b, w);
  var O = te(b);
  function b() {
    var e;
    Z(this, b);
    for (var i = arguments.length, a = new Array(i), x = 0; x < i; x++)
      a[x] = arguments[x];
    return e = O.call.apply(O, [this].concat(a)), e.state = {
      dragNodeHighlight: !1
    }, e.selectHandle = void 0, e.cacheIndent = void 0, e.onSelectorClick = function(o) {
      var t = e.props.context.onNodeClick;
      t(o, u(e.props)), e.isSelectable() ? e.onSelect(o) : e.onCheck(o);
    }, e.onSelectorDoubleClick = function(o) {
      var t = e.props.context.onNodeDoubleClick;
      t(o, u(e.props));
    }, e.onSelect = function(o) {
      if (!e.isDisabled()) {
        var t = e.props.context.onNodeSelect;
        t(o, u(e.props));
      }
    }, e.onCheck = function(o) {
      if (!e.isDisabled()) {
        var t = e.props, n = t.disableCheckbox, r = t.checked, c = e.props.context.onNodeCheck;
        if (!(!e.isCheckable() || n)) {
          var f = !r;
          c(o, u(e.props), f);
        }
      }
    }, e.onMouseEnter = function(o) {
      var t = e.props.context.onNodeMouseEnter;
      t(o, u(e.props));
    }, e.onMouseLeave = function(o) {
      var t = e.props.context.onNodeMouseLeave;
      t(o, u(e.props));
    }, e.onContextMenu = function(o) {
      var t = e.props.context.onNodeContextMenu;
      t(o, u(e.props));
    }, e.onDragStart = function(o) {
      var t = e.props.context.onNodeDragStart;
      o.stopPropagation(), e.setState({
        dragNodeHighlight: !0
      }), t(o, S(e));
      try {
        o.dataTransfer.setData("text/plain", "");
      } catch {
      }
    }, e.onDragEnter = function(o) {
      var t = e.props.context.onNodeDragEnter;
      o.preventDefault(), o.stopPropagation(), t(o, S(e));
    }, e.onDragOver = function(o) {
      var t = e.props.context.onNodeDragOver;
      o.preventDefault(), o.stopPropagation(), t(o, S(e));
    }, e.onDragLeave = function(o) {
      var t = e.props.context.onNodeDragLeave;
      o.stopPropagation(), t(o, S(e));
    }, e.onDragEnd = function(o) {
      var t = e.props.context.onNodeDragEnd;
      o.stopPropagation(), e.setState({
        dragNodeHighlight: !1
      }), t(o, S(e));
    }, e.onDrop = function(o) {
      var t = e.props.context.onNodeDrop;
      o.preventDefault(), o.stopPropagation(), e.setState({
        dragNodeHighlight: !1
      }), t(o, S(e));
    }, e.onExpand = function(o) {
      var t = e.props, n = t.loading, r = t.context.onNodeExpand;
      n || r(o, u(e.props));
    }, e.setSelectHandle = function(o) {
      e.selectHandle = o;
    }, e.getNodeState = function() {
      var o = e.props.expanded;
      return e.isLeaf() ? null : o ? G : A;
    }, e.hasChildren = function() {
      var o = e.props.eventKey, t = e.props.context.keyEntities, n = R(t, o) || {}, r = n.children;
      return !!(r || []).length;
    }, e.isLeaf = function() {
      var o = e.props, t = o.isLeaf, n = o.loaded, r = e.props.context.loadData, c = e.hasChildren();
      return t === !1 ? !1 : t || !r && !c || r && n && !c;
    }, e.isDisabled = function() {
      var o = e.props.disabled, t = e.props.context.disabled;
      return !!(t || o);
    }, e.isCheckable = function() {
      var o = e.props.checkable, t = e.props.context.checkable;
      return !t || o === !1 ? !1 : t;
    }, e.syncLoadData = function(o) {
      var t = o.expanded, n = o.loading, r = o.loaded, c = e.props.context, f = c.loadData, v = c.onNodeLoad;
      n || f && t && !e.isLeaf() && !e.hasChildren() && !r && v(u(e.props));
    }, e.isDraggable = function() {
      var o = e.props, t = o.data, n = o.context.draggable;
      return !!(n && (!n.nodeDraggable || n.nodeDraggable(t)));
    }, e.renderDragHandler = function() {
      var o = e.props.context, t = o.draggable, n = o.prefixCls;
      return t != null && t.icon ? /* @__PURE__ */ l.createElement("span", {
        className: "".concat(n, "-draggable-icon")
      }, t.icon) : null;
    }, e.renderSwitcherIconDom = function(o) {
      var t = e.props.switcherIcon, n = e.props.context.switcherIcon, r = t || n;
      return typeof r == "function" ? r(H(H({}, e.props), {}, {
        isLeaf: o
      })) : r;
    }, e.renderSwitcher = function() {
      var o = e.props.expanded, t = e.props.context.prefixCls;
      if (e.isLeaf()) {
        var n = e.renderSwitcherIconDom(!0);
        return n !== !1 ? /* @__PURE__ */ l.createElement("span", {
          className: D("".concat(t, "-switcher"), "".concat(t, "-switcher-noop"))
        }, n) : null;
      }
      var r = D("".concat(t, "-switcher"), "".concat(t, "-switcher_").concat(o ? G : A)), c = e.renderSwitcherIconDom(!1);
      return c !== !1 ? /* @__PURE__ */ l.createElement("span", {
        onClick: e.onExpand,
        className: r
      }, c) : null;
    }, e.renderCheckbox = function() {
      var o = e.props, t = o.checked, n = o.halfChecked, r = o.disableCheckbox, c = e.props.context.prefixCls, f = e.isDisabled(), v = e.isCheckable();
      if (!v)
        return null;
      var h = typeof v != "boolean" ? v : null;
      return /* @__PURE__ */ l.createElement("span", {
        className: D("".concat(c, "-checkbox"), t && "".concat(c, "-checkbox-checked"), !t && n && "".concat(c, "-checkbox-indeterminate"), (f || r) && "".concat(c, "-checkbox-disabled")),
        onClick: e.onCheck
      }, h);
    }, e.renderIcon = function() {
      var o = e.props.loading, t = e.props.context.prefixCls;
      return /* @__PURE__ */ l.createElement("span", {
        className: D("".concat(t, "-iconEle"), "".concat(t, "-icon__").concat(e.getNodeState() || "docu"), o && "".concat(t, "-icon_loading"))
      });
    }, e.renderSelector = function() {
      var o = e.state.dragNodeHighlight, t = e.props, n = t.title, r = n === void 0 ? ce : n, c = t.selected, f = t.icon, v = t.loading, h = t.data, p = e.props.context, m = p.prefixCls, y = p.showIcon, _ = p.icon, L = p.loadData, N = p.titleRender, I = e.isDisabled(), $ = "".concat(m, "-node-content-wrapper"), k;
      if (y) {
        var E = f || _;
        k = E ? /* @__PURE__ */ l.createElement("span", {
          className: D("".concat(m, "-iconEle"), "".concat(m, "-icon__customize"))
        }, typeof E == "function" ? E(e.props) : E) : e.renderIcon();
      } else
        L && v && (k = e.renderIcon());
      var g;
      typeof r == "function" ? g = r(h) : N ? g = N(h) : g = r;
      var s = /* @__PURE__ */ l.createElement("span", {
        className: "".concat(m, "-title")
      }, g);
      return /* @__PURE__ */ l.createElement("span", {
        ref: e.setSelectHandle,
        title: typeof r == "string" ? r : "",
        className: D("".concat($), "".concat($, "-").concat(e.getNodeState() || "normal"), !I && (c || o) && "".concat(m, "-node-selected")),
        onMouseEnter: e.onMouseEnter,
        onMouseLeave: e.onMouseLeave,
        onContextMenu: e.onContextMenu,
        onClick: e.onSelectorClick,
        onDoubleClick: e.onSelectorDoubleClick
      }, k, s, e.renderDropIndicator());
    }, e.renderDropIndicator = function() {
      var o = e.props, t = o.disabled, n = o.eventKey, r = e.props.context, c = r.draggable, f = r.dropLevelOffset, v = r.dropPosition, h = r.prefixCls, p = r.indent, m = r.dropIndicatorRender, y = r.dragOverNodeKey, _ = r.direction, L = !!c, N = !t && L && y === n, I = p ?? e.cacheIndent;
      return e.cacheIndent = p, N ? m({
        dropPosition: v,
        dropLevelOffset: f,
        indent: I,
        prefixCls: h,
        direction: _
      }) : null;
    }, e;
  }
  return ee(b, [{
    key: "componentDidMount",
    value: (
      // Isomorphic needn't load data in server side
      function() {
        this.syncLoadData(this.props);
      }
    )
  }, {
    key: "componentDidUpdate",
    value: function() {
      this.syncLoadData(this.props);
    }
  }, {
    key: "isSelectable",
    value: function() {
      var i = this.props.selectable, a = this.props.context.selectable;
      return typeof i == "boolean" ? i : a;
    }
  }, {
    key: "render",
    value: (
      // =========================== Render ===========================
      function() {
        var i, a = this.props, x = a.eventKey, o = a.className, t = a.style, n = a.dragOver, r = a.dragOverGapTop, c = a.dragOverGapBottom, f = a.isLeaf, v = a.isStart, h = a.isEnd, p = a.expanded, m = a.selected, y = a.checked, _ = a.halfChecked, L = a.loading, N = a.domRef, I = a.active;
        a.data;
        var $ = a.onMouseMove, k = a.selectable, E = Y(a, ie), g = this.props.context, s = g.prefixCls, T = g.filterTreeNode, z = g.keyEntities, F = g.dropContainerKey, U = g.dropTargetKey, W = g.draggingNodeKey, M = this.isDisabled(), q = re(E, {
          aria: !0,
          data: !0
        }), J = R(z, x) || {}, Q = J.level, V = h[h.length - 1], C = this.isDraggable(), P = !M && C, K = W === x, X = k !== void 0 ? {
          "aria-selected": !!k
        } : void 0;
        return /* @__PURE__ */ l.createElement("div", B({
          ref: N,
          className: D(o, "".concat(s, "-treenode"), (i = {}, d(i, "".concat(s, "-treenode-disabled"), M), d(i, "".concat(s, "-treenode-switcher-").concat(p ? "open" : "close"), !f), d(i, "".concat(s, "-treenode-checkbox-checked"), y), d(i, "".concat(s, "-treenode-checkbox-indeterminate"), _), d(i, "".concat(s, "-treenode-selected"), m), d(i, "".concat(s, "-treenode-loading"), L), d(i, "".concat(s, "-treenode-active"), I), d(i, "".concat(s, "-treenode-leaf-last"), V), d(i, "".concat(s, "-treenode-draggable"), C), d(i, "dragging", K), d(i, "drop-target", U === x), d(i, "drop-container", F === x), d(i, "drag-over", !M && n), d(i, "drag-over-gap-top", !M && r), d(i, "drag-over-gap-bottom", !M && c), d(i, "filter-node", T && T(u(this.props))), i)),
          style: t,
          draggable: P,
          "aria-grabbed": K,
          onDragStart: P ? this.onDragStart : void 0,
          onDragEnter: C ? this.onDragEnter : void 0,
          onDragOver: C ? this.onDragOver : void 0,
          onDragLeave: C ? this.onDragLeave : void 0,
          onDrop: C ? this.onDrop : void 0,
          onDragEnd: C ? this.onDragEnd : void 0,
          onMouseMove: $
        }, X, q), /* @__PURE__ */ l.createElement(ae, {
          prefixCls: s,
          level: Q,
          isStart: v,
          isEnd: h
        }), this.renderDragHandler(), this.renderSwitcher(), this.renderCheckbox(), this.renderSelector());
      }
    )
  }]), b;
}(l.Component), j = function(O) {
  return /* @__PURE__ */ l.createElement(ne.Consumer, null, function(b) {
    return /* @__PURE__ */ l.createElement(de, B({}, O, {
      context: b
    }));
  });
};
j.displayName = "TreeNode";
j.isTreeNode = 1;
export {
  j as default
};
