import { ref as t, watch as m, unref as v, nextTick as w } from "vue";
function F({ feedRef: u, emit: i, isLoadingMoreRef: p }) {
  const s = t(!1), a = t(!1), n = t(!1), r = t(!1), h = t(0), T = t(0), c = t(!1), f = t(!1), d = (o = !0) => {
    const l = v(u);
    if (!l) return;
    const e = l.scrollHeight - l.scrollTop - l.clientHeight;
    r.value ? (l.scrollTop < 300 && (n.value = !1, s.value = !1), e < 300 && (a.value = !1, n.value = !0)) : o && (l.scrollTop < 300 && (h.value = l.scrollHeight, T.value = l.scrollTop, c.value = !0, s.value = !1), e < 300 && (a.value = !1));
  }, S = (o = 0) => {
    const l = () => {
      w(() => {
        requestAnimationFrame(() => {
          try {
            const e = v(u);
            if (!e || !c.value) return;
            const g = e.style.scrollBehavior;
            e.style.scrollBehavior = "auto";
            const B = e.scrollHeight - h.value;
            e.scrollTop = T.value + B, setTimeout(() => {
              e.style.scrollBehavior = g || "auto";
            }, 50);
          } finally {
            c.value = !1, f.value = !0, setTimeout(() => {
              f.value = !1;
            }, 500);
          }
        });
      });
    };
    o > 0 ? setTimeout(l, o) : l();
  };
  return i && m(
    () => [a.value, s.value],
    () => {
      a.value || i("loadMoreDown"), s.value || i("loadMore");
    }
  ), p && m(
    () => p.value,
    (o, l) => {
      l === !0 && o === !1 && S(0);
    }
  ), {
    allowLoadMoreTop: s,
    allowLoadMoreBottom: a,
    movingDown: n,
    isScrollByMouseButton: r,
    pendingTopRestore: c,
    topLoadJustHappened: f,
    checkScrollPosition: d,
    restoreScrollPosition: S,
    startScrollWatch: (o) => {
      const l = v(u);
      if (!l) return;
      (o.offsetX > l.clientWidth || o.offsetY > l.clientHeight) && (r.value = !0);
    },
    stopScrollWatch: () => {
      r.value = !1;
    },
    resetAllowFlags: () => {
      s.value = !0, a.value = !0;
    }
  };
}
export {
  F as useFeedLoadMore
};
