import b from "../../../@babel/runtime/helpers/esm/typeof.mjs";
import N from "../../../@babel/runtime/helpers/esm/toConsumableArray.mjs";
import x from "../../../@babel/runtime/helpers/esm/objectSpread2.mjs";
import D from "../../../@babel/runtime/helpers/esm/objectWithoutProperties.mjs";
import w from "../../../rc-util/es/Children/toArray.mjs";
import F from "../../../rc-util/es/omit.mjs";
import { warningOnce as G } from "../../../rc-util/es/warning.mjs";
import j from "./keyUtil.mjs";
var B = ["children"];
function E(e, n) {
  return "".concat(e, "-").concat(n);
}
function W(e) {
  return e && e.type && e.type.isTreeNode;
}
function T(e, n) {
  return e ?? n;
}
function C(e) {
  var n = e || {}, l = n.title, r = n._title, t = n.key, s = n.children, a = l || "title";
  return {
    title: a,
    _title: r || [a],
    key: t || "key",
    children: s || "children"
  };
}
function Q(e, n) {
  var l = /* @__PURE__ */ new Map();
  function r(t) {
    var s = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : "";
    (t || []).forEach(function(a) {
      var c = a[n.key], f = a[n.children];
      G(c != null, "Tree node must have a certain key: [".concat(s).concat(c, "]"));
      var o = String(c);
      G(!l.has(o) || c === null || c === void 0, "Same 'key' exist in the Tree: ".concat(o)), l.set(o, !0), r(f, "".concat(s).concat(o, " > "));
    });
  }
  r(e);
}
function R(e) {
  function n(l) {
    var r = w(l);
    return r.map(function(t) {
      if (!W(t))
        return G(!t, "Tree/TreeNode can only accept TreeNode as children."), null;
      var s = t.key, a = t.props, c = a.children, f = D(a, B), o = x({
        key: s
      }, f), h = n(c);
      return h.length && (o.children = h), o;
    }).filter(function(t) {
      return t;
    });
  }
  return n(e);
}
function U(e, n, l) {
  var r = C(l), t = r._title, s = r.key, a = r.children, c = new Set(n === !0 ? [] : n), f = [];
  function o(h) {
    var u = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : null;
    return h.map(function(i, v) {
      for (var d = E(u ? u.pos : "0", v), y = T(i[s], d), p, K = 0; K < t.length; K += 1) {
        var k = t[K];
        if (i[k] !== void 0) {
          p = i[k];
          break;
        }
      }
      var g = x(x({}, F(i, [].concat(N(t), [s, a]))), {}, {
        title: p,
        key: y,
        parent: u,
        pos: d,
        children: null,
        data: i,
        isStart: [].concat(N(u ? u.isStart : []), [v === 0]),
        isEnd: [].concat(N(u ? u.isEnd : []), [v === h.length - 1])
      });
      return f.push(g), n === !0 || c.has(y) ? g.children = o(i[a] || [], g) : g.children = [], g;
    });
  }
  return o(e), f;
}
function A(e, n, l) {
  var r = {};
  b(l) === "object" ? r = l : r = {
    externalGetKey: l
  }, r = r || {};
  var t = r, s = t.childrenPropName, a = t.externalGetKey, c = t.fieldNames, f = C(c), o = f.key, h = f.children, u = s || h, i;
  a ? typeof a == "string" ? i = function(y) {
    return y[a];
  } : typeof a == "function" && (i = function(y) {
    return a(y);
  }) : i = function(y, p) {
    return T(y[o], p);
  };
  function v(d, y, p, K) {
    var k = d ? d[u] : e, g = d ? E(p.pos, y) : "0", O = d ? [].concat(N(K), [d]) : [];
    if (d) {
      var m = i(d, g), P = {
        node: d,
        index: y,
        pos: g,
        key: m,
        parentPos: p.node ? p.pos : null,
        level: p.level + 1,
        nodes: O
      };
      n(P);
    }
    k && k.forEach(function(S, _) {
      v(S, _, {
        node: d,
        pos: g,
        level: p ? p.level + 1 : -1
      }, O);
    });
  }
  v(null);
}
function V(e) {
  var n = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {}, l = n.initWrapper, r = n.processEntity, t = n.onProcessFinished, s = n.externalGetKey, a = n.childrenPropName, c = n.fieldNames, f = arguments.length > 2 ? arguments[2] : void 0, o = s || f, h = {}, u = {}, i = {
    posEntities: h,
    keyEntities: u
  };
  return l && (i = l(i) || i), A(e, function(v) {
    var d = v.node, y = v.index, p = v.pos, K = v.key, k = v.parentPos, g = v.level, O = v.nodes, m = {
      node: d,
      nodes: O,
      index: y,
      key: K,
      pos: p,
      level: g
    }, P = T(K, p);
    h[p] = m, u[P] = m, m.parent = h[k], m.parent && (m.parent.children = m.parent.children || [], m.parent.children.push(m)), r && r(m, i);
  }, {
    externalGetKey: o,
    childrenPropName: a,
    fieldNames: c
  }), t && t(i), i;
}
function X(e, n) {
  var l = n.expandedKeys, r = n.selectedKeys, t = n.loadedKeys, s = n.loadingKeys, a = n.checkedKeys, c = n.halfCheckedKeys, f = n.dragOverNodeKey, o = n.dropPosition, h = n.keyEntities, u = j(h, e), i = {
    eventKey: e,
    expanded: l.indexOf(e) !== -1,
    selected: r.indexOf(e) !== -1,
    loaded: t.indexOf(e) !== -1,
    loading: s.indexOf(e) !== -1,
    checked: a.indexOf(e) !== -1,
    halfChecked: c.indexOf(e) !== -1,
    pos: String(u ? u.pos : ""),
    // [Legacy] Drag props
    // Since the interaction of drag is changed, the semantic of the props are
    // not accuracy, I think it should be finally removed
    dragOver: f === e && o === 0,
    dragOverGapTop: f === e && o === -1,
    dragOverGapBottom: f === e && o === 1
  };
  return i;
}
function Y(e) {
  var n = e.data, l = e.expanded, r = e.selected, t = e.checked, s = e.loaded, a = e.loading, c = e.halfChecked, f = e.dragOver, o = e.dragOverGapTop, h = e.dragOverGapBottom, u = e.pos, i = e.active, v = e.eventKey, d = x(x({}, n), {}, {
    expanded: l,
    selected: r,
    checked: t,
    loaded: s,
    loading: a,
    halfChecked: c,
    dragOver: f,
    dragOverGapTop: o,
    dragOverGapBottom: h,
    pos: u,
    active: i,
    key: v
  });
  return "props" in d || Object.defineProperty(d, "props", {
    get: function() {
      return G(!1, "Second param return from event is node data instead of TreeNode instance. Please read value directly instead of reading from `props`."), e;
    }
  }), d;
}
export {
  V as convertDataToEntities,
  Y as convertNodePropsToEventData,
  R as convertTreeToData,
  C as fillFieldNames,
  U as flattenTreeData,
  T as getKey,
  E as getPosition,
  X as getTreeNodeProps,
  W as isTreeNode,
  A as traverseDataNodes,
  Q as warningWithoutKey
};
