import pe from "../../../@babel/runtime/helpers/esm/extends.mjs";
import X from "../../../@babel/runtime/helpers/esm/objectSpread2.mjs";
import S from "../../../@babel/runtime/helpers/esm/defineProperty.mjs";
import me from "../../../@babel/runtime/helpers/esm/toConsumableArray.mjs";
import Y from "../../../@babel/runtime/helpers/esm/slicedToArray.mjs";
import de from "../../../classnames/index.mjs";
import * as i from "react";
import he from "../context.mjs";
import { toPathValueStr as ge, isLeaf as V, toPathKeys as z, getFullPathKeys as Se, toPathKey as K, scrollIntoParentView as Ce } from "../utils/commonUtil.mjs";
import { toPathOptions as G } from "../utils/treeUtil.mjs";
import Oe from "./CacheContent.mjs";
import ye, { FIX_LABEL as Pe } from "./Column.mjs";
import _e from "./useActive.mjs";
import Ve from "./useKeyboard.mjs";
var be = /* @__PURE__ */ i.forwardRef(function(s, H) {
  var c, v, C, J = s.prefixCls, O = s.multiple, p = s.searchValue, b = s.toggleOpen, Q = s.notFoundContent, w = s.direction, L = s.open, x = i.useRef(), U = w === "rtl", l = i.useContext(he), m = l.options, N = l.values, A = l.halfValues, o = l.fieldNames, k = l.changeOnSelect, W = l.onSelect, R = l.searchOptions, Z = l.dropdownPrefixCls, M = l.loadData, ee = l.expandTrigger, y = Z || J, te = i.useState([]), D = Y(te, 2), P = D[0], T = D[1], re = function(e) {
    if (!(!M || p)) {
      var t = G(e, m, o), r = t.map(function(d) {
        var h = d.option;
        return h;
      }), n = r[r.length - 1];
      if (n && !V(n, o)) {
        var f = K(e);
        T(function(d) {
          return [].concat(me(d), [f]);
        }), M(r);
      }
    }
  };
  i.useEffect(function() {
    P.length && P.forEach(function(a) {
      var e = ge(a), t = G(e, m, o, !0).map(function(n) {
        var f = n.option;
        return f;
      }), r = t[t.length - 1];
      (!r || r[o.children] || V(r, o)) && T(function(n) {
        return n.filter(function(f) {
          return f !== a;
        });
      });
    });
  }, [m, P, o]);
  var ae = i.useMemo(function() {
    return new Set(z(N));
  }, [N]), oe = i.useMemo(function() {
    return new Set(z(A));
  }, [A]), ne = _e(O, L), F = Y(ne, 2), u = F[0], ie = F[1], I = function(e) {
    ie(e), re(e);
  }, $ = function(e) {
    var t = e.disabled, r = V(e, o);
    return !t && (r || k || O);
  }, j = function(e, t) {
    var r = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : !1;
    W(e), !O && (t || k && (ee === "hover" || r)) && b(!1);
  }, _ = i.useMemo(function() {
    return p ? R : m;
  }, [p, R, m]), q = i.useMemo(function() {
    for (var a = [{
      options: _
    }], e = _, t = Se(e, o), r = function() {
      var d = u[n], h = e.find(function(ve, B) {
        return (t[B] ? K(t[B]) : ve[o.value]) === d;
      }), g = h == null ? void 0 : h[o.children];
      if (!(g != null && g.length))
        return 1;
      e = g, a.push({
        options: g
      });
    }, n = 0; n < u.length && !r(); n += 1)
      ;
    return a;
  }, [_, u, o]), le = function(e, t) {
    $(t) && j(e, V(t, o), !0);
  };
  Ve(H, _, o, u, I, le, {
    direction: w,
    searchValue: p,
    toggleOpen: b,
    open: L
  }), i.useEffect(function() {
    for (var a = 0; a < u.length; a += 1) {
      var e, t = u.slice(0, a + 1), r = K(t), n = (e = x.current) === null || e === void 0 ? void 0 : e.querySelector(
        'li[data-path-key="'.concat(r.replace(/\\{0,2}"/g, '\\"'), '"]')
        // matches unescaped double quotes
      );
      n && Ce(n);
    }
  }, [u]);
  var E = !((c = q[0]) !== null && c !== void 0 && (c = c.options) !== null && c !== void 0 && c.length), ue = [(v = {}, S(v, o.value, "__EMPTY__"), S(v, Pe, Q), S(v, "disabled", !0), v)], se = X(X({}, s), {}, {
    multiple: !E && O,
    onSelect: j,
    onActive: I,
    onToggleOpen: b,
    checkedSet: ae,
    halfCheckedSet: oe,
    loadingKeys: P,
    isSelectable: $
  }), ce = E ? [{
    options: ue
  }] : q, fe = ce.map(function(a, e) {
    var t = u.slice(0, e), r = u[e];
    return /* @__PURE__ */ i.createElement(ye, pe({
      key: e
    }, se, {
      searchValue: p,
      prefixCls: y,
      options: a.options,
      prevValuePath: t,
      activeValue: r
    }));
  });
  return /* @__PURE__ */ i.createElement(Oe, {
    open: L
  }, /* @__PURE__ */ i.createElement("div", {
    className: de("".concat(y, "-menus"), (C = {}, S(C, "".concat(y, "-menu-empty"), E), S(C, "".concat(y, "-rtl"), U), C)),
    ref: x
  }, fe));
});
process.env.NODE_ENV !== "production" && (be.displayName = "RawOptionList");
export {
  be as default
};
