1import{j as e}from"./vendor-ui-D7X1860i.js";import{r as n}from"./vendor-react-BchadoZV.js";import{s as c,a9 as K,r as m,as as Q,L as Z,h as J,K as S,N as X,O as ee,Q as E,R as A,U as I,v as d,w as g,aj as se,ak as ae,al as te,am as ie,an as le,aZ as L,ao as re,d as z,e as D,B as ne,ag as ce,aT as oe,aW as me}from"./index-BNuU0U7e.js";import{u}from"./vendor-utils-Dgo5sBm9.js";import{D as de}from"./DataViewSkeleton-Bbfp03VT.js";import{u as xe}from"./vendor-i18n-rFxkqqo2.js";import{V as pe}from"./variable-D8dUMSaM.js";import{C as ue}from"./code-Cz6X3rMB.js";import{P as he}from"./pencil-BaUuDjVf.js";import"./vendor-supabase-DBq2mNUo.js";import"./vendor-charts-DLW0tl0w.js";const O=["general","case","survey","alert","onboarding","escalation"],H=["{{user_name}}","{{user_email}}","{{case_id}}","{{case_title}}","{{survey_link}}","{{company_name}}","{{date}}","{{assignee_name}}","{{priority}}","{{status}}","{{link}}","{{message}}"];function Se(){const{t}=xe(),[V,j]=n.useState([]),[P,M]=n.useState(!0),[k,h]=n.useState(!1),[B,f]=n.useState(!1),[x,v]=n.useState(null),[R,U]=n.useState(""),[b,N]=n.useState(!1),[i,o]=n.useState({name:"",slug:"",category:"general",subject:"",body_html:"",variables:[]});n.useEffect(()=>{p()},[]);const p=async()=>{const{data:s}=await c.from("notification_templates").select("*").order("category").order("name");j(s||[]),M(!1)},y=()=>{o({name:"",slug:"",category:"general",subject:"",body_html:"",variables:[]}),v(null)},$=s=>{o({name:s.name,slug:s.slug,category:s.category,subject:s.subject,body_html:s.body_html,variables:Array.isArray(s.variables)?s.variables:[]}),v(s.id),h(!0)},q=async()=>{const{data:{user:s}}=await c.auth.getUser();if(!s)return;const a={...i,variables:i.variables,updated_at:new Date().toISOString()};x?await c.from("notification_templates").update(a).eq("id",x):await c.from("notification_templates").insert({...a,created_by:s.id}),u.success(x?"Template updated":"Template created"),h(!1),y(),p()},F=async(s,a)=>{await c.from("notification_templates").update({is_active:a}).eq("id",s),j(l=>l.map(r=>r.id===s?{...r,is_active:a}:r))},G=async s=>{await c.from("notification_templates").delete().eq("id",s),u.success(t("toast.templateDeleted","Template deleted")),p()},W=s=>{o(a=>({...a,body_html:a.body_html+s,variables:a.variables.includes(s)?a.variables:[...a.variables,s]}))},w=(s,a)=>{let l=s;H.forEach(r=>{l=l.replace(new RegExp(r.replace(/[{}]/g,"\\$&"),"g"),`<span style="background:#fef3c7;padding:1px 4px;border-radius:3px;">${r}</span>`)}),U(`<div style="font-family:Arial,sans-serif;max-width:600px;margin:0 auto;"><h3 style="border-bottom:1px solid #e5e7eb;padding-bottom:8px;">${a}</h3>${l}</div>`),f(!0)};if(P)return e.jsx(de,{variant:"list"});const _=O.map(s=>({category:s,items:V.filter(a=>a.category===s)})).filter(s=>s.items.length>0);return e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("div",{className:"p-1.5 rounded-md bg-gradient-to-br from-blue-500/20 to-cyan-500/20",children:e.jsx(K,{className:"h-4 w-4 text-info"})}),e.jsxs("div",{children:[e.jsx("h3",{className:"text-sm font-semibold",children:t("setup.etm.emailNotificationTemplates","Email & Notification Templates")}
1),e.jsx("p",{className:"text-[10px] text-muted-foreground",children:t("setup.etm.manageHtmlTemplatesWithDynamicVariables","Manage HTML templates with dynamic variables")})]})]}),e.jsxs("div",{className:"flex gap-2",children:[e.jsxs(m,{size:"sm",variant:"outline",disabled:b,className:"gap-1.5 text-xs h-7",onClick:async()=>{N(!0);try{const{data:s,error:a}=await c.functions.invoke("ai-setup-suggestions",{body:{suggestion_type:"email_templates"}});if(Q({actionType:"ai_setup_suggestions",module:"setup",description:"AI email template suggestions"}),a)throw a;const l=(s==null?void 0:s.templates)||[],{data:{user:r}}=await c.auth.getUser();if(!r)throw new Error("Not authenticated");let C=0;for(const T of l){const{error:Y}=await c.from("notification_templates").insert({...T,is_active:!0,created_by:r.id,variables:T.variables||[]});Y||C++}u.success(`AI generated ${C} email templates`),p()}catch(s){u.error(s.message)}N(!1)},children:[b?e.jsx(Z,{className:"h-3 w-3 animate-spin"}):e.jsx(J,{className:"h-3 w-3"})," AI Generate"]}),e.jsxs(S,{open:k,onOpenChange:s=>{h(s),s||y()},children:[e.jsx(X,{asChild:!0,children:e.jsxs(m,{size:"sm",className:"gap-1.5 text-xs h-7",children:[e.jsx(ee,{className:"h-3 w-3"}),t("setup.etm.newTemplate","New Template")]})}),e.jsxs(E,{className:"max-w-2xl max-h-[85vh] overflow-y-auto",children:[e.jsx(A,{children:e.jsxs(I,{children:[x?"Edit":"Create"," Template"]})}),e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[e.jsxs("div",{children:[e.jsx(d,{className:"text-xs",children:"Name"}),e.jsx(g,{value:i.name,onChange:s=>o(a=>({...a,name:s.target.value})),placeholder:t("setup.etm.caseAssignmentNotification","Case Assignment Notification")})]}),e.jsxs("div",{children:[e.jsx(d,{className:"text-xs",children:"Slug"}),e.jsx(g,{value:i.slug,onChange:s=>o(a=>({...a,slug:s.target.value})),placeholder:"case-assignment",className:"font-mono text-xs"})]})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[e.jsxs("div",{children:[e.jsx(d,{className:"text-xs",children:t("setup.etm.category","Category")}),e.jsxs(se,{value:i.category,onValueChange:s=>o(a=>({...a,category:s})),children:[e.jsx(ae,{children:e.jsx(te,{})}),e.jsx(ie,{children:O.map(s=>e.jsx(le,{value:s,className:"capitalize",children:s},s))})]})]}),e.jsxs("div",{children:[e.jsx(d,{className:"text-xs",children:t("setup.etm.subjectLine","Subject Line")}),e.jsx(g,{value:i.subject,onChange:s=>o(a=>({...a,subject:s.target.value})),placeholder:t("placeholder.Newcaseassignedcasetitle","New case assigned: {{case_title}}")})]})]}),e.jsxs("div",{children:[e.jsxs("div",{className:"flex items-center justify-between mb-1",children:[e.jsx(d,{className:"text-xs",children:t("setup.etm.bodyHtml","Body HTML")}),e.jsxs(m,{variant:"ghost",size:"sm",className:"h-6 text-[10px] gap-1",onClick:()=>w(i.body_html,i.subject),children:[e.jsx(L,{className:"h-3 w-3"}),t("setup.etm.preview","Preview")]})]}),e.jsx(re,{value:i.body_html,onChange:s=>o(a=>({...a,body_html:s.target.value})),rows:10,placeholder:"<div>Hello {{user_name}},<br/>...</div>",className:"font-mono text-xs"})]}),e.jsxs("div",{children:[e.jsxs(d,{className:"text-xs flex items-center gap-1",children:[e.jsx(pe,{className:"h-3 w-3"}),t("setup.etm.insertVariable","Insert Variable")]}),e.jsx("div",{className:"flex flex-wrap gap-1 mt-1",children:H.map(s=>e.jsx(m,{variant:"outline",size:"sm",className:"h-6 text-[10px] font-mono",onClick:()=>W(s),children:s},s))})]}),e.jsxs(m,{onClick:q,className:"w-full",children:[x?"Update":"Create"," Template"]})]})]})]})]})]}),_.length===0&&e.jsx(z,{children:e.jsx(D,{className:"p-6 text-center text-sm text-muted-foreground",children:t("setup.etm.noTemplatesYetCreateYourFirst","No templates yet. Create your first one.")})}),_.map(s=>e.jsxs("div",{children:[e.jsx("p",{className:"text-xs font-semibold capitalize mb-2 text-muted-foreground",children:s.category}),e.jsx("div",{className:"space-y-2",children:s.items.map(a=>e.jsx(z,{children:e.jsxs(D,{className:"p-3 flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center gap-3 min-w-0",children:[e.jsx(ue,{className:"h-4 w-4 text-muted-foreground shrink-0"}),e.jsxs("div",{className:"min-w-0",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("p",{className:"text-sm font-medium truncate",children:a.name}),e.jsx(ne,{variant:"outline",className:"text-[10px] font-mono",children:a.slug})]}),e.jsx("p",{className:"text-[10px] text-muted-foreground truncate",children:a.subject||"No subject"})]})]}),e.jsxs("div",{className:"flex items-center gap-2 shrink-0",children:[e.jsx(ce,{checked:a.is_active,onCheckedChange:l=>F(a.id,l)}),e.jsx(m,{variant:"ghost",size:"sm",className:"h-7 w-7 p-0",onClick:()=>w(a.body_html,a.subject),children:e.jsx(L,{className:"h-3.5 w-3.5"})}),e.jsx(m,{variant:"ghost",size:"sm",className:"h-7 w-7 p-0",onClick:()=>$(a),children:e.jsx(he,{className:"h-3.5 w-3.5"})}),e.jsx(m,{variant:"ghost",size:"sm",className:"h-7 w-7 p-0 text-destructive",onClick:()=>G(a.id),children:e.jsx(oe,{className:"h-3.5 w-3.5"})})]})]})},a.id))})]},s.category)),e.jsx(S,{open:B,onOpenChange:f,children:e.jsxs(E,{className:"max-w-2xl",children:[e.jsx(A,{children:e.jsx(I,{children:t("setup.etm.templatePreview","Template Preview")})}),e.jsx("div",{className:"border rounded-lg p-4 bg-white",dangerouslySetInnerHTML:{__html:me(R)}})]})})]})}export{Se as default};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.