import type { ScrapedPost } from './types.js';

/**
 * Reels whose view count should be (re)fetched this run: type 'reel', has a
 * shortcode, and uploaded within `windowDays` of `nowIso`. IG's feed returns
 * view_count=null for reels, so views are fetched per-media — but only while a
 * reel is "fresh" (the client tracks day-1..3 growth), which bounds the request
 * volume at 500-channel scale. Pure — unit-tested.
 */
export function selectReelsForViewFetch(
  posts: ScrapedPost[],
  nowIso: string,
  windowDays: number,
): ScrapedPost[] {
  const cutoff = new Date(nowIso).getTime() - windowDays * 86_400_000;
  return posts.filter(
    (p) => p.type === 'reel' && !!p.shortcode && new Date(p.uploadedAt).getTime() >= cutoff,
  );
}
