// src/security.ts
function escapeHtml(str) {
  return str.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#039;");
}

// src/email-templates/layout.ts
var FONT_STACK = "-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif";
function darkenHex(hex, percent) {
  const num = parseInt(hex.replace("#", ""), 16);
  const r = Math.max(0, (num >> 16) - Math.round(2.55 * percent));
  const g = Math.max(0, (num >> 8 & 255) - Math.round(2.55 * percent));
  const b = Math.max(0, (num & 255) - Math.round(2.55 * percent));
  return `#${(r << 16 | g << 8 | b).toString(16).padStart(6, "0")}`;
}
function htmlToText(html) {
  return html.replace(/<br\s*\/?>/gi, "\n").replace(/<\/p>/gi, "\n\n").replace(/<\/div>/gi, "\n").replace(/<\/tr>/gi, "\n").replace(/<\/li>/gi, "\n").replace(/<li[^>]*>/gi, "- ").replace(/<a[^>]*href="([^"]*)"[^>]*>([^<]*)<\/a>/gi, "$2 ($1)").replace(/<[^>]+>/g, "").replace(/&nbsp;/g, " ").replace(/&amp;/g, "&").replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&quot;/g, '"').replace(/&#39;/g, "'").replace(/&rarr;/g, "\u2192").replace(/\n{3,}/g, "\n\n").trim();
}
function emailLayout(branding, options) {
  const gradientFrom = branding.gradientFrom ?? branding.primaryColor;
  const gradientTo = branding.gradientTo ?? darkenHex(branding.primaryColor, 15);
  const accentColor = branding.accentColor ?? branding.primaryColor;
  const preheaderHtml = options.preheader ? `<div style="display:none;font-size:1px;color:#f3f4f6;line-height:1px;max-height:0;max-width:0;opacity:0;overflow:hidden;">${escapeHtml(options.preheader)}</div>` : "";
  const iconHtml = options.icon ? `${options.icon} ` : "";
  const subtitleHtml = options.subtitle ? `<p style="margin:4px 0 0;color:rgba(255,255,255,0.85);font-size:14px;">${escapeHtml(options.subtitle)}</p>` : "";
  const ctaHtml = options.cta ? `
        <tr>
          <td style="padding:0 32px 24px;text-align:center;">
            <a href="${escapeHtml(options.cta.url)}"
               style="display:inline-block;padding:12px 28px;background:${accentColor};color:#ffffff;text-decoration:none;border-radius:6px;font-weight:600;font-size:14px;">
              ${escapeHtml(options.cta.text)}
            </a>
          </td>
        </tr>` : "";
  const footerLinksHtml = options.footerLinks && options.footerLinks.length > 0 ? options.footerLinks.map(
    (link) => `<a href="${escapeHtml(link.url)}" style="color:${accentColor};text-decoration:none;">${escapeHtml(link.label)}</a>`
  ).join(" &middot; ") : "";
  const preferencesHtml = branding.preferencesUrl ? `<p style="margin:4px 0 0;font-size:12px;color:#9ca3af;">
        <a href="${escapeHtml(branding.preferencesUrl)}" style="color:${accentColor};text-decoration:none;">Update email preferences</a>
      </p>` : "";
  const html = `<!DOCTYPE html>
<html lang="en">
<head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1.0"><title>${escapeHtml(options.subject)}</title></head>
<body style="margin:0;padding:0;background:#f3f4f6;font-family:${FONT_STACK};">
  ${preheaderHtml}
  <table width="100%" cellpadding="0" cellspacing="0" style="background:#f3f4f6;padding:24px 0;">
    <tr><td align="center">
      <table width="600" cellpadding="0" cellspacing="0" style="background:#ffffff;border-radius:8px;overflow:hidden;max-width:600px;">
        <!-- Header -->
        <tr>
          <td style="background:linear-gradient(135deg,${gradientFrom},${gradientTo});padding:28px 32px;text-align:center;">
            <h1 style="margin:0;color:#ffffff;font-size:22px;font-weight:700;">${iconHtml}${escapeHtml(branding.appName)}</h1>
            ${subtitleHtml}
          </td>
        </tr>

        <!-- Body -->
        <tr>
          <td style="padding:28px 32px;color:#374151;font-size:15px;line-height:1.6;">
            ${options.body}
          </td>
        </tr>

        ${ctaHtml}

        <!-- Footer -->
        <tr>
          <td style="padding:16px 32px;background:#f9fafb;text-align:center;border-top:1px solid #e5e7eb;">
            ${footerLinksHtml ? `<p style="margin:0 0 4px;font-size:12px;color:#9ca3af;">${footerLinksHtml}</p>` : ""}
            <p style="margin:0;font-size:12px;color:#9ca3af;">
              ${branding.footerText ? escapeHtml(branding.footerText) + " &middot; " : ""}
              <a href="${escapeHtml(branding.baseUrl)}" style="color:${accentColor};text-decoration:none;">${escapeHtml(branding.appName)}</a>
            </p>
            ${preferencesHtml}
          </td>
        </tr>
      </table>
    </td></tr>
  </table>
</body>
</html>`;
  const text = `${options.icon ? options.icon + " " : ""}${options.subject}
${"=".repeat(40)}

${htmlToText(options.body)}${options.cta ? `

${options.cta.text}: ${options.cta.url}` : ""}

---
${branding.footerText ? branding.footerText + "\n" : ""}${branding.appName} - ${branding.baseUrl}${branding.preferencesUrl ? "\nEmail preferences: " + branding.preferencesUrl : ""}`;
  return { subject: options.subject, html, text };
}
function calloutBlock(content, color = "#3b82f6") {
  return `<div style="background:#f9fafb;padding:16px 20px;border-radius:8px;border-left:4px solid ${color};margin:16px 0;">${content}</div>`;
}
function stepBlock(number, title, description, color = "#667eea") {
  return `<div style="background:#f3f4f6;padding:16px;border-radius:8px;margin-bottom:12px;border-left:4px solid ${color};">
    <p style="margin:0;font-weight:600;color:#111827;">${number}. ${title}</p>
    <p style="margin:8px 0 0;font-size:14px;color:#6b7280;">${description}</p>
  </div>`;
}
function statsBar(stats) {
  const cells = stats.map(
    (s) => `<span style="display:inline-block;margin:0 16px;font-size:14px;color:#374151;"><strong>${escapeHtml(String(s.value))}</strong> ${escapeHtml(s.label)}</span>`
  ).join("");
  return `<div style="padding:12px 0;text-align:center;background:#f0fdf4;border-radius:6px;margin-bottom:16px;">${cells}</div>`;
}
function dataTable(headers, rows) {
  const headerCells = headers.map(
    (h) => `<th style="padding:8px 12px;text-align:left;font-size:13px;color:#374151;border-bottom:1px solid #e5e7eb;">${escapeHtml(h)}</th>`
  ).join("");
  const bodyRows = rows.map(
    (row) => `<tr>${row.map((cell) => `<td style="padding:8px 12px;border-bottom:1px solid #e5e7eb;font-size:14px;color:#374151;">${cell}</td>`).join("")}</tr>`
  ).join("");
  return `<table width="100%" cellpadding="0" cellspacing="0" style="border:1px solid #e5e7eb;border-radius:6px;overflow:hidden;">
    <tr style="background:#f9fafb;">${headerCells}</tr>
    ${bodyRows}
  </table>`;
}
function tipBlock(content, icon = "\u{1F4A1}") {
  return `<div style="background:#eff6ff;border:2px solid #3b82f6;padding:16px 20px;border-radius:8px;margin:16px 0;">
    <p style="margin:0;color:#1e40af;font-size:14px;"><strong>${icon} Pro Tip:</strong> ${content}</p>
  </div>`;
}
function sectionHeading(text) {
  return `<h2 style="margin:24px 0 12px;font-size:16px;color:#111827;">${escapeHtml(text)}</h2>`;
}
function divider() {
  return `<hr style="border:none;border-top:1px solid #e5e7eb;margin:24px 0;">`;
}

// src/email-templates/templates.ts
function welcomeEmail(branding, data) {
  const safeName = escapeHtml(data.userName);
  const stepsHtml = data.steps.map(
    (s, i) => stepBlock(
      i + 1,
      escapeHtml(s.title),
      escapeHtml(s.description),
      branding.accentColor ?? branding.primaryColor
    )
  ).join("");
  const tipHtml = data.tip ? tipBlock(escapeHtml(data.tip)) : "";
  const body = `
    <p style="font-size:16px;margin-bottom:20px;">Hi ${safeName},</p>
    <p style="font-size:15px;margin-bottom:20px;">
      Welcome aboard! Your account is all set up and ready to go.
    </p>
    ${sectionHeading("Get Started")}
    ${stepsHtml}
    ${tipHtml}
  `;
  return emailLayout(branding, {
    subject: `Welcome to ${branding.appName}!`,
    preheader: `Your ${branding.appName} account is ready`,
    icon: "\u{1F389}",
    subtitle: "Welcome aboard",
    body,
    cta: { text: "Go to Dashboard", url: data.dashboardUrl },
    footerLinks: branding.supportEmail ? [{ label: "Get Help", url: `mailto:${branding.supportEmail}` }] : void 0
  });
}
function digestEmail(branding, data) {
  const safeName = escapeHtml(data.recipientName);
  const safePeriod = escapeHtml(data.period);
  const body = `
    <p style="font-size:16px;margin-bottom:20px;">Hi ${safeName},</p>
    <p style="font-size:15px;margin-bottom:16px;">
      Here's your ${safePeriod.toLowerCase()} summary:
    </p>
    ${statsBar(data.stats)}
    ${data.contentHtml}
    ${data.subscriptionNote ? `<p style="margin-top:20px;font-size:13px;color:#9ca3af;">${escapeHtml(data.subscriptionNote)}</p>` : ""}
  `;
  return emailLayout(branding, {
    subject: `${branding.appName} \u2014 ${data.period} Summary`,
    preheader: `Your ${data.period.toLowerCase()} activity on ${branding.appName}`,
    icon: "\u{1F4CA}",
    subtitle: safePeriod,
    body,
    cta: data.reportUrl ? { text: "View Full Report", url: data.reportUrl } : void 0
  });
}
function notificationEmail(branding, data) {
  const safeName = escapeHtml(data.recipientName);
  const safeHeadline = escapeHtml(data.headline);
  const safeMessage = escapeHtml(data.message);
  const detailHtml = data.detail ? calloutBlock(
    escapeHtml(data.detail),
    branding.accentColor ?? branding.primaryColor
  ) : "";
  const body = `
    <p style="font-size:16px;margin-bottom:20px;">Hi ${safeName},</p>
    <h2 style="margin:0 0 12px;font-size:18px;color:#111827;">${safeHeadline}</h2>
    <p style="font-size:15px;margin-bottom:16px;">${safeMessage}</p>
    ${detailHtml}
  `;
  return emailLayout(branding, {
    subject: `${branding.appName}: ${data.headline}`,
    preheader: data.message.slice(0, 100),
    body,
    cta: data.actionUrl ? { text: data.actionText ?? "View Details", url: data.actionUrl } : void 0
  });
}
var MODERATION_CONFIG = {
  warning: { icon: "\u26A0\uFE0F", color: "#f59e0b", label: "Account Warning" },
  suspension: { icon: "\u{1F512}", color: "#ef4444", label: "Account Suspended" },
  ban: { icon: "\u{1F6AB}", color: "#dc2626", label: "Account Banned" },
  reinstatement: { icon: "\u2705", color: "#10b981", label: "Account Reinstated" }
};
function moderationEmail(branding, data) {
  const safeName = escapeHtml(data.userName);
  const config = MODERATION_CONFIG[data.action];
  const reasonHtml = calloutBlock(
    `<strong>Reason:</strong> ${escapeHtml(data.reason)}`,
    config.color
  );
  const nextStepsHtml = data.nextSteps ? `<p style="font-size:15px;margin:16px 0;">${escapeHtml(data.nextSteps)}</p>` : "";
  const body = `
    <p style="font-size:16px;margin-bottom:20px;">Hi ${safeName},</p>
    <p style="font-size:15px;margin-bottom:16px;">
      ${data.action === "reinstatement" ? "Good news \u2014 your account has been reinstated." : `Your account has received a ${escapeHtml(data.action)}.`}
    </p>
    ${reasonHtml}
    ${nextStepsHtml}
  `;
  return emailLayout(branding, {
    subject: `${branding.appName}: ${config.label}`,
    preheader: config.label,
    icon: config.icon,
    subtitle: config.label,
    body,
    cta: data.appealUrl ? { text: "Appeal or Contact Support", url: data.appealUrl } : void 0
  });
}
function transactionEmail(branding, data) {
  const safeName = escapeHtml(data.recipientName);
  const safeHeadline = escapeHtml(data.headline);
  const rows = data.lineItems.map(([label, value]) => [
    escapeHtml(label),
    escapeHtml(value)
  ]);
  if (data.total) {
    rows.push([
      `<strong>Total</strong>`,
      `<strong>${escapeHtml(data.total)}</strong>`
    ]);
  }
  const messageHtml = data.message ? `<p style="font-size:15px;margin-bottom:16px;">${escapeHtml(data.message)}</p>` : "";
  const body = `
    <p style="font-size:16px;margin-bottom:20px;">Hi ${safeName},</p>
    <h2 style="margin:0 0 12px;font-size:18px;color:#111827;">${safeHeadline}</h2>
    ${messageHtml}
    ${dataTable(["Item", "Amount"], rows)}
  `;
  return emailLayout(branding, {
    subject: `${branding.appName}: ${data.headline}`,
    preheader: data.headline,
    icon: "\u{1F9FE}",
    body,
    cta: data.receiptUrl ? { text: "View Receipt", url: data.receiptUrl } : void 0
  });
}
function securityEmail(branding, data) {
  const safeName = escapeHtml(data.userName);
  const detailHtml = calloutBlock(escapeHtml(data.details), "#ef4444");
  const expiryHtml = data.expiryNote ? `<p style="font-size:13px;color:#9ca3af;margin-top:12px;text-align:center;">${escapeHtml(data.expiryNote)}</p>` : "";
  const body = `
    <p style="font-size:16px;margin-bottom:20px;">Hi ${safeName},</p>
    <p style="font-size:15px;margin-bottom:16px;">
      ${escapeHtml(data.event)}
    </p>
    ${detailHtml}
    ${expiryHtml}
    <p style="font-size:14px;color:#6b7280;margin-top:16px;">
      If you didn't initiate this action, please contact support immediately.
    </p>
  `;
  return emailLayout(branding, {
    subject: `${branding.appName}: ${data.event}`,
    preheader: data.event,
    icon: "\u{1F510}",
    subtitle: "Security Alert",
    body,
    cta: data.actionUrl ? { text: data.actionText ?? "Take Action", url: data.actionUrl } : void 0
  });
}
export {
  calloutBlock,
  dataTable,
  digestEmail,
  divider,
  emailLayout,
  escapeHtml,
  moderationEmail,
  notificationEmail,
  sectionHeading,
  securityEmail,
  statsBar,
  stepBlock,
  tipBlock,
  transactionEmail,
  welcomeEmail
};
//# sourceMappingURL=email-templates.mjs.map