/**
 * Human-like collection pacing, matched to the competitor teardown's CONFIG
 * (restored crawler.py) — the behavior of an Instagram scraper that stays
 * unbanned. We adopt its values verbatim EXCEPT its flaws (e.g. a Windows UA on a
 * non-Windows host); our user agent stays matched to the actual OS.
 *
 * slow_mo=300 delays every Playwright action; the scroll waits + occasional long
 * pauses + End-key/scrollBy variety make the request cadence non-robotic. Ban
 * avoidance is about TIME and irregularity, not raw speed.
 */
export const HUMAN_PACING = {
  slowMoMs: 300,
  profileSettleMinMs: 5000,
  profileSettleMaxMs: 10000,
  scrollWaitMinMs: 5000,
  scrollWaitMaxMs: 10000,
  scrollDistMin: 4000,
  scrollDistMax: 6000,
  endKeyProb: 0.5,
  longPauseProb: 0.1,
  longPauseMinMs: 10000,
  longPauseMaxMs: 20000,
  noNewDataLimit: 4,
} as const;

export type ScrollAction = { mode: 'end' } | { mode: 'wheel'; distance: number };

/**
 * Decide the next scroll, mirroring the competitor's scroll_page(): 50% press the
 * End key, otherwise scrollBy a random 4000-6000px. Pure (randoms injected) so it's
 * unit-tested; the caller applies it and does the timing.
 */
export function pickScroll(rndMode: number, rndDist: number): ScrollAction {
  if (rndMode < HUMAN_PACING.endKeyProb) return { mode: 'end' };
  const span = HUMAN_PACING.scrollDistMax - HUMAN_PACING.scrollDistMin;
  return { mode: 'wheel', distance: Math.floor(HUMAN_PACING.scrollDistMin + rndDist * span) };
}
