(function installConversationThreads(root, factory) { const api = factory(); if (typeof module === 'object' && module.exports) module.exports = api; else root.epMailConversationThreads = api; })(typeof window === 'undefined' ? globalThis : window, function () { function messageIds(value) { const text = Array.isArray(value) ? value.join(' ') : String(value || ''); return [...new Set((text.match(/<[\x21-\x3b\x3d\x3f-\x7e]+@[\x21-\x3b\x3d\x3f-\x7e]+>/g) || []) .filter(id => id.length <= 254))]; } function replyHeaders(message) { const inReplyTo = messageIds(message?.messageId)[0] || ''; if (!inReplyTo) return { inReplyTo: '', references: '' }; const ids = [...new Set([...messageIds(message.references), inReplyTo])]; while (ids.join(' ').length > 900 && ids.length > 1) ids.splice(1, 1); return { inReplyTo, references: ids.join(' ') }; } function groupMessages(messages) { const parents = messages.map((_, index) => index); const find = index => { while (parents[index] !== index) { parents[index] = parents[parents[index]]; index = parents[index]; } return index; }; const owners = new Map(); messages.forEach((message, index) => { const ids = [...messageIds(message.messageId), ...messageIds(message.inReplyTo), ...messageIds(message.references)]; for (const id of new Set(ids)) { if (owners.has(id)) parents[find(index)] = find(owners.get(id)); else owners.set(id, index); } }); const groups = new Map(); messages.forEach((message, index) => { const root = find(index); if (!groups.has(root)) groups.set(root, []); groups.get(root).push(message); }); return [...groups.values()]; } return { messageIds, replyHeaders, groupMessages }; });