1import{r as h,j as e}from"./vendor-Dz9lJ0A9.js";import{ak as $t,u as tt,e as le,B as z,aB as at,Q as ge,d as Gt,f as re,m as ee,a0 as Oe,a1 as We,a3 as Pe,a4 as Yt,X as Ut,as as Jt,P as ve,K as U,af as ke,a9 as ne,S as st,M as Xt,F as Zt,A as wt,aA as ea,a2 as rt,ad as ta,L as aa}from"./index-BDOD5E4V.js";import{M as Qe,a as vt,i as sa}from"./invoice-reminder-workflow-Biqtmpi4.js";import{u as At}from"./use-unsaved-changes-warning-8-gDvkSk.js";import{I as M}from"./input-uKcRzRZU.js";import{L as we}from"./label-xakz0IwZ.js";import{S as De}from"./switch-CTKr17l3.js";import{S as L,a as K,b as H,c as V,d as w}from"./select-BqpPFFZ7.js";import{R as ra}from"./receipt-text-CZTSY4ja.js";import{P as ue}from"./plus-DXEHL3ds.js";import{D as kt}from"./disclosure-summary-Cx23ywG7.js";import{p as Nt,h as Ct}from"./html-text-jWEjkLI5.js";import{a as na}from"./crm-shortcodes-C9arKWs7.js";import{T as ia}from"./TimelineRichEditor-CWcsB4h6.js";import{u as oa}from"./use-rich-text-caret-BDNN0iTE.js";import{A as la}from"./AiWritingAssist-B2CsnXYN.js";import{W as da,b as ca,P as xe,J as Ee,s as Ae,e as Ge,c as ma}from"./WorkflowTemplates-Mo9B1Q4i.js";import{C as he}from"./chevron-right-DSIqreSQ.js";import{B as Je}from"./bookmark-plus-BF9kreD1.js";import{C as Ie}from"./check-DtEZOD33.js";import{H as ua}from"./heart-handshake-BQPuam2W.js";import{C as pa}from"./calendar-check-BrssALxT.js";import{P as xa}from"./party-popper-DEXQZVum.js";import{D as ha,a as ga,b as fa,c as ba}from"./dropdown-menu-CIia9Fy3.js";import{W as ya}from"./workflow-Dx8fMn9D.js";import{G as Me}from"./git-branch-B5dRu4cU.js";import{G as Dt}from"./grip-vertical-WdKeR_bN.js";import{C as ja}from"./clock-3-waWsKnmm.js";import{S as Xe}from"./square-check-big-BGeSStJh.js";import{C as wa}from"./chevron-up-Cxk5DR03.js";import{A as va}from"./arrow-up-T2GzoczE.js";import{A as ka}from"./arrow-down-DB2vZ9g8.js";import{u as Na}from"./use-autosave-B4BSB5oU.js";import{P as Ca}from"./PageHelper-DlLkbNgT.js";import{T as Sa}from"./textarea-B6S3DUiX.js";import{E as Ta}from"./empty-state-BuhuU5ze.js";import{C as _a}from"./circle-check-CJ_VUuj_.js";import{Z as me}from"./zap-BFvx7yl0.js";import{C as $a}from"./calendar-Djt6tY64.js";import{C as Aa}from"./clock-BGjkcAN7.js";import{S as Et}from"./send-D2ID-sFR.js";import{B as It}from"./bell-BVf4PkaI.js";import{C as nt}from"./clipboard-list-BdSF-x85.js";import{T as Re}from"./trash-2-DB6k7OfX.js";import{T as Rt}from"./timer-BsT2kEWA.js";import{P as Ze}from"./pin-CvPNoUzR.js";import{L as Da}from"./layout-grid-D3ok2cOJ.js";import{P as Ea}from"./play-Da0bYY7C.js";import{C as Ia}from"./copy-dUWtYfC2.js";import{P as Ra}from"./pencil-Ct3hcvbE.js";import{S as Oa}from"./settings-2-0Dw1bLtV.js";(function(){try{var a=typeof window<"u"?window:typeof global<"u"?global:typeof globalThis<"u"?globalThis:typeof self<"u"?self:{};a.SENTRY_RELEASE={id:"8dd115ac638cf6a2a2cf5668d0686c96509328ed"};var i=new a.Error().stack;i&&(a._sentryDebugIds=a._sentryDebugIds||{},a._sentryDebugIds[i]="e2c3af3d-eaad-434b-b21a-b2850930bbec",a._sentryDebugIdIdentifier="sentry-dbid-e2c3af3d-eaad-434b-b21a-b2850930bbec")}catch{}})();const ce="/api/crm/invoice-reminder-workflow",St="font-sans text-[10px] font-normal uppercase tracking-[0.15em] text-foreground/70",et=(a,i)=>`${a} ${i}${a===1?"":"s"}`,ye=a=>a.daysBeforeList?.length?a.daysBeforeList:[a.daysBefore];function Wa(a){const i=[...ye(a)].sort((n,l)=>l-n),s=i.length===1?`${et(i[0],"day")} before`:`${i.slice(0,-1).join(", ")} and ${i[i.length-1]} days before`,o=a.daysAfterOverdue>0?`, then every ${et(a.daysAfterOverdue,"day")} until paid`:"";return`${s}, on the due date${o}`}function Pa(){const a=$t(),[,i]=tt(),s=h.useRef(null),o=new URLSearchParams(a).get("workflow")==="invoice-reminders",{data:n,isLoading:l,isError:g,isFetching:c,refetch:m}=le({queryKey:[ce]}),y=()=>{const N=new URLSearchParams(a);N.set("tab","library"),N.set("workflow","invoice-reminders"),i(`/automations?${N.toString()}`)},r=()=>{const N=new URLSearchParams(a);N.delete("workflow"),i(`/automations?${N.toString()}`),requestAnimationFrame(()=>s.current?.focus())},x=n?.mode==="needs_setup"?"Needs setup":n?.enabled?"On · Automatic emails":"Off";return e.jsxs("section",{className:"border-b border-border/5","data-testid":"invoice-reminder-workflow",children:[e.jsxs("div",{className:"flex items-center gap-3 py-5 md:gap-4",children:[e.jsx(ra,{className:"h-3.5 w-3.5 shrink-0 text-foreground
1/70","aria-hidden":"true"}),e.jsxs("div",{className:"min-w-0 flex-1",children:[e.jsxs("div",{className:"flex flex-wrap items-center gap-x-2 gap-y-1",children:[e.jsx("p",{className:"break-words text-[13px] font-medium",children:"Invoice reminders"}),n&&e.jsx("span",{className:"text-xs text-muted-foreground","data-testid":"invoice-reminder-status",children:x})]}),n&&e.jsx("p",{className:"mt-1.5 text-[11px] text-foreground/70","data-testid":"invoice-reminder-summary",children:Wa(n)})]}),e.jsx("span",{className:"hidden shrink-0 whitespace-nowrap text-[11px] uppercase tracking-[0.18em] text-foreground/70 md:inline",children:"Built-in"}),e.jsx(z,{ref:s,variant:"ghost",size:"sm",onClick:y,"aria-haspopup":"dialog",className:"shrink-0",disabled:!n,"data-testid":"button-manage-invoice-reminders",children:n?.canManage?"Manage":"View"})]}),l&&e.jsx("p",{role:"status",className:"pb-4 text-sm text-muted-foreground",children:"Loading invoice remindersâ¦"}),g&&e.jsxs("div",{role:"alert",className:"pb-4 space-y-2 text-sm",children:[e.jsx("p",{children:"We couldn't load invoice reminders."}),e.jsx(z,{variant:"outline",disabled:c,onClick:()=>{m()},children:"Try again"})]}),o&&n&&e.jsx(qa,{settings:n,onClose:r})]})}const Ma=[7,14,1,30,21,2,60];function Ye(){return e.jsx("span",{"aria-hidden":"true",className:"absolute -left-[25px] top-1.5 h-2 w-2 rounded-full bg-foreground
1/40"})}function qa({settings:a,onClose:i}){const s=at(),{toast:o}=ge(),n=Gt(),l=h.useRef(null),g=h.useRef(null),c=h.useRef(null),[m,y]=h.useState(a),[r,x]=h.useState(a.enabled),[N,C]=h.useState(a.defaultEnabled),[T,$]=h.useState(()=>ye(a).map(String)),[_,S]=h.useState(String(a.daysAfterOverdue)),[f,Q]=h.useState("upcoming"),[q,O]=h.useState(!1),E=T.join(",")!==ye(m).join(","),W=r!==m.enabled||N!==m.defaultEnabled||E||_!==String(m.daysAfterOverdue);At(W);const u=!a.canManage,v=le({queryKey:[ce,"preview",f],queryFn:async()=>(await ee("GET",`${ce}/preview?kind=${f}`)).json(),enabled:q}),A=re({mutationFn:async()=>{const j=T.map(k=>k.trim());if(j.some(k=>!/^\d+$/.test(k)||Number(k)<1||Number(k)>Qe))throw new Error(`Choose 1 to ${Qe} days for each reminder before the due date.`);if(new Set(j.map(Number)).size!==j.length)throw new Error("Each reminder needs a different number of days.");if(!/^\d+$/.test(_)||Number(_)>30)throw new Error("Choose 0 to 30 days between overdue reminders.");return await(await ee("PATCH",ce,{...r!==m.enabled||m.mode==="needs_setup"?{enabled:r}:{},...N!==m.defaultEnabled?{defaultEnabled:N}:{},...E?{daysBeforeList:j.map(Number)}:{},..._!==String(m.daysAfterOverdue)?{daysAfterOverdue:Number(_)}:{}})).json()},onSuccess:j=>{n.setQueryData([ce],j),n.setQueryData(["/api/crm/settings"],d=>d&&{...d,reminderEnabled:j.defaultEnabled,reminderDaysBefore:j.daysBefore,reminderDaysBeforeList:ye(j),reminderDaysAfterOverdue:j.daysAfterOverdue}),n.invalidateQueries({queryKey:["/api/crm/settings"]}),n.invalidateQueries({queryKey:[ce,"preview"]}),y(j),x(j.enabled),C(j.defaultEnabled),$(ye(j).map(String)),S(String(j.daysAfterOverdue)),o({title:"Invoice reminder settings saved"})},onError:j=>o({title:"Couldn't save invoice reminders",description:j.message,variant:"destructive"})}),X=u||A.isPending,G=()=>{const j=new Set(T.map(Number)),d=Ma.find(D=>!j.has(D))??10,k=T.length;$(D=>[...D,String(d)]),requestAnimationFrame(()=>g.current?.querySelectorAll("input")[k]?.focus())},J=j=>{$(d=>d.filter((k,D)=>D!==j)),requestAnimationFrame(()=>c.current?.focus())},Z=async()=>{A.isPending||W&&!await s({title:"Discard your invoice reminder changes?",description:"Your unsaved changes will be lost. Saved reminder settings will stay as they are.",confirmLabel:"Discard changes"})||i()},ae=a.canManage?a.activeInvoiceCount:null;return e.jsx(Oe,{open:!0,onOpenChange:j=>{j||Z()},children:e.jsxs(We,{id:"invoice-reminder-editor",className:"w-[calc(100%-2rem)] max-w-xl max-h-[85dvh] gap-6 overflow-y-auto rounded-lg","data-testid":"invoice-reminder-editor",onOpenAutoFocus:j=>{j.preventDefault(),l.current?.focus({preventScroll:!0})},children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(Pe,{ref:l,tabIndex:-1,className:"pr-8 font-display text-xl outline-none",children:"Manage invoice reminders"}),e.jsx(Yt,{children:"Emails your clients before a payment is due, on the day, and after, each with their payment link."})]}),u&&e.jsx("p",{role:"status",className:"text-sm text-muted-foreground",children:"Only the workspace owner or an admin can change invoice reminder settings."}),e.jsxs("div",{className:"flex items-start justify-between gap-4",children:[e.jsxs("div",{className:"space-y-1",children:[e.jsx(we,{htmlFor:"invoice-reminders-enabled",children:"Automatic reminder emails"}),e.jsx("p",{className:"text-xs text-muted-foreground",children:r?m.enabled?"Reminders send on their own for invoices that have them on.":"When you save, only payments due today or later are included, so old overdue invoices won't get a backlog of emails.":"Nothing sends while this is off. Previewing an email never turns it on."})]}),e.jsx(De,{id:"invoice-reminders-enabled",checked:r,onCheckedChange:j=>{x(j),j&&m.mode==="needs_setup"&&C(!0)},disabled:X})]}),e.jsxs("section",{"aria-labelledby":"invoice-reminder-schedule",className:"space-y-4",children:[e.jsx("h3",{id:"invoice-reminder-schedule",className:St,children:"Schedule"}),e.jsxs("ol",{className:"ml-1 space-y-5 border-l border-border/40 pl-5",children:[e.jsxs("li",{className:"relative",children:[e.jsx(Ye,{}),e.jsx("p",{className:"text-sm font-medium",children:"Before it's due"}),e.jsx("div",{ref:g,className:"mt-2 space-y-2",children:T.map((j,d)=>e.jsxs("div",{className:"flex items-center gap-2","data-testid":`invoice-reminder-before-${d}
1`,children:[e.jsx(M,{type:"number",inputMode:"numeric",min:1,max:Qe,step:1,"aria-label":`Reminder ${d+1}, days before payment is due`,value:j,onChange:k=>$(D=>D.map((Y,se)=>se===d?k.target.value:Y)),className:"h-9 w-16 text-center",disabled:X}),e.jsx("span",{className:"text-sm text-muted-foreground",children:"days before"}),!u&&T.length>1&&e.jsx(z,{variant:"ghost",size:"icon",className:"h-9 w-9 text-foreground/70","aria-label":`Remove reminder ${d+1}`,onClick:()=>J(d),disabled:A.isPending,children:e.jsx(Ut,{className:"h-3.5 w-3.5","aria-hidden":"true"})})]},d))}),!u&&(T.length<vt?e.jsxs(z,{ref:c,variant:"ghost",size:"sm",className:"mt-2 -ml-2 h-8 gap-1.5 px-2 text-xs",onClick:G,disabled:A.isPending,children:[e.jsx(ue,{className:"h-3.5 w-3.5","aria-hidden":"true"})," Add a reminder"]}):e.jsxs("p",{className:"mt-2 text-xs text-muted-foreground",children:["Up to ",vt," reminders before the due date."]}))]}),e.jsxs("li",{className:"relative",children:[e.jsx(Ye,{}),e.jsx("p",{className:"text-sm font-medium",children:"On the due date"}),e.jsx("p",{className:"mt-1 text-xs text-muted-foreground",children:"One reminder, always included."})]}),e.jsxs("li",{className:"relative",children:[e.jsx(Ye,{}),e.jsx("p",{className:"text-sm font-medium",children:"After it's due"}),e.jsxs("div",{className:"mt-2 flex flex-wrap items-center gap-2 text-sm text-muted-foreground",children:[e.jsx("span",{"aria-hidden":"true",children:"Repeat every"}),e.jsx(M,{type:"number",inputMode:"numeric",min:0,max:30,step:1,"aria-label":"Repeat overdue reminders every","aria-describedby":"invoice-reminders-overdue-hint",value:_,onChange:j=>S(j.target.value),className:"h-9 w-16 text-center text-foreground",disabled:X}),e.jsx("span",{children:"days until paid"})]}),e.jsx("p",{id:"invoice-reminders-overdue-hint",className:"mt-1 text-xs text-muted-foreground",children:"Set to 0 to skip repeats."})]})]})]}),e.jsxs("div",{className:"space-y-2 border-t border-border/20 pt-5",children:[e.jsxs("div",{className:"flex items-start justify-between gap-4",children:[e.jsxs("div",{className:"space-y-1",children:[e.jsx(we,{htmlFor:"invoice-reminders-new-invoices",children:"Include new invoices automatically"}),e.jsx("p",{className:"text-xs text-muted-foreground",children:N?"New invoices start with this schedule. You can change it on each invoice.":"New invoices are not included automatically. You can still turn reminders on for each invoice."})]}),e.jsx(De,{id:"invoice-reminders-new-invoices",checked:N,onCheckedChange:C,disabled:X})]}),e.jsx("p",{className:"text-xs text-muted-foreground",children:ae!=null&&ae>0?`Changes apply to new invoices. The ${et(ae,"invoice")} with reminders on keep their own settings.`:"Changes apply to new invoices. Existing invoices keep their own settings."})]}),e.jsxs("section",{"aria-labelledby":"invoice-reminder-preview",className:"space-y-3 border-t border-border/20 pt-5",children:[e.jsx("h3",{id:"invoice-reminder-preview",className:St,children:"Preview"}),e.jsxs("div",{className:"flex flex-wrap items-center gap-3",children:[e.jsx(we,{htmlFor:"invoice-reminder-preview-kind",className:"sr-only",children:"Reminder type"}),e.jsxs(L,{value:f,onValueChange:j=>Q(j),children:[e.jsx(K,{id:"invoice-reminder-preview-kind",className:"w-full sm:w-52",children:e.jsx(H,{})}),e.jsxs(V,{children:[e.jsx(w,{value:"upcoming",children:"Upcoming payment"}),e.jsx(w,{value:"due_today",children:"Due today"}),e.jsx(w,{value:"overdue",children:"Overdue payment"})]})]}),e.jsx(z,{variant:"outline",onClick:()=>O(!0),children:"Preview email"})]}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Uses your saved schedule and sample details. Nothing is sent."}),q&&v.isLoading&&e.jsx("p",{role:"status",children:"Loading email previewâ¦"}),q&&v.isError&&e.jsxs("div",{role:"alert",className:"space-y-2 text-sm",children:[e.jsx("p",{children:"We couldn't load this email preview."}),e.jsx(z,{variant:"outline",onClick:()=>{v.refetch()},children:"Try preview again"})]}),q&&v.data&&e.jsxs("div",{className:"space-y-3",children:[e.jsxs("p",{className:"text-sm",children:[e.jsx("span",{className:"font-medium",children:"Subject:"})," ",v.data.subject]}),e.jsx("iframe",{title:"Invoice reminder email preview",srcDoc:v.data.html,sandbox:"",className:"w-full min-h-[400px] rounded border border-border/20 bg-white"})]})]}),e.jsxs(Jt,{className:"gap-3 sm:flex-wrap sm:items-center",children:[e.jsx(z,{variant:"outline",onClick:()=>{Z()},disabled:A.isPending,children:"Back to workflows"}),a.canManage&&e.jsx(ve,{disabled:A.isPending||!W&&m.mode!=="needs_setup",onClick:()=>A.mutate(),children:A.isPending?"Savingâ¦":r&&!m.enabled?"Enable automatic emails":m.mode==="needs_setup"&&!r?"Keep reminders off":"Save changes"}),W&&e.jsx("p",{role:"status",className:"text-xs text-muted-foreground",children:"Unsaved changes"})]})]})})}function Fa(a){return sa(a.workflow)?e.jsx(Ba,{...a}):null}function Ba({workflow:a,confirmNavigation:i=!1}
1){const[,s]=tt(),o=$t(),n=at(),{data:l}=le({queryKey:[ce]}),g=async()=>{if(i&&!await n({title:"Open invoice reminder settings?",description:"Any unsaved edits to this workflow will be discarded. Its saved tasks and history will stay here.",confirmLabel:"Open invoice reminders"}))return;const c=new URLSearchParams(o);c.set("tab","library"),c.set("workflow","invoice-reminders"),s(`/automations?${c.toString()}`)};return l?.canReachClients===!1?null:e.jsxs("p",{className:"py-3 text-sm text-muted-foreground","data-testid":`invoice-reminders-managed-${a?.id??"workflow"}`,children:["Payment emails are managed by"," ",e.jsx("button",{type:"button",className:"underline underline-offset-4 text-foreground",onClick:()=>{g()},children:"Invoice reminders"}),". Existing tasks and history stay here."]})}const Ot=na.map(a=>({token:a.code,label:a.label}));function Wt({step:a,emailTemplates:i,expanded:s,onToggle:o,onUpdate:n}){const l=a.automationConfig||{},g=l.emailSource||"custom",c=g==="template"&&!!l.templateId,m=h.useRef(null),{containerRef:y,insertText:r}=oa(),[x,N]=h.useState("body"),[C,T]=h.useState(!1),[$,_]=h.useState(!1),[S,f]=h.useState(""),{toast:Q}=ge(),q=re({mutationFn:async u=>(await ee("POST","/api/crm/email-templates",u)).json(),onSuccess:u=>{U.refetchQueries({queryKey:["/api/crm/email-templates"]}),n({...l,emailSource:"template",templateId:u.id,templateName:u.name,subject:u.subject,body:u.body}),_(!1),f(""),Q({title:"Template saved",description:`"${u.name}" is now available in your email templates.`})},onError:()=>{Q({title:"Failed to save template",description:"Please try again.",variant:"destructive"})}}),O=u=>{const v={...l,...u};return c&&(v.emailSource="custom",delete v.templateId,delete v.templateName),v},E=u=>{if(x==="body"){r(u)||n(O({body:(l.body||"")+u}));return}const v=m.current,A=O({});if(!v){A[x]=(l[x]||"")+u,n(A);return}const X=v.selectionStart,G=v.selectionEnd,J=v.value;A[x]=J.slice(0,X)+u+J.slice(G),n(A),requestAnimationFrame(()=>{v.selectionStart=v.selectionEnd=X+u.length,v.focus()})},W=u=>{if(u==="__custom__"){n({emailSource:"custom",subject:l.subject||"",body:l.body||"",attachments:l.attachments});return}const v=i.find(A=>A.id===parseInt(u));v&&n({emailSource:"template",templateId:v.id,templateName:v.name,subject:Ct(v.subject),body:v.body,attachments:l.attachments})};return e.jsxs("div",{className:"rounded-md border border-amber-200/20 dark:border-amber-700/15 bg-amber-50/30 dark:bg-amber-900/5",children:[e.jsxs("button",{type:"button",onClick:o,className:"w-full flex items-center gap-2 px-3 py-2 text-[11px] text-muted-foreground hover:text-foreground transition-colors","data-testid":"button-toggle-email-config",children:[e.jsx(ke,{className:"w-3.5 h-3.5"}),e.jsx("span",{className:"flex-1 text-left",children:l.emailSource==="template"&&l.templateName?`Template: ${l.templateName}`:l.subject?`Custom: ${l.subject}`:"Configure email content..."}),s?e.jsx(ne,{className:"w-3 h-3"}):e.jsx(he,{className:"w-3 h-3"})]}),s&&e.jsxs("div",{className:"px-3 pb-3 space-y-3",children:[e.jsxs("div",{children:[e.jsx("label",{className:"text-[11px] tracking-[0.15em] uppercase text-foreground/70 mb-1 block",children:"Email Source"}),e.jsxs(L,{value:g==="template"&&l.templateId?String(l.templateId):"__custom__",onValueChange:W,children:[e.jsx(K,{className:"h-8 md:h-8 text-[12px] md:text-[12px] bg-background/50","data-testid":"select-email-source",children:e.jsx(H,{placeholder:"Choose source..."})}),e.jsxs(V,{children:[e.jsx(w,{value:"__custom__",children:"Draft custom email"}),i.map(u=>e.jsx(w,{value:String(u.id),"data-testid":`email-template-option-${u.id}`,children:u.name},u.id))]})]})]}),c&&e.jsx("p",{className:"text-[10px] text-foreground/70 italic",children:"Preview from template â edits will switch to custom draft"}),e.jsxs("div",{children:[e.jsx("label",{className:"text-[11px] tracking-[0.15em] uppercase text-foreground/70 mb-1 block",children:"Subject"}),e.jsx("textarea",{ref:m,value:l.subject||"",onChange:u=>{const v={...l,subject:u.target.value};c&&(v.emailSource="custom",delete v.templateId,delete v.templateName),n(v)},onFocus:()=>N("subject"),placeholder:"Email subject line...",rows:1,className:`w-full rounded-md border border-border/30 px-3 py-1.5 text-[12px] resize-none focus:outline-none focus:ring-1 focus:ring-primary/30 ${c?"bg-muted/40 text-muted-foreground":"bg-background/50"}`,"data-testid":"input-email-step-subject"})]}),e.jsxs("div",{children:[e.jsxs("div",{className:"flex items-center justify-between mb-1",children:[e.jsx("label",{className:"text-[11px] tracking-[0.15em] uppercase text-foreground
1/70 block",children:"Body"}),e.jsx(la,{field:"crm_email_template",currentText:Ct(l.body||""),onTextGenerated:u=>n(O({body:Nt(u)})),context:{question:`This email is sent automatically by a workflow step${l.subject?` with subject "${l.subject}"`:""}. Use shortcodes like {first_name} and {wedding_date} for personalization.`},mode:"both",size:"xs"})]}),e.jsx("div",{ref:y,onFocus:()=>N("body"),className:c?"rounded-md bg-muted/40":"rounded-md bg-background/50",children:e.jsx(ia,{content:Nt(l.body||""),onChange:u=>n(O({body:u})),minHeight:"160px",showLink:!0,showImage:!0,placeholder:"Email body. Use merge fields below to insert dynamic content.",dataTestId:"input-email-step-body"},a.id)})]}),e.jsx(da,{attachments:l.attachments,onChange:u=>n({...l,attachments:u})}),e.jsxs("div",{children:[e.jsxs("button",{type:"button",onClick:()=>T(!C),className:"flex items-center gap-1.5 text-[11px] tracking-[0.12em] uppercase text-foreground/70 hover:text-foreground transition-colors","data-testid":"button-toggle-merge-fields",children:[e.jsx(st,{className:"w-3 h-3"}),"Merge Fields",e.jsxs("span",{className:"text-[10px] normal-case tracking-normal text-foreground/70",children:["(",x,")"]}),C?e.jsx(ne,{className:"w-3 h-3"}):e.jsx(he,{className:"w-3 h-3"})]}),C&&e.jsx("div",{className:"mt-2 flex flex-wrap gap-1.5",children:Ot.map(u=>e.jsx("button",{type:"button",onClick:()=>E(u.token),className:"px-2 py-0.5 text-[10px] text-foreground/65 hover:text-foreground border border-border/30 rounded hover:border-foreground
1/20 transition-colors","data-testid":`merge-field-${u.token.replace(/[{}]/g,"")}`,children:u.label},u.token))})]}),!c&&l.subject&&l.body&&e.jsx("div",{children:$?e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("input",{autoFocus:!0,value:S,onChange:u=>f(u.target.value),onKeyDown:u=>{u.key==="Enter"&&S.trim()&&q.mutate({name:S.trim(),subject:l.subject,body:l.body}),u.key==="Escape"&&_(!1)},placeholder:"Template name...",className:"flex-1 h-7 rounded-md border border-border/30 px-2 text-[11px] bg-background/50 focus:outline-none focus:ring-1 focus:ring-primary/30","data-testid":"input-template-name"}),e.jsx("button",{type:"button",onClick:()=>{S.trim()&&q.mutate({name:S.trim(),subject:l.subject,body:l.body})},disabled:!S.trim()||q.isPending,className:"h-7 px-3 text-[10px] rounded-md bg-foreground/8 text-foreground/65 hover:bg-foreground/12 hover:text-foreground disabled:opacity-40 transition-colors","data-testid":"button-confirm-save-template",children:q.isPending?"Saving...":"Save"}),e.jsx("button",{type:"button",onClick:()=>_(!1),className:"h-7 px-2 text-[10px] text-foreground/65 hover:text-foreground transition-colors","data-testid":"button-cancel-save-template",children:"Cancel"})]}):e.jsxs("button",{type:"button",onClick:()=>{f(l.subject||""),_(!0)},className:"flex items-center gap-1.5 text-[11px] tracking-[0.12em] uppercase text-foreground/70 hover:text-foreground transition-colors","data-testid":"button-save-as-template",children:[e.jsx(Je,{className:"w-3 h-3"}),"Save as Template"]})})]})]})}function Pt({step:a,expanded:i,onToggle:s,onUpdate:o}){const n=a.automationConfig||{},l=h.useRef(null),[g,c]=h.useState(!1),m=n.smsBody||"",y=r=>{const x=l.current;if(!x){o({...n,smsBody:m+r});return}const N=x.selectionStart,C=x.selectionEnd,T=x.value;o({...n,smsBody:T.slice(0,N)+r+T.slice(C)}),requestAnimationFrame(()=>{x.selectionStart=x.selectionEnd=N+r.length,x.focus()})};return e.jsxs("div",{className:"rounded-md border border-sky-200/20 dark:border-sky-700/15 bg-sky-50/30 dark:bg-sky-900/5",children:[e.jsxs("button",{type:"button",onClick:s,className:"w-full flex items-center gap-2 px-3 py-2 text-[11px] text-muted-foreground hover:text-foreground transition-colors","data-testid":"button-toggle-sms-config",children:[e.jsx(Xt,{className:"w-3.5 h-3.5"}),e.jsx("span",{className:"flex-1 text-left",children:m?`Text: ${m.slice(0,40)}`:"Write the text message..."}),i?e.jsx(ne,{className:"w-3 h-3"}):e.jsx(he,{className:"w-3 h-3"})]}),i&&e.jsxs("div",{className:"px-3 pb-3 space-y-3",children:[e.jsxs("div",{children:[e.jsx("label",{className:"text-[11px] tracking-[0.15em] uppercase text-foreground/70 mb-1 block",children:"Text Message"}),e.jsx("textarea",{ref:l,value:m,onChange:r=>o({...n,smsBody:r.target.value}),placeholder:"Hi {first_name}, just a quick note about your wedding on {wedding_date}...",rows:3,className:"w-full rounded-md border border-border/30 px-3 py-2 text-[12px] leading-relaxed resize-none bg-background/50 focus:outline-none focus:ring-1 focus:ring-primary/30","data-testid":"input-sms-step-body"}),e.jsxs("p",{className:"mt-1 text-[10px] text-muted-foreground",children:[m.length,' characters. Texts go to the client phone on file. "Reply STOP to opt out" is added automatically.']}),e.jsx("p",{className:"mt-1.5 text-[10px] text-muted-foreground leading-relaxed",children:"Only clients you've marked as consenting to texts will receive this. Turn consent on from the client's profile (under their phone number). Clients who reply STOP are skipped automatically."})]}),e.jsxs("div",{children:[e.jsxs("button",{type:"button",onClick:()=>c(!g),className:"flex items-center gap-1.5 text-[11px] tracking-[0.12em] uppercase text-foreground/70 hover:text-foreground transition-colors","data-testid":"button-toggle-sms-merge-fields",children:[e.jsx(st,{className:"w-3 h-3"}),"Merge Fields",g?e.jsx(ne,{className:"w-3 h-3"}):e.jsx(he,{className:"w-3 h-3"})]}),g&&e.jsx("div",{className:"mt-2 flex flex-wrap gap-1.5",children:Ot.map(r=>e.jsx("button",{type:"button",onClick:()=>
1y(r.token),className:"px-2 py-0.5 text-[10px] text-foreground/65 hover:text-foreground border border-border/30 rounded hover:border-foreground/20 transition-colors","data-testid":`sms-merge-field-${r.token.replace(/[{}]/g,"")}`,children:r.label},r.token))})]})]})]})}function La(){return[{id:`drip-${Date.now()}-1`,title:"Welcome email",sortOrder:0,type:"automation",automationAction:"send_email",automationConfig:{timing:"immediate"}},{id:`drip-${Date.now()}-2`,title:"Wait 3 days",sortOrder:1,type:"automation",automationAction:"wait",automationConfig:{timing:"days_after_applied",timingDays:3}},{id:`drip-${Date.now()}-3`,title:"Follow-up email",sortOrder:2,type:"automation",automationAction:"send_email",automationConfig:{timing:"days_after_applied",timingDays:3}},{id:`drip-${Date.now()}-4`,title:"Wait 4 days",sortOrder:3,type:"automation",automationAction:"wait",automationConfig:{timing:"days_after_applied",timingDays:7}},{id:`drip-${Date.now()}-5`,title:"Check if opened",sortOrder:4,type:"automation",automationAction:"condition",automationConfig:{conditionType:"email_opened",skipSteps:"1"}},{id:`drip-${Date.now()}-6`,title:"Engaged version",sortOrder:5,type:"automation",automationAction:"send_email",automationConfig:{timing:"days_after_applied",timingDays:7}},{id:`drip-${Date.now()}-7`,title:"Re-engagement email",sortOrder:6,type:"automation",automationAction:"send_email",automationConfig:{timing:"days_after_applied",timingDays:10}}]}function Ka({stage:a,started:i,onChange:s,canBuild:o,canReview:n}){return e.jsx("nav",{"aria-label":"Workflow setup progress",className:"flex border-b border-primary/15",children:[i?"Name & start":"Choose a start","Build your steps","Review & save"].map((l,g)=>e.jsxs("button",{type:"button",disabled:g===1?!o:g===2?!n:!1,onClick:()=>s(g),"aria-current":a===g?"step":void 0,className:`flex flex-1 items-center justify-center gap-2 border-b-2 px-2 py-4 text-xs sm:text-sm transition-colors disabled:opacity-50 ${a===g?"border-primary text-primary font-semibold":"border-transparent text-muted-foreground hover:text-foreground"}`,children:[e.jsx("span",{className:`flex h-6 w-6 shrink-0 items-center justify-center rounded-full text-xs ${a===g?"bg-primary text-primary-foreground":"border border-primary/25"}`,children:g<a?e.jsx(Ie,{className:"h-3.5 w-3.5"}):g+1}),e.jsx("span",{children:l})]},l))})}const Ha=[{key:"proposal_follow_up",title:"Follow up on a proposal",detail:"Stay in touch while your client decides.",icon:Zt,color:"bg-violet-100 text-violet-800 dark:bg-violet-950 dark:text-violet-200"},{key:"booked_onboarding",title:"Welcome a booked client",detail:"Start every booking with a personal welcome.",icon:ua,color:"bg-teal-100 text-teal-800 dark:bg-teal-950 dark:text-teal-200"},{key:"inquiry_consultation",title:"Respond to a new inquiry",detail:"Help a new lead take their next step.",icon:ke,color:"bg-sky-100 text-sky-800 dark:bg-sky-950 dark:text-sky-200"},{key:"wedding_preparation",title:"Prepare for wedding day",detail:"Keep final details and deadlines in order.",icon:pa,color:"bg-amber-100 text-amber-900 dark:bg-amber-950 dark:text-amber-200"},{key:"post_wedding_wrap_up",title:"Wrap up after the wedding",detail:"Thank your client and finish the little things.",icon:xa,color:"bg-rose-100 text-rose-800 dark:bg-rose-950 dark:text-rose-200"}];function Va({onChoose:a,onBlank:i}){return e.jsxs("section",{className:"space-y-6","aria-labelledby":"workflow-start-heading",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx("p",{className:"text-sm font-semibold text-primary",children:"Start here"}),e.jsx("h2",{id:"workflow-start-heading",className:"font-display text-2xl sm:text-3xl font-normal",children:"What would you like help with?"}),e.jsx("p",{className:"text-sm sm:text-base text-muted-foreground",children:"Choose a ready-made starting point. You can change every step."})]}),e.jsxs("div",{className:"grid gap-3 sm:grid-cols-2",children:[Ha.map(({key:s,title:o,detail:n,icon:l,color:g})=>e.jsxs("button",{type:"button","data-testid":`choose-workflow-${s}`,onClick:()=>{const c=ca.find(m=>m.key===s);c&&a(c)}
1,className:"group flex items-center gap-4 rounded-xl border border-primary/15 bg-card p-5 text-left shadow-sm transition hover:-translate-y-0.5 hover:border-primary/60 hover:shadow-md focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring",children:[e.jsx("span",{className:`flex h-12 w-12 shrink-0 items-center justify-center rounded-xl ${g}`,children:e.jsx(l,{className:"h-6 w-6","aria-hidden":"true"})}),e.jsxs("span",{className:"flex-1 space-y-1",children:[e.jsx("span",{className:"block text-base font-semibold",children:o}),e.jsx("span",{className:"block text-sm leading-relaxed text-muted-foreground",children:n})]}),e.jsx(wt,{className:"h-4 w-4 shrink-0 text-primary transition-transform group-hover:translate-x-1","aria-hidden":"true"})]},s)),e.jsxs("button",{type:"button",onClick:i,className:"group flex items-center gap-4 rounded-xl border border-dashed border-primary/35 bg-primary/5 p-5 text-left transition hover:border-primary hover:bg-primary/10","data-testid":"workflow-start-blank",children:[e.jsx("span",{className:"flex h-12 w-12 shrink-0 items-center justify-center rounded-xl border border-primary/20 bg-card text-primary",children:e.jsx(ue,{className:"h-6 w-6"})}),e.jsxs("span",{className:"flex-1",children:[e.jsx("span",{className:"block text-base font-semibold",children:"Build my own"}),e.jsx("span",{className:"mt-1 block text-sm text-muted-foreground",children:"Start fresh with the steps you need."})]}),e.jsx(wt,{className:"h-4 w-4 text-primary"})]})]}),e.jsxs("p",{className:"flex items-center gap-2 text-sm text-muted-foreground",children:[e.jsx(Ie,{className:"h-4 w-4 text-teal-700 dark:text-teal-300"}),"Nothing runs until you turn your workflow on."]})]})}function za({name:a,start:i,onEdit:s}){return e.jsxs("div",{className:"flex flex-wrap items-center justify-between gap-4 rounded-xl border border-primary/15 bg-primary/5 p-5",children:[e.jsxs("div",{children:[e.jsx("h2",{className:"font-display text-xl sm:text-2xl font-normal",children:a||"Your workflow"}),e.jsxs("p",{className:"mt-1 text-sm text-muted-foreground",children:["Starts: ",i]})]}),e.jsx(z,{type:"button",variant:"ghost",className:"text-primary",onClick:s,children:"Edit name & start"})]})}const Ne=[{key:"immediate",label:"Immediately",description:"Fires the moment the workflow is applied.",anchor:"none",takesOffset:!1,supportsHours:!1},{key:"after_previous_completed",label:"After previous task completed",description:"Waits for the step before it to be checked off, then counts the offset from that moment.",anchor:"previous",takesOffset:!0,supportsHours:!0},{key:"days_after_applied",label:"Days after applied",description:"Counts from when the workflow was applied. This is the one to use for a drip sequence.",anchor:"applied",takesOffset:!0,supportsHours:!0},{key:"days_before_event",label:"Days before event date",description:"Counts back from the contact's wedding or event date, not from a consultation.",anchor:"event",takesOffset:!0,supportsHours:!0},{key:"days_after_event",label:"Days after event date",description:"Counts forward from the contact's wedding or event date.",anchor:"event",takesOffset:!0,supportsHours:!0},{key:"days_after_contract_signed",label:"Days after contract signed",description:"Counts from when the contact signed their contract. Never fires if nothing is signed.",anchor:"contract_signed",takesOffset:!0,supportsHours:!0},{key:"days_after_booking",label:"Days after booking date",description:"Counts from when the contact record was created.",anchor:"booking",takesOffset:!0,supportsHours:!0},{key:"before_consultation",label:"Before the consultation",description:"Counts back from the contact's next booked consultation slot. Use it for reminders ahead of a call, including same-day ones: set hours rather than days.",anchor:"consultation",takesOffset:!0,supportsHours:!0},{key:"after_consultation",label:"After the consultation",description:"Counts forward from the contact's most recent past consultation slot.",anchor:"consultation",takesOffset:!0,supportsHours:!0}];Ne.map(a=>a.key);Ne.filter(a=>a.anchor==="consultation").map(a=>a.key);function Qa(a,i){const s=a;if(s==="immediate")return{days:0,hours:0};const o=Math.max(0,Number(i?.timingHours)||0),n=Math.max(0,Number(i?.timingDays)||0);return s==="after_previous_completed"?{days:n,hours:o}:o>0?{days:n,hours:o}:{days:n||1,hours:0}}const Mt=[{key:"consultation_booked",label:"When a consultation is booked",description:"Fires the moment someone books a slot on your booking page, for a brand-new lead or an existing contact."}];Mt.map(a=>a.key);function je(a){if(!Array.isArray(a))return[];const i=new Set;return a.map((s,o)=>{const n=s;let l=typeof n.id=="string"&&n.id?n.id:`linear-step-${o}`;for(;i.has(l);
1)l=`${l}-${o}`;i.add(l);const g={...n,id:l,title:n.title||"",sortOrder:n.sortOrder??o,...n.automationConfig?{automationConfig:{...n.automationConfig}}:{}};if(n.automationAction!=="wait"||!g.automationConfig)return g;const c=g.automationConfig,m=c.unit||"days",y=Number(c.amount??c.days??c.waitDays??c.timingDays??c.hours??0)||0,r=(Number(c.hours)||0)+(Number(c.minutes)||0)/60,x=m==="minutes"?y/60:m==="hours"?y:y*24,N=m==="hours"?x:x+r;c.timingDays=Math.max(0,Math.round(N*36e5))/864e5;for(const C of["amount","days","waitDays","hours","minutes","unit"])delete c[C];return g})}function Ga({page:a,open:i,onOpenChange:s,isEdit:o,children:n,onAdvanced:l,advancedOptions:g=!1,onAdvancedOptionsChange:c}){const m=o?"Edit workflow":"New workflow";return a?i?e.jsxs("div",{className:"mx-auto w-full max-w-4xl scroll-mt-20 space-y-6 px-4 py-6 sm:px-6","data-testid":"workflow-simple-editor",children:[e.jsxs("header",{className:"space-y-3",children:[e.jsxs(z,{type:"button",variant:"ghost",className:"-ml-3 gap-2 text-muted-foreground hover:text-foreground",onClick:()=>s(!1),children:[e.jsx(ea,{className:"h-4 w-4","aria-hidden":"true"})," Back"]}),e.jsxs("div",{className:"flex flex-wrap items-center justify-between gap-3 sm:gap-5 rounded-2xl border border-primary/15 bg-gradient-to-br from-primary/10 via-background to-primary/5 px-4 py-4 sm:px-7 sm:py-6",children:[e.jsxs("div",{className:"flex items-center gap-4",children:[e.jsx("div",{className:"flex h-12 w-12 shrink-0 items-center justify-center rounded-xl bg-primary/10 text-primary ring-1 ring-primary/10",children:e.jsx(ya,{className:"h-6 w-6","aria-hidden":"true"})}),e.jsxs("div",{children:[e.jsx("h1",{className:"font-display text-2xl sm:text-3xl font-normal tracking-tight text-foreground",children:m}),e.jsx("p",{className:"mt-1.5 text-sm text-foreground/70",children:"Choose when it starts, then add what happens next."})]})]}),!o&&l&&e.jsxs(z,{type:"button",variant:"outline",className:"gap-2 bg-background",onClick:l,children:[e.jsx(Me,{className:"h-4 w-4","aria-hidden":"true"})," Open branching builder"]})]}),c&&e.jsx("nav",{"aria-label":"Workflow editor mode",className:"flex gap-5 border-b border-border/60",children:[[!1,"Step editor"],[!0,"All settings"]].map(([y,r])=>e.jsx("button",{type:"button","aria-pressed":g===y,onClick:()=>c(y===!0),className:`border-b-2 px-0 py-3 text-sm ${g===y?"border-primary font-medium text-primary":"border-transparent text-muted-foreground hover:text-foreground"}`,"data-testid":y?"workflow-mode-advanced":"workflow-mode-step",children:r},String(y)))})]}),n]}):null:e.jsx(Oe,{open:i,onOpenChange:s,children:e.jsxs(We,{className:"max-w-lg max-h-[90vh] overflow-y-auto",children:[e.jsx(rt,{children:e.jsx(Pe,{children:m})}),n]})})}const de=(a,i)=>`${a} ${i}${a===1?"":"s"}`;function Ya(a){const i=a.automationConfig||{};if(a.automationAction==="wait"){const c=je([a])[0].automationConfig?.timingDays||0,m=Math.round(c*86400),y=Math.floor(m/86400),r=Math.floor(m%86400/3600),x=Math.floor(m%3600/60),N=m%60;return`Wait ${[y?de(y,"day"):"",r?de(r,"hour"):"",x?de(x,"minute"):"",N?de(N,"second"):""].filter(Boolean).join(" and ")||"0 days"} before later steps`}const s=i.timing||"immediate";if(s==="immediate"||s==="after_previous")return"When the workflow starts";const{days:o,hours:n}=Qa(s,i),l=[o?de(o,"day"):"",n?de(n,"hour"):""].filter(Boolean).join(" and ")||"0 days",g={days_after_applied:"after the workflow starts",days_before_event:"before the wedding or event",days_after_event:"after the wedding or event",days_after_contract_signed:"after the contract is signed",days_after_booking:"after the contact was created",before_consultation:"before the consultation",after_consultation:"after the consultation",after_previous_completed:"after the previous task is completed"};return g[s]?`${l} ${g[s]}`:Ne.find(c=>c.key===s)?.label||"Check the saved timing"}const Ue="application/x-workflow-step";function Ua({step:a,index:i,emailSendMode:s,children:o,onMoveTo:n,total:l,simple:g=!0,priorWaitDays:c=0,openOnAdd:m=!1,forceExpanded:y=!1}){const[r,x]=h.useState(m),[N,C]=h.useState(!1),T=h.useId(),$=y||r;if(!g)return e.jsx(e.Fragment,{children:o});const _=a.title||"Untitled step",S=a.automationAction==="wait",f=["send_email","send_questionnaire","send_portal_link"].includes(a.automationAction||""),Q=a.type!=="automation"?"Your task":S?"Wait":f?s==="review"?"Review before sending":"Automatic email":"Automatic action",q=S?ja:f?ke:Xe,O=S?{edge:"border-l-amber-300 dark:border-l-amber-600",icon:"bg-amber-50 text-amber-700 dark:bg-amber-950/50 dark:text-amber-300",badge:"bg-amber-50 text-amber-800 dark:bg-amber-950/50 dark:text-amber-200"}:f?{edge:"border-l-violet-300 dark:border-l-violet-600",icon:"bg-violet-50 text-violet-700 dark:bg-violet-950/50 dark:text-violet-300",badge:"bg-violet-50 text-violet-800 dark:bg-violet-950/50 dark:text-violet-200"}:{edge:"border-l-teal-300 dark:border-l-teal-600",icon:"bg-teal-50 text-teal-700 dark:bg-teal-950/50 dark:text-teal-300",badge:"bg-teal-50 text-teal-800 dark:bg-teal-950/50 dark:text-teal-200"};return e.jsxs("article",{className:`w-full overflow-hidden rounded-xl border border-l-4 bg-card text-card-foreground shadow-sm transition-all ${O.edge} ${N?"border-primary ring-2 ring-primary/20":"border-border/70"}`,"data-testid":`workflow-step-card-${i}`,onDragOver:E=>{Array.from(E.dataTransfer.types).includes(Ue)&&(E.preventDefault(),E.dataTransfer.dropEffect="move",C(!0))},onDragLeave:E=>{(!(E.relatedTarget instanceof Node)||!E.currentTarget.contains(E.relatedTarget))&&C(!1)},onDrop:E=>{C(!1);const W=E.dataTransfer.getData(Ue);if(W){E.preventDefault();try{const u=JSON.parse(W).index;Number.isInteger(u)&&u>=0&&u<l&&u!==i&&n(u,i)}catch{}}},children:[e.jsxs("div",{className:"flex items-start gap-2 p-4 sm:gap-3 sm:p-5",children:[e.jsx("button",{type:"button",draggable:!0,className:"mt-1 flex h-9 w-6 shrink-0 cursor-grab items-center justify-center rounded text-muted-foreground hover:bg-muted hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring active:cursor-grabbing","aria-label":`Drag step ${i+1}: ${_}`,onDragStart:E=>{E.dataTransfer.setData(Ue,JSON.stringify({index:i,id:a.id})),E.dataTransfer.effectAllowed="move"},onDragEnd:()=>C(!1),children:e.jsx(Dt,{className:"h-4 w-4","aria-hidden":"true"})}),e.jsxs("div",{className:"min-w-0 flex-1 space-y-2.5",children:[e.jsxs("div",{className:"flex items-center gap-2.5",children:[e.jsx("span",{className:`flex h-9 w-9 shrink-0 items-center justify-center rounded-lg ${O.icon}`,children:e.jsx(q,{className:"h-4 w-4","aria-hidden":"true"})}),e.jsxs("div",{className:"flex flex-wrap items-center gap-x-2 gap-y-1",children:[e.jsxs("span",{className:"text-xs font-medium text-muted-foreground",children:["Step ",i+1]}),e.jsx("span",{className:`rounded-md px-2 py-1 text-[11px] font-medium ${O.badge}`,children:Q})]})]}),e.jsx("h2",{className:"break-words text-base font-semibold leading-snug",children:_}
1),e.jsx("p",{className:"text-sm text-foreground/70",children:Ya(a)}),!S&&c>0&&e.jsxs("p",{className:"text-xs text-muted-foreground",children:["Plus ",de(c,"day")," from earlier wait steps."]})]}),e.jsxs("div",{className:"flex shrink-0 flex-col items-end gap-1",children:[!y&&e.jsxs(z,{type:"button",variant:"ghost",size:"sm",className:"bg-primary/5 font-medium text-primary hover:bg-primary/10 hover:text-primary","aria-expanded":r,"aria-controls":T,onClick:()=>x(!r),children:[r?"Done":"Edit",r?e.jsx(wa,{className:"ml-1 h-4 w-4","aria-hidden":"true"}):e.jsx(ne,{className:"ml-1 h-4 w-4","aria-hidden":"true"})]}),e.jsxs("div",{className:"flex",children:[e.jsx(z,{type:"button",variant:"ghost",size:"icon",className:"h-8 w-8",disabled:i===0,"aria-label":`Move step ${i+1} up`,onClick:()=>n(i,i-1),children:e.jsx(va,{className:"h-3.5 w-3.5","aria-hidden":"true"})}),e.jsx(z,{type:"button",variant:"ghost",size:"icon",className:"h-8 w-8",disabled:i===l-1,"aria-label":`Move step ${i+1} down`,onClick:()=>n(i,i+1),children:e.jsx(ka,{className:"h-3.5 w-3.5","aria-hidden":"true"})})]})]})]}),$&&e.jsx("div",{id:T,className:"border-t border-border/60 bg-primary/[0.02] p-4 sm:p-6",children:o})]})}function Tt({index:a,onAdd:i,prominent:s=!1}){return e.jsx("div",{className:"flex justify-center py-2",children:e.jsxs(ha,{children:[e.jsx(ga,{asChild:!0,children:e.jsxs(z,{type:"button",variant:s?"default":"outline",size:s?"default":"sm",className:s?"gap-2 px-6 shadow-sm":"gap-1.5 border-primary/25 bg-background text-primary hover:border-primary/50 hover:bg-primary/5 hover:text-primary",children:[e.jsx(ue,{className:"h-4 w-4","aria-hidden":"true"})," ",s&&a===0?"Add your first step":"Add step"]})}),e.jsx(fa,{align:"center",children:[["task","Task for you"],["send_email","Email"],["wait","Wait"],["send_questionnaire","Questionnaire invitation"],["send_portal_link","Client portal invitation"]].map(([o,n])=>e.jsx(ba,{onSelect:()=>i(a,o),children:n},o))})]})})}function Ja(a){const i=a;return i.triggerStage&&i.triggerStage!=="none"?`a client moves to ${xe.find(s=>s.value===i.triggerStage)?.label||i.triggerStage}`:i.triggerEvent&&i.triggerEvent!=="none"?"a consultation is booked":i.jobType&&i.jobType!=="none"?`a ${Ee.find(s=>s.value===i.jobType)?.label||i.jobType} job is created`:i.triggerTags?.length?`a client is tagged ${i.triggerTags.join(", ")}`:"you apply it to a client"}function Xa({wf:a}){return e.jsxs("p",{className:"mt-1.5 text-[12px] text-foreground/80","data-testid":`workflow-starts-when-${a.id}`,children:["Starts when: ",Ja(a)]})}function Za({wf:a}){const{toast:i}=ge(),s=at(),o=a.enabled!==!1,n=re({mutationFn:async l=>{await ee("PATCH",`/api/crm/client-workflows/${a.id}`,{enabled:l})},onSuccess:(l,g)=>{U.refetchQueries({queryKey:["/api/crm/client-workflows"]}),i({title:g?`"${a.name}" is on`:`"${a.name}" is off`,description:g?"It will start for new clients when its trigger happens.":"No new clients will start it, and anything it had queued to send is cancelled."})},onError:l=>ta({error:l,title:"Couldn't change this workflow"})});return e.jsxs("label",{className:"mr-2 flex min-h-11 cursor-pointer items-center gap-2 text-[12px] text-foreground/80 md:min-h-0",children:[e.jsx(De,{checked:o,disabled:n.isPending,onCheckedChange:async l=>{(l||await s({title:`Turn off "${a.name}" for everyone?`,description:"No new clients will start it, and anything it has queued to send, for any client, is cancelled. You can turn it back on here anytime.",confirmLabel:"Turn off",testId:`button-confirm-disable-workflow-${a.id}`}))&&n.mutate(l)},"aria-label":`${a.name} is ${o?"on":"off"}`,"data-testid":`switch-workflow-enabled-${a.id}`}),o?"On":"Off"]})}function es({enabled:a,emailSendMode:i,onEnabledChange:s,onEmailSendModeChange:o}){const n=h.useId();return e.jsxs("section",{className:"space-y-4 rounded-lg border border-border/20 p-4","aria-label":"Workflow delivery settings",children:[e.jsxs("div",{className:"flex items-start justify-between gap-4",children:[e.jsxs("div",{children:[e.jsxs(we,{htmlFor:`${n}-enabled`,className:"text-sm font-medium",children:["Workflow ",a?"On":"Off"]}),e.jsx("p",{className:"mt-1 text-xs text-muted-foreground",children:"Turning this off stops new runs and cancels pending automatic actions. Existing tasks stay visible. Turning it back on starts new runs; canceled messages do not restart."})]}),e.jsx(De,{id:`${n}-enabled`,checked:a,onCheckedChange:s,"data-testid":"switch-workflow-enabled"})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(we,{htmlFor:`${n}-email-mode`,children:"Email sending"}),e.jsxs(L,{value:i,onValueChange:l=>o(l),children:[e.jsx(K,{id:`${n}-email-mode`,"data-testid":"select-workflow-email-mode",children:e.jsx(H,{})}),e.jsxs(V,{children:[e.jsx(w,{value:"review",children:"Review before sending"}),e.jsx(w,{value:"automatic",children:"Send automatically"})]})]}),e.jsx("p",{className:"text-xs text-muted-foreground",children:i==="review"?"Emails wait in Email review until you edit and send them. Tasks and reminders still run.":"Future emails in this workflow send using its saved templates and timing. Drafts already awaiting review still need your approval."})]})]})}const qt=[{key:"send_email",label:"Send Email",engines:["linear","graph"],aiCreatable:!0,description:"Emails the contact. Needs a subject and body; a step with a blank body is held, not sent."},{key:"send_sms",label:"Send Text (SMS)",engines:["linear","graph"],aiCreatable:!0,description:"Texts the contact. Only reaches contacts with a phone number and SMS consent on file."},{key:"send_questionnaire",label:"Send Questionnaire",engines:["linear","graph"],aiCreatable:!0,description:"Emails a questionnaire template. Without a template id it degrades to a reminder task."},{key:"send_portal_link",label:"Send Client Portal Link",engines:["linear"],aiCreatable:!0,description:"Emails the client their portal invite, the same email the planner sends by hand."},{key:"send_proposal",label:"Send Proposal",engines:["linear","graph"],aiCreatable:!0,description:"Drafts a proposal from a proposal template and leaves a task to review and send it. It does NOT email the client: a priced proposal is never delivered unattended."},{key:"send_contract",label:"Send Contract",engines:["linear","graph"],aiCreatable:!0,description:"Drafts a contract from a contract template and leaves a task to review and send it for signature. It does NOT email the client."},{key:"notify_team",label:"Notify Team",engines:["linear","graph"],aiCreatable:!0,description:"Notifies a team member rather than the client."},{key:"add_checklist_task",label:"Add Task to Board",engines:["linear"],aiCreatable:!0,description:"Creates a task on the planning board for this contact's wedding."},{key:"wait",label:"Wait (delay)",engines:["linear","graph"],aiCreatable:!0,description:"Delays every step after it. Linear workflows are day-granular, so a sub-day wait counts as one day; prefer setting a step's own timing where you can."},{key:"condition",label:"If/Then",engines:["linear","graph"],aiCreatable:!1,description:"Branches on contact state. Configured in the step editor, not by the assistant."}];qt.map(a=>a.key);function Ft(a){return qt.filter(i=>i.engines.includes(a))}function Bt({value:a,onChange:i,testId:s,className:o}){const{data:n=[]}=le({queryKey:["/api/team/members-for-assignment"]});return n.length<=1?null:e.jsxs(L,{value:a||"unassigned",onValueChange:l=>i(l==="unassigned"?null:l),children:[e.jsx(K,{className:o||"bg-white/60 dark:bg-background/40 border-0 h-7 text-[12px]","data-testid":s,children:e.jsx(H,{placeholder:"Assign to (optional)"})}),e.jsxs(V,{children:[e.jsx(w,{value:"unassigned",children:"Unassigned"}
1),n.map(l=>e.jsxs(w,{value:l.userId,children:[l.name,l.title?` · ${l.title}`:""]},l.userId))]})]})}function _t(a){if(!a)return null;const i=/^\d{4}-\d{2}-\d{2}$/.test(a)?new Date(a+"T12:00:00"):new Date(a),s=new Date,o=Math.ceil((i.getTime()-s.getTime())/(1e3*60*60*24));return o<0?{text:`${Math.abs(o)}d overdue`,className:"text-red-500"}:o===0?{text:"due today",className:"text-amber-600 dark:text-amber-400"}:o<=3?{text:`due in ${o}d`,className:"text-amber-600 dark:text-amber-400"}:{text:`due in ${o}d`,className:"text-foreground/70"}}function ts(a){if(!a)return null;const i=/^\d{4}-\d{2}-\d{2}$/.test(a)?a+"T12:00:00":a;return new Date(i).toLocaleDateString("en-US",{month:"short",day:"numeric",year:"numeric"})}function as({workflowId:a,onClientClick:i}){const s=re({mutationFn:async({taskId:c,completed:m})=>{await ee("PATCH",`/api/crm/tasks/${c}`,{completed:m})},onSuccess:()=>{U.refetchQueries({queryKey:["/api/crm/workflow-tracker",a]}),U.refetchQueries({queryKey:["/api/crm/client-workflow-progress"]})}}),o=re({mutationFn:async c=>{await ee("POST",`/api/crm/workflow-step/skip/${c}`)},onSuccess:()=>{U.refetchQueries({queryKey:["/api/crm/workflow-tracker",a]}),U.refetchQueries({queryKey:["/api/crm/client-workflow-progress"]})}}),{data:n,isLoading:l}=le({queryKey:["/api/crm/workflow-tracker",a],queryFn:async()=>{const c=await ee("GET",`/api/crm/workflow-tracker/${a}`);if(!c.ok)throw new Error("Failed to fetch");return c.json()}});if(l)return e.jsx("div",{className:"flex items-center justify-center py-12",children:e.jsx(aa,{className:"w-4 h-4 animate-spin text-foreground/65"})});if(!n||n.clients.length===0)return e.jsxs("div",{className:"py-6 text-center flex flex-col items-center gap-1.5",children:[e.jsx(_a,{className:"w-5 h-5 text-green-500/40"}),e.jsx("p",{className:"text-[11px] text-foreground/70 italic",children:"All clients are caught up"})]});const g=[...n.steps].sort((c,m)=>c.sortOrder-m.sortOrder);return e.jsxs("div",{className:"overflow-x-auto","data-testid":`workflow-tracker-${a}`,children:[e.jsxs("table",{className:"w-full min-w-[400px]",children:[e.jsx("thead",{children:e.jsxs("tr",{children:[e.jsx("th",{className:"text-left py-3 pr-4 sticky left-0 bg-background z-10",children:e.jsx("span",{className:"text-[11px] tracking-[0.18em] uppercase text-foreground/70 font-medium",children:"Contact"})}),g.map((c,m)=>{const y=c.type==="automation";return e.jsx("th",{className:"text-center px-2 py-3 min-w-[80px]",children:e.jsxs("div",{className:"flex flex-col items-center gap-1",children:[y&&e.jsx(me,{className:"w-3 h-3 text-amber-500/50"}),e.jsx("span",{className:"text-[10px] text-foreground/60 truncate max-w-[100px] block",children:c.title}),y&&e.jsx("span",{className:"text-[10px] text-amber-500/40 tracking-[0.1em] uppercase",children:"auto"})]})},m)}),e.jsx("th",{className:"text-right py-3 pl-4",children:e.jsx("span",{className:"text-[11px] tracking-[0.18em] uppercase text-foreground/70 font-medium",children:"Progress"})})]})}),e.jsx("tbody",{children:n.clients.map(c=>{const m=g.length>0?Math.round(c.completedSteps.length/g.length*100):0,y=_t(c.nextDueDate),r=ts(c.eventDate);return e.jsxs("tr",{className:"border-t border-border/5",children:[e.jsx("td",{className:"py-3 pr-4 sticky left-0 bg-background z-10",children:e.jsxs("button",{onClick:()=>i?.(c.contactId),className:"hover:text-primary transition-colors text-left","data-testid":`tracker-client-${c.contactId}`,children:[e.jsx("p",{className:"text-[12px] font-medium truncate max-w-[200px]",children:c.contactName}),r&&e.jsxs("p",{className:"text-[11px] text-muted-foreground/70 mt-0.5 flex items-center gap-1",children:[e.jsx($a,{className:"w-2.5 h-2.5"}),r]}),y&&e.jsx("p",{className:`text-[11px] mt-0.5 ${y.className}`,children:y.text})]})}),g.map((x,N)=>{const C=c.completedSteps.includes(x.sortOrder),T=x.type==="automation",$=c.stepTasks?.[x.sortOrder],_=$?.dueDate&&!C?_t($.dueDate):null;return e.jsx("td",{className:"text-center py-3 px-2",children:e.jsxs("button",{className:"flex flex-col items-center gap-0.5 mx-auto cursor-pointer hover:scale-110 transition-transform disabled:opacity-50",disabled:s.isPending&&s.variables?.taskId===$?.taskId||o.isPending&&o.variables===$?.taskId,title:C?"Completed":T?"Click to skip":"Click to complete",onClick:()=>{$?.taskId&&(C?T||s.mutate({taskId:$.taskId,completed:!1}):T?o.mutate($.taskId):s.mutate({taskId:$.taskId,completed:!0}))},"data-testid":`tracker-cell-${c.contactId}-${N}`,children:[C?e.jsx("div",{className:"inline-flex items-center justify-center w-5 h-5 rounded-full bg-green-500/10",children:e.jsx(Ie,{className:"w-3 h-3 text-green-600/70"})}):T?e.jsx("div",{className:"inline-flex items-center justify-center w-5 h-5 rounded-full bg-amber-50 dark:bg-amber-900/20 border border-amber-200/40 dark:border-amber-700/30",children:e.jsx(me,{className:"w-2.5 h-2.5 text-amber-500/50"})}):e.jsx("div",{className:"inline-flex items-center justify-center w-5 h-5 rounded-full border border-border/1
15"}),_&&e.jsx("span",{className:`text-[10px] leading-tight ${_.className}`,children:_.text})]})},N)}),e.jsx("td",{className:"text-right py-3 pl-4",children:e.jsxs("div",{className:"flex items-center justify-end gap-2",children:[e.jsx("div",{className:"w-16 h-1 bg-muted/20 rounded-full overflow-hidden",children:e.jsx("div",{className:`h-full rounded-full transition-all ${m===100?"bg-green-500/60":"bg-primary/40"}`,style:{width:`${m}%`}})}),e.jsxs("span",{className:"text-[11px] text-foreground/70 w-8 text-right",children:[m,"%"]})]})})]},c.contactId)})})]}),e.jsxs("div",{className:"flex items-center gap-4 pt-3 text-[11px] text-foreground/70",children:[e.jsxs("span",{className:"flex items-center gap-1.5",children:[e.jsx(Ie,{className:"w-3 h-3 text-green-600/50"})," Complete"]}),e.jsxs("span",{className:"flex items-center gap-1.5",children:[e.jsx("span",{className:"w-3 h-3 rounded-full border border-border/20 inline-block"})," Pending"]}),e.jsxs("span",{className:"flex items-center gap-1.5",children:[e.jsx("span",{className:"w-3 h-3 rounded-full border border-red-300/40 bg-red-50 dark:bg-red-900/20 inline-flex items-center justify-center text-[6px] text-red-500",children:"!"})," ","Overdue"]}),e.jsxs("span",{className:"flex items-center gap-1.5",children:[e.jsx(me,{className:"w-3 h-3 text-amber-500/50"})," Automation"]}),e.jsx("span",{className:"ml-auto text-foreground/65",children:"Click cells to complete or skip"})]})]})}const ss=Ne.map(a=>({value:a.key,label:a.label})),rs=new Map(Ne.map(a=>[a.key,a]));function ns({step:a,onUpdate:i}){const s=a.automationConfig||{},o=s.timing||"immediate",n=s.timingDays||0,l=s.timingHours||0,g=rs.get(o),c=g?.supportsHours||o==="after_previous_completed"?0:1;return e.jsxs("div",{className:"flex flex-col gap-1",children:[e.jsxs("div",{className:"flex items-center gap-2 flex-wrap",children:[e.jsx(Aa,{className:"w-3 h-3 text-foreground/70 shrink-0"}),e.jsxs(L,{value:o,onValueChange:m=>i({automationConfig:{...s,timing:m,timingDays:m==="immediate"?void 0:m==="after_previous_completed"?0:n||1,timingHours:m==="immediate"?void 0:l||void 0}}),children:[e.jsx(K,{className:"h-7 text-[11px] bg-muted/20 border-0 w-auto min-w-[160px]","data-testid":"select-step-timing",children:e.jsx(H,{})}),e.jsx(V,{children:ss.map(m=>e.jsx(w,{value:m.value,children:m.label},m.value))})]}),o!=="immediate"&&e.jsxs("div",{className:"flex items-center gap-1.5",children:[e.jsx(M,{type:"number",min:c,max:365,value:o==="after_previous_completed"?n??0:c===0?n:n||1,onChange:m=>i({automationConfig:{...s,timing:o,timingDays:parseInt(m.target.value)||0}}),className:"h-7 w-16 text-[11px] bg-muted/20 border-0 text-center","data-testid":"input-step-timing-days"}),e.jsx("span",{className:"text-[10px] text-foreground/70",children:"days"})]}),o!=="immediate"&&g?.supportsHours&&e.jsxs("div",{className:"flex items-center gap-1.5",children:[e.jsx(M,{type:"number",min:0,max:23,value:l,onChange:m=>i({automationConfig:{...s,timing:o,timingHours:parseInt(m.target.value)||0}}),className:"h-7 w-16 text-[11px] bg-muted/20 border-0 text-center","data-testid":"input-step-timing-hours"}),e.jsx("span",{className:"text-[10px] text-foreground/70",children:"hours"})]})]}),g&&g.anchor==="consultation"&&e.jsx("p",{className:"text-[10px] text-foreground/70 pl-5",children:g.description})]})}function is({contacts:a,onClientClick:i}){const{toast:s}=ge(),[,o]=tt(),[n,l]=h.useState(!1),[g,c]=h.useState(null),[m,y]=h.useState(null),[r,x]=h.useState(""),[N,C]=h.useState(null),[T,$]=h.useState(!1),_=h.useRef(null),S=h.useRef(null),f=h.useRef(!1),Q=h.useCallback(()=>{_.current?.scrollIntoView({behavior:"smooth",block:"start"}),_.current?.focus({preventScroll:!0})},[]),q=()=>{E(!0),requestAnimationFrame(Q)},[O,E]=h.useState(()=>typeof window>"u"?!1:new URLSearchParams(window.location.search).get("showTemplates")==="1");h.useEffect(()=>{if(typeof window>"u")return;const d=new URLSearchParams(window.location.search);if(d.get("showTemplates")==="1"){f.current=!0,E(!0),d.delete("showTemplates");const k=d.toString(),D=`${window.location.pathname}${k?`?${k}`:""}${window.location.hash}`;window.history.replaceState(null,"",D)}},[]);const{data:W=[],isLoading:u,isError:v,isFetching:A,refetch:X}=le({queryKey:["/api/crm/client-workflows"]});h.useEffect(()=>{if(!f.current||u||!O)return;const d=requestAnimationFrame(()=>{f.current=!1,Q()});return()=>cancelAnimationFrame(d)},[u,O,Q]);const{data:G}=le({queryKey:["/api/crm/pinned-workflows"]}),J=G?.pinnedWorkflowIds||[],Z=re({mutationFn:async d=>{await ee("DELETE",`/api/crm/client-workflows/${d}`)},onSuccess:()=>{U.refetchQueries({queryKey:["/api/crm/client-workflows"]}),s({title:"Workflow deleted"})},onError:d=>{s({title:"Couldn't delete workflow",description:d.message,variant:"destructive"})}}),ae=re({mutationFn:async d=>{const{id:k,createdAt:D,userId:Y,starterKey:se,...ie}=d;await ee("POST","/api/crm/client-workflows",{...ie,name:`${d.name} (Copy)`,enabled:!1,emailSendMode:"review",emailPreferencePromptDismissed:!1})},onSuccess:()=>{U.refetchQueries({queryKey:["/api/crm/client-workflows"]}),s({title:"Workflow duplicated"})},onError:d=>{s({title:"Couldn't duplicate workflow",description:d.message,variant:"destructive"})}}),j=re({mutationFn:async({workflowId:d,contactId:k})=>(await ee("POST",`/api/crm/client-workflows/${d}/apply`,{contactId:k})).json(),onSuccess:d=>{U.refetchQueries({queryKey:["/api/crm/tasks"]}),U.refetchQueries({queryKey:["/api/crm/client-workflow-progress"]}),U.refetchQueries({queryKey:["/api/crm/dashboard"]}),y(null),x(""),s({title:`Applied "${d.workflow}"`,description:d.summary||`${d.tasks.length} tasks created`})},onError:()=>{s({title:"Failed to apply workflow",variant:"destructive"})}});return e.jsxs("div",{className:"space-y-8",children:[e.jsx(Ca,{title:"How does this work?",description:"Workflows organize your client tasks and follow-ups. Choose what starts each workflow, then decide whether its emails need your review or send automatically.",guideSlug:"workflows",storageKey:"workflows",steps:["Click 'New Workflow' to create an automated sequence.","Choose a trigger (e.g., when a client reaches 'Booked' stage).","Add steps and review the email sending preference. Turn the workflow on when it is ready."]}),e.jsxs("div",{className:"flex flex-col gap-4 sm:flex-row sm:justify-between sm:items-center",children:[e.jsxs("div",{children:[e.jsx("h3",{className:"text-lg font-display font-normal tracking-tight","data-testid":"text-client-workflows-title",children:"Your workflows"}
1),e.jsx("p",{className:"text-[11px] text-foreground/70 mt-1",children:"Manage the workflows you have added or created, then apply them to clients."})]}),e.jsxs("div",{className:"flex flex-wrap items-center gap-3",children:[J.length===0&&W.length>0&&e.jsxs("button",{onClick:()=>$(!0),className:"text-[11px] tracking-[0.15em] uppercase text-foreground/70 hover:text-primary transition-colors flex items-center gap-1","data-testid":"button-pin-workflows",children:[e.jsx(Ze,{className:"w-3 h-3 rotate-45"})," Pin to Dashboard"]}),e.jsxs("button",{ref:S,onClick:q,"aria-expanded":O,"aria-controls":"workflow-template-catalog",className:"text-[11px] tracking-[0.15em] uppercase text-foreground/70 hover:text-primary transition-colors flex items-center gap-1.5","data-testid":"button-browse-templates",children:[e.jsx(Je,{className:"w-3 h-3"})," Add from template"]}),e.jsxs("button",{onClick:()=>o("/crm/workflow/new"),className:"text-[11px] tracking-[0.15em] uppercase text-foreground border-b border-foreground pb-0.5 hover:text-primary hover:border-primary transition-colors flex items-center gap-1.5","data-testid":"button-new-client-workflow",children:[e.jsx(ue,{className:"w-3 h-3"})," New Workflow"]})]})]}),e.jsx(Pa,{}),v&&e.jsxs("div",{role:"alert",className:"space-y-3 rounded-lg border border-border/30 p-4","data-testid":"workflow-library-load-error",children:[e.jsx("p",{className:"text-sm",children:"We couldn't load your saved workflows. Please try again."}),e.jsx(z,{variant:"outline",disabled:A,onClick:()=>{X()},children:"Try again"})]}),u?e.jsx("div",{className:"text-center py-24",children:e.jsx("p",{className:"text-sm text-foreground/70 italic",children:"Loading..."})}):v&&W.length===0?null:W.length===0?e.jsx(Ta,{icon:nt,title:"No custom workflows yet",description:"Start with a ready-made workflow for inquiries, proposals, onboarding, wedding preparation, or follow-ups.",primaryAction:{label:"Add from template",icon:Je,onClick:q,testId:"empty-state-workflows-create"},secondaryAction:{label:"Build my own",icon:ue,onClick:()=>o("/crm/workflow/new")},testId:"text-no-workflows"}):e.jsx("div",{className:"space-y-1",children:W.map(d=>{const k=d.steps||[],D=N===d.id,Y=d.jobType,se=d.triggerStage,ie=d.triggerTags||[];return e.jsxs("div",{className:"border-b border-border/5","data-testid":`workflow-card-${d.id}`,children:[e.jsxs("div",{className:"flex flex-col gap-3 py-5 md:flex-row md:items-center md:gap-4",children:[e.jsxs("button",{type:"button",className:"flex w-full min-w-0 flex-1 items-start gap-3 text-left md:items-center md:gap-4",onClick:()=>C(D?null:d.id),"aria-expanded":D,"aria-controls":`workflow-preview-${d.id}`,"aria-label":`${D?"Hide":"Show"} steps for ${d.name}`,children:[D?e.jsx(ne,{className:"mt-1 h-3.5 w-3.5 shrink-0 text-foreground/70 md:mt-0"}):e.jsx(he,{className:"mt-1 h-3.5 w-3.5 shrink-0 text-foreground/70 md:mt-0"}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsxs("div",{className:"flex items-center gap-2 flex-wrap",children:[e.jsx("p",{className:"w-full break-words text-[13px] font-medium md:w-auto",children:d.name}),e.jsx("span",{className:"w-full text-xs text-muted-foreground md:w-auto","data-testid":`workflow-delivery-status-${d.id}`,children:d.emailSendMode==="automatic"?"Automatic emails":"Review emails before sending"}),d.format==="graph"&&e.jsxs("span",{className:"text-[10px] tracking-[0.15em] uppercase px-1.5 py-0.5 rounded-full bg-blue-500/10 text-blue-700 dark:text-blue-400 font-medium flex items-center gap-1","data-testid":`badge-flow-${d.id}`,children:[e.jsx(Da,{className:"w-2.5 h-2.5"})," Flow"]}),d.category==="drip"&&e.jsxs("span",{className:"text-[10px] tracking-[0.15em] uppercase px-1.5 py-0.5 rounded-full bg-primary/10 text-primary font-medium flex items-center gap-1","data-testid":`badge-drip-${d.id}`,children:[e.jsx(Et,{className:"w-2.5 h-2.5"})," Drip"]}),Y&&Y!=="none"&&e.jsxs("span",{className:"inline-block max-w-full break-words rounded-md bg-primary/10 text-primary px-2 py-1 text-[11px] font-medium leading-relaxed","data-testid":`badge-auto-${d.id}`,children:["Auto: ",Ee.find(F=>F.value===Y)?.label||Y]}),se&&se!=="none"&&e.jsxs("span",{className:"inline-block max-w-full break-words rounded-md bg-amber-500/10 text-amber-700 dark:text-amber-400 px-2 py-1 text-[11px] font-medium leading-relaxed","data-testid":`badge-stage-${d.id}`,children:["Stage: ",xe.find(F=>F.value===se)?.label||se]}),ie.length>0&&e.jsxs("span",{className:"inline-block max-w-full break-words rounded-md bg-vi
1olet-500/10 text-violet-700 dark:text-violet-400 px-2 py-1 text-[11px] font-medium leading-relaxed","data-testid":`badge-tags-${d.id}`,children:["Tags: ",ie.join(", ")]})]}),e.jsx(Xa,{wf:d}),d.description&&e.jsx("p",{className:"mt-2 break-words text-[11px] text-foreground/70",children:Ae(d.description)})]}),e.jsxs("span",{className:"hidden shrink-0 whitespace-nowrap text-[11px] tracking-[0.18em] uppercase text-foreground/70 md:inline",children:[k.length," step",k.length!==1?"s":""]})]}),e.jsxs("div",{className:"ml-[26px] flex flex-wrap items-center justify-between gap-2 md:ml-0 md:shrink-0",children:[e.jsxs("span",{className:"whitespace-nowrap text-[11px] tracking-[0.18em] uppercase text-foreground/70 md:hidden",children:[k.length," step",k.length!==1?"s":""]}),e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx(Za,{wf:d}),e.jsx("button",{onClick:()=>y(d),disabled:d.enabled===!1,className:"flex h-11 w-11 shrink-0 items-center justify-center rounded md:h-auto md:w-auto md:p-1.5 hover:bg-primary/10 text-foreground/70 hover:text-primary transition-colors",title:"Apply to client","data-testid":`button-apply-workflow-${d.id}`,children:e.jsx(Ea,{className:"w-3.5 h-3.5"})}),e.jsx("button",{disabled:ae.isPending&&ae.variables===d,onClick:()=>ae.mutate(d),className:"flex h-11 w-11 shrink-0 items-center justify-center rounded md:h-auto md:w-auto md:p-1.5 hover:bg-muted text-foreground/70 hover:text-foreground transition-colors disabled:opacity-30",title:"Duplicate","data-testid":`button-duplicate-workflow-${d.id}`,children:e.jsx(Ia,{className:"w-3.5 h-3.5"})}),e.jsx("button",{onClick:()=>o(`/crm/workflow/${d.id}`),className:"flex h-11 w-11 shrink-0 items-center justify-center rounded md:h-auto md:w-auto md:p-1.5 hover:bg-muted text-foreground/70 hover:text-foreground transition-colors",title:"Edit","data-testid":`button-edit-workflow-${d.id}`,children:e.jsx(Ra,{className:"w-3.5 h-3.5"})}),e.jsx("button",{onClick:()=>{confirm("Delete this workflow template?")&&Z.mutate(d.id)},className:"flex h-11 w-11 shrink-0 items-center justify-center rounded md:h-auto md:w-auto md:p-1.5 hover:bg-destructive/10 text-foreground/70 hover:text-destructive transition-colors",title:d.starterKey?"Built-in workflows can't be deleted. Use the On/Off switch to turn it off.":"Delete","data-testid":`button-delete-workflow-${d.id}`,disabled:!!d.starterKey||Z.isPending&&Z.variables===d.id,children:e.jsx(Re,{className:"w-3.5 h-3.5"})})]})]})]}),e.jsx(Fa,{workflow:d}),D&&e.jsx("div",{id:`workflow-preview-${d.id}`,className:"pl-10 pb-5 space-y-2",children:k.length===0?e.jsx("p",{className:"text-sm text-muted-foreground",children:"No steps yet. Edit this workflow to add its first step."}):d.category==="drip"?e.jsx(ls,{steps:k}):[...k].sort((F,I)=>F.sortOrder-I.sortOrder).map((F,I)=>e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("span",{className:"text-[10px] text-foreground/65 w-5 text-right",children:I+1}),F.type==="automation"?e.jsx(me,{className:"w-3 h-3 text-amber-500/60 flex-shrink-0"}):e.jsx("div",{className:"w-2 h-2 rounded-full border border-border/20 flex-shrink-0"}),e.jsxs("div",{className:"flex-1",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("p",{className:"text-[12px]",children:F.title}),F.type==="automation"&&F.automationAction&&e.jsx("span",{className:"text-[10px] tracking-[0.1em] uppercase px-1 py-0.5 rounded bg-amber-500/10 text-amber-600/70",children:F.automationAction.replace(/_/g," ")})]}),F.description&&e.jsx("p",{className:"text-[10px] text-foreground/70 mt-0.5",children:F.description})]})]},F.id))})]},d.id)})}),!u&&!v&&W.length===1&&W[0].starterKey==="proposal_follow_up"&&!O&&e.jsx("p",{className:"rounded-lg border border-primary/15 bg-primary/5 p-4 text-sm text-foreground/80",children:"Proposal follow-up is included. Use Add from template for inquiry replies, onboarding, wedding preparation, and wrap-up."}),O&&e.jsxs("div",{id:"workflow-template-catalog",ref:_,tabIndex:-1,className:"space-y-4 scroll-mt-24 outline-none",children:[e.jsx(z,{variant:"ghost",onClick:()=>{f.current=!1,E(!1),S.current?.focus()},children:"Back to saved workflows"}),e.jsx(ma,{})]}),J.length>0&&e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("h3",{className:"text-lg font-display font-normal tracking-tight",children:"Tracker"}),e.jsx("button",{onClick:()=>
1$(!0),className:"p-1.5 rounded hover:bg-muted text-foreground/70 hover:text-foreground transition-colors",title:"Configure pinned workflows","data-testid":"button-configure-pins",children:e.jsx(Oa,{className:"w-3.5 h-3.5"})})]}),J.map(d=>{const k=W.find(D=>D.id===d);return e.jsxs("div",{className:"space-y-2",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(Ze,{className:"w-3 h-3 text-foreground/65 rotate-45"}),e.jsx("span",{className:"text-[11px] font-medium",children:k?.name||`Workflow #${d}`}),k?.jobType&&k.jobType!=="none"&&e.jsx("span",{className:"text-[10px] tracking-[0.15em] uppercase px-1.5 py-0.5 rounded-full bg-primary/10 text-primary",children:Ee.find(D=>D.value===k.jobType)?.label||k.jobType})]}),e.jsx("div",{className:"rounded-lg border border-border/8 p-4",children:e.jsx(as,{workflowId:d,onClientClick:i})})]},d)})]}),e.jsx(Lt,{open:n,onOpenChange:l,workflow:g}),e.jsx(Oe,{open:!!m,onOpenChange:d=>{d||(y(null),x(""))},children:e.jsxs(We,{className:"max-w-md",children:[e.jsx(rt,{children:e.jsx(Pe,{className:"font-display font-normal text-lg",children:"Apply Workflow"})}),e.jsxs("p",{className:"text-sm text-muted-foreground mb-4",children:["Apply â",m?.name,"â to a client. This will create"," ",(m?.steps||[]).length," tasks for them."]}),e.jsxs(L,{value:r,onValueChange:x,children:[e.jsx(K,{"data-testid":"select-apply-contact",children:e.jsx(H,{placeholder:"Select a client..."})}),e.jsx(V,{children:a.map(d=>e.jsx(w,{value:String(d.id),children:d.name},d.id))})]}),e.jsxs("div",{className:"flex justify-end gap-2 mt-4",children:[e.jsx(z,{variant:"outline",onClick:()=>{y(null),x("")},"data-testid":"button-cancel-apply",children:"Cancel"}),e.jsx(ve,{onClick:()=>{m&&r&&j.mutate({workflowId:m.id,contactId:parseInt(r)})},disabled:!r||j.isPending,"data-testid":"button-confirm-apply",children:j.isPending?"Applying...":"Apply Workflow"})]})]})}),e.jsx(os,{open:T,onOpenChange:$,workflows:W,pinnedIds:J})]})}function os({open:a,onOpenChange:i,workflows:s,pinnedIds:o}){const{toast:n}=ge(),[l,g]=h.useState(o);h.useEffect(()=>{a&&g(o)},[a,o]);const c=re({mutationFn:async y=>{await ee("PATCH","/api/crm/pinned-workflows",{pinnedWorkflowIds:y})},onSuccess:()=>{U.refetchQueries({queryKey:["/api/crm/pinned-workflows"]}),i(!1),n({title:"Dashboard trackers updated"})}}),m=y=>{g(r=>r.includes(y)?r.filter(x=>x!==y):r.length<3?[...r,y]:r)};return e.jsx(Oe,{open:a,onOpenChange:i,children:e.jsxs(We,{className:"max-w-sm",children:[e.jsx(rt,{children:e.jsx(Pe,{className:"font-display font-normal text-lg",children:"Pin Workflows"})}),e.jsx("p",{className:"text-[11px] text-foreground/70 mb-3",children:"Select up to 3 workflows to display as tracker grids on this page."}),s.length===0?e.jsx("p",{className:"text-sm text-foreground/70 text-center py-6 italic",children:"Create a workflow first"}):e.jsx("div",{className:"space-y-1 max-h-[300px] overflow-y-auto",children:s.map(y=>e.jsxs("button",{onClick:()=>m(y.id),className:`w-full text-left px-3 py-2.5 rounded-lg transition-colors flex items-center gap-3 ${l.includes(y.id)?"bg-primary/10 text-primary":"hover:bg-muted/50"}`,"data-testid":`pin-workflow-${y.id}`,children:[e.jsx("div",{className:`w-4 h-4 rounded border flex items-center justify-center flex-shrink-0 transition-colors ${l.includes(y.id)?"bg-primary border-primary":"border-border/30"}`,children:l.includes(y.id)&&e.jsx(Ze,{className:"w-2.5 h-2.5 text-primary-foreground rotate-45"})}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsx("p",{className:"text-[12px] font-medium truncate",children:y.name}),e.jsxs("p",{className:"text-[10px] text-foreground/70 truncate",children:[(y.steps||[]).length," steps"]})]})]},y.id))}),e.jsxs("div",{className:"flex justify-between items-center mt-4 pt-4 border-t border-border/10",children:[e.jsxs("span",{className:"text-[10px] text-foreground/70",children:[l.length,"/3 selected"]}),e.jsxs("div",{className:"flex gap-2",children:[e.jsx(z,{variant:"outline",size:"sm",onClick:()=>i(!1),children:"Cancel"}),e.jsx(ve,{size:"sm",onClick:()=>c.mutate(l),disabled:c.isPending,"data-testid":"button-save-pins",children:c.isPending?"Saving...":"Save"})]})]})]})})}function ls({steps:a}){let i=0;return e.jsxs("div",{className:"relative",children:[e.jsx("div",{className:"absolute left-[10px] top-0 bottom-0 w-px bg-primary/10"}),a.sort((s,o)=>s.sortOrder-o.sortOrder).map((s,o)=>
1{if(s.automationAction==="wait")return i+=s.automationConfig?.timingDays??0,e.jsxs("div",{className:"flex items-center gap-3 py-1.5 pl-0",children:[e.jsx("div",{className:"relative z-10 w-[20px] h-[20px] rounded-full border border-primary/15 bg-background flex items-center justify-center",children:e.jsx(Rt,{className:"w-2.5 h-2.5 text-primary"})}),e.jsxs("span",{className:"text-[10px] text-foreground/70 italic",children:["Wait ",s.automationConfig?.timingDays??0," days"]})]},s.id);const n=s.automationAction==="send_email",l=s.automationAction==="condition";return e.jsxs("div",{className:"flex items-center gap-3 py-1.5 pl-0",children:[e.jsx("div",{className:`relative z-10 w-[20px] h-[20px] rounded-full flex items-center justify-center ${n?"bg-primary/10 border border-primary/20":l?"bg-blue-50 dark:bg-blue-900/20 border border-blue-200/30":"bg-muted border border-border/20"}`,children:n?e.jsx(ke,{className:"w-2.5 h-2.5 text-primary"}):l?e.jsx(Me,{className:"w-2.5 h-2.5 text-blue-400"}):e.jsx(me,{className:"w-2.5 h-2.5 text-amber-500/60"})}),e.jsxs("div",{className:"flex items-center gap-2 flex-1",children:[e.jsx("p",{className:"text-[12px]",children:s.title||(n?"Untitled email":s.automationAction?.replace(/_/g," "))}),i>0&&e.jsxs("span",{className:"text-[9px] text-foreground/35",children:["Day ",i]})]})]},s.id)})]})}function ds({steps:a,emailTemplates:i,expandedEmailStep:s,setExpandedEmailStep:o,onUpdateStep:n,onRemoveStep:l,onMoveStep:g,triggerAutoSave:c}){let m=0;const y=a.map(r=>(r.automationAction==="wait"&&(m+=r.automationConfig?.timingDays??0),{...r,dayMarker:m}));return e.jsxs("div",{className:"relative",children:[e.jsx("div",{className:"absolute left-[18px] top-0 bottom-0 w-px bg-primary/15"}),e.jsx("div",{className:"space-y-0",children:y.map((r,x)=>{const N=r.automationAction==="send_email",C=r.automationAction==="send_sms",T=r.automationAction==="wait",$=r.automationAction==="condition",_=r.automationAction==="notify_team",S=r.automationAction==="add_checklist_task";return T?e.jsxs("div",{className:"flex items-center gap-3 py-2 pl-2","data-testid":`drip-wait-${x}`,children:[e.jsx("div",{className:"relative z-10 w-[22px] h-[22px] rounded-full border-2 border-primary/20 bg-background flex items-center justify-center",children:e.jsx(Rt,{className:"w-2.5 h-2.5 text-primary/40"})}),e.jsxs("div",{className:"flex items-center gap-2 flex-1",children:[e.jsx("span",{className:"text-[10px] tracking-[0.15em] uppercase text-foreground/70",children:"Wait"}),e.jsx(M,{type:"number",min:1,max:90,value:r.automationConfig?.timingDays??3,onChange:f=>{const Q=f.target.value===""?1:Math.max(0,Number(f.target.value));n(r.id,{title:`Wait ${Q} days`,automationConfig:{...r.automationConfig,timing:"days_after_applied",timingDays:Q}})},className:"w-14 h-6 text-[11px] text-center bg-muted/30 border-0 rounded","data-testid":`input-drip-wait-days-${x}`}),e.jsx("span",{className:"text-[10px] text-foreground/70",children:"days"})]}),e.jsx("button",{onClick:()=>l(r.id),className:"p-1 rounded hover:bg-destructive/10 text-foreground/70 hover:text-destructive transition-colors","data-testid":`button-remove-drip-step-${x}`,"aria-label":"Remove drip step",children:e.jsx(Re,{className:"w-3 h-3"})})]},r.id):e.jsx("div",{className:"pl-2 py-1.5","data-testid":`drip-step-${x}`,children:e.jsxs("div",{className:"flex items-start gap-3",children:[e.jsxs("div",{className:"relative z-10 flex flex-col items-center",children:[e.jsx("div",{className:`w-[22px] h-[22px] rounded-full flex items-center justify-center ${N?"bg-primary/15 border-2 border-primary/30":$?"bg-blue-100 dark:bg-blue-900/30 border-2 border-blue-300/50":_?"bg-purple-100 dark:bg-purple-900/30 border-2 border-purple-300/50":S?"bg-green-100 dark:bg-green-900/30 border-2 border-green-300/50":"bg-muted border-2 border-border/30"}`,children:N?e.jsx(ke,{className:"w-2.5 h-2.5 text-primary"}):$?e.jsx(Me,{className:"w-2.5 h-2.5 text-blue-500"}):_?e.jsx(It,{className:"w-2.5 h-2.5 text-purple-500"}):S?e.jsx(nt,{className:"w-2.5 h-2.5 text-green-500"}):e.jsx(me,{className:"w-2.5 h-2.5 text-amber-500"})}),x===0&&e.jsx("span",{className:"text-[9px] font-medium text-primary mt-0.5 whitespace-nowrap",children:"Day 0"}),x>0&&r.dayMarker>0&&e.jsxs("span",{className:"text-[9px] font-medium text-foreground
1/70 mt-0.5 whitespace-nowrap",children:["Day ",r.dayMarker]})]}),e.jsxs("div",{className:"flex-1 space-y-1.5 min-w-0",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[!N&&e.jsxs(L,{value:r.automationAction||"",onValueChange:f=>n(r.id,{automationAction:f,automationConfig:{}}),children:[e.jsx(K,{className:"h-7 text-[11px] bg-muted/20 border-0 w-auto min-w-[120px]","data-testid":`select-drip-action-${x}`,children:e.jsx(H,{placeholder:"Action..."})}),e.jsx(V,{children:Ft("linear").map(f=>e.jsx(w,{value:f.key,children:f.label},f.key))})]}),e.jsx(M,{value:r.title,onChange:f=>n(r.id,{title:f.target.value}),placeholder:N?"Email subject or name...":"Step name...",className:"bg-muted/20 border-0 h-7 text-[12px] flex-1","data-testid":`input-drip-step-title-${x}`}),e.jsxs("div",{className:"flex items-center gap-0.5",children:[e.jsx("button",{onClick:()=>g(x,"up"),disabled:x===0,className:"p-0.5 text-foreground/70 hover:text-foreground disabled:opacity-20","data-testid":`button-drip-move-up-${x}`,"aria-label":"Move drip step up",children:e.jsx(ne,{className:"w-3 h-3 rotate-180"})}),e.jsx("button",{onClick:()=>g(x,"down"),disabled:x===a.length-1,className:"p-0.5 text-foreground/70 hover:text-foreground disabled:opacity-20","data-testid":`button-drip-move-down-${x}`,"aria-label":"Move drip step down",children:e.jsx(ne,{className:"w-3 h-3"})}),e.jsx("button",{onClick:()=>l(r.id),className:"p-0.5 text-foreground/70 hover:text-destructive transition-colors","data-testid":`button-remove-drip-step-${x}`,"aria-label":"Remove drip step",children:e.jsx(Re,{className:"w-3 h-3"})})]})]}),N&&e.jsx(Wt,{step:r,emailTemplates:i,expanded:s===r.id,onToggle:()=>o(s===r.id?null:r.id),onUpdate:f=>n(r.id,{automationConfig:f})}),C&&e.jsx(Pt,{step:r,expanded:s===r.id,onToggle:()=>o(s===r.id?null:r.id),onUpdate:f=>n(r.id,{automationConfig:f})}),$&&e.jsxs("div",{className:"space-y-1.5 rounded border border-blue-200/20 bg-blue-50/20 dark:bg-blue-900/5 p-2",children:[e.jsxs(L,{value:r.automationConfig?.conditionType||"",onValueChange:f=>n(r.id,{automationConfig:{...r.automationConfig,conditionType:f}}),children:[e.jsx(K,{className:"bg-white/60 dark:bg-background/40 border-0 h-7 text-[11px]","data-testid":`select-drip-condition-${x}`,children:e.jsx(H,{placeholder:"If..."})}),e.jsxs(V,{children:[e.jsx(w,{value:"email_opened",children:"Email was opened"}),e.jsx(w,{value:"email_not_opened",children:"Email was NOT opened"}),e.jsx(w,{value:"has_tag",children:"Contact has tag"}),e.jsx(w,{value:"is_booked",children:"Is booked"})]})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("span",{className:"text-[10px] text-blue-500/60",children:"If false, skip"}),e.jsxs(L,{value:String(r.automationConfig?.skipSteps||"1"),onValueChange:f=>n(r.id,{automationConfig:{...r.automationConfig,skipSteps:f}}),children:[e.jsx(K,{className:"bg-white/60 dark:bg-background/40 border-0 h-6 text-[11px] w-20","data-testid":`select-drip-skip-${x}`,children:e.jsx(H,{})}),e.jsx(V,{children:[1,2,3,4,5].map(f=>e.jsxs(w,{value:String(f),children:[f," step",f>1?"s":""]},f))})]})]})]}),_&&e.jsx("div",{className:"space-y-1.5 rounded border border-purple-200/20 bg-purple-50/20 dark:bg-purple-900/5 p-2",children:e.jsx(M,{value:r.automationConfig?.title||"",onChange:f=>n(r.id,{automationConfig:{...r.automationConfig,title:f.target.value}}),placeholder:"Notification title...",className:"bg-white/60 dark:bg-background/40 border-0 h-6 text-[11px]","data-testid":`input-drip-notify-title-${x}`})}),S&&e.jsxs("div",{className:"space-y-1.5 rounded border border-green-200/20 bg-green-50/20 dark:bg-green-900/5 p-2",children:[e.jsx(M,{value:r.automationConfig?.title||"",onChange:f=>n(r.id,{automationConfig:{...r.automationConfig,title:f.target.value}}),placeholder:"Task title (e.g., Send {name}'s welcome gift)",className:"bg-white/60 dark:bg-background/40 border-0 h-6 text-[11px]","data-testid":`input-drip-checklist-title-${x}`}),e.jsx(M,{value:r.automationConfig?.notes||"",onChange:f=>n(r.id,{automationConfig:{...r.automationConfig,notes:f.target.value}}),placeholder:"Notes (optional)",className:"bg-white/60 dark:bg-background/40 border-0 h-6 text-[11px]","data-testid":`input-drip-checklist-notes-${x}`}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs(L,{value:r.automationConfig?.listType||"planner",onValueChange:f=>n(r.id,{automationConfig:{...r.automationConfig,listType:f}}),children:[e.jsx(K,{className:"bg-white/60 dark:bg-background/40 border-0 h-6 text-[11px] w-28","data-testid":`select-drip-checklist-listtype-${x}`,children:e.jsx(H,{})}),e.jsxs(V,{children:[e.jsx(w,{value:"planner",children:"Planner list"}),e.jsx(w,{value:"photographer",children:"Photographer list"}),e.jsx(w,{value:"couple",children:"Couple list"})]})]}
1),e.jsx(M,{type:"number",value:r.automationConfig?.dueDays||"7",onChange:f=>n(r.id,{automationConfig:{...r.automationConfig,dueDays:f.target.value}}),className:"bg-white/60 dark:bg-background/40 border-0 h-6 text-[11px] w-14 text-center","data-testid":`input-drip-checklist-duedays-${x}`}),e.jsx("span",{className:"text-[10px] text-foreground/70",children:"days"})]}),e.jsx(Bt,{value:r.automationConfig?.assignedTo,onChange:f=>n(r.id,{automationConfig:{...r.automationConfig,assignedTo:f||void 0}}),testId:`select-drip-checklist-assignee-${x}`,className:"bg-white/60 dark:bg-background/40 border-0 h-6 text-[11px]"})]})]})]})},r.id)})}),e.jsxs("div",{className:"mt-3 flex items-center gap-2 pl-2",children:[e.jsx("div",{className:"relative z-10 w-[22px] h-[22px] rounded-full border-2 border-dashed border-primary/20 bg-background flex items-center justify-center",children:e.jsx(ue,{className:"w-2.5 h-2.5 text-primary"})}),e.jsx("div",{className:"flex items-center gap-2",children:e.jsx("span",{className:"text-[10px] text-foreground/35",children:'Use "+ Add Email" above to add more steps'})})]})]})}function Lt({open:a,onOpenChange:i,workflow:s,page:o=!1,onAdvanced:n,initialAdvancedOptions:l=!1}){const{toast:g}=ge(),c=!!s,[m,y]=h.useState(l),r=o&&!m,[x,N]=h.useState(c?1:0),[C,T]=h.useState(c),[$,_]=h.useState(null);function S(t){t===2&&!jt()||(N(t),requestAnimationFrame(()=>document.querySelector('[data-testid="workflow-simple-editor"]')?.scrollIntoView({block:"start",behavior:"smooth"})))}const[f,Q]=h.useState(s?.name||""),[q,O]=h.useState(Ae(s?.description)),[E,W]=h.useState(Ge(s?.description)),[u,v]=h.useState(s?.category||"job"),[A,X]=h.useState(s?.jobType||""),[G,J]=h.useState(s?.triggerStage||""),[Z,ae]=h.useState(s?s.enabled!==!1:!1),[j,d]=h.useState(s?.emailSendMode==="automatic"?"automatic":"review"),[k,D]=h.useState(s?.triggerTags||[]),[Y,se]=h.useState(s?.triggerEvent||""),[ie,F]=h.useState(""),[I,te]=h.useState(()=>je(s?.steps)),[fe,Ce]=h.useState(null),qe=h.useRef(!1),it=JSON.stringify([f,q,u,A,G,Y,k,Z,j,I]),Kt=h.useRef(it),Se=o&&it!==Kt.current;At(Se);const Ht=()=>{(!Se||window.confirm("Discard your unsaved workflow changes?"))&&i(!1)},ot=h.useRef(f);ot.current=f;const lt=h.useRef(q);lt.current=q;const dt=h.useRef(E);dt.current=E;const ct=h.useRef(u);ct.current=u;const Te=h.useRef(A);Te.current=A;const _e=h.useRef(G);_e.current=G;const mt=h.useRef(Z);mt.current=Z;const ut=h.useRef(j);ut.current=j;const Fe=h.useRef(k);Fe.current=k;const $e=h.useRef(Y);$e.current=Y;const Be=h.useRef(I);Be.current=I;const pt=h.useRef(s?.id??null);pt.current=s?.id??null;const pe=Na({onSave:async()=>{const t=pt.current;if(!t)return;const b=Be.current.filter(p=>p.title.trim());await ee("PATCH",`/api/crm/client-workflows/${t}`,{name:ot.current.trim(),description:lt.current.trim()+dt.current,category:ct.current,enabled:mt.current,emailSendMode:ut.current,jobType:Te.current&&Te.current!=="none"?Te.current:null,triggerStage:_e.current&&_e.current!=="none"?_e.current:null,triggerTags:Fe.current.length>0?Fe.current:null,triggerEvent:$e.current&&$e.current!=="none"?$e.current:null,steps:b.length>0?b:Be.current},{noRedirectOn401:!0}),U.refetchQueries({queryKey:["/api/crm/client-workflows"]})},enabled:c&&a&&!o,debounceMs:2e3,onError:()=>{g({title:"Auto-save failed",description:"Your workflow changes couldn't be saved. Please try saving manually.",variant:"destructive"})}}),B=h.useCallback(()=>{c&&!o&&qe.current&&pe.trigger()},[c,o,pe]),xt=h.useCallback(()=>{const t=ie.trim();t&&(k.includes(t)||(D(b=>[...b,t]),B()),F(""))},[ie,k,B]),ht=h.useRef(s),Le=h.useRef(pe);ht.current=s,Le.current=pe,h.useEffect(()=>{const t=ht.current;a?(Le.current.cancel(),qe.current=!1,Q(t?.name||""),O(Ae(t?.description)),W(Ge(t?.description)),v(t?.category||"job"),ae(t?t.enabled!==!1:!1),d(t?.emailSendMode==="automatic"?"automatic":"review"),X(t?.jobType||""),J(t?.triggerStage||""),D(t?.triggerTags||[]),se(t?.triggerEvent||""),F(""),te(je(t?.steps)),Ce(null),setTimeout(()=>{qe.current=!0},100)):Le.current.cancel()},[a,s?.id]);const{data:Ke=[]}=le({queryKey:["/api/crm/email-templates"]}),{data:Vt=[]}=le({queryKey:["/api/questionnaire-templates"]}),gt=()=>
1{Q(""),O(""),W(""),v("job"),ae(!1),d("review"),X(""),J(""),D([]),F(""),te([])},He=re({mutationFn:async t=>{c?await ee("PATCH",`/api/crm/client-workflows/${s.id}`,t):await ee("POST","/api/crm/client-workflows",t)},onSuccess:()=>{U.refetchQueries({queryKey:["/api/crm/client-workflows"]}),U.refetchQueries({queryKey:["/api/crm/contacts"]}),U.refetchQueries({queryKey:["/api/crm/workflow-tracker"]}),U.refetchQueries({queryKey:["/api/crm/client-workflow-progress"]}),i(!1),gt(),g({title:c?"Workflow updated":"Workflow created"})},onError:()=>{g({title:"Failed to save workflow",variant:"destructive"})}});function ft(){te(t=>[...t,{id:`step-${Date.now()}-${Math.random().toString(36).slice(2,6)}`,title:"",description:"",sortOrder:t.length,type:"task"}]),B()}function R(t,b){te(p=>p.map(P=>P.id===t?{...P,...b}:P)),B()}function bt(t){te(b=>b.filter(p=>p.id!==t).map((p,P)=>({...p,sortOrder:P}))),B()}function Ve(t,b){te(p=>{const P=[...p],oe=b==="up"?t-1:t+1;return oe<0||oe>=P.length?p:([P[t],P[oe]]=[P[oe],P[t]],P.map((be,ze)=>({...be,sortOrder:ze})))}),B()}function zt(t,b){t===b||t<0||b<0||t>=I.length||b>=I.length||te(p=>{const P=[...p];return P.splice(b,0,P.splice(t,1)[0]),P.map((oe,be)=>({...oe,sortOrder:be}))})}function yt(t,b){const p={task:"New task",send_email:"Email your client",wait:"Wait",send_questionnaire:"Send a questionnaire",send_portal_link:"Share the client portal"},P={id:crypto.randomUUID(),title:p[b]||"New step",type:b==="task"?"task":"automation",sortOrder:t,...b==="task"?{}:{automationAction:b},automationConfig:b==="wait"?{timingDays:3}:{timing:"immediate"}};_(P.id),te(oe=>[...oe.slice(0,t),P,...oe.slice(t)].map((be,ze)=>({...be,sortOrder:ze})))}function jt(){const t=I.findIndex(b=>!b.title.trim());return t<0?!0:(g({title:`Give step ${t+1} a name before continuing`,variant:"destructive"}),N(1),!1)}function Qt(){if(o&&!jt())return;if(!f.trim()){g({title:"Name is required",variant:"destructive"});return}const t=u==="drip"?I.map(b=>({...b,title:b.title.trim()||(b.automationAction==="wait"?`Wait ${b.automationConfig?.timingDays??0} days`:b.automationAction==="send_email"?"Email":b.automationAction?.replace(/_/g," ")||"Step")})):I.filter(b=>b.title.trim());if(t.length===0){g({title:u==="drip"?"Add at least one email":"Add at least one step",variant:"destructive"});return}He.mutate({name:f.trim(),description:q.trim()+E,category:u,enabled:Z,emailSendMode:j,jobType:A&&A!=="none"?A:null,triggerStage:G&&G!=="none"?G:null,triggerTags:k.length>0?k:null,triggerEvent:Y&&Y!=="none"?Y:null,steps:t})}return e.jsxs(Ga,{page:o,isEdit:c,advancedOptions:m,onAdvancedOptionsChange:o?t=>{!t&&(f.trim()||I.length)&&T(!0),y(t)}:void 0,onAdvanced:n?()=>{(!Se||window.confirm("Open branching builder? Unsaved steps will be discarded."))&&n()}:void 0,open:a,onOpenChange:t=>{!t&&Se&&!window.confirm("Discard your unsaved workflow changes?")||(i(t),t&&s?(Q(s.name),O(Ae(s.description)),W(Ge(s.description)),v(s.category||"job"),ae(s.enabled!==!1),d(s.emailSendMode==="automatic"?"automatic":"review"),X(s.jobType||""),J(s.triggerStage||""),D(s.triggerTags||[]),se(s.triggerEvent||""),F(""),te(je(s.steps))):t&&!s&>())},children:[e.jsxs("div",{className:o?"space-y-8":"space-y-4 mt-2 overflow-y-auto flex-1 pr-1",children:[r&&e.jsx(Ka,{stage:x,started:C,onChange:S,canBuild:C&&!!f.trim(),canReview:!!f.trim()&&I.length>0}),r&&x===0&&!C&&e.jsx(Va,{onBlank:()=>{T(!0),S(0)},onChoose:t=>{Q(t.name),O(t.description),J(t.triggerStage),te(je(t.steps)),T(!0),S(1)}}),e.jsxs("div",{hidden:r&&(x!==0||!C),className:o?"space-y-5 rounded-2xl border border-primary/15 bg-card p-5 sm:p-7 shadow-sm":"space-y-4",children:[o&&e.jsxs("div",{children:[e.jsx("p",{className:"text-sm font-semibold text-primary",children:"Make it yours"}),e.jsx("h2",{className:"mt-1 font-display text-2xl font-normal",children:"Give your workflow a name"}),e.jsx("p",{className:"mt-2 text-sm text-muted-foreground",children:"Then choose when it should start. You can also start it manually."})]}),!o&&e.jsx(e.Fragment,{children:e.jsx("div",{className:"flex items-center gap-2 mb-2",children:e.jsxs("div",{className:"flex gap-2 border-b border-border/20",children:[e.jsxs("button",{type:"button",onClick:()=>{v("job"),B()},className:`text-[11px] tracking-[0.1em] uppercase px-3 py-1.5 flex items-center gap-1.5 transition-colors ${u==="job"?"border-b-2 border-foreground font-medium":"text-muted-foreground"}`,"data-testid":"button-category-job",children:[e.jsx(Xe,{className:"w-3 h-3"})," Workflow"]}),e.jsxs("button",{type:"button",onClick:()=>{v("drip"),B()},className:`text-[11px] tracking-[0.1em] uppercase px-3 py-1.5 flex items-center gap-1.5 transition-colors ${u==="drip"?"border-b-2 border-primary font-medium":"text-muted-foreground"}`,"data-testid":"button-category-drip",children:[e.jsx(Et,{className:"w-3 h-3"})," Drip Campaign"]})]})})}),e.jsxs("div",{children:[e.jsx("label",{htmlFor:"workflow-name",className:"text-sm font-medium mb-2 block",children:"Workflow name"}),e.jsx(M,{value:f,onChange:t=>{Q(t.target.value),B()},placeholder:u==="drip"?"e.g., New Booking Welcome Sequence":"e.g., Wedding Photography Workflow",className:"h-12 border border-primary/25 bg-background text-base md:text-base",id:"workflow-name","data-testid":"input-workflow-name"})]}),e.jsxs("details",{open:m||void 0,children:[e.jsx(kt,{className:"text-sm text-muted-foreground",children:"Description (optional)"}),e.jsx(Sa,{value:q,onChange:t=>{O(t.target.value),B()},placeholder:u==="drip"?"Describe your email sequence...":"Optional description...",rows:2,className:"bg-muted/30 border-0 resize-none","aria-label":"Workflow description","data-testid":"input-workflow-description"})]}),e.jsxs("details",{open:m||!o?!0:void 0,className:"rounded-xl border border-primary/15 bg-card p-4",children:[e.jsx(kt,{className:"text-base font-medium",children:e.jsxs("span",{children:["When it starts:"," ",G?xe.find(t=>t.value===G)?.label||G:Y?"A consultation is booked":A?"A matching job is created":k.length?"Contact tags match":"I start it manually"]})}),e.jsxs("div",{className:"mt-4 space-y-4",children:[e.jsxs("div",{children:[e.jsx("label",{className:"text-sm font-medium mb-2 block",children:"When a job is created"}),e.jsxs(L,{value:A||"none",onValueChange:t=>{X(t==="none"?"":t),B()},children:[e.jsx(K,{className:"bg-background border border-primary/20 h-11 text-sm","data-testid":"select-workflow-job-type",children:e.jsx(H,{placeholder:"None"})}),e.jsxs(V,{children:[e.jsx(w,{value:"none",children:"None"}),Ee.map(t=>e.jsx(w,{value:t.value,children:t.label},t.value))]})]})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-sm font-medium mb-2 block",children:"When a contact moves to stage"}),e.jsxs(L,{value:G||"none",onValueChange:t=>{J(t==="none"?"":t),B()},children:[e.jsx(K,{className:"bg-background border border-primary/20 h-11 text-sm","data-testid":"select-workflow-trigger-stage",children:e.jsx(H,{placeholder:"None"})}),e.jsxs(V,{children:[e.jsx(w,{value:"none",children:"None"}),xe.map(t=>e.jsx(w,{value:t.value,children:t.label},t.value))]})]})]}),e.jsxs("div",{children:[e.jsxs("label",{className:"text-sm font-medium mb-2 block",children:["Or when this happens"," ",e.jsx("span",{className:"normal-case tracking-normal text-muted-foreground
1/70",children:"(optional)"})]}),e.jsxs(L,{value:Y||"none",onValueChange:t=>{se(t==="none"?"":t),B()},children:[e.jsx(K,{className:"bg-background border border-primary/20 h-11 text-sm","data-testid":"select-workflow-trigger-event",children:e.jsx(H,{placeholder:"None"})}),e.jsxs(V,{children:[e.jsx(w,{value:"none",children:"None"}),Mt.map(t=>e.jsx(w,{value:t.key,children:t.label},t.key))]})]})]}),e.jsxs("div",{children:[e.jsxs("label",{className:"text-sm font-medium mb-2 block",children:["When contact has tag(s)"," ",e.jsx("span",{className:"normal-case tracking-normal text-muted-foreground/70",children:"(optional)"})]}),e.jsx("div",{className:"flex flex-wrap gap-1.5 mb-2",children:k.map(t=>e.jsxs("span",{className:"inline-flex items-center gap-1 text-[11px] bg-primary/10 text-primary px-2 py-0.5 rounded-full",children:[t,e.jsx("button",{onClick:()=>{D(b=>b.filter(p=>p!==t)),B()},className:"hover:text-red-500 transition-colors","data-testid":`btn-remove-trigger-tag-${t}`,children:"Ã"})]},t))}),e.jsx("div",{className:"flex gap-2",children:e.jsx(M,{value:ie,onChange:t=>F(t.target.value),onKeyDown:t=>{t.key==="Enter"&&ie.trim()&&(t.preventDefault(),xt())},onBlur:xt,placeholder:"Type a tag and press Enter...",className:"bg-background border border-primary/20 h-11 text-sm flex-1","data-testid":"input-trigger-tag"})})]}),e.jsx("p",{className:"text-[13px] text-muted-foreground leading-relaxed",children:"Stage and contact tags work together. A job type starts this workflow independently when that job is created. Leave these blank to start it manually."})]})]})]}),e.jsxs("div",{hidden:r&&x!==1,className:"space-y-4",children:[r&&e.jsx(za,{name:f,start:G?xe.find(t=>t.value===G)?.label||G:Y?"A consultation is booked":A?"A matching job is created":k.length?"Contact tags match":"I start it manually",onEdit:()=>S(0)}),e.jsxs("div",{className:"flex items-center justify-between mb-3",children:[e.jsx("label",{className:"text-[12px] tracking-[0.18em] uppercase text-muted-foreground",children:o?"What happens next":u==="drip"?"Email Sequence":"Steps"}),e.jsxs("div",{className:o?"hidden":"flex items-center gap-2",children:[u==="drip"&&I.length===0&&e.jsxs("button",{onClick:()=>{const t=La();te(t),B()},className:"text-[10px] tracking-[0.12em] uppercase text-primary hover:text-primary transition-colors flex items-center gap-1","data-testid":"button-use-drip-template",children:[e.jsx(st,{className:"w-3 h-3"})," Use Template"]}),e.jsxs("button",{onClick:()=>{u==="drip"?te(t=>[...t,{id:`step-${Date.now()}-w`,title:`Wait ${t.length>0?"3":"0"} days`,sortOrder:t.length,type:"automation",automationAction:"wait",automationConfig:{timing:"days_after_applied",timingDays:t.length>0?3:0}},{id:`step-${Date.now()}-e`,title:"",sortOrder:t.length+1,type:"automation",automationAction:"send_email",automationConfig:{timing:"days_after_applied",timingDays:t.length>0?3:0}}]):ft(),B()},className:"text-[10px] tracking-[0.15em] uppercase text-primary hover:text-primary/80 transition-colors flex items-center gap-1","data-testid":"button-add-step",children:[e.jsx(ue,{className:"w-3 h-3"})," ",u==="drip"?"Add Email":"Add Step"]})]})]}),o&&e.jsx("p",{className:"text-sm text-muted-foreground mb-3",children:"Each step uses its own timing. Add a wait to delay the steps after it."}),o&&e.jsx(Tt,{index:0,onAdd:yt,prominent:I.length===0}),I.length===0&&o?e.jsx("p",{className:"py-6 text-center text-sm text-muted-foreground",children:"Choose Add step to create a task, prepare an email, or add a wait."}):I.length===0?e.jsxs("div",{className:"text-center py-8 border border-dashed border-border/15 rounded-lg",children:[e.jsx("p",{className:"text-xs text-foreground/70",children:u==="drip"?"No emails in sequence yet":"No steps yet"}),e.jsx("button",{onClick:()=>{u==="drip"?te([{id:`step-${Date.now()}-e`,title:"",sortOrder:0,type:"automation",automationAction:"send_email",automationConfig:{timing:"immediate"}}]):ft(),B()},className:"text-xs text-primary mt-1 hover:underline","data-testid":"button-add-first-step",children:u==="drip"?"Add your first email":"Add your first step"})]}):u==="drip"&&!o?e.jsx(ds,{steps:I,emailTemplates:Ke,expandedEmailStep:fe,setExpandedEmailStep:Ce,onUpdateStep:R,onRemoveStep:bt,onMoveStep:Ve,triggerAutoSave:B}):e.jsx("div",{className:"space-y-3",children:I.map((t,b)=>e.jsxs(h.Fragment,{children:[e.jsx(Ua,{step:t,index:b,total:I.length,emailSendMode:j,onMoveTo:zt,simple:o,forceExpanded:m,openOnAdd:t.id===$,priorWaitDays:I.slice(0,b).reduce((p,P)=>p+(P.automationAction==="wait"&&Number(P.automationConfig?.timingDays)||0),0),children:e.jsx("div",{className:o?"pt-4":"rounded-lg border border-border/8 p-3","data-testid":`workflow-step-${b}`,children:e.jsxs("div",{className:"flex items-start gap-2",children:[e.jsxs("div",{className:o?"hidden":"flex flex-col items-center pt-1 gap-0.5",children:[e.jsx("button",{onClick:()=>Ve(b,"up"),disabled:b===0,className:"text-muted-foreground hover:text-foreground disabled:opacity-30 transition-colors","aria-label":"Move step up",children:e.jsx(ne,{className:"w-3 h-3 rotate-180"})}),e.jsx(Dt,{className:"w-3 h-3 text-muted-foreground"}),e.jsx("button",{onClick:()=>Ve(b,"down"),disabled:b===I.length-1,className:"text-muted-foreground hover:text-foreground disabled:opacity-30 transition-colors","aria-label":"Move step down",children:e.jsx(ne,{className:"w-3 h-3"})})]}
1),e.jsx("span",{className:"text-[10px] text-foreground/65 pt-1.5 w-4 text-right",children:b+1}),e.jsxs("div",{className:"flex-1 space-y-2",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs("div",{className:"flex gap-2 border-b border-border/20",children:[e.jsxs("button",{onClick:()=>R(t.id,{type:"task"}),className:`text-[11px] tracking-[0.1em] uppercase px-2 py-1 flex items-center gap-1 transition-colors ${(t.type||"task")==="task"?"bg-foreground text-background":"hover:bg-muted/50"}`,"data-testid":`button-step-type-task-${b}`,children:[e.jsx(Xe,{className:"w-2.5 h-2.5"})," Task"]}),e.jsxs("button",{onClick:()=>R(t.id,{type:"automation"}),className:`text-[11px] tracking-[0.1em] uppercase px-2 py-1 flex items-center gap-1 transition-colors ${t.type==="automation"?"border-b-2 border-primary font-medium":"text-muted-foreground"}`,"data-testid":`button-step-type-automation-${b}`,children:[e.jsx(me,{className:"w-2.5 h-2.5"})," Auto"]})]}),e.jsx(M,{value:t.title,onChange:p=>R(t.id,{title:p.target.value}),placeholder:t.type==="automation"?"e.g., Send welcome email":"e.g., Send Contract",className:"bg-muted/30 border-0 h-8 text-[13px] flex-1","data-testid":`input-step-title-${b}`})]}),t.type==="automation"&&e.jsxs(L,{value:t.automationAction||"",onValueChange:p=>R(t.id,{automationAction:p,automationConfig:{}}),children:[e.jsx(K,{className:"bg-amber-50/50 dark:bg-amber-900/10 border-amber-200/30 dark:border-amber-700/20 h-8 text-[12px]","data-testid":`select-automation-action-${b}`,children:e.jsx(H,{placeholder:"Select action..."})}),e.jsx(V,{children:Ft("linear").map(p=>e.jsx(w,{value:p.key,children:p.label},p.key))})]}),o&&t.automationAction==="wait"&&e.jsxs("label",{className:"block text-sm",children:["Wait this many days",e.jsx(M,{"aria-label":"Wait in days",type:"number",min:0,step:"any",value:t.automationConfig?.timingDays??0,onChange:p=>R(t.id,{automationConfig:{...t.automationConfig,timingDays:Math.max(0,Number(p.target.value)||0)}})})]}),t.type==="automation"&&t.automationAction==="send_email"&&e.jsx(Wt,{step:t,emailTemplates:Ke,expanded:fe===t.id,onToggle:()=>Ce(fe===t.id?null:t.id),onUpdate:p=>R(t.id,{automationConfig:p})}),t.type==="automation"&&t.automationAction==="send_sms"&&e.jsx(Pt,{step:t,expanded:fe===t.id,onToggle:()=>Ce(fe===t.id?null:t.id),onUpdate:p=>R(t.id,{automationConfig:p})}),t.type==="automation"&&t.automationAction==="send_questionnaire"&&e.jsxs("div",{className:"space-y-2",children:[e.jsxs(L,{value:String(t.automationConfig?.templateId||""),onValueChange:p=>R(t.id,{automationConfig:{...t.automationConfig,templateId:parseInt(p)}}),children:[e.jsx(K,{className:"bg-amber-50/50 dark:bg-amber-900/10 border-amber-200/30 dark:border-amber-700/20 h-8 text-[12px]","data-testid":`select-questionnaire-template-${b}`,children:e.jsx(H,{placeholder:"Select questionnaire template..."})}),e.jsx(V,{children:Vt.map(p=>e.jsx(w,{value:String(p.id),children:p.name},p.id))})]}),e.jsxs(L,{value:t.automationConfig?.emailTemplateId?String(t.automationConfig.emailTemplateId):"default",onValueChange:p=>R(t.id,{automationConfig:{...t.automationConfig,emailTemplateId:p==="default"?null:parseInt(p)}}),children:[e.jsx(K,{className:"bg-amber-50/50 dark:bg-amber-900/10 border-amber-200/30 dark:border-amber-700/20 h-8 text-[12px]","data-testid":`select-questionnaire-email-template-${b}`,children:e.jsx(H,{})}),e.jsxs(V,{children:[e.jsx(w,{value:"default",children:"Invitation email: standard wording"}),Ke.map(p=>e.jsxs(w,{value:String(p.id),children:["Invitation email: ",p.name]},p.id))]})]}),e.jsxs("p",{className:"text-[10px] text-muted-foreground",children:["Put ","{questionnaire_link}"," in your template where the button should go. If it is missing, one is added at the end."]})]}),t.type==="automation"&&t.automationAction==="notify_team"&&e.jsxs("div",{className:"space-y-2 rounded border border-purple-200/30 dark:border-purple-700/20 bg-purple-50/30 dark:bg-purple-900/10 p-2.5",children:[e.jsxs("div",{className:"flex items-center gap-1.5 mb-1",children:[e.jsx(It,{className:"w-3 h-3 text-purple-500"}),e.jsx("span",{className:"text-[10px] tracking-[0.1em] uppercase text-purple-600 dar
1k:text-purple-400",children:"Notify your team"})]}),e.jsx(M,{value:t.automationConfig?.title||"",onChange:p=>R(t.id,{automationConfig:{...t.automationConfig,title:p.target.value}}),placeholder:"Notification title (e.g., New booking for {name})",className:"bg-white/60 dark:bg-background/40 border-0 h-7 text-[12px]","data-testid":`input-notify-title-${b}`}),e.jsx(M,{value:t.automationConfig?.message||"",onChange:p=>R(t.id,{automationConfig:{...t.automationConfig,message:p.target.value}}),placeholder:"Message (e.g., {name} has been booked â send welcome packet)",className:"bg-white/60 dark:bg-background/40 border-0 h-7 text-[12px]","data-testid":`input-notify-message-${b}`}),e.jsxs("p",{className:"text-[10px] text-purple-500/60",children:["All team members will receive an in-app notification. Use ","{name}",", ","{email}",","," ","{venue}"," placeholders."]})]}),t.type==="automation"&&t.automationAction==="add_checklist_task"&&e.jsxs("div",{className:"space-y-2 rounded border border-green-200/30 dark:border-green-700/20 bg-green-50/30 dark:bg-green-900/10 p-2.5",children:[e.jsxs("div",{className:"flex items-center gap-1.5 mb-1",children:[e.jsx(nt,{className:"w-3 h-3 text-green-500"}),e.jsx("span",{className:"text-[10px] tracking-[0.1em] uppercase text-green-600 dark:text-green-400",children:"Add task to board"})]}),e.jsx(M,{value:t.automationConfig?.title||"",onChange:p=>R(t.id,{automationConfig:{...t.automationConfig,title:p.target.value}}),placeholder:"Task title (e.g., Send {name}'s welcome gift)",className:"bg-white/60 dark:bg-background/40 border-0 h-7 text-[12px]","data-testid":`input-checklist-title-${b}`}),e.jsx(M,{value:t.automationConfig?.notes||"",onChange:p=>R(t.id,{automationConfig:{...t.automationConfig,notes:p.target.value}}),placeholder:"Notes (optional)",className:"bg-white/60 dark:bg-background/40 border-0 h-7 text-[12px]","data-testid":`input-checklist-notes-${b}`}),e.jsxs("div",{className:"flex gap-2",children:[e.jsxs(L,{value:t.automationConfig?.listType||"planner",onValueChange:p=>R(t.id,{automationConfig:{...t.automationConfig,listType:p}}),children:[e.jsx(K,{className:"bg-white/60 dark:bg-background/40 border-0 h-7 text-[12px] flex-1","data-testid":`select-checklist-listtype-${b}`,children:e.jsx(H,{})}),e.jsxs(V,{children:[e.jsx(w,{value:"planner",children:"Planner list"}),e.jsx(w,{value:"photographer",children:"Photographer list"}),e.jsx(w,{value:"couple",children:"Couple list"})]})]}),e.jsx(M,{type:"number",value:t.automationConfig?.dueDays||"7",onChange:p=>R(t.id,{automationConfig:{...t.automationConfig,dueDays:p.target.value}}),placeholder:"Due in days",className:"bg-white/60 dark:bg-background/40 border-0 h-7 text-[12px] w-24","data-testid":`input-checklist-duedays-${b}`}),e.jsx("span",{className:"text-[10px] text-muted-foreground self-center",children:"days"})]}),e.jsx(Bt,{value:t.automationConfig?.assignedTo,onChange:p=>R(t.id,{automationConfig:{...t.automationConfig,assignedTo:p||void 0}}),testId:`select-checklist-assignee-${b}`}),e.jsxs("p",{className:"text-[10px] text-green-500/60",children:["Creates a task on the wedding board. Use ","{name}",", ","{email}",", ","{venue}"," ","placeholders in title/notes."]})]}),t.type==="automation"&&t.automationAction==="condition"&&e.jsxs("div",{className:"space-y-2 rounded border border-blue-200/30 dark:border-blue-700/20 bg-blue-50/30 dark:bg-blue-900/10 p-2.5",children:[e.jsxs("div",{className:"flex items-center gap-1.5 mb-1",children:[e.jsx(Me,{className:"w-3 h-3 text-blue-500"}),e.jsx("span",{className:"text-[10px] tracking-[0.1em] uppercase text-blue-600 dark:text-blue-400",children:"If / Then Branch"})]}),e.jsxs(L,{value:t.automationConfig?.conditionType||"",onValueChange:p=>R(t.id,{automationConfig:{...t.automationConfig,conditionType:p}}),children:[e.jsx(K,{className:"bg-white/60 dark:bg-background/40 border-0 h-8 text-[12px]","data-testid":`select-condition-type-${b}`,children:e.jsx(H,{placeholder:"If..."})}),e.jsxs(V,{children:[e.jsx(w,{value:"email_opened",children:"Email was opened"}),e.jsx(w,{value:"email_not_opened",children:"Email was NOT opened"}),e.jsx(w,{value:"has_tag",children:"Contact has tag"}),e.jsx(w,{value:"stage_is",children:"Contact is in stage"}),e.jsx(w,{value:"has_proposal",children:"Has a proposal"}),e.jsx(w,{value:"has_contract",children:"Has a contract"}),e.jsx(w,{value:"is_booked",children:"Is booked"})]})]}),t.automationConfig?.conditionType==="has_tag"&&e.jsx(M,{value:t.automationConfig?.tagName||"",onChange:p=>R(t.id,{automationConfig:{...t.automationConfig,tagName:p.target.value}}),placeholder:"Tag name...",className:"bg-white/60 dark:bg-background/40 border-0 h-7 text-[12px]","data-testid":`input-condition-tag-${b}`}),t.automationConfig?.conditionType==="stage_is"&&e.jsxs(L,{value:t.automationConfig?.stageName||"",onValueChange:p=>R(t.id,{automationConfig:{...t.automationConfig,stageName:p}}),children:[e.jsx(K,{className:"bg-white/60 dark:bg-background/40 border-0 h-8 text-[12px]","data-testid":`select-condition-stage-${b}`,children:e.jsx(H,{placeholder:"Select stage..."})}),e.jsx(V,{children:xe.map(p=>e.jsx(w,{value:p.value,children:p.label},p.value))})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-[10px] text-blue-500/70 block mb-1",children:"If condition is NOT met, skip next:"}),e.jsxs(L,{value:String(t.automationConfig?.skipSteps||"1"),onValueChange:p=>R(t.id,{automationConfig:{...t.automationConfig,skipSteps:p}}),children:[e.jsx(K,{className:"bg-white/60 dark:bg-background/40 border-0 h-8 text-[12px] w-32","data-testid":`select-skip-steps-${b}`,children:e.jsx(H,{})}),e.jsxs(V,{children:[e.jsx(w,{value:"1",children:"1 step"}),e.jsx(w,{value:"2",children:"2 steps"}),e.jsx(w,{value:"3",children:"3 steps"}),e.jsx(w,{value:"4",children:"4 steps"}),e.jsx(w,{value:"5",children:"5 steps"})]})]})]}),e.jsx("p",{className:"text-[10px] text-blue-500/60",children:"If the condition is true, continue to the next step. If false, skip ahead the specified number of steps."})]}),(t.type==="automation"&&t.automationAction&&t.automationAction!=="wait"&&t.automationAction!=="condition"||(t.type||"task")==="task")&&e.jsx(ns,{step:t,onUpdate:p=>R(t.id,p)}),e.jsx(M,{value:t.description||"",onChange:p=>R(t.id,{description:p.target.value}),placeholder:"Optional notes...",className:"bg-muted/20 border-0 h-7 text-[11px] text-muted-foreground","data-testid":`input-step-description-${b}`})]}),e.jsx("button",{onClick:()=>bt(t.id),className:"mt-1 p-1 rounded hover:bg-destructive/10 text-foreground
1/65 hover:text-destructive transition-colors","data-testid":`button-remove-step-${b}`,"aria-label":"Remove step",children:e.jsx(Re,{className:"w-3.5 h-3.5"})})]})})}),o&&e.jsx(Tt,{index:b+1,onAdd:yt})]},t.id))})]})]}),e.jsxs("section",{hidden:r&&x!==2,className:"space-y-5 rounded-2xl border border-primary/15 bg-card p-5 sm:p-7 shadow-sm",children:[r&&e.jsxs("div",{className:"rounded-xl bg-primary/5 p-4",children:[e.jsx("p",{className:"text-sm font-semibold text-primary",children:"Ready for a final look"}),e.jsx("h2",{className:"mt-1 font-display text-2xl",children:f}),e.jsxs("p",{className:"mt-2 text-sm text-muted-foreground",children:[I.length," steps ·"," ",j==="review"?"Emails need your review":"Emails send automatically"]}),e.jsx(z,{variant:"ghost",className:"mt-2 px-0 text-primary",onClick:()=>S(1),children:"Review your steps"})]}),e.jsx("h2",{className:"text-lg font-medium",children:"Review before you turn it on"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:Z?"This workflow is on. Saving updates its future runs.":"This workflow is off. Save it as a draft, or turn it on when you are ready."})," ",e.jsx(es,{enabled:Z,emailSendMode:j,onEnabledChange:t=>{ae(t),B()},onEmailSendModeChange:t=>{d(t),B()}})]}),e.jsxs("div",{hidden:r&&!C,style:{display:r&&!C?"none":void 0},className:o?"sticky bottom-0 z-20 flex items-center justify-between gap-3 border-t border-primary/15 bg-card/95 p-4 shadow-sm backdrop-blur pb-[max(1rem,env(safe-area-inset-bottom))]":"flex items-center justify-end gap-2 mt-4 pt-4 border-t border-border/10 shrink-0 bg-background",children:[c&&pe.status==="saving"&&e.jsx("span",{className:"text-[10px] text-muted-foreground mr-auto",children:"Saving..."}),c&&pe.status==="saved"&&e.jsx("span",{className:"text-[10px] text-emerald-600/70 mr-auto",children:"Saved"}),e.jsx(z,{variant:"ghost",onClick:r&&x>0?()=>S(x-1):Ht,"data-testid":"button-cancel-workflow",children:r&&x>0?"Back":c?"Close":"Cancel"}),r&&x<2?e.jsxs(ve,{className:"min-h-11 px-6",onClick:()=>{if(!f.trim()){g({title:"Give your workflow a name first",variant:"destructive"});return}if(x===1&&!I.length){g({title:"Add your first step to continue",variant:"destructive"});return}S(x+1)}
1,children:[x===0?"Continue to steps":"Review workflow",e.jsx(he,{className:"ml-2 h-4 w-4"})]}):e.jsx(ve,{onClick:Qt,disabled:He.isPending,"data-testid":"button-save-workflow",children:He.isPending?"Saving...":o?Z?"Save workflow":"Save draft":c?"Save & Close":"Create Workflow"})]})]})}const or=Object.freeze(Object.defineProperty({__proto__:null,ClientWorkflowsView:is,WorkflowFormDialog:Lt},Symbol.toStringTag,{value:"Module"}));export{or as C,Fa as I,Lt as W,es as a};
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.