import Handlebars from "handlebars"; // Translations for Order Created emails const translations = { en: { subject: "Order {{orderNumber}} Confirmed", title: "Order Confirmed", greeting: "Dear {{customerName}},", body: "Thank you for your order! We have received it and it is now being processed.", orderLabel: "Order:", dateLabel: "Date:", statusLabel: "Status:", itemsLabel: "Items", subtotalLabel: "Subtotal:", shippingLabel: "Shipping:", taxLabel: "Tax:", totalLabel: "Total:", shippingAddressLabel: "Shipping Address", phoneLabel: "Phone:", phoneNotProvided: "Not provided", viewOrderButton: "View Order", thankYou: "Thank you!", companyName: "ManoonOils", footerText: "support@manoonoils.com" }, sr: { subject: "Porudžbina {{orderNumber}} Potvrđena", title: "Porudžbina Potvrđena", greeting: "Poštovani {{customerName}},", body: "Hvala vam na porudžbini! Primili smo je i sada je obrađujemo.", orderLabel: "Porudžbina:", dateLabel: "Datum:", statusLabel: "Status:", itemsLabel: "Artikli", subtotalLabel: "Međuzbir:", shippingLabel: "Dostava:", taxLabel: "Porez:", totalLabel: "Ukupno:", shippingAddressLabel: "Adresa za Dostavu", phoneLabel: "Telefon:", phoneNotProvided: "Nije navedeno", viewOrderButton: "Pogledaj Porudžbinu", thankYou: "Hvala vam!", companyName: "ManoonOils", footerText: "support@manoonoils.com" }, de: { subject: "Bestellung {{orderNumber}} Bestätigt", title: "Bestellung Bestätigt", greeting: "Sehr geehrte/r {{customerName}},", body: "Vielen Dank für Ihre Bestellung! Wir haben sie erhalten und bearbeiten sie jetzt.", orderLabel: "Bestellung:", dateLabel: "Datum:", statusLabel: "Status:", itemsLabel: "Artikel", subtotalLabel: "Zwischensumme:", shippingLabel: "Versand:", taxLabel: "Steuer:", totalLabel: "Gesamt:", shippingAddressLabel: "Lieferadresse", phoneLabel: "Telefon:", phoneNotProvided: "Nicht angegeben", viewOrderButton: "Bestellung Ansehen", thankYou: "Vielen Dank!", companyName: "ManoonOils", footerText: "support@manoonoils.com" }, fr: { subject: "Commande {{orderNumber}} Confirmée", title: "Commande Confirmée", greeting: "Cher/Chère {{customerName}},", body: "Merci pour votre commande! Nous l'avons reçue et elle est en cours de traitement.", orderLabel: "Commande:", dateLabel: "Date:", statusLabel: "Statut:", itemsLabel: "Articles", subtotalLabel: "Sous-total:", shippingLabel: "Livraison:", taxLabel: "Taxe:", totalLabel: "Total:", shippingAddressLabel: "Adresse de Livraison", phoneLabel: "Téléphone:", phoneNotProvided: "Non fourni", viewOrderButton: "Voir la Commande", thankYou: "Merci!", companyName: "ManoonOils", footerText: "support@manoonoils.com" } }; // Customer email template const customerEmailTemplate = `
{{companyName}}

{{title}}

{{greeting}}

{{body}}

{{orderLabel}} {{orderNumber}}

{{dateLabel}} {{orderDate}}

{{statusLabel}} {{orderStatus}}

{{itemsLabel}}

{{#each items}}

{{quantity}}x {{name}} - {{price}}

{{/each}}

{{subtotalLabel}} {{subtotal}}

{{shippingLabel}} {{shipping}}

{{taxLabel}} {{tax}}


{{totalLabel}} {{total}}

{{#if shippingAddress}}

{{shippingAddressLabel}}

{{shippingAddress}}

{{phoneLabel}} {{#if phone}}{{phone}}{{else}}{{phoneNotProvided}}{{/if}}

{{/if}}
{{viewOrderButton}}

{{footerText}}

{{thankYou}}

{{companyName}}

`; // Admin email template const adminEmailTemplate = `

{{adminTitle}}

{{adminOrderLabel}} #{{orderNumber}}

{{dateLabel}} {{orderDate}}

{{statusLabel}} {{orderStatus}}

{{paymentLabel}} {{paymentMethod}}

{{customerLabel}}

{{nameLabel}} {{customerName}}

{{emailLabel}} {{customerEmail}}

{{phoneLabel}} {{#if phone}}{{phone}}{{else}}{{phoneNotProvided}}{{/if}}

{{itemsLabel}}

{{#each items}}

{{quantity}}x {{name}} - {{price}}

{{/each}}

{{subtotalLabel}} {{subtotal}}

{{shippingLabel}} {{shipping}}

{{taxLabel}} {{tax}}


{{totalLabel}} {{total}}

{{#if shippingAddress}}

{{shippingAddressLabel}}

{{shippingAddress}}

{{/if}}
Dashboard
`; // Compile templates const compileCustomerEmail = Handlebars.compile(customerEmailTemplate); const compileAdminEmail = Handlebars.compile(adminEmailTemplate); // Admin translations (always in English) const adminTranslations = { adminTitle: "New Order! 🎉", adminOrderLabel: "Order", customerLabel: "Customer", paymentLabel: "Payment:", paymentMethod: "Card" }; export function getOrderCreatedEmails(order: any) { // Detect language const lang = order.languageCode?.toLowerCase() || "en"; const t = translations[lang as keyof typeof translations] || translations.en; // Format data const customerName = order.shippingAddress?.firstName ? `${order.shippingAddress.firstName} ${order.shippingAddress.lastName || ""}`.trim() : order.userEmail?.split("@")[0] || "Customer"; const currency = order.total?.gross?.currency || "EUR"; const items = (order.lines || []).map((line: any) => ({ name: line.variant?.product?.name || line.variant?.name || "Product", quantity: line.quantity || 0, price: `${line.totalPrice?.gross?.amount?.toFixed(2) || "0.00"} ${currency}` })); const shippingAddress = order.shippingAddress ? `${order.shippingAddress.firstName || ""} ${order.shippingAddress.lastName || ""}\n${order.shippingAddress.streetAddress1 || ""}\n${order.shippingAddress.city || ""}, ${order.shippingAddress.postalCode || ""}\n${order.shippingAddress.country?.country || ""}` : ""; const orderDate = order.created ? new Date(order.created).toLocaleString(lang === "sr" ? "sr-RS" : lang === "de" ? "de-DE" : lang === "fr" ? "fr-FR" : "en-US") : new Date().toLocaleString(); // Customer email data const customerData = { ...t, orderNumber: order.number || order.id, customerName, orderDate, orderStatus: order.status || "unfulfilled", items, subtotal: `${order.subtotal?.gross?.amount?.toFixed(2) || "0.00"} ${currency}`, shipping: `${order.shippingPrice?.gross?.amount?.toFixed(2) || "0.00"} ${currency}`, tax: `${order.total?.tax?.amount?.toFixed(2) || "0.00"} ${currency}`, total: `${order.total?.gross?.amount?.toFixed(2) || "0.00"} ${currency}`, shippingAddress, phone: order.shippingAddress?.phone || "" }; // Admin email data (English) const adminData = { ...t, ...adminTranslations, orderNumber: order.number || order.id, customerName, customerEmail: order.userEmail || "", orderDate, orderStatus: order.status || "unfulfilled", items, subtotal: `${order.subtotal?.gross?.amount?.toFixed(2) || "0.00"} ${currency}`, shipping: `${order.shippingPrice?.gross?.amount?.toFixed(2) || "0.00"} ${currency}`, tax: `${order.total?.tax?.amount?.toFixed(2) || "0.00"} ${currency}`, total: `${order.total?.gross?.amount?.toFixed(2) || "0.00"} ${currency}`, shippingAddress, phone: order.shippingAddress?.phone || "" }; return { customerSubject: Handlebars.compile(t.subject)({ orderNumber: order.number || order.id }), customerHtml: compileCustomerEmail(customerData), adminSubject: `New Order! 🎉 #${order.number || order.id}`, adminHtml: compileAdminEmail(adminData) }; }