1import{u as f,d as Y,b as F,j as e}from"./vendor-query-DPZO-Gta.js";import{b as Z,j as I,r as c,L as _}from"./vendor-react-BomFe3BT.js";import{z as i,J as N,A as R,b as ee,i as te}from"./index-BLzMsaFd.js";import{u as ae}from"./useChatbotTabs-TDDcKjl5.js";import{f as q}from"./format-DkOMWmK9.js";import"./vendor-charts-DodUWJ0z.js";import"./en-US-BOoww7nS.js";function ce(){var $;const{id:p}=Z();let h=null;try{h=I()}catch{}const w=!!(h!=null&&h.business),{data:k}=f({queryKey:["business-bots",p],queryFn:async()=>(await ee.getBots(p)).data,enabled:!!p&&w}),r=w?($=k==null?void 0:k[0])==null?void 0:$.id:p,C=Y(),[g,D]=c.useState(""),[y,P]=c.useState(0),b=20,[v,B]=c.useState("leads"),[T,L]=c.useState(!1),[S,M]=c.useState(""),[W,E]=c.useState(""),[n,u]=c.useState(null),{data:x}=f({queryKey:["chatbot",r],queryFn:async()=>(await te.get(r)).data,enabled:!!r}),{data:o,isLoading:H}=f({queryKey:["leads",r,y],queryFn:async()=>(await R.getLeads(r,y*b,b)).data,enabled:!!r}),{data:m,isLoading:Q}=f({queryKey:["lead-notifications",r],queryFn:async()=>(await N.list(r)).data.emails,enabled:!!r&&v==="notifications"}),A=F({mutationFn:async t=>await N.create(r,t),onSuccess:()=>{C.invalidateQueries({queryKey:["lead-notifications",r]}),L(!1),M(""),E(""),i.success("Notification email added")},onError:t=>{var a,s;i.error(((s=(a=t.response)==null?void 0:a.data)==null?void 0:s.detail)||"Failed to add email")}}),j=F({mutationFn:async({emailId:t,data:a})=>await N.update(r,t,a),onSuccess:()=>{C.invalidateQueries({queryKey:["lead-notifications",r]}),u(null),i.success("Notification email updated")},onError:t=>{var a,s;i.error(((s=(a=t.response)==null?void 0:a.data)==null?void 0:s.detail)||"Failed to update email")}}),z=F({mutationFn:async t=>await N.delete(r,t),onSuccess:()=>{C.invalidateQueries({queryKey:["lead-notifications",r]}),i.success("Notification email removed")},onError:t=>{var a,s;i.error(((s=(a=t.response)==null?void 0:a.data)==null?void 0:s.detail)||"Failed to remove email")}}),l=o==null?void 0:o.filter(t=>{var s,d,K;if(!g)return!0;const a=g.toLowerCase();return((s=t.email)==null?void 0:s.toLowerCase().includes(a))||((d=t.name)==null?void 0:d.toLowerCase().includes(a))||((K=t.phone)==null?void 0:K.toLowerCase().includes(a))}),U=async()=>{try{const t=await R.exportCsv(r,"leads",{days:365}),a=new Blob([t.data],{type:"text/csv"}),s=window.URL.createObjectURL(a),d=document.createElement("a");d.href=s,d.download=`${x==null?void 0:x.name}_leads_${q(new Date,"yyyyMMdd")}.csv`,document.body.appendChild(d),d.click(),document.body.removeChild(d),window.URL.revokeObjectURL(s),i.success("Leads exported successfully")}catch(t){console.error("Export failed:",t),i.error("Failed to export leads")}},V=async t=>{try{await navigator.clipboard.writeText(t),i.success("Copied to clipboard")}catch{const s=document.createElement("textarea");s.value=t,s.style.position="fixed",s.style.left="-999999px",document.body.appendChild(s),s.select();try{document.execCommand("copy"),i.success("Copied to clipboard")}catch{i.error("Failed to copy")}document.body.removeChild(s)}},O=t=>{t.preventDefault(),S.trim()&&A.mutate({email:S.trim(),label:W.trim()||void 0})},J=t=>{j.mutate({emailId:t.id,data:{is_active:!t.is_active}})},G=t=>{window.confirm("Are you sure you want to remove this notification email?")&&z.mutate(t)},X=ae(r||p,"Leads");return e.jsxs("div",{children:[!w&&e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"flex items-center gap-4 mb-6",children:[e.jsx(_,{to:"/chatbots",className:"text-gray-500 hover:text-gray-700",children:e.jsx("svg",{className:"w-5 h-5",fill:"none",viewBox:"0 0 24 24",stroke:"currentColor",children:e.jsx("path",{strokeLinecap:"round",strokeLinejoin:"round",strokeWidth:2,d:"M10 19l-7-7m0 0l7-7m-7 7h18"})})}),e.jsxs("h1",{className:"text-2xl font-bold text-gray-900",children:[x==null?void 0:x.name," - Leads"]})]}),e.jsx("div",{className:"border-b border-gray-200 mb-6",children:e.jsx("nav",{className:"-mb-px flex space-x-8",children:X.map(t=>e.jsx(_,{to:t.href,className:`py-4 px-1 border-b-2 font-medium text-sm ${t.current?"border-primary-500 text-primary-600":"border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300"}`,children:t.name}
1,t.name))})})]}),e.jsxs("div",{className:"flex gap-4 mb-6",children:[e.jsx("button",{onClick:()=>B("leads"),className:`px-4 py-2 text-sm font-medium rounded-lg transition ${v==="leads"?"bg-primary-100 text-primary-700":"text-gray-600 hover:bg-gray-100"}`,children:"Leads List"}),e.jsx("button",{onClick:()=>B("notifications"),className:`px-4 py-2 text-sm font-medium rounded-lg transition ${v==="notifications"?"bg-primary-100 text-primary-700":"text-gray-600 hover:bg-gray-100"}`,children:"Lead Notifications"})]}),v==="leads"?e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"flex flex-wrap items-center justify-between gap-4 mb-6",children:[e.jsxs("div",{className:"flex items-center gap-4",children:[e.jsxs("div",{className:"relative",children:[e.jsx("input",{type:"text",placeholder:"Search leads...",value:g,onChange:t=>D(t.target.value),className:"pl-10 pr-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary-500 focus:border-transparent"}),e.jsx("svg",{className:"absolute left-3 top-1/2 transform -translate-y-1/2 w-4 h-4 text-gray-400",fill:"none",viewBox:"0 0 24 24",stroke:"currentColor",children:e.jsx("path",{strokeLinecap:"round",strokeLinejoin:"round",strokeWidth:2,d:"M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"})})]}),e.jsxs("span",{className:"text-sm text-gray-500",children:[(l==null?void 0:l.length)||0," leads"]})]}),e.jsxs("button",{onClick:U,className:"flex items-center gap-2 px-4 py-2 bg-primary-600 text-white rounded-lg hover:bg-primary-700 transition",children:[e.jsx("svg",{className:"w-4 h-4",fill:"none",viewBox:"0 0 24 24",stroke:"currentColor",children:e.jsx("path",{strokeLinecap:"round",strokeLinejoin:"round",strokeWidth:2,d:"M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4"})}),"Export CSV"]})]}),e.jsxs("div",{className:"card overflow-hidden",children:[e.jsx("div",{className:"overflow-x-auto",children:e.jsxs("table",{className:"w-full",children:[e.jsx("thead",{className:"bg-gray-50 border-b border-gray-200",children:e.jsxs("tr",{children:[e.jsx("th",{className:"px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider",children:"Email"}),e.jsx("th",{className:"px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider",children:"Name"}),e.jsx("th",{className:"px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider",children:"Phone"}),e.jsx("th",{className:"px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider",children:"Source"}),e.jsx("th",{className:"px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider",children:"Messages"}),e.jsx("th",{className:"px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider",children:"Date"}),e.jsx("th",{className:"px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider",children:"Actions"})]})}),e.jsx("tbody",{className:"bg-white divide-y divide-gray-200",children:H?e.jsx("tr",{children:e.jsx("td",{colSpan:7,className:"px-6 py-12 text-center text-gray-500",children:"Loading leads..."})}):(l==null?void 0:l.length)===0?e.jsx("tr",{children:e.jsx("td",{colSpan:7,className:"px-6 py-12 text-center text-gray-500",children:g?"No leads match your search":"No leads captured yet"})}):l==null?void 0:l.map(t=>e.jsxs("tr",{className:"hover:bg-gray-50",children:[e.jsx("td",{className:"px-6 py-4 whitespace-nowrap",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("span",{className:"text-sm font-medium text-gray-900",children:t.email}),e.jsx("button",{onClick:()=>V(t.email),className:"text-gray-400 hover:text-gray-600",title:"Copy email",children:e.jsx("svg",{className:"w-4 h-4",fill:"none",viewBox:"0 0 24 24",stroke:"currentColor",children:e.jsx("path",{strokeLinecap:"round",strokeLinejoin:"round",strokeWidth:2,d:"M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z"})})})]})}),e.jsx("td",{className:"px-6 py-4 whitespace-nowrap",children:e.jsx("span",{className:"text-sm text-gray-700",children:t.name||"-"})}),e.jsx("td",{className:"px-6 py-4 whitespace-nowrap",children:t.phone?e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("span",{className:"text-sm text-gray-700",children:t.phone}),e.jsx("button",{onClick:()=>V(t.phone),className:"text-gray-400 hover:text-gray-600",title:"Copy phone",children:e.jsx("svg",{className:"w-4 h-4",fill:"none",viewBox:"0 0 24 24",stroke:"currentColor",children:e.jsx("path",{strokeLinecap:"round",strokeLinejoin:"round",strokeWidth:2,d:"M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z"})})})]}):e.jsx("span",{className:"text-sm text-gray-400",children:"-"})}),e.jsx("td",{className:"px-6 py-4 whitespace-nowrap",children:e.jsx("span",{className:"text-sm text-gray-500 truncate max-w-[200px] block",title:t.source_url||void 0,children:t.source_url?new URL(t.source_url).hostname:"-"})}),e.jsx("td",{className:"px-6 py-4 whitespace-nowrap",children:e.jsx("span",{className:"text-sm text-gray-700",children:t.message_count})}),e.jsx("td",{className:"px-6 py-4 whitespace-nowrap",children:e.jsx("span",{className:"text-sm text-gray-500",children:q(ne
1w Date(t.created_at),"MMM d, yyyy h:mm a")})}),e.jsx("td",{className:"px-6 py-4 whitespace-nowrap",children:e.jsx(_,{to:`/chatbots/${r}/conversations`,state:{selectedConversation:t.conversation_id},className:"text-primary-600 hover:text-primary-800 text-sm font-medium",children:"View Chat"})})]},t.conversation_id))})]})}),((o==null?void 0:o.length)||0)>=b&&e.jsxs("div",{className:"px-6 py-4 border-t border-gray-200 flex items-center justify-between",children:[e.jsx("button",{onClick:()=>P(t=>Math.max(0,t-1)),disabled:y===0,className:"px-4 py-2 text-sm font-medium text-gray-700 bg-white border border-gray-300 rounded-md hover:bg-gray-50 disabled:opacity-50 disabled:cursor-not-allowed",children:"Previous"}),e.jsxs("span",{className:"text-sm text-gray-500",children:["Page ",y+1]}),e.jsx("button",{onClick:()=>P(t=>t+1),disabled:((o==null?void 0:o.length)||0)<b,className:"px-4 py-2 text-sm font-medium text-gray-700 bg-white border border-gray-300 rounded-md hover:bg-gray-50 disabled:opacity-50 disabled:cursor-not-allowed",children:"Next"})]})]})]}):e.jsxs("div",{className:"card",children:[e.jsxs("div",{className:"p-6 border-b border-gray-200",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx("h3",{className:"text-lg font-medium text-gray-900",children:"Lead Notification Emails"}),e.jsx("p",{className:"mt-1 text-sm text-gray-500",children:"Email addresses that will receive notifications when a new lead is captured. Each email will include lead info, chat summary, and full transcript."})]}),!T&&e.jsxs("button",{onClick:()=>L(!0),className:"flex items-center gap-2 px-4 py-2 bg-primary-600 text-white rounded-lg hover:bg-primary-700 transition",children:[e.jsx("svg",{className:"w-4 h-4",fill:"none",viewBox:"0 0 24 24",stroke:"currentColor",children:e.jsx("path",{strokeLinecap:"round",strokeLinejoin:"round",strokeWidth:2,d:"M12 4v16m8-8H4"})}),"Add Email"]})]}),T&&e.jsxs("form",{onSubmit:O,className:"mt-4 p-4 bg-gray-50 rounded-lg",children:[e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-4",children:[e.jsxs("div",{children:[e.jsx("label",{htmlFor:"newEmail",className:"block text-sm font-medium text-gray-700 mb-1",children:"Email Address *"}),e.jsx("input",{type:"email",id:"newEmail",value:S,onChange:t=>M(t.target.value),placeholder:"[email protected]",required:!0,className:"w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary-500 focus:border-transparent"})]}),e.jsxs("div",{children:[e.jsx("label",{htmlFor:"newLabel",className:"block text-sm font-medium text-gray-700 mb-1",children:"Label (optional)"}),e.jsx("input",{type:"text",id:"newLabel",value:W,onChange:t=>E(t.target.value),placeholder:"e.g., Sales Team, Support",className:"w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary-500 focus:border-transparent"})]})]}),e.jsxs("div",{className:"mt-4 flex gap-3",children:[e.jsx("button",{type:"submit",disabled:A.isPending,className:"px-4 py-2 bg-primary-600 text-white rounded-lg hover:bg-primary-700 transition disabled:opacity-50",children:A.isPending?"Adding...":"Add Email"}),e.jsx("button",{type:"button",onClick:()=>{L(!1),M(""),E("")},className:"px-4 py-2 text-gray-700 bg-white border border-gray-300 rounded-lg hover:bg-gray-50 transition",children:"Cancel"})]})]})]}),e.jsx("div",{className:"divide-y divide-gray-200",children:Q?e.jsx("div",{className:"p-6 text-center text-gray-500",children:"Loading notification emails..."}):(m==null?void 0:m.length)===0?e.jsxs("div",{className:"p-6 text-center text-gray-500",children:[e.jsx("svg",{className:"mx-auto h-12 w-12 text-gray-400",fill:"none",viewBox:"0 0 24 24",stroke:"currentColor",children:e.jsx("path",{strokeLinecap:"round",strokeLinejoin:"round",strokeWidth:1.5,d:"M3 8l7.89 5.26a2 2 0 002.22 0L21 8M5 19h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z"})}),e.jsx("p",{className:"mt-2 text-sm",children:"No notification emails configured yet."}),e.jsx("p",{className:"text-sm text-gray-400",children:"Add an email address to receive lead notifications."})]}):m==null?void 0:m.map(t=>e.jsx("div",{className:"p-4 hover:bg-gray-50",children:(n==null?void 0:n.id)===t.id?e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-4",children:[e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 mb-1",children:"Email Address"}),e.jsx("input",{type:"email",value:n.email,onChange:a=>u({...n,email:a.target.value}),className:"w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary-500 focus:border-transparent"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 mb-1",children:"Label"}),e.jsx("input",{type:"text",value:n.label||"",onChange:a=>u({...n,label:a.target.value||null}),placeholder:"e.g., Sales Team",className:"w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary-500 focus:border-transparent"})]})]}),e.jsxs("div",{className:"flex gap-2",children:[e.jsx("button",{onClick:()=>{j.mutate({emailId:n.id,data:{email:n.email,label:n.label||void 0}})},disabled:j.isPending,className:"px-3 py-1.5 bg-primary-600 text-white text-sm rounded-lg hover:bg-primary-700 transition disabled:opacity-50",children:"Save"}),e.jsx("button",{onClick:()=>u(null),className:"px-3 py-1.5 text-gray-700 bg-white border border-gray-300 text-sm rounded-lg hover:bg-gray-50 transition",children:"Cancel"})]})]}):e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("div",{className:`w-2 h-2 rounded-full ${t.is_active?"bg-green-500":"bg-gray-300"}`}),e.jsxs("div",{children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("span",{className:"text-sm font-medium text-gray-900",children:t.email}),t.label&&e.jsx("span",{className:"text-xs px-2 py-0.5 bg-gray-100 text-gray-600 rounded-full",children:t.label})]}),e.jsxs("span",{className:"text-xs text-gray-500",children:["Added ",q(new Date(t.created_at),"MMM d, yyyy")]})]})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("button",{onClick:()=>J(t),disabled:j.isPending,className:`px-3 py-1.5 text-xs font-medium rounded-lg transition ${t.is_active?"bg-green-100 text-green-700 hover:bg-green-200":"bg-gray-100 text-gray-600 hover:bg-gray-200"}`,title:t.is_active?"Click to disable":"Click to enable",children:t.is_active?"Active":"Inactive"}),e.jsx("button",{onClick:()=>u(t),className:"p-1.5 text-gray-400 hover:text-gray-600 hover:bg-gray-100 rounded-lg transition",title:"Edit",children:e.jsx("svg",{className:"w-4 h-4",fill:"none",viewBox:"0 0 24 24",stroke:"currentColor",children:e.jsx("path",{strokeLinecap:"round",strokeLinejoin:"round",strokeWidth:2,d:"M15.232 5.232l3.536 3.536m-2.036-5.036a2.5 2.5 0 113.536 3.536L6.5 21.036H3v-3.572L16.732 3.732z"})})}),e.jsx("button",{onClick:()=>G(t.id),disabled:z.isPending,className:"p-1.5 text-gray-400 hover:text-red-600 hover:bg-red-50 rounded-lg transition",title:"Remove",children:e.jsx("svg",{className:"w-4 h-4",fill:"none",viewBox:"0 0 24 24",stroke:"currentColor",children:e.jsx("path",{strokeLinecap:"round",strokeLinejoin:"round",strokeWidth:2,d:"M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"})})})]})]})},t.id))})]})]})}export{ce 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.