function buildEmailTemplate(bodyText, data, isDE) { const esc = (v) => String(v ?? "") .replace(/&/g, "&") .replace(//g, ">") .replace(/"/g, """) .replace(/'/g, "'"); const fullName = [data.first_name, data.last_name].filter(Boolean).join(" ") || ""; const greeting = fullName ? isDE ? `Liebe/Lieber ${esc(fullName)}` : `Hello ${esc(fullName)}` : isDE ? "Hallo" : "Hello"; const risk = data.risk_level || "ASSESSED"; const scenarios = (data.scenarios || "").split(",").map((s) => s.trim()).filter(Boolean); const scenarioLabels = { total_collapse: isDE ? "Totaler Kollaps" : "Total Collapse", partial_collapse: isDE ? "Partieller Kollaps" : "Partial Collapse", hyperinflation: isDE ? "Hyperinflation" : "Hyperinflation", supply_shock: isDE ? "Versorgungsengpass" : "Supply Side Shock", food_crisis: isDE ? "Lebensmittelkrise" : "Food Crisis", bank_crisis: isDE ? "Banken- / Finanzkrise" : "Bank / Financial Crisis", power_outage: isDE ? "Laengerer Stromausfall" : "Extended Power Outage", water_failure: isDE ? "Wasserversorgungsausfall" : "Water Supply Failure", war: isDE ? "Krieg / Bewaffneter Konflikt" : "War / Armed Conflict" }; const scenarioBadges = scenarios.map((s) => `${esc(scenarioLabels[s] || s)}` ).join(""); const formatText = (text) => { return esc(text).split(/\n\n+/).filter((p) => p.trim()).map((p) => { if (p.startsWith("* ") || p.startsWith("- ") || p.startsWith("•") || p.includes("\n•")) { const items = p.split("\n").filter((l) => l.trim()).map((l) => { const clean = l.trim().replace(/^(\*|-|•)\s*/, ""); const linked = clean.replace(/(https?:\/\/[^\s<]+)/g, `${isDE ? "Ansehen" : "View"}`); const bolded = linked.replace(/\*\*(.+?)\*\*/g, '$1'); return `
${bolded}
`; }).join(""); }; const body = formatText(bodyText); const restoreQuery = data.report_id && data.report_token ? `?r=${encodeURIComponent(data.report_id)}&t=${encodeURIComponent(data.report_token)}` : ""; const reportUrl = `https://plan-b.now/${restoreQuery}#results-section`; const analysisUrl = `https://plan-b.now/${restoreQuery}#narrative-section`; const recommendationsUrl = `https://plan-b.now/${restoreQuery}#recs-anchor`; const budgetUrl = `https://plan-b.now/${restoreQuery}#panel-budget`; const timelineUrl = `https://plan-b.now/${restoreQuery}#panel-timeline`; const proteinUrl = `https://plan-b.now/${restoreQuery}#protein-offer-section`; return `