// engine/usage-tracker/core.ts — Core usage event recording and filtering logic.
// All functions are pure (no I/O). Storage layer handles persistence.
// ---------------------------------------------------------------------------
// Event creation
// ---------------------------------------------------------------------------
/**
 * Creates a new UsageEvent object. Does NOT persist — that is storage's job.
 */
export function recordUsageEvent(toolName, tier, success) {
    return {
        toolName,
        tier,
        timestamp: new Date().toISOString(),
        success,
    };
}
// ---------------------------------------------------------------------------
// Period filtering
// ---------------------------------------------------------------------------
/**
 * Returns events within the specified time period relative to now.
 * 'all' returns the full array unchanged.
 */
export function filterEventsByPeriod(events, period) {
    if (period === 'all') {
        return events;
    }
    const now = Date.now();
    const periodDays = period === '7d' ? 7 : 30;
    const cutoffMs = periodDays * 24 * 60 * 60 * 1000;
    const cutoffTime = now - cutoffMs;
    return events.filter((event) => {
        const eventTime = new Date(event.timestamp).getTime();
        return eventTime >= cutoffTime;
    });
}
// ---------------------------------------------------------------------------
// Date-based counting
// ---------------------------------------------------------------------------
/**
 * Counts events whose timestamp matches the given YYYY-MM-DD date string.
 * Comparison is based on UTC date extraction from ISO 8601 timestamps.
 */
export function countEventsForDate(events, date) {
    return events.filter((event) => {
        const eventDate = event.timestamp.slice(0, 10);
        return eventDate === date;
    }).length;
}
// ---------------------------------------------------------------------------
// Timestamp helpers
// ---------------------------------------------------------------------------
/**
 * Returns the most recent event's timestamp, or null if the array is empty.
 */
export function getLastEventTimestamp(events) {
    if (events.length === 0) {
        return null;
    }
    let latest = events[0];
    for (const event of events) {
        if (event.timestamp > (latest?.timestamp ?? '')) {
            latest = event;
        }
    }
    return latest?.timestamp ?? null;
}
//# sourceMappingURL=core.js.map