import { timestampDate } from "@bufbuild/protobuf/wkt";
/**
 * Groups sessions by their creation timestamp into time-based buckets:
 * "Today", "Yesterday", "Previous 7 Days", "Previous 30 Days", "Older".
 *
 * Empty groups are omitted. Sessions with missing timestamps are placed
 * in the last group. Input order is preserved within each group.
 *
 * @param sessions - List of sessions to group (typically from `useSessionList`).
 * @param now - Reference time for grouping; defaults to the current time.
 *              Accepts an explicit value for deterministic testing.
 *
 * @example
 * ```tsx
 * function GroupedSessionList() {
 *   const { sessions } = useSessionList();
 *   const groups = groupSessionsByTime(sessions);
 *
 *   return (
 *     <div>
 *       {groups.map((group) => (
 *         <section key={group.label}>
 *           <h3>{group.label}</h3>
 *           <ul>
 *             {group.sessions.map((s) => (
 *               <li key={s.metadata?.id}>{s.status?.subject}</li>
 *             ))}
 *           </ul>
 *         </section>
 *       ))}
 *     </div>
 *   );
 * }
 * ```
 */
export function groupSessionsByTime(sessions, now) {
    const ref = now ?? new Date();
    const todayStart = startOfDay(ref);
    const yesterdayStart = addDays(todayStart, -1);
    const sevenDaysAgo = addDays(todayStart, -6);
    const thirtyDaysAgo = addDays(todayStart, -29);
    const buckets = [
        { label: "Today", sessions: [] },
        { label: "Yesterday", sessions: [] },
        { label: "Previous 7 Days", sessions: [] },
        { label: "Previous 30 Days", sessions: [] },
        { label: "Older", sessions: [] },
    ];
    for (const session of sessions) {
        const ts = session.status?.audit?.specAudit?.createdAt;
        const date = ts ? timestampDate(ts) : null;
        if (!date) {
            buckets[buckets.length - 1].sessions.push(session);
            continue;
        }
        if (date >= todayStart) {
            buckets[0].sessions.push(session);
        }
        else if (date >= yesterdayStart) {
            buckets[1].sessions.push(session);
        }
        else if (date >= sevenDaysAgo) {
            buckets[2].sessions.push(session);
        }
        else if (date >= thirtyDaysAgo) {
            buckets[3].sessions.push(session);
        }
        else {
            buckets[4].sessions.push(session);
        }
    }
    return buckets.filter((b) => b.sessions.length > 0);
}
function startOfDay(date) {
    const d = new Date(date);
    d.setHours(0, 0, 0, 0);
    return d;
}
function addDays(date, days) {
    const d = new Date(date);
    d.setDate(d.getDate() + days);
    return d;
}
//# sourceMappingURL=group-sessions.js.map