"use client";
import re from "../../@babel/runtime/helpers/esm/extends.js";
import U from "../../@babel/runtime/helpers/esm/assertThisInitialized.js";
import ie from "../../@babel/runtime/helpers/esm/inheritsLoose.js";
import D from "../../memoize-one/dist/memoize-one.esm.js";
import { createElement as k, PureComponent as ae } from "react";
var ne = typeof performance == "object" && typeof performance.now == "function", V = ne ? function() {
  return performance.now();
} : function() {
  return Date.now();
};
function q(l) {
  cancelAnimationFrame(l.id);
}
function oe(l, t) {
  var r = V();
  function a() {
    V() - r >= t ? l.call(null) : n.id = requestAnimationFrame(a);
  }
  var n = {
    id: requestAnimationFrame(a)
  };
  return n;
}
var A = -1;
function H(l) {
  if (l === void 0 && (l = !1), A === -1 || l) {
    var t = document.createElement("div"), r = t.style;
    r.width = "50px", r.height = "50px", r.overflow = "scroll", document.body.appendChild(t), A = t.offsetWidth - t.clientWidth, document.body.removeChild(t);
  }
  return A;
}
var b = null;
function K(l) {
  if (l === void 0 && (l = !1), b === null || l) {
    var t = document.createElement("div"), r = t.style;
    r.width = "50px", r.height = "50px", r.overflow = "scroll", r.direction = "rtl";
    var a = document.createElement("div"), n = a.style;
    return n.width = "100px", n.height = "100px", t.appendChild(a), document.body.appendChild(t), t.scrollLeft > 0 ? b = "positive-descending" : (t.scrollLeft = 1, t.scrollLeft === 0 ? b = "negative" : b = "positive-ascending"), document.body.removeChild(t), b;
  }
  return b;
}
process.env.NODE_ENV;
var le = 150, se = function(t, r) {
  return t;
}, N = null, R = null;
process.env.NODE_ENV !== "production" && typeof window < "u" && typeof window.WeakSet < "u" && (N = /* @__PURE__ */ new WeakSet(), R = /* @__PURE__ */ new WeakSet());
function ce(l) {
  var t, r = l.getItemOffset, a = l.getEstimatedTotalSize, n = l.getItemSize, c = l.getOffsetForIndexAndAlignment, s = l.getStartIndexForOffset, h = l.getStopIndexForStartIndex, y = l.initInstanceProps, I = l.shouldResetStyleCacheOnItemSizeChange, g = l.validateProps;
  return t = /* @__PURE__ */ (function(w) {
    ie(z, w);
    function z(S) {
      var e;
      return e = w.call(this, S) || this, e._instanceProps = y(e.props, U(e)), e._outerRef = void 0, e._resetIsScrollingTimeoutId = null, e.state = {
        instance: U(e),
        isScrolling: !1,
        scrollDirection: "forward",
        scrollOffset: typeof e.props.initialScrollOffset == "number" ? e.props.initialScrollOffset : 0,
        scrollUpdateWasRequested: !1
      }, e._callOnItemsRendered = void 0, e._callOnItemsRendered = D(function(i, o, f, u) {
        return e.props.onItemsRendered({
          overscanStartIndex: i,
          overscanStopIndex: o,
          visibleStartIndex: f,
          visibleStopIndex: u
        });
      }), e._callOnScroll = void 0, e._callOnScroll = D(function(i, o, f) {
        return e.props.onScroll({
          scrollDirection: i,
          scrollOffset: o,
          scrollUpdateWasRequested: f
        });
      }), e._getItemStyle = void 0, e._getItemStyle = function(i) {
        var o = e.props, f = o.direction, u = o.itemSize, p = o.layout, d = e._getItemStyleCache(I && u, I && p, I && f), m;
        if (d.hasOwnProperty(i))
          m = d[i];
        else {
          var v = r(e.props, i, e._instanceProps), T = n(e.props, i, e._instanceProps), M = f === "horizontal" || p === "horizontal", C = f === "rtl", E = M ? v : 0;
          d[i] = m = {
            position: "absolute",
            left: C ? void 0 : E,
            right: C ? E : void 0,
            top: M ? 0 : v,
            height: M ? "100%" : T,
            width: M ? T : "100%"
          };
        }
        return m;
      }, e._getItemStyleCache = void 0, e._getItemStyleCache = D(function(i, o, f) {
        return {};
      }), e._onScrollHorizontal = function(i) {
        var o = i.currentTarget, f = o.clientWidth, u = o.scrollLeft, p = o.scrollWidth;
        e.setState(function(d) {
          if (d.scrollOffset === u)
            return null;
          var m = e.props.direction, v = u;
          if (m === "rtl")
            switch (K()) {
              case "negative":
                v = -u;
                break;
              case "positive-descending":
                v = p - f - u;
                break;
            }
          return v = Math.max(0, Math.min(v, p - f)), {
            isScrolling: !0,
            scrollDirection: d.scrollOffset < v ? "forward" : "backward",
            scrollOffset: v,
            scrollUpdateWasRequested: !1
          };
        }, e._resetIsScrollingDebounced);
      }, e._onScrollVertical = function(i) {
        var o = i.currentTarget, f = o.clientHeight, u = o.scrollHeight, p = o.scrollTop;
        e.setState(function(d) {
          if (d.scrollOffset === p)
            return null;
          var m = Math.max(0, Math.min(p, u - f));
          return {
            isScrolling: !0,
            scrollDirection: d.scrollOffset < m ? "forward" : "backward",
            scrollOffset: m,
            scrollUpdateWasRequested: !1
          };
        }, e._resetIsScrollingDebounced);
      }, e._outerRefSetter = function(i) {
        var o = e.props.outerRef;
        e._outerRef = i, typeof o == "function" ? o(i) : o != null && typeof o == "object" && o.hasOwnProperty("current") && (o.current = i);
      }, e._resetIsScrollingDebounced = function() {
        e._resetIsScrollingTimeoutId !== null && q(e._resetIsScrollingTimeoutId), e._resetIsScrollingTimeoutId = oe(e._resetIsScrolling, le);
      }, e._resetIsScrolling = function() {
        e._resetIsScrollingTimeoutId = null, e.setState({
          isScrolling: !1
        }, function() {
          e._getItemStyleCache(-1, null);
        });
      }, e;
    }
    z.getDerivedStateFromProps = function(e, i) {
      return de(e, i), g(e), null;
    };
    var _ = z.prototype;
    return _.scrollTo = function(e) {
      e = Math.max(0, e), this.setState(function(i) {
        return i.scrollOffset === e ? null : {
          scrollDirection: i.scrollOffset < e ? "forward" : "backward",
          scrollOffset: e,
          scrollUpdateWasRequested: !0
        };
      }, this._resetIsScrollingDebounced);
    }, _.scrollToItem = function(e, i) {
      i === void 0 && (i = "auto");
      var o = this.props, f = o.itemCount, u = o.layout, p = this.state.scrollOffset;
      e = Math.max(0, Math.min(e, f - 1));
      var d = 0;
      if (this._outerRef) {
        var m = this._outerRef;
        u === "vertical" ? d = m.scrollWidth > m.clientWidth ? H() : 0 : d = m.scrollHeight > m.clientHeight ? H() : 0;
      }
      this.scrollTo(c(this.props, e, i, p, this._instanceProps, d));
    }, _.componentDidMount = function() {
      var e = this.props, i = e.direction, o = e.initialScrollOffset, f = e.layout;
      if (typeof o == "number" && this._outerRef != null) {
        var u = this._outerRef;
        i === "horizontal" || f === "horizontal" ? u.scrollLeft = o : u.scrollTop = o;
      }
      this._callPropsCallbacks();
    }, _.componentDidUpdate = function() {
      var e = this.props, i = e.direction, o = e.layout, f = this.state, u = f.scrollOffset, p = f.scrollUpdateWasRequested;
      if (p && this._outerRef != null) {
        var d = this._outerRef;
        if (i === "horizontal" || o === "horizontal")
          if (i === "rtl")
            switch (K()) {
              case "negative":
                d.scrollLeft = -u;
                break;
              case "positive-ascending":
                d.scrollLeft = u;
                break;
              default:
                var m = d.clientWidth, v = d.scrollWidth;
                d.scrollLeft = v - m - u;
                break;
            }
          else
            d.scrollLeft = u;
        else
          d.scrollTop = u;
      }
      this._callPropsCallbacks();
    }, _.componentWillUnmount = function() {
      this._resetIsScrollingTimeoutId !== null && q(this._resetIsScrollingTimeoutId);
    }, _.render = function() {
      var e = this.props, i = e.children, o = e.className, f = e.direction, u = e.height, p = e.innerRef, d = e.innerElementType, m = e.innerTagName, v = e.itemCount, T = e.itemData, M = e.itemKey, C = M === void 0 ? se : M, E = e.layout, G = e.outerElementType, Z = e.outerTagName, J = e.style, Q = e.useIsScrolling, X = e.width, P = this.state.isScrolling, W = f === "horizontal" || E === "horizontal", Y = W ? this._onScrollHorizontal : this._onScrollVertical, L = this._getRangeToRender(), ee = L[0], te = L[1], F = [];
      if (v > 0)
        for (var x = ee; x <= te; x++)
          F.push(k(i, {
            data: T,
            key: C(x, T),
            index: x,
            isScrolling: Q ? P : void 0,
            style: this._getItemStyle(x)
          }));
      var $ = a(this.props, this._instanceProps);
      return k(G || Z || "div", {
        className: o,
        onScroll: Y,
        ref: this._outerRefSetter,
        style: re({
          position: "relative",
          height: u,
          width: X,
          overflow: "auto",
          WebkitOverflowScrolling: "touch",
          willChange: "transform",
          direction: f
        }, J)
      }, k(d || m || "div", {
        children: F,
        ref: p,
        style: {
          height: W ? "100%" : $,
          pointerEvents: P ? "none" : void 0,
          width: W ? $ : "100%"
        }
      }));
    }, _._callPropsCallbacks = function() {
      if (typeof this.props.onItemsRendered == "function") {
        var e = this.props.itemCount;
        if (e > 0) {
          var i = this._getRangeToRender(), o = i[0], f = i[1], u = i[2], p = i[3];
          this._callOnItemsRendered(o, f, u, p);
        }
      }
      if (typeof this.props.onScroll == "function") {
        var d = this.state, m = d.scrollDirection, v = d.scrollOffset, T = d.scrollUpdateWasRequested;
        this._callOnScroll(m, v, T);
      }
    }, _._getRangeToRender = function() {
      var e = this.props, i = e.itemCount, o = e.overscanCount, f = this.state, u = f.isScrolling, p = f.scrollDirection, d = f.scrollOffset;
      if (i === 0)
        return [0, 0, 0, 0];
      var m = s(this.props, d, this._instanceProps), v = h(this.props, m, d, this._instanceProps), T = !u || p === "backward" ? Math.max(1, o) : 1, M = !u || p === "forward" ? Math.max(1, o) : 1;
      return [Math.max(0, m - T), Math.max(0, Math.min(i - 1, v + M)), m, v];
    }, z;
  })(ae), t.defaultProps = {
    direction: "ltr",
    itemData: void 0,
    layout: "vertical",
    overscanCount: 2,
    useIsScrolling: !1
  }, t;
}
var de = function(t, r) {
  var a = t.children, n = t.direction, c = t.height, s = t.layout, h = t.innerTagName, y = t.outerTagName, I = t.width, g = r.instance;
  if (process.env.NODE_ENV !== "production") {
    (h != null || y != null) && R && !R.has(g) && (R.add(g), console.warn("The innerTagName and outerTagName props have been deprecated. Please use the innerElementType and outerElementType props instead."));
    var w = n === "horizontal" || s === "horizontal";
    switch (n) {
      case "horizontal":
      case "vertical":
        N && !N.has(g) && (N.add(g), console.warn('The direction prop should be either "ltr" (default) or "rtl". Please use the layout prop to specify "vertical" (default) or "horizontal" orientation.'));
        break;
      case "ltr":
      case "rtl":
        break;
      default:
        throw Error('An invalid "direction" prop has been specified. Value should be either "ltr" or "rtl". ' + ('"' + n + '" was specified.'));
    }
    switch (s) {
      case "horizontal":
      case "vertical":
        break;
      default:
        throw Error('An invalid "layout" prop has been specified. Value should be either "horizontal" or "vertical". ' + ('"' + s + '" was specified.'));
    }
    if (a == null)
      throw Error('An invalid "children" prop has been specified. Value should be a React component. ' + ('"' + (a === null ? "null" : typeof a) + '" was specified.'));
    if (w && typeof I != "number")
      throw Error('An invalid "width" prop has been specified. Horizontal lists must specify a number for width. ' + ('"' + (I === null ? "null" : typeof I) + '" was specified.'));
    if (!w && typeof c != "number")
      throw Error('An invalid "height" prop has been specified. Vertical lists must specify a number for height. ' + ('"' + (c === null ? "null" : typeof c) + '" was specified.'));
  }
}, fe = 50, O = function(t, r, a) {
  var n = t, c = n.itemSize, s = a.itemMetadataMap, h = a.lastMeasuredIndex;
  if (r > h) {
    var y = 0;
    if (h >= 0) {
      var I = s[h];
      y = I.offset + I.size;
    }
    for (var g = h + 1; g <= r; g++) {
      var w = c(g);
      s[g] = {
        offset: y,
        size: w
      }, y += w;
    }
    a.lastMeasuredIndex = r;
  }
  return s[r];
}, ue = function(t, r, a) {
  var n = r.itemMetadataMap, c = r.lastMeasuredIndex, s = c > 0 ? n[c].offset : 0;
  return s >= a ? j(t, r, c, 0, a) : he(t, r, Math.max(0, c), a);
}, j = function(t, r, a, n, c) {
  for (; n <= a; ) {
    var s = n + Math.floor((a - n) / 2), h = O(t, s, r).offset;
    if (h === c)
      return s;
    h < c ? n = s + 1 : h > c && (a = s - 1);
  }
  return n > 0 ? n - 1 : 0;
}, he = function(t, r, a, n) {
  for (var c = t.itemCount, s = 1; a < c && O(t, a, r).offset < n; )
    a += s, s *= 2;
  return j(t, r, Math.min(a, c - 1), Math.floor(a / 2), n);
}, B = function(t, r) {
  var a = t.itemCount, n = r.itemMetadataMap, c = r.estimatedItemSize, s = r.lastMeasuredIndex, h = 0;
  if (s >= a && (s = a - 1), s >= 0) {
    var y = n[s];
    h = y.offset + y.size;
  }
  var I = a - s - 1, g = I * c;
  return h + g;
}, Se = /* @__PURE__ */ ce({
  getItemOffset: function(t, r, a) {
    return O(t, r, a).offset;
  },
  getItemSize: function(t, r, a) {
    return a.itemMetadataMap[r].size;
  },
  getEstimatedTotalSize: B,
  getOffsetForIndexAndAlignment: function(t, r, a, n, c, s) {
    var h = t.direction, y = t.height, I = t.layout, g = t.width, w = h === "horizontal" || I === "horizontal", z = w ? g : y, _ = O(t, r, c), S = B(t, c), e = Math.max(0, Math.min(S - z, _.offset)), i = Math.max(0, _.offset - z + _.size + s);
    switch (a === "smart" && (n >= i - z && n <= e + z ? a = "auto" : a = "center"), a) {
      case "start":
        return e;
      case "end":
        return i;
      case "center":
        return Math.round(i + (e - i) / 2);
      case "auto":
      default:
        return n >= i && n <= e ? n : n < i ? i : e;
    }
  },
  getStartIndexForOffset: function(t, r, a) {
    return ue(t, a, r);
  },
  getStopIndexForStartIndex: function(t, r, a, n) {
    for (var c = t.direction, s = t.height, h = t.itemCount, y = t.layout, I = t.width, g = c === "horizontal" || y === "horizontal", w = g ? I : s, z = O(t, r, n), _ = a + w, S = z.offset + z.size, e = r; e < h - 1 && S < _; )
      e++, S += O(t, e, n).size;
    return e;
  },
  initInstanceProps: function(t, r) {
    var a = t, n = a.estimatedItemSize, c = {
      itemMetadataMap: {},
      estimatedItemSize: n || fe,
      lastMeasuredIndex: -1
    };
    return r.resetAfterIndex = function(s, h) {
      h === void 0 && (h = !0), c.lastMeasuredIndex = Math.min(c.lastMeasuredIndex, s - 1), r._getItemStyleCache(-1), h && r.forceUpdate();
    }, c;
  },
  shouldResetStyleCacheOnItemSizeChange: !1,
  validateProps: function(t) {
    var r = t.itemSize;
    if (process.env.NODE_ENV !== "production" && typeof r != "function")
      throw Error('An invalid "itemSize" prop has been specified. Value should be a function. ' + ('"' + (r === null ? "null" : typeof r) + '" was specified.'));
  }
});
export {
  Se as VariableSizeList
};
