// retoor <retoor@molodetz.nl>

export const GROUP_GAP_SECONDS = 300;

export function toEpochMs(value) {
    if (typeof value === "number") return Number.isFinite(value) ? value : null;
    if (!value) return null;
    const parsed = Date.parse(value);
    return Number.isNaN(parsed) ? null : parsed;
}

export function shouldGroup(previousMessage, currentMessage) {
    if (!previousMessage || !currentMessage) return false;
    if (previousMessage.senderUid !== currentMessage.senderUid) return false;
    const previousMs = toEpochMs(previousMessage.createdAt);
    const currentMs = toEpochMs(currentMessage.createdAt);
    if (previousMs === null || currentMs === null) return false;
    const gapSeconds = (currentMs - previousMs) / 1000;
    if (gapSeconds > GROUP_GAP_SECONDS) return false;
    return true;
}
