import { Subject } from "rxjs";
import { throttleTime } from "rxjs/operators";

const SCROLL_END_THROTTLE_MS = 1000;
const scrollEndReachedSubject = new Subject<void>();

/* Throttle so we only emit at most once per second; RN often fires onEndReached repeatedly (e.g. on Android) when near the bottom. */
export const scrollEndReached$ = scrollEndReachedSubject.pipe(
  throttleTime(SCROLL_END_THROTTLE_MS)
);

/* Call from scroll container (ComponentsMap or Tabs) when scroll reaches end. */
export const emitScrollEndReached = (): void => {
  scrollEndReachedSubject.next();
};
