import moment from "moment";

/**
 * The «تاريخ النشر» filter picks one day, but `GET /posts/` reads `start_date`
 * as `created_at >= start_date` (`PostController::index`). Sent alone it meant
 * «published on or after». Bounding it with `end_date` on the same day (the
 * backend compares with `whereDate(... '<=')`, so the whole day is included)
 * makes it mean what the label says. An explicit `endDate`, should a range
 * picker ever come back, still wins.
 */
export function communityDateParams(filters?: {
  startDate?: string | Date | null;
  endDate?: string | Date | null;
} | null): { start_date?: string; end_date?: string } {
  const day = (value?: string | Date | null) =>
    value ? moment(value).format("YYYY-MM-DD") : undefined;
  const start_date = day(filters?.startDate);
  const end_date = day(filters?.endDate) ?? start_date;
  return { start_date, end_date };
}
