export function sessionize(messages, gapHours) {
    if (gapHours <= 0) {
        throw new Error(`sessionize: gapHours must be positive, got ${gapHours}`);
    }
    if (messages.length === 0)
        return [];
    const gapMs = gapHours * 60 * 60 * 1000;
    const sessions = [];
    let currentMessages = [messages[0]];
    const flush = () => {
        sessions.push({
            index: sessions.length,
            firstMessageAt: currentMessages[0].dateSent,
            lastMessageAt: currentMessages[currentMessages.length - 1].dateSent,
            messages: currentMessages,
        });
    };
    for (let i = 1; i < messages.length; i++) {
        const prevMs = Date.parse(messages[i - 1].dateSent);
        const currMs = Date.parse(messages[i].dateSent);
        if (currMs - prevMs >= gapMs) {
            flush();
            currentMessages = [messages[i]];
        }
        else {
            currentMessages.push(messages[i]);
        }
    }
    flush();
    return sessions;
}
//# sourceMappingURL=sessionize.js.map