1import{r as l,j as e}from"./vendor-react-Dak0qFzn.js";import{cE as ke,cn as g,c5 as Me,c4 as Ve,B as d,F as ee,h as y,C as D,E as k,i as O,H as P,J as _,K as L,M as B,N as $,L as u,I as b,W as z,aq as We,t as m}from"./index-CkP0Ysi6.js";import{T as Ne,a as Se,b as H,c as j,d as Ce,e as p}from"./table-Bztvit-x.js";import{S as M,a as V,b as W,c as G,d as x}from"./select-BM3YwBkF.js";import{E as se}from"./empty-state-BbW66hnb.js";import{E as Ge}from"./error-banner-CErsG9mr.js";import{T as we}from"./page-skeleton-CgSJ5_Pb.js";import{f as J}from"./format-D32Lv-fM.js";import{b1 as Je,bS as qe,d as I,at as Ee,U as Te,bs as Ke,w as Qe,r as te,aR as ae,b3 as Xe,g as Ye}from"./vendor-icons-DQbXH2Ms.js";import"./vendor-ui-B9Hlh5h7.js";function Ze(n){return g("/api/sales/sequences",{method:"POST",body:n})}function es(){return ke("/api/sales/sequences")}function ss(n){return g(`/api/sales/sequences/${encodeURIComponent(n)}`)}function ts(n,i){return g(`/api/sales/sequences/${encodeURIComponent(n)}`,{method:"PATCH",body:i})}function as(n){return g(`/api/sales/sequences/${encodeURIComponent(n)}`,{method:"DELETE"})}function ns(n,i){return g(`/api/sales/sequences/${encodeURIComponent(n)}/steps`,{method:"POST",body:i})}function ls(n,i,F){return g(`/api/sales/sequences/steps/${encodeURIComponent(i)}`,{method:"PATCH",body:F})}function rs(n,i){return g(`/api/sales/sequences/steps/${encodeURIComponent(i)}`,{method:"DELETE"})}function cs(n){return g(`/api/sales/sequences/${encodeURIComponent(n.sequenceId)}/enroll`,{method:"POST",body:n})}function is(n){return ke(`/api/sales/sequences/${encodeURIComponent(n)}/enrollments`)}function ds(n){return g(`/api/sales/sequences/${encodeURIComponent(n)}/stats`)}function De(n){switch(n){case"active":return{label:"Active",className:"bg-emerald-100 text-emerald-700 dark:bg-emerald-900/30 dark:text-emerald-400"};case"paused":return{label:"Paused",className:"bg-amber-100 text-amber-700 dark:bg-amber-900/30 dark:text-amber-400"};default:return{label:"Draft",className:"bg-muted text-muted-foreground dark:bg-gray-800 dark:text-muted-foreground"}}}function os(n){switch(n){case"active":return{label:"Active",className:"bg-blue-100 text-primary dark:bg-blue-900/30 dark:text-blue-400"};case"completed":return{label:"Completed",className:"bg-emerald-100 text-emerald-700 dark:bg-emerald-900/30 dark:text-emerald-400"};case"paused":return{label:"Paused",className:"bg-amber-100 text-amber-700 dark:bg-amber-900/30 dark:text-amber-400"};case"unsubscribed":return{label:"Unsubscribed",className:"bg-red-100 text-red-700 dark:bg-red-900/30 dark:text-red-400"};default:return{label:n,className:"bg-muted text-muted-foreground dark:bg-gray-800 dark:text-muted-foreground"}}}function ys(){const{can:n}=Me(),i=n(Ve.MANAGE_SEQUENCES),[F,A]=l.useState([]),[K,ne]=l.useState(!0),[Oe,Q]=l.useState(null),[r,C]=l.useState(null),[X,U]=l.useState([]),[le,re]=l.useState([]),[w,Ie]=l.useState(null),[Fe,ce]=l.useState(!1),[ie,v]=l.useState(!1),[f,de]=l.useState(null),[c,h]=l.useState({name:"",description:"",triggerType:"manual",status:"draft"}),[R,oe]=l.useState(!1),[Ae,q]=l.useState(!1),[E,ue]=l.useState(null),[o,N]=l.useState({stepOrder:1,delayDays:1,subject:"",bodyText:""}),[me,xe]=l.useState(!1),[Ue,T]=l.useState(!1),[Y,he]=l.useState(""),[je,pe]=l.useState(!1),[ge,fe]=l.useState(null),Z=l.useCallback(async()=>{ne(!0),Q(null);try{const t=await es();A(t)}catch(t){Q(t instanceof Error?t.message:"Failed to load sequences")}finally{ne(!1)}},[]);l.useEffect(()=>{Z()},[Z]);async function Re(t){C(t),ce(!0);try{const[s,a,S]=await Promise.all([ss(t.id),is(t.id),ds(t.id)]);C(s),re(a),Ie(S),U(s.steps??[])}catch(s){m.error(s instanceof Error?s.message:"Failed to load sequence details")}finally{ce(!1)}}function be(){de(null),h({name:"",description:"",triggerType:"manual",status:"draft"}),v(!0)}function Pe(t){de(t),h({name:t.name,description:t.description??"",triggerType:t.triggerType,status:t.status}),v(!0)}async function ve(){oe(!0);try{if(f){const t={name:c.name,description:c.description||void 0,triggerType:c.triggerType,status:c.status},s=await ts(f.id,t);A(a=>a.map(S=>
1S.id===s.id?s:S)),r?.id===s.id&&C(s),m.success("Sequence updated")}else{const t={name:c.name,description:c.description||void 0,triggerType:c.triggerType,status:c.status},s=await Ze(t);A(a=>[...a,s]),m.success("Sequence created")}v(!1)}catch(t){m.error(t instanceof Error?t.message:"Failed to save sequence")}finally{oe(!1)}}async function _e(t){fe(t);try{await as(t),A(s=>s.filter(a=>a.id!==t)),r?.id===t&&C(null),m.success("Sequence deleted")}catch(s){m.error(s instanceof Error?s.message:"Failed to delete sequence")}finally{fe(null)}}function ye(){r&&(ue(null),N({stepOrder:X.length+1,delayDays:1,subject:"",bodyText:""}),q(!0))}function Le(t){ue(t),N({stepOrder:t.stepOrder,delayDays:t.delayDays,subject:t.subject,bodyText:t.bodyText??""}),q(!0)}async function Be(){if(r){xe(!0);try{if(E){const t=await ls(r.id,E.id,{stepOrder:o.stepOrder,delayDays:o.delayDays,subject:o.subject,bodyText:o.bodyText||void 0});U(s=>s.map(a=>a.id===t.id?t:a)),m.success("Step updated")}else{const t={stepOrder:o.stepOrder,delayDays:o.delayDays,subject:o.subject,bodyText:o.bodyText||void 0},s=await ns(r.id,t);U(a=>[...a,s].sort((S,He)=>S.stepOrder-He.stepOrder)),m.success("Step added")}q(!1)}catch(t){m.error(t instanceof Error?t.message:"Failed to save step")}finally{xe(!1)}}}async function $e(t){if(r)try{await rs(r.id,t),U(s=>s.filter(a=>a.id!==t)),m.success("Step removed")}catch(s){m.error(s instanceof Error?s.message:"Failed to remove step")}}async function ze(){if(r){pe(!0);try{const t=await cs({contactId:Y,sequenceId:r.id});re(s=>[t,...s]),T(!1),he(""),m.success("Contact enrolled")}catch(t){m.error(t instanceof Error?t.message:"Failed to enroll contact")}finally{pe(!1)}}}if(r){const t=De(r.status);return e.jsxs("div",{className:"section-gap",children:[e.jsx("div",{className:"flex items-center gap-3",children:e.jsxs(d,{variant:"ghost",size:"sm",onClick:()=>C(null),children:[e.jsx(Je,{className:"h-4 w-4 mr-1"})," Back"]})}),e.jsxs("div",{className:"flex items-start justify-between",children:[e.jsxs("div",{children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("h1",{className:"page-title",children:r.name}),e.jsx(ee,{className:t.className,children:t.label})]}),r.description&&e.jsx("p",{className:"page-subtitle mt-1",children:r.description}),e.jsxs("p",{className:"text-sm text-muted-foreground mt-1",children:["Trigger: ",r.triggerType.replace(/_/g," ")," · Created ",J(r.createdAt)]})]}),i&&e.jsxs("div",{className:"flex gap-2",children:[e.jsxs(d,{variant:"outline",size:"sm",onClick:()=>Pe(r),children:[e.jsx(qe,{className:"h-4 w-4 mr-1"})," Edit"]}),e.jsxs(d,{variant:"outline",size:"sm",className:"text-destructive hover:text-destructive",onClick:()=>_e(r.id),disabled:ge===r.id,children:[ge===r.id?e.jsx(I,{className:"h-4 w-4 mr-1 animate-spin"}):e.jsx(Ee,{className:"h-4 w-4 mr-1"}),"Delete"]})]})]}),Fe?e.jsx(we,{rows:4}):e.jsxs(e.Fragment,{children:[w&&e.jsxs("div",{className:"grid gap-4 sm:grid-cols-2 lg:grid-cols-4",children:[e.jsxs(y,{children:[e.jsxs(D,{className:"flex flex-row items-center justify-between pb-2",children:[e.jsx(k,{className:"text-sm font-medium text-muted-foreground",children:"Total Enrollments"}),e.jsx(Te,{className:"h-4 w-4 text-muted-foreground"})]}),e.jsx(O,{children:e.jsx("div",{className:"text-2xl font-bold",children:w.totalEnrollments})})]}),e.jsxs(y,{children:[e.jsxs(D,{className:"flex flex-row items-center justify-between pb-2",children:[e.jsx(k,{className:"text-sm font-medium text-muted-foreground",children:"Active"}),e.jsx(Ke,{className:"h-4 w-4 text-blue-500"})]}),e.jsx(O,{children:e.jsx("div",{className:"text-2xl font-bold",children:w.activeEnrollments})})]}),e.jsxs(y,{children:[e.jsxs(D,{className:"flex flex-row items-center justify-between pb-2",children:[e.jsx(k,{className:"text-sm font-medium text-muted-foreground",children:"Completed"}),e.jsx(Qe,{className:"h-4 w-4 text-emerald-500"})]}),e.jsx(O,{children:e.jsx("div",{className:"text-2xl font-bold",children:w.completedEnrollments})})]}),e.jsxs(y,{children:[e.jsxs(D,{className:"flex flex-row items-center justify-between pb-2",children:[e.jsx(k,{className:"text-sm font-medium text-muted-foreground",children:"Open Rate"}),e.jsx(te,{className:"h-4 w-4 text-muted-foreground"})]}),e.jsx(O,{children:e.jsxs("div",{className:"text-2xl font-bold",children:[w.openRate.toFixed(1),"%"]})})]})]}),e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("h2",{className:"text-lg font-semibold",children:"Steps"}),i&&e.jsxs(d,{size:"sm",onClick:ye,children:[e.jsx(ae,{className:"h-4 w-4 mr-1"})," Add Step"]})]}),X.length===0?e.jsx(se,{icon:te,title:"No steps yet",description:"Add the first step to this sequence to start building your outreach flow.",action:i?{label:"Add Step",onClick:ye}:void 0}):e.jsx(y,{children:e.jsxs(Ne,{children:[e.jsx(Se,{children:e.jsxs(H,{
1children:[e.jsx(j,{className:"w-16",children:"#"}),e.jsx(j,{children:"Subject"}),e.jsx(j,{children:"Delay"}),e.jsx(j,{children:"Created"}),i&&e.jsx(j,{className:"text-right",children:"Actions"})]})}),e.jsx(Ce,{children:X.map(s=>e.jsxs(H,{children:[e.jsx(p,{className:"font-medium",children:s.stepOrder}),e.jsx(p,{children:s.subject}),e.jsxs(p,{children:[s.delayDays," day",s.delayDays!==1?"s":""]}),e.jsx(p,{className:"text-muted-foreground",children:J(s.createdAt)}),i&&e.jsx(p,{className:"text-right",children:e.jsxs("div",{className:"flex justify-end gap-1",children:[e.jsx(d,{variant:"ghost",size:"sm",onClick:()=>Le(s),children:e.jsx(qe,{className:"h-3.5 w-3.5"})}),e.jsx(d,{variant:"ghost",size:"sm",className:"text-destructive hover:text-destructive",onClick:()=>$e(s.id),children:e.jsx(Ee,{className:"h-3.5 w-3.5"})})]})})]},s.id))})]})})]}),e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("h2",{className:"text-lg font-semibold",children:"Enrollments"}),i&&e.jsxs(d,{size:"sm",onClick:()=>T(!0),children:[e.jsx(ae,{className:"h-4 w-4 mr-1"})," Enroll Contact"]})]}),le.length===0?e.jsx(se,{icon:Te,title:"No enrollments",description:"Enroll contacts into this sequence to start automated outreach.",action:i?{label:"Enroll Contact",onClick:()=>T(!0)}:void 0}):e.jsx(y,{children:e.jsxs(Ne,{children:[e.jsx(Se,{children:e.jsxs(H,{children:[e.jsx(j,{children:"Contact"}),e.jsx(j,{children:"Email"}),e.jsx(j,{children:"Status"}),e.jsx(j,{children:"Current Step"}),e.jsx(j,{children:"Enrolled"})]})}),e.jsx(Ce,{children:le.map(s=>{const a=os(s.status);return e.jsxs(H,{children:[e.jsx(p,{className:"font-medium",children:s.contactName??s.contactId.slice(0,8)}),e.jsx(p,{className:"text-muted-foreground",children:s.contactEmail??"â"}),e.jsx(p,{children:e.jsx(ee,{className:a.className,children:a.label})}),e.jsx(p,{children:s.currentStep}),e.jsx(p,{className:"text-muted-foreground",children:J(s.enrolledAt)})]},s.id)})})]})})]})]}),e.jsx(P,{open:Ae,onOpenChange:q,children:e.jsxs(_,{className:"sm:max-w-[480px]",children:[e.jsxs(L,{children:[e.jsx(B,{children:E?"Edit Step":"Add Step"}),e.jsx($,{children:E?"Update this sequence step.":"Add a new step to the sequence."})]}),e.jsxs("div",{className:"space-y-4 py-2",children:[e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(u,{htmlFor:"step-order",children:"Step #"}),e.jsx(b,{id:"step-order",type:"number",min:1,value:o.stepOrder,onChange:s=>N(a=>({...a,stepOrder:parseInt(s.target.value)||1}))})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(u,{htmlFor:"step-delay",children:"Delay (days)"}),e.jsx(b,{id:"step-delay",type:"number",min:0,value:o.delayDays,onChange:s=>N(a=>({...a,delayDays:parseInt(s.target.value)||0}))})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsxs(u,{htmlFor:"step-subject",children:["Subject Line ",e.jsx("span",{className:"text-destructive",children:"*"})]}),e.jsx(b,{id:"step-subject",placeholder:"e.g., Following up on our conversation",value:o.subject,onChange:s=>N(a=>({...a,subject:s.target.value}))})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(u,{htmlFor:"step-body",children:"Body (plain text)"}),e.jsx("textarea",{id:"step-body",rows:4,className:"flex w-full rounded-md border border-input bg-background px-3 py-2 text-sm shadow-sm placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring",placeholder:"Email body content...",value:o.bodyText,onChange:s=>N(a=>({...a,bodyText:s.target.value}))})]})]}),e.jsxs(z,{children:[e.jsx(d,{variant:"outline",onClick:()=>q(!1),children:"Cancel"}),e.jsxs(d,{onClick:Be,disabled:me||!o.subject.trim(),children:[me&&e.jsx(I,{className:"h-4 w-4 mr-2 animate-spin"}),E?"Update":"Add"]})]})]})}),e.jsx(P,{open:Ue,onOpenChange:T,children:e.jsxs(_,{className:"sm:max-w-[400px]",children:[e.jsxs(L,{children:[e.jsx(B,{children:"Enroll Contact"}),e.jsx($,{children:"Enter a contact ID to enroll them in this sequence."})]}),e.jsx("div",{className:"space-y-4 py-2",children:e.jsxs("div",{className:"space-y-2",children:[e.jsxs(u,{htmlFor:"enroll-contact",children:["Contact ID ",e.jsx("span",{className:"text-destructive",children:"*"})]}),e.jsx(b,{id:"enroll-contact",placeholder:"Contact ID",value:Y,onChange:s=>he(s.target.value)})]})}),e.jsxs(z,{children:[e.jsx(d,{variant:"outline",onClick:()=>T(!1),children:"Cancel"}),e.jsxs(d,{onClick:ze,disabled:je||!Y.trim(),children:[je&&e.jsx(I,{className:"h-4 w-4 mr-2 animate-spin"}),"Enroll"]})]})]})}),e.jsx(P,{open:ie,onOpenChange:v,children:e.jsxs(_,{className:"sm:max-w-[480px]",children:[e.jsxs(L,{children:[e.jsx(B,{children:f?"Edit Sequence":"Create Sequence"}),e.jsx($,{children:f?"Update sequence settings.":"Create a new email sequence to automate outreach."})]}),e.jsxs("div",{className:"space-y-4 py-2",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsxs(u,{htmlFor:"seq-name",children:["Name ",e.jsx("span",{className:"text-destructive",children:"*"})]}),e.jsx(b,{id:"seq-name",placeholder:"e.g., Welcome Series",value:c.name,onChange:s=>h(a=>({...a,name:s.target.value}))})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(u,{htmlFor:"seq-desc",children:"Description"}),e.jsx(b,{id:"seq-desc",placeholder:"Brief description",value:c.description,onChange:s=>h(a=>({...a,description:s.target.value}))})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(u,{children:"Trigger Type"}),e.jsxs(M,{value:c.triggerType,onValueChange:s=>h(a=>({...a,triggerType:s})),children:[e.jsx(V,{children:e.jsx(W,{})}),e.jsxs(G,{children:[e.jsx(x,{value:"manual",children:"Manual"}),e.jsx(x,{value:"on_lead_created",children:"On Lead Created"}),e.jsx(x,{value:"on_deal_stage_change",children:"On Deal Stage Change"})]})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(u,{children:"Status"}),e.jsxs(M,{value:c.status,onValueChange:s=>h(a=>({...a,status:s})),children:[e.jsx(V,{children:e.jsx(W,{})}),e.jsxs(G,{children:[e.jsx(x,{value:"draft",children:"Draft"}),e.jsx(x,{value:"active",children:"Active"}),e.jsx(x,{value:"paused",children:"Paused"})]})]})]})]}),e.jsxs(z,{children:[e.jsx(d,{variant:"outline",onClick:()=>v(!1),children:"Cancel"}),e.jsxs(d,{onClick:ve,disabled:R||!c.name.trim(),children:[R&&e.jsx(I,{className:"h-4 w-4 mr-2 animate-spin"}),f?"Update":"Create"]})]})]})})]})}return e.jsxs("div",{className:"section-gap",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"page-title",children:"Email Sequences"}),e.jsx("p",{className:"page-subtitle mt-1",children:"Automate email outreach with multi-step sequences"})]}),e.jsxs("div",{className:"flex gap-2",children:[e.jsx(d,{variant:"outline",size:"sm",onClick:Z,disabled:K,children:e.jsx(Xe,{className:`h-4 w-4 ${K?"animate-spin":""}`})}),i&&e.jsxs(d,{size:"sm",onClick:be,children:[e.jsx(ae,{className:"h-4 w-4 mr-1"})," New Sequence"]})]})]}),e.jsx(Ge,{message:Oe,onDismiss:()=>Q(null)}),K?e.jsx(we,{rows:5}):F.length===0?e.jsx(se,{icon:te,title:"No sequences yet",description:"Create your first email sequence to start automating outreach.",action:i?{label:"Create Sequence",onClick:be}:void 0}):e.jsx("div",{className:"grid gap-4 sm:grid-cols-2 lg:grid-cols-3",children:F.map(t=>{const s=De(t.status);return e.jsxs(y,{className:"cursor-pointer hover:shadow-md transition-shadow",onClick:()=>Re(t),children:[e.jsx(D,{className:"pb-3",children:e.jsxs("div",{className:"flex items-start justify-between",children:[e.jsxs("div",{className:"space-y-1",children:[e.jsx(k,{className:"text-base",children:t.name}),t.description&&e.jsx(We,{className:"line-clamp-2",children:t.description})]}),e.jsx(ee,{className:s.className,children:s.label})]})}),e.jsxs(O,{children:[e.jsxs("div",{className:"flex items-center justify-between text-sm text-muted-foreground",children:[e.jsxs("div",{className:"flex items-center gap-4",children:[e.jsxs("span",{children:[t.stepsCount??0," steps"]}),e.jsxs("span",{children:[t.enrollmentsCount??0," enrolled"]})]}),e.jsx(Ye,{className:"h-4 w-4"})]}),e.jsxs("p",{className:"text-xs text-muted-foreground mt-2",children:["Created ",J(t.createdAt)]})]})]},t.id)})}),e.jsx(P,{open:ie,onOpenChange:v,children:e.jsxs(_,{className:"sm:max-w-[480px]",children:[e.jsxs(L,{children:[e.jsx(B,{children:f?"Edit Sequence":"Create Sequence"}),e.jsx($,{children:f?"Update sequence settings.":"Create a new email sequence to automate outreach."})]}),e.jsxs("div",{className:"space-y-4 py-2",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsxs(u,{htmlFor:"seq-name-list",children:["Name ",e.jsx("span",{className:"text-destructive",children:"*"})]}),e.jsx(b,{id:"seq-name-list",placeholder:"e.g., Welcome Series",value:c.name,onChange:t=>h(s=>({...s,name:t.target.value}))})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(u,{htmlFor:"seq-desc-list",children:"Description"}),e.jsx(b,{id:"seq-desc-list",placeholder:"Brief description",value:c.description,onChange:t=>h(s=>({...s,description:t.target.value}))})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(u,{children:"Trigger Type"}),e.jsxs(M,{value:c.triggerType,onValueChange:t=>h(s=>({...s,triggerType:t})),children:[e.jsx(V,{children:e.jsx(W,{})}),e.jsxs(G,{children:[e.jsx(x,{value:"manual",children:"Manual"}),e.jsx(x,{value:"on_lead_created",children:"On Lead Created"}),e.jsx(x,{value:"on_deal_stage_change",children:"On Deal Stage Change"})]})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(u,{children:"Status"}),e.jsxs(M,{value:c.status,onValueChange:t=>h(s=>({...s,status:t})),children:[e.jsx(V,{children:e.jsx(W,{})}),e.jsxs(G,{children:[e.jsx(x,{value:"draft",children:"Draft"}),e.jsx(x,{value:"active",children:"Active"}),e.jsx(x,{value:"paused",children:"Paused"})]})]})]})]}),e.jsxs(z,{children:[e.jsx(d,{variant:"outline",onClick:()=>v(!1),children:"Cancel"}),e.jsxs(d,{onClick:ve,disabled:R||!c.name.trim(),children:[R&&e.jsx(I,{className:"h-4 w-4 mr-2 animate-spin"}),f?"Update":"Create"]})]})]})})]})}export{ys as default};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.