import it from "../../@babel/runtime/helpers/esm/extends.mjs";
import v from "../../@babel/runtime/helpers/esm/toConsumableArray.mjs";
import Ie from "../../@babel/runtime/helpers/esm/defineProperty.mjs";
import F from "../../@babel/runtime/helpers/esm/objectSpread2.mjs";
import S from "../../@babel/runtime/helpers/esm/slicedToArray.mjs";
import at from "../../@babel/runtime/helpers/esm/objectWithoutProperties.mjs";
import nt from "../../@babel/runtime/helpers/esm/typeof.mjs";
import Ce from "../../rc-util/es/hooks/useMergedState.mjs";
import { warningOnce as ot } from "../../rc-util/es/warning.mjs";
import * as l from "react";
import lt, { isMultiple as Me } from "./BaseSelect.mjs";
import ut from "./OptGroup.mjs";
import ct from "./Option.mjs";
import dt from "./OptionList.mjs";
import st from "./SelectContext.mjs";
import ft from "./hooks/useCache.mjs";
import vt from "./hooks/useFilterOptions.mjs";
import mt from "./hooks/useId.mjs";
import pt from "./hooks/useOptions.mjs";
import we from "./hooks/useRefFunc.mjs";
import { isComboNoValue as ht, hasValue as St, toArray as gt } from "./utils/commonUtil.mjs";
import { fillFieldNames as bt, flattenOptions as Ot, injectPropsWithOption as xe } from "./utils/valueUtil.mjs";
import Vt, { warningNullOptions as _t } from "./utils/warningPropsUtil.mjs";
var yt = ["id", "mode", "prefixCls", "backfill", "fieldNames", "inputValue", "searchValue", "onSearch", "autoClearSearchValue", "onSelect", "onDeselect", "dropdownMatchSelectWidth", "filterOption", "filterSort", "optionFilterProp", "optionLabelProp", "options", "optionRender", "children", "defaultActiveFirstOption", "menuItemSelectedIcon", "virtual", "direction", "listHeight", "listItemHeight", "value", "defaultValue", "labelInValue", "onChange"], It = ["inputValue"];
function Ct(a) {
  return !a || nt(a) !== "object";
}
var Ae = /* @__PURE__ */ l.forwardRef(function(a, Ne) {
  var Pe = a.id, u = a.mode, B = a.prefixCls, Re = B === void 0 ? "rc-select" : B, J = a.backfill, K = a.fieldNames, Ee = a.inputValue, q = a.searchValue, k = a.onSearch, z = a.autoClearSearchValue, Q = z === void 0 ? !0 : z, U = a.onSelect, X = a.onDeselect, Y = a.dropdownMatchSelectWidth, H = Y === void 0 ? !0 : Y, L = a.filterOption, $ = a.filterSort, M = a.optionFilterProp, w = a.optionLabelProp, Z = a.options, ee = a.optionRender, te = a.children, re = a.defaultActiveFirstOption, ie = a.menuItemSelectedIcon, ae = a.virtual, ne = a.direction, oe = a.listHeight, le = oe === void 0 ? 200 : oe, ue = a.listItemHeight, ce = ue === void 0 ? 20 : ue, Fe = a.value, ke = a.defaultValue, de = a.labelInValue, se = a.onChange, He = at(a, yt), fe = mt(Pe), x = Me(u), g = !!(!Z && te), Le = l.useMemo(function() {
    return L === void 0 && u === "combobox" ? !1 : L;
  }, [L, u]), d = l.useMemo(
    function() {
      return bt(K, g);
    },
    /* eslint-disable react-hooks/exhaustive-deps */
    [
      // We stringify fieldNames to avoid unnecessary re-renders.
      JSON.stringify(K),
      g
    ]
    /* eslint-enable react-hooks/exhaustive-deps */
  ), $e = Ce("", {
    value: q !== void 0 ? q : Ee,
    postState: function(r) {
      return r || "";
    }
  }), ve = S($e, 2), m = ve[0], A = ve[1], V = pt(Z, te, d, M, w), _ = V.valueOptions, We = V.labelOptions, D = V.options, W = l.useCallback(function(t) {
    var r = gt(t);
    return r.map(function(e) {
      var i, n, o, p, f;
      if (Ct(e))
        i = e;
      else {
        var I;
        o = e.key, n = e.label, i = (I = e.value) !== null && I !== void 0 ? I : o;
      }
      var c = _.get(i);
      if (c) {
        var O;
        if (n === void 0 && (n = c == null ? void 0 : c[w || d.label]), o === void 0 && (o = (O = c == null ? void 0 : c.key) !== null && O !== void 0 ? O : i), p = c == null ? void 0 : c.disabled, f = c == null ? void 0 : c.title, process.env.NODE_ENV !== "production" && !w) {
          var C = c == null ? void 0 : c[d.label];
          C !== void 0 && !/* @__PURE__ */ l.isValidElement(C) && !/* @__PURE__ */ l.isValidElement(n) && C !== n && ot(!1, "`label` of `value` is not same as `label` in Select options.");
        }
      }
      return {
        label: n,
        value: i,
        key: o,
        disabled: p,
        title: f
      };
    });
  }, [d, w, _]), je = Ce(ke, {
    value: Fe
  }), me = S(je, 2), pe = me[0], Te = me[1], Ge = l.useMemo(function() {
    var t, r = W(pe);
    return u === "combobox" && ht((t = r[0]) === null || t === void 0 ? void 0 : t.value) ? [] : r;
  }, [pe, W, u]), Be = ft(Ge, _), he = S(Be, 2), s = he[0], Se = he[1], Je = l.useMemo(function() {
    if (!u && s.length === 1) {
      var t = s[0];
      if (t.value === null && (t.label === null || t.label === void 0))
        return [];
    }
    return s.map(function(r) {
      var e;
      return F(F({}, r), {}, {
        label: (e = r.label) !== null && e !== void 0 ? e : r.value
      });
    });
  }, [u, s]), N = l.useMemo(function() {
    return new Set(s.map(function(t) {
      return t.value;
    }));
  }, [s]);
  l.useEffect(function() {
    if (u === "combobox") {
      var t, r = (t = s[0]) === null || t === void 0 ? void 0 : t.value;
      A(St(r) ? String(r) : "");
    }
  }, [s]);
  var P = we(function(t, r) {
    var e, i = r ?? t;
    return e = {}, Ie(e, d.value, t), Ie(e, d.label, i), e;
  }), Ke = l.useMemo(function() {
    if (u !== "tags")
      return D;
    var t = v(D), r = function(i) {
      return _.has(i);
    };
    return v(s).sort(function(e, i) {
      return e.value < i.value ? -1 : 1;
    }).forEach(function(e) {
      var i = e.value;
      r(i) || t.push(P(i, e.label));
    }), t;
  }, [P, D, _, s, u]), b = vt(Ke, d, m, Le, M), j = l.useMemo(function() {
    return u !== "tags" || !m || b.some(function(t) {
      return t[M || "value"] === m;
    }) || b.some(function(t) {
      return t[d.value] === m;
    }) ? b : [P(m)].concat(v(b));
  }, [P, M, u, b, m, d]), ge = l.useMemo(function() {
    return $ ? v(j).sort(function(t, r) {
      return $(t, r);
    }) : j;
  }, [j, $]), T = l.useMemo(function() {
    return Ot(ge, {
      fieldNames: d,
      childrenAsData: g
    });
  }, [ge, d, g]), y = function(r) {
    var e = W(r);
    if (Te(e), se && // Trigger event only when value changed
    (e.length !== s.length || e.some(function(o, p) {
      var f;
      return ((f = s[p]) === null || f === void 0 ? void 0 : f.value) !== (o == null ? void 0 : o.value);
    }))) {
      var i = de ? e : e.map(function(o) {
        return o.value;
      }), n = e.map(function(o) {
        return xe(Se(o.value));
      });
      se(
        // Value
        x ? i : i[0],
        // Option
        x ? n : n[0]
      );
    }
  }, qe = l.useState(null), be = S(qe, 2), ze = be[0], R = be[1], Qe = l.useState(0), Oe = S(Qe, 2), Ue = Oe[0], Xe = Oe[1], Ve = re !== void 0 ? re : u !== "combobox", _e = l.useCallback(function(t, r) {
    var e = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : {}, i = e.source, n = i === void 0 ? "keyboard" : i;
    Xe(r), J && u === "combobox" && t !== null && n === "keyboard" && R(String(t));
  }, [J, u]), E = function(r, e, i) {
    var n = function() {
      var G, h = Se(r);
      return [de ? {
        label: h == null ? void 0 : h[d.label],
        value: r,
        key: (G = h == null ? void 0 : h.key) !== null && G !== void 0 ? G : r
      } : r, xe(h)];
    };
    if (e && U) {
      var o = n(), p = S(o, 2), f = p[0], I = p[1];
      U(f, I);
    } else if (!e && X && i !== "clear") {
      var c = n(), O = S(c, 2), C = O[0], rt = O[1];
      X(C, rt);
    }
  }, ye = we(function(t, r) {
    var e, i = x ? r.selected : !0;
    i ? e = x ? [].concat(v(s), [t]) : [t] : e = s.filter(function(n) {
      return n.value !== t;
    }), y(e), E(t, i), u === "combobox" ? R("") : (!Me || Q) && (A(""), R(""));
  }), Ye = function(r, e) {
    y(r);
    var i = e.type, n = e.values;
    (i === "remove" || i === "clear") && n.forEach(function(o) {
      E(o.value, !1, i);
    });
  }, Ze = function(r, e) {
    if (A(r), R(null), e.source === "submit") {
      var i = (r || "").trim();
      if (i) {
        var n = Array.from(new Set([].concat(v(N), [i])));
        y(n), E(i, !0), A("");
      }
      return;
    }
    e.source !== "blur" && (u === "combobox" && y(r), k == null || k(r));
  }, et = function(r) {
    var e = r;
    u !== "tags" && (e = r.map(function(n) {
      var o = We.get(n);
      return o == null ? void 0 : o.value;
    }).filter(function(n) {
      return n !== void 0;
    }));
    var i = Array.from(new Set([].concat(v(N), v(e))));
    y(i), i.forEach(function(n) {
      E(n, !0);
    });
  }, tt = l.useMemo(function() {
    var t = ae !== !1 && H !== !1;
    return F(F({}, V), {}, {
      flattenOptions: T,
      onActiveValue: _e,
      defaultActiveFirstOption: Ve,
      onSelect: ye,
      menuItemSelectedIcon: ie,
      rawValues: N,
      fieldNames: d,
      virtual: t,
      direction: ne,
      listHeight: le,
      listItemHeight: ce,
      childrenAsData: g,
      optionRender: ee
    });
  }, [V, T, _e, Ve, ye, ie, N, d, ae, H, le, ce, g, ee]);
  return process.env.NODE_ENV !== "production" && (Vt(a), _t(D, d)), /* @__PURE__ */ l.createElement(st.Provider, {
    value: tt
  }, /* @__PURE__ */ l.createElement(lt, it({}, He, {
    // >>> MISC
    id: fe,
    prefixCls: Re,
    ref: Ne,
    omitDomProps: It,
    mode: u,
    displayValues: Je,
    onDisplayValuesChange: Ye,
    direction: ne,
    searchValue: m,
    onSearch: Ze,
    autoClearSearchValue: Q,
    onSearchSplit: et,
    dropdownMatchSelectWidth: H,
    OptionList: dt,
    emptyOptions: !T.length,
    activeValue: ze,
    activeDescendantId: "".concat(fe, "_list_").concat(Ue)
  })));
});
process.env.NODE_ENV !== "production" && (Ae.displayName = "Select");
var De = Ae;
De.Option = ct;
De.OptGroup = ut;
export {
  De as default
};
