import { useState } from "react" import { Label } from "@/components/ui/label" import { Input } from "@/components/ui/input" import { Switch } from "@/components/ui/switch" import { Button } from "@/components/ui/button" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import { Plus, Trash2, Mail, Paintbrush, Ticket } from "lucide-react" import { AbandonedCartSettings, EmailSequenceItem } from "../../types" import { EmailCustomizer } from "@/components/email-customizer" import type { EmailContentSettings, PlaceholderConfig, PlaceholderCategoryLabel, ProductCardConfig, CouponDisplayConfig } from "@/components/email-customizer/sections" // --------------------------------------------------------------------------- // Abandoned Cart-specific placeholders for the Email Customizer // --------------------------------------------------------------------------- const cartPlaceholders: PlaceholderConfig[] = [ { tag: '{customer_name}', description: 'Customer first name or "there" for guests', example: 'John', category: 'customer' }, { tag: '{customer_email}', description: "Customer's email address", example: 'john@example.com', category: 'customer' }, { tag: '{cart_total}', description: 'Cart total with currency symbol', example: '$149.99', category: 'cart' }, { tag: '{recovery_link}', description: 'Unique cart recovery URL', example: 'https://store.com/?recover=abc123', category: 'cart' }, { tag: '{site_name}', description: 'Your website name', example: 'My Store', category: 'site' }, { tag: '{site_url}', description: 'Your website URL', example: 'https://mystore.com', category: 'site' }, { tag: '{unsubscribe_link}', description: 'Opt-out link for recovery emails', example: 'https://store.com/?unsubscribe=abc123', category: 'site' }, ] const cartCategoryLabels: Record = { customer: { label: 'Customer', color: 'bg-green-100 text-green-700' }, cart: { label: 'Cart', color: 'bg-blue-100 text-blue-700' }, site: { label: 'Site', color: 'bg-orange-100 text-orange-700' }, } /** Sample product data shown in the Email Customizer preview. */ const sampleProduct = { name: 'Premium Wireless Headphones', price: '$79.99', imageUrl: "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='200' height='200' viewBox='0 0 200 200'%3E%3Crect fill='%23f3f4f6' width='200' height='200'/%3E%3Cpath d='M70 130l30-50 30 50' fill='none' stroke='%23d1d5db' stroke-width='4' stroke-linejoin='round'/%3E%3Cpath d='M95 130l12-20 12 20' fill='none' stroke='%23d1d5db' stroke-width='4' stroke-linejoin='round'/%3E%3Ccircle cx='78' cy='82' r='8' fill='%23d1d5db'/%3E%3C/svg%3E", productUrl: '#', } interface AutomationTabProps { settings: AbandonedCartSettings updateSettings: (updates: Partial) => void updateEmail: (index: number, updates: Partial) => void addEmail: () => void removeEmail: (index: number) => void updateUnpaidEmail: (index: number, updates: Partial) => void addUnpaidEmail: () => void removeUnpaidEmail: (index: number) => void } export function AutomationTab({ settings, updateSettings, updateEmail, addEmail, removeEmail, updateUnpaidEmail, addUnpaidEmail, removeUnpaidEmail }: AutomationTabProps) { const maxEmails = 5 const [showCustomizer, setShowCustomizer] = useState(false) const [showUnpaidCustomizer, setShowUnpaidCustomizer] = useState(false) const maxUnpaidEmails = 5 // ---- Map settings ↔ EmailContentSettings ---- const emailContent: EmailContentSettings = { subject: settings.emails[0]?.subject || 'You left something in your cart!', heading: settings.emailHeading || "Your cart is waiting!", body: settings.emailBodyText || "Hi {customer_name},\n\nIt looks like you left some items in your cart at {site_name}. Your cart is saved and ready for you.", additionalContent: '', preheaderText: '', } const handleSaveContent = (content: EmailContentSettings) => { updateSettings({ emailHeading: content.heading, emailBodyText: content.body, }) // Also update the first email's subject if changed in the customizer if (content.subject !== settings.emails[0]?.subject) { updateEmail(0, { subject: content.subject }) } } // ---- Product Card config ---- const productCard: ProductCardConfig = { showImage: settings.emailShowImage ?? true, showTitle: settings.emailShowTitle ?? true, showPrice: settings.emailShowPrice ?? true, buttonText: settings.emailButtonText || 'Complete Your Order', sample: sampleProduct, } const handleSaveProductCard = (card: ProductCardConfig) => { updateSettings({ emailShowImage: card.showImage, emailShowTitle: card.showTitle, emailShowPrice: card.showPrice, emailButtonText: card.buttonText, }) } // ---- Coupon Display config ---- const couponDisplay: CouponDisplayConfig = { boxBgColor: settings.couponBoxBgColor || '#f0fdf4', boxBorderColor: settings.couponBoxBorderColor || '#bbf7d0', boxTextColor: settings.couponBoxTextColor || '#166534', codeBgColor: settings.couponCodeBgColor || '#dcfce7', messageText: settings.couponMessageText || 'Use code {coupon_code} at checkout for a special discount!', } const handleSaveCouponDisplay = (config: CouponDisplayConfig) => { updateSettings({ couponBoxBgColor: config.boxBgColor, couponBoxBorderColor: config.boxBorderColor, couponBoxTextColor: config.boxTextColor, couponCodeBgColor: config.codeBgColor, couponMessageText: config.messageText, }) } return ( <> {/* Email Customizer Full-Screen Overlay */} {showCustomizer && ( setShowCustomizer(false)} emailType={{ id: 'abandoned_cart_reminder', label: 'Cart Recovery Email', defaultContent: { subject: 'You left something in your cart!', heading: "Your cart is waiting!", body: "Hi {customer_name},\n\nIt looks like you left some items in your cart at {site_name}. Your cart is saved and ready for you.", additionalContent: '', preheaderText: '', }, }} initialContent={emailContent} onSaveContent={handleSaveContent} productCard={productCard} onSaveProductCard={handleSaveProductCard} couponDisplay={couponDisplay} onSaveCouponDisplay={handleSaveCouponDisplay} placeholders={cartPlaceholders} placeholderCategories={cartCategoryLabels} subjectPlaceholderHint="Available: {customer_name}, {site_name}, {cart_total}" /> )} {/* Unpaid Order Email Customizer Full-Screen Overlay */} {showUnpaidCustomizer && ( setShowUnpaidCustomizer(false)} emailType={{ id: 'unpaid_order_reminder', label: 'Unpaid Order Reminder', defaultContent: { subject: 'Complete your payment for order #{order_number}', heading: 'Your order is waiting for payment', body: "Hi {customer_name},\n\nYour order #{order_number} at {site_name} is awaiting payment. Please complete your payment to ensure your items are reserved.", additionalContent: '', preheaderText: '', }, }} initialContent={{ subject: settings.unpaidOrderEmails?.[0]?.subject || 'Complete your payment for order #{order_number}', heading: settings.unpaidEmailHeading || 'Your order is waiting for payment', body: settings.unpaidEmailBodyText || "Hi {customer_name},\n\nYour order #{order_number} at {site_name} is awaiting payment. Please complete your payment to ensure your items are reserved.", additionalContent: '', preheaderText: '', }} onSaveContent={(content: EmailContentSettings) => { updateSettings({ unpaidEmailHeading: content.heading, unpaidEmailBodyText: content.body, }) if (content.subject !== settings.unpaidOrderEmails?.[0]?.subject) { updateUnpaidEmail(0, { subject: content.subject }) } }} placeholders={[ { tag: '{customer_name}', description: 'Customer first name', example: 'John', category: 'customer' }, { tag: '{customer_email}', description: "Customer's email address", example: 'john@example.com', category: 'customer' }, { tag: '{order_number}', description: 'WooCommerce order number', example: '#1042', category: 'order' }, { tag: '{order_total}', description: 'Order total with currency', example: '$149.99', category: 'order' }, { tag: '{order_date}', description: 'Date the order was placed', example: 'Feb 24, 2026', category: 'order' }, { tag: '{payment_link}', description: 'Direct link to the payment page', example: 'https://store.com/checkout/pay/1042/', category: 'order' }, { tag: '{site_name}', description: 'Your website name', example: 'My Store', category: 'site' }, { tag: '{site_url}', description: 'Your website URL', example: 'https://mystore.com', category: 'site' }, ]} placeholderCategories={{ customer: { label: 'Customer', color: 'bg-green-100 text-green-700' }, order: { label: 'Order', color: 'bg-purple-100 text-purple-700' }, site: { label: 'Site', color: 'bg-orange-100 text-orange-700' }, }} subjectPlaceholderHint="Available: {customer_name}, {order_number}, {order_total}" couponDisplay={couponDisplay} onSaveCouponDisplay={handleSaveCouponDisplay} /> )}
Recovery Email Sequence Configure the automated emails sent to customers who abandon their carts. {settings.emails.map((email, index) => (
{index + 1}

{index === 0 ? 'First Reminder' : index === 1 ? 'Second Reminder' : index === 2 ? 'Final Reminder' : `Email ${index + 1}`}

updateEmail(index, { enabled: checked })} /> {index > 0 && ( )}
{email.enabled && (
updateEmail(index, { subject: e.target.value })} placeholder="You left something in your cart!" />
updateEmail(index, { delay: parseInt(e.target.value) || 1 })} />

{index === 0 ? 'Sent after the cart is flagged as abandoned.' : 'Sent after the previous email in the sequence.' }

{/* Per-email coupon controls */}
updateEmail(index, { includeCoupon: checked })} />
{email.includeCoupon && (
updateEmail(index, { couponAmount: parseFloat(e.target.value) || 1 })} />
updateEmail(index, { couponExpiry: parseInt(e.target.value) || 1 })} />
)}
)}
))} {settings.emails.length < maxEmails && ( )}
{/* Email Template Card with Customize Button */} Email Template Customize the look and content of your recovery emails using the visual Email Customizer.

Cart Recovery Email

Heading, body text, product card layout, and branding

{/* Unpaid Order Email Sequence */} {settings.recoverUnpaidOrders && ( <>
Unpaid Order Email Sequence
Configure automated payment reminder emails for orders awaiting payment. These are separate from cart recovery emails and target placed orders.
{(settings.unpaidOrderEmails || []).map((email, index) => (
{index + 1}

{index === 0 ? 'First Payment Reminder' : index === 1 ? 'Second Reminder' : index === 2 ? 'Final Reminder' : `Email ${index + 1}`}

updateUnpaidEmail(index, { enabled: checked })} /> {index > 0 && ( )}
{email.enabled && (
updateUnpaidEmail(index, { subject: e.target.value })} placeholder="Complete your payment for order #{order_number}" />
updateUnpaidEmail(index, { delay: parseInt(e.target.value) || 1 })} />

{index === 0 ? 'Sent after the order exceeds the payment timeout threshold.' : 'Sent after the previous email in the sequence.' }

{/* Per-email coupon controls for unpaid orders */}
updateUnpaidEmail(index, { includeCoupon: checked })} />
{email.includeCoupon && (
updateUnpaidEmail(index, { couponAmount: parseFloat(e.target.value) || 1 })} />
updateUnpaidEmail(index, { couponExpiry: parseInt(e.target.value) || 1 })} />
)}
)}
))} {(settings.unpaidOrderEmails || []).length < maxUnpaidEmails && ( )}
{/* Unpaid Order Email Template */}
Unpaid Order Email Template
Customize the payment reminder email content and appearance.

Unpaid Order Reminder

Heading, body text, and payment link button

)}
) }