import w from "../../@babel/runtime/helpers/esm/extends.mjs";
import N from "../../@babel/runtime/helpers/esm/objectSpread2.mjs";
import o from "../../@babel/runtime/helpers/esm/slicedToArray.mjs";
import Ue from "../../@babel/runtime/helpers/esm/objectWithoutProperties.mjs";
import * as f from "react";
import { useState as V, useMemo as me, useCallback as ve } from "react";
import qe from "../../classnames/index.mjs";
import He from "../../rc-resize-observer/es/index.mjs";
import Je from "../../rc-util/es/hooks/useLayoutEffect.mjs";
import $ from "./Item.mjs";
import y, { useBatcher as Qe } from "./hooks/useEffectState.mjs";
import Ye from "./RawItem.mjs";
import { OverflowContext as de } from "./context.mjs";
var Ze = ["prefixCls", "data", "renderItem", "renderRawItem", "itemKey", "itemWidth", "ssr", "style", "className", "maxCount", "renderRest", "renderRawRest", "suffix", "component", "itemComponent", "onVisibleChange"], ce = "responsive", Se = "invalidate";
function et(r) {
  return "+ ".concat(r.length, " ...");
}
function tt(r, pe) {
  var L = r.prefixCls, j = L === void 0 ? "rc-overflow" : L, F = r.data, a = F === void 0 ? [] : F, G = r.renderItem, X = r.renderRawItem, d = r.itemKey, B = r.itemWidth, U = B === void 0 ? 10 : B, ye = r.ssr, Re = r.style, _e = r.className, l = r.maxCount, Ee = r.renderRest, I = r.renderRawRest, x = r.suffix, q = r.component, he = q === void 0 ? "div" : q, Ce = r.itemComponent, W = r.onVisibleChange, ge = Ue(r, Ze), z = ye === "full", c = Qe(), we = y(c, null), H = o(we, 2), R = H[0], Ne = H[1], u = R || 0, be = y(c, /* @__PURE__ */ new Map()), J = o(be, 2), Q = J[0], Ie = J[1], xe = y(c, 0), Y = o(xe, 2), We = Y[0], ze = Y[1], Pe = y(c, 0), Z = o(Pe, 2), _ = Z[0], Ae = Z[1], Oe = y(c, 0), ee = o(Oe, 2), E = ee[0], Me = ee[1], ke = V(null), te = o(ke, 2), P = te[0], re = te[1], De = V(null), ie = o(De, 2), h = ie[0], Ke = ie[1], v = f.useMemo(function() {
    return h === null && z ? Number.MAX_SAFE_INTEGER : h || 0;
  }, [h, R]), Te = V(!1), ae = o(Te, 2), Ve = ae[0], $e = ae[1], A = "".concat(j, "-item"), ne = Math.max(We, _), O = l === ce, s = a.length && O, se = l === Se, Le = s || typeof l == "number" && a.length > l, m = me(function() {
    var e = a;
    return s ? R === null && z ? e = a : e = a.slice(0, Math.min(a.length, u / U)) : typeof l == "number" && (e = a.slice(0, l)), e;
  }, [a, U, R, l, s]), M = me(function() {
    return s ? a.slice(v + 1) : a.slice(m.length);
  }, [a, m, s, v]), C = ve(function(e, t) {
    var i;
    return typeof d == "function" ? d(e) : (i = d && (e == null ? void 0 : e[d])) !== null && i !== void 0 ? i : t;
  }, [d]), je = ve(G || function(e) {
    return e;
  }, [G]);
  function g(e, t, i) {
    h === e && (t === void 0 || t === P) || (Ke(e), i || ($e(e < a.length - 1), W == null || W(e)), t !== void 0 && re(t));
  }
  function Fe(e, t) {
    Ne(t.clientWidth);
  }
  function fe(e, t) {
    Ie(function(i) {
      var n = new Map(i);
      return t === null ? n.delete(e) : n.set(e, t), n;
    });
  }
  function Ge(e, t) {
    Ae(t), ze(_);
  }
  function Xe(e, t) {
    Me(t);
  }
  function k(e) {
    return Q.get(C(m[e], e));
  }
  Je(function() {
    if (u && typeof ne == "number" && m) {
      var e = E, t = m.length, i = t - 1;
      if (!t) {
        g(0, null);
        return;
      }
      for (var n = 0; n < t; n += 1) {
        var p = k(n);
        if (z && (p = p || 0), p === void 0) {
          g(n - 1, void 0, !0);
          break;
        }
        if (e += p, // Only one means `totalWidth` is the final width
        i === 0 && e <= u || // Last two width will be the final width
        n === i - 1 && e + k(i) <= u) {
          g(i, null);
          break;
        } else if (e + ne > u) {
          g(n - 1, e - p - E + _);
          break;
        }
      }
      x && k(0) + E > u && re(null);
    }
  }, [u, Q, _, E, C, m]);
  var oe = Ve && !!M.length, le = {};
  P !== null && s && (le = {
    position: "absolute",
    left: P,
    top: 0
  });
  var S = {
    prefixCls: A,
    responsive: s,
    component: Ce,
    invalidate: se
  }, Be = X ? function(e, t) {
    var i = C(e, t);
    return /* @__PURE__ */ f.createElement(de.Provider, {
      key: i,
      value: N(N({}, S), {}, {
        order: t,
        item: e,
        itemKey: i,
        registerSize: fe,
        display: t <= v
      })
    }, X(e, t));
  } : function(e, t) {
    var i = C(e, t);
    return /* @__PURE__ */ f.createElement($, w({}, S, {
      order: t,
      key: i,
      item: e,
      renderItem: je,
      itemKey: i,
      registerSize: fe,
      display: t <= v
    }));
  }, D, ue = {
    order: oe ? v : Number.MAX_SAFE_INTEGER,
    className: "".concat(A, "-rest"),
    registerSize: Ge,
    display: oe
  };
  if (I)
    I && (D = /* @__PURE__ */ f.createElement(de.Provider, {
      value: N(N({}, S), ue)
    }, I(M)));
  else {
    var K = Ee || et;
    D = /* @__PURE__ */ f.createElement($, w({}, S, ue), typeof K == "function" ? K(M) : K);
  }
  var T = /* @__PURE__ */ f.createElement(he, w({
    className: qe(!se && j, _e),
    style: Re,
    ref: pe
  }, ge), m.map(Be), Le ? D : null, x && /* @__PURE__ */ f.createElement($, w({}, S, {
    responsive: O,
    responsiveDisabled: !s,
    order: v,
    className: "".concat(A, "-suffix"),
    registerSize: Xe,
    display: !0,
    style: le
  }), x));
  return O && (T = /* @__PURE__ */ f.createElement(He, {
    onResize: Fe,
    disabled: !s
  }, T)), T;
}
var b = /* @__PURE__ */ f.forwardRef(tt);
b.displayName = "Overflow";
b.Item = Ye;
b.RESPONSIVE = ce;
b.INVALIDATE = Se;
export {
  de as OverflowContext,
  b as default
};
