import F from "../../../@babel/runtime/helpers/esm/toConsumableArray.mjs";
import W from "../../../@babel/runtime/helpers/esm/slicedToArray.mjs";
import c from "../../../rc-util/es/KeyCode.mjs";
import * as D from "react";
import { SEARCH_MARK as _ } from "../hooks/useSearchOptions.mjs";
import { getFullPathKeys as G, toPathKey as M } from "../utils/commonUtil.mjs";
const Q = function(P, f, l, s, S, K, h) {
  var T = h.direction, A = h.searchValue, O = h.toggleOpen, U = h.open, k = T === "rtl", H = D.useMemo(function() {
    for (var t = -1, e = f, a = [], r = [], v = s.length, n = G(f, l), u = function(R) {
      var E = e.findIndex(function($, w) {
        return (n[w] ? M(n[w]) : $[l.value]) === s[R];
      });
      if (E === -1)
        return 1;
      t = E, a.push(t), r.push(s[R]), e = e[t][l.children];
    }, g = 0; g < v && e && !u(g); g += 1)
      ;
    for (var y = f, C = 0; C < a.length - 1; C += 1)
      y = y[a[C]][l.children];
    return [r, t, y, n];
  }, [s, l, f]), d = W(H, 4), i = d[0], p = d[1], o = d[2], x = d[3], m = function(e) {
    S(e);
  }, V = function(e) {
    var a = o.length, r = p;
    r === -1 && e < 0 && (r = a);
    for (var v = 0; v < a; v += 1) {
      r = (r + e + a) % a;
      var n = o[r];
      if (n && !n.disabled) {
        var u = i.slice(0, -1).concat(x[r] ? M(x[r]) : n[l.value]);
        m(u);
        return;
      }
    }
  }, b = function() {
    if (i.length > 1) {
      var e = i.slice(0, -1);
      m(e);
    } else
      O(!1);
  }, I = function() {
    var e, a = ((e = o[p]) === null || e === void 0 ? void 0 : e[l.children]) || [], r = a.find(function(n) {
      return !n.disabled;
    });
    if (r) {
      var v = [].concat(F(i), [r[l.value]]);
      m(v);
    }
  };
  D.useImperativeHandle(P, function() {
    return {
      // scrollTo: treeRef.current?.scrollTo,
      onKeyDown: function(e) {
        var a = e.which;
        switch (a) {
          case c.UP:
          case c.DOWN: {
            var r = 0;
            a === c.UP ? r = -1 : a === c.DOWN && (r = 1), r !== 0 && V(r);
            break;
          }
          case c.LEFT: {
            if (A)
              break;
            k ? I() : b();
            break;
          }
          case c.RIGHT: {
            if (A)
              break;
            k ? b() : I();
            break;
          }
          case c.BACKSPACE: {
            A || b();
            break;
          }
          case c.ENTER: {
            if (i.length) {
              var v = o[p], n = (v == null ? void 0 : v[_]) || [];
              n.length ? K(n.map(function(u) {
                return u[l.value];
              }), n[n.length - 1]) : K(i, o[p]);
            }
            break;
          }
          case c.ESC:
            O(!1), U && e.stopPropagation();
        }
      },
      onKeyUp: function() {
      }
    };
  });
};
export {
  Q as default
};
