1import{$ as Y,r,aj as Q,j as e,C as H,g as X,h as D,B as E,ap as Z,ae as ee,b as te,L as se,bl as ae}from"./index-D9fAHJMx.js";import{R as le,i as re}from"./RichTextEditor-DoY9DHVX.js";import{a as ne,b as ie,T as de,C as ce,c as oe,u as me}from"./useIntegrations-DWuGt-2P.js";import{s as xe}from"./sanitize-UOrmfCTw.js";import"./index-D2UenjJA.js";import"./with-selector-Bm3q7FxU.js";import"./link-BUuwlCwx.js";import"./list-Dtkblub_.js";import"./undo-2-BFjeQyVU.js";import"./purify.es-BF6PGFKN.js";const ue=/\{\{(\w+)\}\}/g;function F(s,c){return s.replace(ue,(a,d)=>{const x=c[d];return x==null?"":String(x)})}const pe={first_name:"Maria",preferred_name:"Maria",full_name:"Maria Alvarez",agent_name:"Maria Alvarez",recipient_name:"Maria Alvarez",client_name:"Jordan Smith",property_address:"4217 Bayshore Blvd, Tampa, FL 33611",asset_identifier:"LB-042",years:"3",anniversary_date:"July 15",days_overdue:"4",due_date:"July 10",closing_date:"July 24",critical_count:"2",warning_count:"5",info_count:"11"};function he(s){return oe[s]??pe[s]??`Sample ${s.replace(/_/g," ")}`}function V(s){return s===null||s.template_key.startsWith("custom_")?[...ce]:s.available_variables??[]}function I({template:s,onClose:c}){Y(!0);const a=s===null,[d,x]=r.useState(s?.name??""),[h,y]=r.useState(s?.description??""),[l,v]=r.useState(s?.subject_template??""),[o,u]=r.useState(s?.body_html_template??"<p></p>"),[f,N]=r.useState(s?.is_active??!0),[g,M]=r.useState(!1),[i,S]=r.useState(!1),[j,q]=r.useState(()=>Object.fromEntries(V(s).map(t=>[t,he(t)]))),[P,_]=r.useState(null),B=ne(),z=ie(),b=B.isPending||z.isPending,k=Q(i&&!b,c),C=r.useRef(null),A=r.useRef(null),T=r.useRef("body"),w=V(s),U=s===null||s.template_key.startsWith("custom_"),J=r.useMemo(()=>F(l,j),[l,j]),G=r.useMemo(()=>F(o,j),[o,j]),p=()=>S(!0);function W(t){const n=`{{${t}}}`;if(p(),T.current==="subject"&&C.current){const m=C.current,L=m.selectionStart??l.length,O=m.selectionEnd??l.length,$=l.slice(0,L)+n+l.slice(O);v($),requestAnimationFrame(()=>{m.focus(),m.setSelectionRange(L+n.length,L+n.length)});return}A.current?re(A.current,n):u(m=>m+n)}async function K(t){if(t.preventDefault(),_(null),d.trim().length<2){_("Name required");return}if(!l.trim()||!o.trim()){_("Subject and body required");return}try{a?await z.mutateAsync({name:d.trim(),description:h.trim()||null,subject_template:l,body_html_template:o}):await B.mutateAsync({id:s.id,patch:{name:d.trim(),description:h.trim()||null,subject_template:l,body_html_template:o,is_active:f}}),c()}catch(n){_(n instanceof Error?n.message:"Save failed")}}return e.jsxs("div",{className:"fixed inset-0 z-modal flex items-start justify-center overflow-y-auto bg-black/40 p-4",onClick:t=>{t.target===t.currentTarget&&!b&&k.requestClose()}
1,children:[e.jsxs(H,{className:"my-auto w-full max-w-3xl",children:[e.jsxs(X,{children:[e.jsx("div",{className:"text-sm font-medium text-slate-700 dark:text-slate-200",children:a?"New email template":e.jsxs(e.Fragment,{children:["Edit template â ",e.jsx("code",{children:s.template_key})]})}),a&&e.jsx("p",{className:"mt-0.5 text-xs text-slate-500 dark:text-slate-400",children:"Custom templates arenât sent automatically â they show up as starting points when composing an email."})]}),e.jsx(D,{children:e.jsxs("form",{onSubmit:K,noValidate:!0,className:"space-y-3",children:[e.jsxs("div",{className:"grid grid-cols-1 gap-3 sm:grid-cols-2",children:[e.jsxs("label",{className:"block text-xs font-medium text-slate-600 dark:text-slate-300",children:["Name",e.jsx("input",{value:d,onChange:t=>{p(),x(t.target.value)},className:"mt-1 block w-full rounded-md border-slate-300 text-sm dark:border-slate-600 dark:bg-slate-800 dark:text-slate-100"})]}),e.jsxs("label",{className:"block text-xs font-medium text-slate-600 dark:text-slate-300",children:["Description",e.jsx("input",{value:h,onChange:t=>{p(),y(t.target.value)},className:"mt-1 block w-full rounded-md border-slate-300 text-sm dark:border-slate-600 dark:bg-slate-800 dark:text-slate-100"})]})]}),w.length>0&&e.jsxs("label",{className:"block text-xs font-medium text-slate-600 dark:text-slate-300",children:["Insert a variable",e.jsxs("select",{value:"",onChange:t=>{t.target.value&&W(t.target.value)},className:"mt-1 block w-full rounded-md border-slate-300 text-sm dark:border-slate-600 dark:bg-slate-800 dark:text-slate-100",children:[e.jsx("option",{value:"",children:"Pick one to drop it where your cursor isâ¦"}),U?de.map(t=>e.jsx("optgroup",{label:t.group,children:t.vars.map(n=>e.jsx("option",{value:n.key,children:n.label},n.key))},t.group)):w.map(t=>e.jsx("option",{value:t,children:t.replace(/_/g," ")},t))]}),e.jsx("span",{className:"mt-1 block text-[11px] font-normal text-slate-500 dark:text-slate-400",children:"Inserts into the subject or body (wherever you clicked last). It fills in with the deal's real data when the email is composed."})]}),e.jsxs("label",{className:"block text-xs font-medium text-slate-600 dark:text-slate-300",children:["Subject",e.jsx("input",{ref:C,value:l,onFocus:()=>T.current="subject",onChange:t=>{p(),v(t.target.value)},className:"mt-1 block w-full rounded-md border-slate-300 text-sm dark:border-slate-600 dark:bg-slate-800 dark:text-slate-100"})]}),e.jsxs("div",{children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("span",{className:"text-xs font-medium text-slate-600 dark:text-slate-300",children:"Body"}),e.jsx("button",{type:"button",onClick:()=>M(t=>
1!t),className:"text-[11px] font-medium text-brand-600 hover:underline dark:text-brand-300",children:g?"Back to editor":"Edit HTML"})]}),g?e.jsx("textarea",{value:o,onChange:t=>{p(),u(t.target.value)},rows:10,className:"mt-1 block w-full rounded-md border-slate-300 font-mono text-sm dark:border-slate-600 dark:bg-slate-800 dark:text-slate-100"}):e.jsx("div",{className:"mt-1",onFocus:()=>T.current="body",children:e.jsx(le,{value:o,onChange:t=>{p(),u(t)},onEditorReady:t=>A.current=t})})]}),e.jsxs("div",{children:[e.jsxs("div",{className:"text-xs font-medium text-slate-600 dark:text-slate-300",children:["Preview ",e.jsx("span",{className:"font-normal text-slate-400",children:"(with sample data)"})]}),e.jsxs("div",{className:"mt-1 rounded-md border border-slate-200 p-3 dark:border-slate-700",children:[e.jsx("div",{className:"text-sm font-semibold text-slate-700 dark:text-slate-200",children:J}),e.jsx("div",{className:"rich-text-body mt-2 text-sm text-slate-700 dark:text-slate-200","data-testid":"email-preview-body",dangerouslySetInnerHTML:{__html:xe(G)}})]}),w.length>0&&e.jsxs("details",{className:"mt-1.5",children:[e.jsx("summary",{className:"cursor-pointer text-[11px] text-slate-500 hover:underline dark:text-slate-400",children:"Change the sample data"}),e.jsx("div",{className:"mt-1.5 grid grid-cols-2 gap-2 sm:grid-cols-3",children:w.map(t=>e.jsxs("label",{className:"block text-[11px] text-slate-500",children:[t.replace(/_/g," "),e.jsx("input",{value:j[t]??"",onChange:n=>q(m=>({...m,[t]:n.target.value})),className:"mt-0.5 block w-full rounded border-slate-300 text-xs dark:border-slate-600 dark:bg-slate-800 dark:text-slate-100"})]},t))})]})]}),!a&&e.jsxs("label",{className:"flex items-center gap-2 text-xs font-medium text-slate-600 dark:text-slate-300",children:[e.jsx("input",{type:"checkbox",checked:f,onChange:t=>{p(),N(t.target.checked)}}),"Active"]}),e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx(E,{type:"submit",size:"md",disabled:b,children:b?"Savingâ¦":a?"Create template":"Save changes"}),e.jsx(E,{type:"button",size:"md",variant:"secondary",onClick:k.requestClose,children:"Cancel"}),i&&!b&&e.jsx("span",{className:"text-xs text-amber-600 dark:text-amber-400",children:"Unsaved changes"}),P&&e.jsx("p",{className:"text-xs text-rose-600",children:P})]})]})})]}),e.jsx(Z,{open:k.confirming,onKeepEditing:k.keepEditing,onDiscard:k.discard})]})}function Ee({embedded:s=!1}={}){const c=ee(),[a,d]=r.useState(null),[x,h]=r.useState(!1),y=me(),l=y.data??[],[v,o]=te(),u=v.get("template");r.useEffect(()=>{if(!u||l.length===0)return;const i=l.find(S=>S.template_key===u);i&&d(i),o({},{replace:!0})},[u,l,o]);const f=l.filter(i=>i.default_channel==="gmail"),N=l.filter(i=>i.default_channel!=="gmail"&&i.template_key.startsWith("milestone_")),g=l.filter(i=>i.default_channel!=="gmail"&&!i.template_key.startsWith("milestone_"));return c.isLoading?e.jsx("p",{className:"text-sm text-slate-500",children:"Loadingâ¦"}):c.data?.is_owner||c.data?.flag_email_templates?e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"flex flex-wrap items-end justify-between gap-3",children:[e.jsxs("div",{children:[!s&&e.jsx("h1",{className:"text-xl font-semibold text-slate-900 dark:text-slate-100",children:"Email Templates"}),e.jsxs("p",{className:"text-sm text-slate-500 dark:text-slate-400",children:["Variables like ","{{first_name}}"," fill in at send time."]})]}),e.jsx(E,{size:"md",onClick:()=>h(!0),children:"+ New client template"})]}),y.isLoading?e.jsx("p",{className:"text-sm text-slate-500",children:"Loading templatesâ¦"}):e.jsxs(e.Fragment,{children:[e.jsxs("section",{className:"space-y-3",children:[e.jsxs("div",{children:[e.jsx("h2",{className:"text-sm font-semibold text-slate-800 dark:text-slate-100",children:"Client emails"}),e.jsx("p",{className:"text-xs text-slate-500 dark:text-slate-400",children:"Starting points staff pick in the email composer â sent to clients from the senderâs own 54 Realty Gmail."})]}),f.length===0?e.jsx("p",{className:"rounded-lg border border-dashed border-slate-300 px-4 py-6 text-center text-sm text-slate-500 dark:border-slate-700",children:"No client templates yet â create one and it appears in the composerâs âStart from a templateâ picker."}):e.jsx(R,{templates:f,onEdit:d})]}),e.jsxs("section",{className:"space-y-3",children:[e.jsxs("div",{children:[e.jsx("h2",{className:"text-sm font-semibold text-slate-800 dark:text-slate-100",children:"Automatic notifications"}),e.jsx("p",{className:"text-xs text-slate-500 dark:text-slate-400",children:"Sent by the system (via Resend) â birthdays, anniversaries, compliance reminders, lockbox returns, and more. Edit the copy; the app decides when they go out."})]}),g.length===0?e.jsx("p",{className:"text-sm text-slate-500",children:"No system templates."}):e.jsx(R,{templates:g,onEdit:d})]}),N.length>0&&e.jsxs("section",{className:"space-y-3",children:[e.jsxs("div",{children:[e.jsx("h2",{className:"text-sm font-semibold text-slate-800 dark:text-slate-100",children:"Milestone emails"}),e.jsxs("p",{className:"text-xs text-slate-500 dark:text-slate-400",children:["The congratulations an agent gets from Joe the morning after a milestone â one note per milestone. Switch each on or off and see what went out on"," ",e.jsx(se,{to:"/admin/milestones",className:"text-brand-700 hover:underline dark:text-brand-300",children:"Milestone Emails"}),"."]})]}),e.jsx(R,{templates:N,onEdit:d})]})]}),a&&e.jsx(I,{template:a,onClose:()=>d(null)}),x&&e.jsx(I,{template:null,onClose:()=>h(!1)})]}):e.jsx("p",{className:"text-sm text-rose-600",children:"Email templates are owner-only."})}function R({templates:s,onEdit:c}){return e.jsx("div",{className:"grid grid-cols-1 gap-3 lg:grid-cols-2",children:s.map(a=>e.jsx(H,{children:e.jsx(D,{children:e.jsxs("div",{className:"flex flex-wrap items-start gap-3",children:[e.jsxs("div",{className:"flex-1",children:[e.jsxs("div",{className:"flex flex-wrap items-center gap-2",children:[e.jsx("span",{className:"text-sm font-semibold text-slate-800 dark:text-slate-100",children:ae(a.name||a.template_key)}),a.is_active?e.jsx("span",{className:"rounded bg-emerald-100 px-1.5 py-0.5 text-[10px] font-medium uppercase tracking-wide text-emerald-700 dark:bg-emerald-900/40 dark:text-emerald-200",children:"Active"}):e.jsx("span",{className:"rounded bg-slate-200 px-1.5 py-0.5 text-[10px] font-medium uppercase tracking-wide text-slate-600 dar
1k:bg-slate-700 dark:text-slate-300",children:"Inactive"})]}),a.subject_template&&e.jsx("p",{className:"mt-0.5 truncate text-xs text-slate-500",children:a.subject_template}),e.jsx("p",{className:"mt-1 text-[11px] text-slate-400 dark:text-slate-500",children:a.template_key})]}),e.jsx("div",{className:"flex items-center gap-2",children:e.jsx(E,{size:"md",variant:"secondary",onClick:()=>c(a),children:"Edit"})})]})})},a.id))})}export{Ee 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.