// Viewer-local calendar date helpers, shared by the Usage dashboard and the
// space Calendar. The server re-buckets UTC timestamps into the viewer's local
// calendar frame via the tz_offset (minutes east of UTC) query param, so the UI
// must compute "today" / month bounds / day arithmetic in the same local frame.

/** Viewer's local calendar today as 'YYYY-MM-DD'. */
export function localToday(): string {
  const d = new Date();
  const mm = String(d.getMonth() + 1).padStart(2, '0');
  const dd = String(d.getDate()).padStart(2, '0');
  return `${d.getFullYear()}-${mm}-${dd}`;
}

/** Minutes east of UTC for the viewer (JST → +540). */
export function localTzOffset(): number {
  return -new Date().getTimezoneOffset();
}

/** day + delta days, as 'YYYY-MM-DD' (calendar arithmetic via UTC midnight). */
export function shiftDay(day: string, delta: number): string {
  const x = new Date(`${day}T00:00:00.000Z`);
  x.setUTCDate(x.getUTCDate() + delta);
  return x.toISOString().slice(0, 10);
}
