import G from "../../../@babel/runtime/helpers/esm/typeof.mjs";
import S from "../../../@babel/runtime/helpers/esm/objectSpread2.mjs";
import J from "../../../@babel/runtime/helpers/esm/slicedToArray.mjs";
import * as I from "react";
import L from "../../../rc-util/es/raf.mjs";
import Q from "../../../rc-util/es/hooks/useLayoutEffect.mjs";
import "../../../rc-util/node_modules/react-is/index.mjs";
import { warningOnce as U } from "../../../rc-util/es/warning.mjs";
var N = 10;
function T(a, t, k, O, l, D, x, P) {
  var V = I.useRef(), X = I.useState(null), y = J(X, 2), e = y[0], c = y[1];
  return Q(function() {
    if (e && e.times < N) {
      if (!a.current) {
        c(function(F) {
          return S({}, F);
        });
        return;
      }
      D();
      var r = e.targetAlign, u = e.originAlign, m = e.index, i = e.offset, n = a.current.clientHeight, s = !1, d = r, f = null;
      if (n) {
        for (var E = r || u, M = 0, o = 0, v = 0, R = Math.min(t.length - 1, m), p = 0; p <= R; p += 1) {
          var $ = l(t[p]);
          o = M;
          var _ = k.get($);
          v = o + (_ === void 0 ? O : _), M = v;
        }
        for (var j = E === "top" ? i : n - i, b = R; b >= 0; b -= 1) {
          var q = l(t[b]), w = k.get(q);
          if (w === void 0) {
            s = !0;
            break;
          }
          if (j -= w, j <= 0)
            break;
        }
        switch (E) {
          case "top":
            f = o - i;
            break;
          case "bottom":
            f = v - n + i;
            break;
          default: {
            var A = a.current.scrollTop, z = A + n;
            o < A ? d = "top" : v > z && (d = "bottom");
          }
        }
        f !== null && x(f), f !== e.lastTop && (s = !0);
      }
      s && c(S(S({}, e), {}, {
        times: e.times + 1,
        targetAlign: d,
        lastTop: f
      }));
    } else
      process.env.NODE_ENV !== "production" && (e == null ? void 0 : e.times) === N && U(!1, "Seems `scrollTo` with `rc-virtual-list` reach the max limitation. Please fire issue for us. Thanks.");
  }, [e, a.current]), function(r) {
    if (r == null) {
      P();
      return;
    }
    if (L.cancel(V.current), typeof r == "number")
      x(r);
    else if (r && G(r) === "object") {
      var u, m = r.align;
      "index" in r ? u = r.index : u = t.findIndex(function(s) {
        return l(s) === r.key;
      });
      var i = r.offset, n = i === void 0 ? 0 : i;
      c({
        times: 0,
        index: u,
        offset: n,
        originAlign: m
      });
    }
  };
}
export {
  T as default
};
