import { HttpError } from './service.js' export const validEmail = (value) => typeof value === 'string' && value.length <= 254 && /^[a-z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-z0-9](?:[a-z0-9.-]*[a-z0-9])?\.[a-z]{2,63}$/i.test(value) export function replyCopies(data, customer) { const used = new Set([customer.toLowerCase()]) const result = {} for (const field of ['cc', 'bcc']) { const value = data[field] ?? [] const entries = typeof value === 'string' ? value.split(/[;,]/).filter((v) => v.trim()) : value if (!Array.isArray(entries) || entries.length > 20) throw new HttpError(400, `${field.toUpperCase()}: höchstens 20 E-Mail-Adressen angeben.`) result[field] = [] for (const entry of entries) { if (typeof entry !== 'string' || /[\r\n]/.test(entry) || !validEmail(entry.trim())) throw new HttpError( 400, `${field.toUpperCase()}: gültige E-Mail-Adressen mit Komma oder Semikolon trennen.`, ) const email = entry.trim(), key = email.toLowerCase() if (!used.has(key)) { result[field].push(email) used.add(key) } } } return result }