1import{j as e}from"./flow-JrIpjoTW.js";import{a as p}from"./redux-Biq_wK1H.js";import{u as U,a as T,aT as q,aU as F,cQ as Y,cR as _,J as X,c2 as $,M as w,aV as G,i as H,d as I,S as J,aW as K,aX as Q,aY as z}from"./index-BhLFEl11.js";import{B as Z}from"./badge-CwYFm3iH.js";import{S as k}from"./switch-BSaIgGsq.js";import{S,a as P,b as A,c as E,d as L}from"./select-C_UrqmHu.js";import{P as ee}from"./phone-BvDc-My5.js";import{C as B}from"./circle-check-BVcpJdNq.js";import{C as te}from"./circle-x-DC5KHGje.js";import{I as ae}from"./info-gJZVkjoF.js";import{L as se}from"./loader-circle-DlGIpbbs.js";import"./charts-BbFShRtf.js";import"./react-vendor-CWrwEdUg.js";import"./chevron-down-DydTYHrR.js";import"./chevron-up-J05RqcHp.js";function M(s){return s.body?s.body:s.components?.find(n=>n.type==="BODY")?.text??""}function ne(s){if(s.requiredParams)return s.requiredParams.filter(o=>o.componentType==="body");const n=M(s).match(/\{\{\d+\}\}/g);return n?n.map((o,d)=>({componentType:"body",index:d,paramName:`variable_${d+1}`})):[]}function oe(s){return s.split(/(\{\{\d+\}\})/g).map((n,o)=>/\{\{\d+\}\}/.test(n)?e.jsx("span",{className:"inline-block rounded bg-primary/15 px-1 py-0.5 text-xs font-semibold text-primary",children:n},o):e.jsx("span",{children:n},o))}function ve(){const s=U(),{status:u,selectedPhoneNumberId:n,hasFetched:o}=T(t=>t.whatsappConnect),{templates:d,isLoading:h}=T(t=>t.templateManager),{configs:f,isLoading:g}=T(t=>t.templateConfig),[j,i]=p.useState([]),N=p.useRef({}),y=(u?.connections??[]).filter(t=>t.status==="active"),R=u?.connected===!0&&y.length>0;p.useEffect(()=>{o||s(q())},[o,s]),p.useEffect(()=>{n&&(s(F({status:"APPROVED",phoneNumberId:n})),s(Y(n)))},[n,s]),p.useEffect(()=>{if(h||g)return;const t=d.filter(a=>a.status==="APPROVED"),r=new Map;for(const a of f)r.set(a.templateName,a);const c=t.map(a=>{const l=r.get(a.name);return{template:a,config:{id:l?.id,saveContact:l?.saveContact??!1,nameIndex:l?.nameIndex??0}}});i(c)},[d,f,h,g]);const v=p.useCallback(t=>{if(!n)return;const r=t.template.name;N.current[r]&&clearTimeout(N.current[r]),N.current[r]=setTimeout(async()=>{i(c=>c.map(a=>a.template.name===r?{...a,saving:!0,saved:!1}:a));try{await s(_({templateName:t.template.name,templateId:t.template.id,phoneNumberId:n,saveContact:t.config.saveContact,nameIndex:t.config.nameIndex})).unwrap(),i(c=>c.map(a=>a.template.name===r?{...a,saving:!1,saved:!0}:a)),setTimeout(()=>{i(c=>c.map(a=>a.template.name===r?{...a,saved:!1}:a))},2e3)}catch{const c=f.find(a=>a.templateName===r);i(a=>a.map(l=>l.template.name===r?{...l,saving:!1,config:{id:c?.id,saveContact:c?.saveContact??!1,nameIndex:c?.nameIndex??0}}:l)),X.error("Failed to save configuration. Please try again.")}},500)},[n,s,f]),V=p.useCallback((t,r)=>{i(c=>{const a=c.map(x=>x.template.name===t?{...x,config:{...x.config,saveContact:r,nameIndex:r?x.config.nameIndex:0}}:x),l=a.find(x=>x.template.name===t);return l&&v(l),a})},[v]),D=p.useCallback((t,r)=>{const c=parseInt(r,10);i(a=>{const l=a.map(b=>b.template.name===t?{...b,config:{...b.config,nameIndex:c}}:b),x=l.find(b=>b.template.name===t);return x&&v(x),l})},[v]),O=p.useCallback(t=>{s($(t))},[s]),C=p.useMemo(()=>y.find(t=>t.phoneNumberId===n),[y,n]),W=h||g;return o&&!R?e.jsxs("div",{className:"flex flex-1 flex-col items-center justify-center gap-4 p-8",children:[e.jsx("div",{className:"flex h-16 w-16 items-center justify-center rounded-full bg-muted",children:e.jsx(w,{className:"h-8 w-8 text-muted-foreground"})}),e.jsx("h2",{className:"text-lg font-semibold",children:"WhatsApp Not Connected"}),e.jsx("p",{className:"max-w-md text-center text-sm text-muted-foreground",children:"Connect your WhatsApp Business account first to configure contact capture for your templates."})]}):e.jsx(G,{children:e.jsxs("div",{className:"flex flex-1 flex-col gap-6 p-6",children:[e.jsxs("div",{className:"flex flex-col gap-1",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(H,{className:"h-5 w-5 text-primary"}),e.jsx("h1",{className:"text-xl font-semibold",children:"Contact Capture"})]}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Configure which WhatsApp templates auto-save recipients as customers."})]}),e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx(ee,{className:"h-4 w-4 text-muted-foreground"}),e.jsxs(S,{value:n??"",onValueChange:O,children:[e.jsx(P,{className:"w-72",children:e.jsx(A,{placeholder:"Select phone number"})}),e.jsx(E,{children:y.map(t=>e.jsxs(L,{value:t.phoneNumberId,children:[t.displayPhoneNumber," â ",t.verifiedName]},t.phoneNumberId))})]}),C&&e.jsxs(Z,{variant:"outline",className:I("gap-1",C.status==="active"?"border-green-500/30 bg-green-500/10 text-green-600":"border-red-500/30 bg-red-500/10 text-red-600"),children:[C.status==="active"?e.jsx(B,{className:"h-3 w-3"}):e.jsx(te,{className:"h-3 w-3"}),C.status==="active"?"Connected":"Disconnected"]})]}),e.jsxs("div",{className:"flex items-start gap-2 rounded-lg border border-blue-200 bg-blue-50 p-3 dark:border-blue-800 dark:bg-blue-950/30",children:[e.jsx(ae,{className:"mt-0.5 h-4 w-4 shrink-0 text-blue-600 dar
1k:text-blue-400"}),e.jsxs("p",{className:"text-sm text-blue-700 dark:text-blue-300",children:["When ",e.jsx("strong",{children:"Save Contact"})," is ON, any recipient of this template gets auto-saved as a customer. The variable you pick becomes the customer's name. If the number already exists, it's updated."]})]}),W?e.jsx("div",{className:"space-y-3",children:Array.from({length:5}).map((t,r)=>e.jsx(J,{className:"h-16 w-full rounded-lg"},r))}):j.length===0?e.jsxs("div",{className:"flex flex-col items-center gap-3 py-16 text-center",children:[e.jsx(w,{className:"h-10 w-10 text-muted-foreground/50"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"No approved templates found for this phone number."})]}):e.jsx("div",{className:"overflow-auto rounded-lg border",children:e.jsxs("table",{className:"w-full text-sm",children:[e.jsx("thead",{children:e.jsxs("tr",{className:"border-b bg-muted/50",children:[e.jsx("th",{className:"px-4 py-3 text-left font-medium",children:"Template"}),e.jsx("th",{className:"px-4 py-3 text-left font-medium",children:"Category"}),e.jsx("th",{className:"px-4 py-3 text-left font-medium",children:"Language"}),e.jsx("th",{className:"px-4 py-3 text-left font-medium",children:"Body Preview"}),e.jsx("th",{className:"px-4 py-3 text-center font-medium",children:"Save Contact"}),e.jsx("th",{className:"px-4 py-3 text-left font-medium",children:"Name Variable"}),e.jsx("th",{className:"w-12 px-4 py-3"})]})}),e.jsx("tbody",{children:j.map(t=>e.jsx(re,{row:t,onToggleSaveContact:V,onNameIndexChange:D},t.template.id))})]})})]})})}function re({row:s,onToggleSaveContact:u,onNameIndexChange:n}){const{template:o,config:d,saving:h,saved:f}=s,g=M(o),j=ne(o),i=j.length>0,N={MARKETING:"bg-purple-100 text-purple-700 dark:bg-purple-900/30 dark:text-purple-300",UTILITY:"bg-blue-100 text-blue-700 dark:bg-blue-900/30 dark:text-blue-300",AUTHENTICATION:"bg-orange-100 text-orange-700 dark:bg-orange-900/30 dark:text-orange-300"};return e.jsxs("tr",{className:"border-b last:border-0 hover:bg-muted/30 transition-colors",children:[e.jsx("td",{className:"px-4 py-3",children:e.jsx("span",{className:"font-medium",children:o.name})}),e.jsx("td",{className:"px-4 py-3",children:e.jsx("span",{className:I("inline-block rounded-full px-2 py-0.5 text-xs font-medium",N[o.category]??"bg-muted text-foreground"),children:o.category})}),e.jsx("td",{className:"px-4 py-3 text-muted-foreground",children:o.language}),e.jsx("td",{className:"max-w-xs px-4 py-3",children:e.jsx("p",{className:"line-clamp-2 text-xs text-muted-foreground leading-relaxed",children:g?oe(g):"â"})}),e.jsx("td",{className:"px-4 py-3 text-center",children:i?e.jsx(k,{checked:d.saveContact,onCheckedChange:m=>u(o.name,m)}):e.jsxs(K,{children:[e.jsx(Q,{asChild:!0,children:e.jsx("div",{className:"inline-flex",children:e.jsx(k,{disabled:!0,checked:!1})})}),e.jsx(z,{children:"No body variables"})]})}),e.jsx("td",{className:"px-4 py-3",children:i?e.jsxs(S,{value:String(d.nameIndex),onValueChange:m=>n(o.name,m),disabled:!d.saveContact,children:[e.jsx(P,{className:I("h-8 w-48 text-xs",!d.saveContact&&"opacity-50"),children:e.jsx(A,{})}),e.jsx(E,{children:j.map(m=>e.jsxs(L,{value:String(m.index),children:[`{{${m.index+1}}}`," â ",m.example||m.paramName]},m.index))})]}):e.jsx("span",{className:"text-xs text-muted-foreground",children:"â"})}),e.jsxs("td",{className:"px-4 py-3",children:[h&&e.jsx(se,{className:"h-4 w-4 animate-spin text-muted-foreground"}),f&&!h&&e.jsx(B,{className:"h-4 w-4 text-green-500"})]})]})}export{ve as ContactCapturePage};
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.