import ne from "../../../@babel/runtime/helpers/esm/extends.mjs";
import ae from "../../../@babel/runtime/helpers/esm/typeof.mjs";
import le from "../../../@babel/runtime/helpers/esm/slicedToArray.mjs";
import { useContext as M } from "../../../@rc-component/context/es/context.mjs";
import * as n from "react";
import "../../../rc-util/node_modules/react-is/index.mjs";
import O from "../../../classnames/index.mjs";
import ie from "../../../rc-virtual-list/es/List.mjs";
import fe from "../Cell/index.mjs";
import ue, { responseImmutable as se } from "../context/TableContext.mjs";
import ce from "../hooks/useFlattenRecords.mjs";
import Y from "./BodyLine.mjs";
import { GridContext as me, StaticContext as de } from "./context.mjs";
var ve = /* @__PURE__ */ n.forwardRef(function(H, j) {
  var D = H.data, V = H.onScroll, f = M(ue, ["flattenColumns", "onColumnResize", "getRowKey", "prefixCls", "expandedKeys", "childrenColumnName", "emptyNode", "scrollX"]), m = f.flattenColumns, X = f.onColumnResize, d = f.getRowKey, W = f.expandedKeys, h = f.prefixCls, $ = f.childrenColumnName, A = f.emptyNode, F = f.scrollX, w = M(de), p = w.sticky, q = w.scrollY, J = w.listItemHeight, y = n.useRef(), u = ce(D, $, W, d), C = n.useMemo(function() {
    var o = 0;
    return m.map(function(r) {
      var e = r.width, t = r.key;
      return o += e, [t, e, o];
    });
  }, [m]), S = n.useMemo(function() {
    return C.map(function(o) {
      return o[2];
    });
  }, [C]);
  n.useEffect(function() {
    C.forEach(function(o) {
      var r = le(o, 2), e = r[0], t = r[1];
      X(e, t);
    });
  }, [C]), n.useImperativeHandle(j, function() {
    var o = {
      scrollTo: function(e) {
        var t;
        (t = y.current) === null || t === void 0 || t.scrollTo(e);
      }
    };
    return Object.defineProperty(o, "scrollLeft", {
      get: function() {
        var e;
        return ((e = y.current) === null || e === void 0 ? void 0 : e.getScrollInfo().x) || 0;
      },
      set: function(e) {
        var t;
        (t = y.current) === null || t === void 0 || t.scrollTo({
          left: e
        });
      }
    }), o;
  });
  var v = function(r, e) {
    var t, x = (t = u[e]) === null || t === void 0 ? void 0 : t.record, R = r.onCell;
    if (R) {
      var s, c = R(x, e);
      return (s = c == null ? void 0 : c.rowSpan) !== null && s !== void 0 ? s : 1;
    }
    return 1;
  }, Q = function(r) {
    var e = r.start, t = r.end, x = r.getSize, R = r.offsetY;
    if (t < 0)
      return null;
    for (var s = m.filter(
      // rowSpan is 0
      function(a) {
        return v(a, e) === 0;
      }
    ), c = e, Z = function(l) {
      if (s = s.filter(function(i) {
        return v(i, l) === 0;
      }), !s.length)
        return c = l, 1;
    }, K = e; K >= 0 && !Z(K); K -= 1)
      ;
    for (var I = m.filter(
      // rowSpan is not 1
      function(a) {
        return v(a, t) !== 1;
      }
    ), T = t, B = function(l) {
      if (I = I.filter(function(i) {
        return v(i, l) !== 1;
      }), !I.length)
        return T = Math.max(l - 1, t), 1;
    }, N = t; N < u.length && !B(N); N += 1)
      ;
    for (var z = [], P = function(l) {
      var i = u[l];
      if (!i)
        return 1;
      m.some(function(k) {
        return v(k, l) > 1;
      }) && z.push(l);
    }, E = c; E <= T; E += 1)
      P(E);
    var ee = z.map(function(a) {
      var l = u[a], i = d(l.record, a), k = function(re) {
        var L = a + re - 1, oe = d(u[L].record, L), G = x(i, oe);
        return G.bottom - G.top;
      }, te = x(i);
      return /* @__PURE__ */ n.createElement(Y, {
        key: a,
        data: l,
        rowKey: i,
        index: a,
        style: {
          top: -R + te.top
        },
        extra: !0,
        getHeight: k
      });
    });
    return ee;
  }, U = n.useMemo(function() {
    return {
      columnsOffset: S
    };
  }, [S]), _ = "".concat(h, "-tbody"), b;
  if (u.length) {
    var g = {};
    p && (g.position = "sticky", g.bottom = 0, ae(p) === "object" && p.offsetScroll && (g.bottom = p.offsetScroll)), b = /* @__PURE__ */ n.createElement(ie, {
      fullHeight: !1,
      ref: y,
      styles: {
        horizontalScrollBar: g
      },
      className: O(_, "".concat(_, "-virtual")),
      height: q,
      itemHeight: J || 24,
      data: u,
      itemKey: function(r) {
        return d(r.record);
      },
      scrollWidth: F,
      onVirtualScroll: function(r) {
        var e = r.x;
        V({
          scrollLeft: e
        });
      },
      extraRender: Q
    }, function(o, r, e) {
      var t = d(o.record, r);
      return /* @__PURE__ */ n.createElement(Y, ne({
        data: o,
        rowKey: t,
        index: r
      }, e));
    });
  } else
    b = /* @__PURE__ */ n.createElement("div", {
      className: O("".concat(h, "-placeholder"))
    }, /* @__PURE__ */ n.createElement(fe, {
      component: "div",
      prefixCls: h
    }, A));
  return /* @__PURE__ */ n.createElement(me.Provider, {
    value: U
  }, b);
}), pe = se(ve);
process.env.NODE_ENV !== "production" && (pe.displayName = "ResponseGrid");
export {
  pe as default
};
