1import{n as _,o as P,p as $,q as w,s as M,v as E,r as j,j as e,V as C}from"./index-DoLX-udD-b1790897239410093205.js";import{c as b}from"./tags-C2qrJ65a-b1790897239410093205.js";import"./Button-BaOymhoh-b1790897239410093205.js";import"./Select-BqCT1Q-m-b1790897239410093205.js";import"./Textarea-PDFE3i3N-b1790897239410093205.js";import{S as N}from"./StatePanel-DiuVOXZm-b1790897239410093205.js";import"./circle-help-C2JhjJQb-b1790897239410093205.js";import"./info-Cy4Dldve-b1790897239410093205.js";import"./circle-check-BumeyqLo-b1790897239410093205.js";import"./triangle-alert-FWYLBM1E-b1790897239410093205.js";const D=async t=>w(`${E.CRM.DUP_CONTACTS}?workspace=${t}`,M.get),I=(t,a={})=>_({queryKey:[b.duplicatesContacts,t],queryFn:()=>D(t),enabled:!!t,...a}),T=async t=>w(`${E.CRM.DUP_COMPANIES}?workspace=${t}`,M.get),A=(t,a={})=>_({queryKey:[b.duplicatesCompanies,t],queryFn:()=>T(t),enabled:!!t,...a}),K=(t={})=>{const a=P();return $({mutationFn:d=>w(E.CRM.MERGE_CONTACTS,M.post,d),onSuccess:(...d)=>{a.invalidateQueries({queryKey:[b.duplicatesContacts]}),a.invalidateQueries({queryKey:[b.contacts]}),a.invalidateQueries({queryKey:[b.companies]}),a.invalidateQueries({queryKey:[b.lifecycleStats]}),a.invalidateQueries({queryKey:[b.deals]}),a.invalidateQueries({queryKey:[b.activities]}),t.onSuccess?.(...d)},...t})},Q=(t={})=>{const a=P();return $({mutationFn:d=>w(E.CRM.MERGE_COMPANIES,M.post,d),onSuccess:(...d)=>{a.invalidateQueries({queryKey:[b.duplicatesCompanies]}),a.invalidateQueries({queryKey:[b.companies]}),a.invalidateQueries({queryKey:[b.contacts]}),a.invalidateQueries({queryKey:[b.deals]}),t.onSuccess?.(...d)},...t})},B=[{key:"contacts",label:"Contacts"},{key:"companies",label:"Companies"}],q={email:"Same email",phone:"Same phone",name:"Same name",domain:"Same domain"},S={email:{bg:"var(--error-bg)",text:"var(--error)"},phone:{bg:"var(--warning-bg)",text:"var(--warning)"},name:{bg:"var(--info-bg)",text:"var(--info)"},domain:{bg:"var(--success-bg)",text:"var(--success)"}},k=t=>t==null||typeof t=="string"&&t.trim()==="",v=t=>[t.firstName,t.lastName].filter(Boolean).join(" ").trim()||t.email||"Unknown contact",O=t=>t?new Date(t).toLocaleDateString("en-US",{month:"short",day:"numeric",year:"numeric"}):"-",F=(t,a)=>{const d=["firstName","lastName","phone","title"],n={firstName:t.firstName||"",lastName:t.lastName||"",email:t.email||"",phone:t.phone||"",title:t.title||""},l={};for(const r of a)for(const m of d)k(n[m])&&!k(r[m])&&(n[m]=r[m],l[m]=r._id);const x=new Set((t.tags||[]).filter(Boolean));a.forEach(r=>(r.tags||[]).forEach(m=>m&&x.add(m)));const c=r=>(t[r]||0)+a.reduce((m,h)=>m+(h[r]||0),0);return{...n,tags:Array.from(x),totalEmailsSent:c("totalEmailsSent"),totalEmailsOpened:c("totalEmailsOpened"),totalReplies:c("totalReplies"),filledFrom:l}},L=(t,a)=>{const d=["domain","industry","size","website"],n={name:t.name||"",domain:t.domain||"",industry:t.industry||"",size:t.size||"",website:t.website||""},l={};for(const c of a)for(const r of d)k(n[r])&&!k(c[r])&&(n[r]=c[r],l[r]=c._id);const x=(t.contactCount||0)+a.reduce((c,r)=>c+(r.contactCount||0),0);return{...n,contactCount:x,filledFrom:l}},ne=({workspaceId:t})=>{const[a,d]=j.useState("contacts");return e.jsxs("div",{className:"h-full flex flex-col p-6 gap-4 overflow-hidden",children:[e.jsx("div",{className:"flex gap-1 flex-shrink-0 border-b border-border",children:B.map(n=>e.jsx("button",{onClick:()=>d(n.key),className:`px-4 py-2 text-[13px] font-medium transition-all border-b-2 ${a===n.key?"text-custom-blue border-custom-blue":"text-text-secondary border-transparent hover:text-black-blue"}`,children:n.label},n.key))}),e.jsx("div",{className:"flex-1 overflow-y-auto",children:a==="contacts"?e.jsx(U,{workspaceId:t}):e.jsx(G,{workspaceId:t})})]})},U=({workspaceId:t})=>{const{data:a,isLoading:d,isError:n,error:l,refetch:x}=I(t),[c,r]=j.useState(null),m=K({onSuccess:i=>{C.success(`Merged ${i?.mergedCount||""} duplicate contact(s)`.trim()),r(null),x()},onError:i=>{C.error(i?.data?.error||i?.message||"Merge failed")}}),h=a?.clusters||[],g=(i,p)=>{m.mutate({workspaceId:t,primaryId:i,duplicateIds:p})};
1return d?e.jsx(N,{status:"loading",title:"Scanning for duplicates...",compact:!0}):n?e.jsx(N,{status:"error",title:l?.data?.error||l?.message||"Failed to load duplicates",compact:!0}):h.length===0?e.jsx(N,{status:"empty",title:"No duplicate contacts found",description:"Contacts are matched by email, phone, and name."}):e.jsxs("div",{className:"space-y-4",children:[e.jsxs("p",{className:"text-[12px] text-text-secondary",children:["Found ",h.length," potential duplicate group",h.length>1?"s":"",". Choose a primary record and the duplicates to merge into it."]}),h.map((i,p)=>e.jsx(z,{cluster:i,onRequestMerge:(u,y,s)=>r({primaryId:u,duplicateIds:y,preview:s}),isMerging:m.isPending},`contact-cluster-${p}`)),c&&e.jsx(R,{title:"Merge contacts?",count:c.duplicateIds.length,previewLabel:v(c.preview),onCancel:()=>r(null),onConfirm:()=>g(c.primaryId,c.duplicateIds),isMerging:m.isPending})]})},z=({cluster:t,onRequestMerge:a,isMerging:d})=>{const n=t.members||[],[l,x]=j.useState(()=>String(n[0]?._id)),[c,r]=j.useState(()=>{const s={};return n.forEach(o=>{s[String(o._id)]=!0}),s}),m=s=>{x(s),r(o=>({...o,[s]:!1}))},h=s=>{s!==l&&r(o=>({...o,[s]:!o[s]}))},g=n.find(s=>String(s._id)===l)||n[0],i=n.filter(s=>String(s._id)!==l&&c[String(s._id)]),p=i.map(s=>String(s._id)),u=j.useMemo(()=>F(g,i),[g,i]),y=S[t.matchedBy]||S.name;return e.jsxs("div",{className:"rounded-xl border border-border overflow-hidden",children:[e.jsxs("div",{className:"flex items-center gap-2 px-4 py-2.5 bg-surface-muted border-b border-border",children:[e.jsx("span",{className:"px-2 py-0.5 rounded-full text-[10px] font-semibold",style:{backgroundColor:y.bg,color:y.text},children:q[t.matchedBy]||t.matchedBy}),e.jsxs("span",{className:"text-[12px] text-text-secondary",children:[n.length," matching records"]})]}),e.jsxs("div",{className:"grid grid-cols-12 px-4 py-2 text-[10px] font-semibold text-text-muted uppercase tracking-wider border-b border-border",children:[e.jsx("div",{className:"col-span-1",children:"Primary"}),e.jsx("div",{className:"col-span-1",children:"Merge"}),e.jsx("div",{className:"col-span-4",children:"Name / Email"}),e.jsx("div",{className:"col-span-2",children:"Phone"}),e.jsx("div",{className:"col-span-2",children:"Stage"}),e.jsx("div",{className:"col-span-2 text-right",children:"Engagement"})]}),n.map(s=>{const o=String(s._id),f=o===l;return e.jsxs("div",{className:`grid grid-cols-12 items-center px-4 py-2.5 border-b border-border text-[12px] ${f?"bg-custom-blue/5":"hover:bg-surface-muted"}`,children:[e.jsx("div",{className:"col-span-1",children:e.jsx("input",{type:"radio",name:`primary-${n[0]?._id}`,checked:f,onChange:()=>m(o),className:"accent-custom-blue w-4 h-4 cursor-pointer","aria-label":`Use ${v(s)} as primary contact`})}),e.jsx("div",{className:"col-span-1",children:e.jsx("input",{type:"checkbox",checked:!f&&!!c[o],disabled:f,onChange:()=>h(o),className:"accent-custom-blue w-4 h-4 cursor-pointer disabled:opacity-30","aria-label":`Merge ${v(s)} into primary contact`})}),e.jsxs("div",{className:"col-span-4 min-w-0",children:[e.jsx("p",{className:"font-medium text-black-blue truncate",children:v(s)}),e.jsx("p",{className:"text-[11px] text-text-muted truncate",children:s.email||"-"})]}),e.jsx("div",{className:"col-span-2 text-text-secondary truncate",children:s.phone||"-"}),e.jsx("div",{className:"col-span-2 text-text-secondary truncate",children:s.lifecycleStage||"-"}),e.jsxs("div",{className:"col-span-2 text-right text-text-secondary",children:[s.totalEmailsSent||0," sent · ",s.totalReplies||0," repl."]})]},o)}),e.jsxs("div",{className:"px-4 py-3 bg-app flex items-start gap-4",children:[e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsx("p",{className:"text-[10px] font-semibold text-text-muted uppercase tracking-wider mb-1",children:"Resulting record"}),e.jsx("p",{className:"text-[13px] font-medium text-black-blue",children:v(u)}),e.jsx("p",{className:"text-[11px] text-text-secondary",children:u.email||"-"}),e.jsxs("div",{className:"flex flex-wrap gap-x-4 gap-y-1 mt-1.5 text-[11px] text-text-secondary",children:[e.jsxs("span",{children:["Phone: ",e.jsx("span",{className:"text-black-blue",children:u.phone||"-"})]}),e.jsxs("span",{children:["Title: ",e.jsx("span",{className:"text-black-blue",children:u.title||"-"})]}),e.jsxs("span",{children:["Emails sent: ",e.jsx("span",{className:"text-black-blue",children:u.totalEmailsSent})]}),e.jsxs("span",{children:["Opens: ",e.jsx("span",{className:"text-black-blue",children:u.totalEmailsOpened})]}),e.jsxs("span",{children:["Replies: ",e.jsx("span",{className:"text-black-blue",children:u.totalReplies})]})]}),u.tags.length>0&&e.jsx("div",{className:"flex flex-wrap gap-1 mt-2",children:u.tags.map(s=>e.jsx("span",{className:"px-2 py-0.5 bg-accent-soft text-accent rounded-full text-[10px]",children:s},s))})]}),e.jsxs("button",{onClick:()=>a(l,p,u),disabled:p.length===0||d,className:"px-4 py-2 bg-custom-blue text-white text-[12px] font-medium rounded-full hover:bg-custom-blue/90 disabled:opacity-40 flex-shrink-0",children:["Merge ",p.length>0?`(${p.length})`:""]})]})]})},G=({workspaceId:t})=>{const{data:a,isLoading:d,isError:n,error:l,refetch:x}=A(t),[c,r]=j.useState(null),m=Q({onSuccess:i=>{C.success(`Merged ${i?.mergedCount||""} duplicate compan${(i?.mergedCount||0)>1?"ies":"y"}`.trim()),r(null),x()},onError:i=>{C.error(i?.data?.error||i?.message||"Merge failed")}}),h=a?.clusters||[],g=(i,p)=>{m.mutate({workspaceId:t,primaryId:i,duplicateIds:p})};
1return d?e.jsx(N,{status:"loading",title:"Scanning for duplicates...",compact:!0}):n?e.jsx(N,{status:"error",title:l?.data?.error||l?.message||"Failed to load duplicates",compact:!0}):h.length===0?e.jsx(N,{status:"empty",title:"No duplicate companies found",description:"Companies are matched by domain and name."}):e.jsxs("div",{className:"space-y-4",children:[e.jsxs("p",{className:"text-[12px] text-text-secondary",children:["Found ",h.length," potential duplicate group",h.length>1?"s":"",". Choose a primary record and the duplicates to merge into it."]}),h.map((i,p)=>e.jsx(H,{cluster:i,onRequestMerge:(u,y,s)=>r({primaryId:u,duplicateIds:y,preview:s}),isMerging:m.isPending},`company-cluster-${p}`)),c&&e.jsx(R,{title:"Merge companies?",count:c.duplicateIds.length,previewLabel:c.preview.name||"company",onCancel:()=>r(null),onConfirm:()=>g(c.primaryId,c.duplicateIds),isMerging:m.isPending})]})},H=({cluster:t,onRequestMerge:a,isMerging:d})=>{const n=t.members||[],[l,x]=j.useState(()=>String(n[0]?._id)),[c,r]=j.useState(()=>{const s={};return n.forEach(o=>{s[String(o._id)]=!0}),s}),m=s=>{x(s),r(o=>({...o,[s]:!1}))},h=s=>{s!==l&&r(o=>({...o,[s]:!o[s]}))},g=n.find(s=>String(s._id)===l)||n[0],i=n.filter(s=>String(s._id)!==l&&c[String(s._id)]),p=i.map(s=>String(s._id)),u=j.useMemo(()=>L(g,i),[g,i]),y=S[t.matchedBy]||S.name;return e.jsxs("div",{className:"rounded-xl border border-border overflow-hidden",children:[e.jsxs("div",{className:"flex items-center gap-2 px-4 py-2.5 bg-surface-muted border-b border-border",children:[e.jsx("span",{className:"px-2 py-0.5 rounded-full text-[10px] font-semibold",style:{backgroundColor:y.bg,color:y.text},children:q[t.matchedBy]||t.matchedBy}),e.jsxs("span",{className:"text-[12px] text-text-secondary",children:[n.length," matching records"]})]}),e.jsxs("div",{className:"grid grid-cols-12 px-4 py-2 text-[10px] font-semibold text-text-muted uppercase tracking-wider border-b border-border",children:[e.jsx("div",{className:"col-span-1",children:"Primary"}),e.jsx("div",{className:"col-span-1",children:"Merge"}),e.jsx("div",{className:"col-span-4",children:"Company"}),e.jsx("div",{className:"col-span-3",children:"Domain"}),e.jsx("div",{className:"col-span-1 text-right",children:"Contacts"}),e.jsx("div",{className:"col-span-2 text-right",children:"Created"})]}),n.map(s=>{const o=String(s._id),f=o===l;return e.jsxs("div",{className:`grid grid-cols-12 items-center px-4 py-2.5 border-b border-border text-[12px] ${f?"bg-custom-blue/5":"hover:bg-surface-muted"}`,children:[e.jsx("div",{className:"col-span-1",children:e.jsx("input",{type:"radio",name:`primary-company-${n[0]?._id}`,checked:f,onChange:()=>m(o),className:"accent-custom-blue w-4 h-4 cursor-pointer","aria-label":`Use ${s.name||"company"} as primary company`})}),e.jsx("div",{className:"col-span-1",children:e.jsx("input",{type:"checkbox",checked:!f&&!!c[o],disabled:f,onChange:()=>h(o),className:"accent-custom-blue w-4 h-4 cursor-pointer disabled:opacity-30","aria-label":`Merge ${s.name||"company"} into primary company`})}),e.jsxs("div",{className:"col-span-4 min-w-0",children:[e.jsx("p",{className:"font-medium text-black-blue truncate",children:s.name||"Unnamed company"}),e.jsx("p",{className:"text-[11px] text-text-muted truncate",children:s.industry||"-"})]}),e.jsx("div",{className:"col-span-3 text-text-secondary truncate",children:s.domain||"-"}),e.jsx("div",{className:"col-span-1 text-right text-text-secondary",children:s.contactCount||0}),e.jsx("div",{className:"col-span-2 text-right text-text-secondary",children:O(s.createdAt)})]},o)}),e.jsxs("div",{className:"px-4 py-3 bg-app flex items-start gap-4",children:[e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsx("p",{className:"text-[10px] font-semibold text-text-muted uppercase tracking-wider mb-1",children:"Resulting record"}),e.jsx("p",{className:"text-[13px] font-medium text-black-blue",children:u.name||"Unnamed company"}),e.jsxs("div",{className:"flex flex-wrap gap-x-4 gap-y-1 mt-1.5 text-[11px] text-text-secondary",children:[e.jsxs("span",{children:["Domain: ",e.jsx("span",{className:"text-black-blue",children:u.domain||"-"})]}),e.jsxs("span",{children:["Industry: ",e.jsx("span",{className:"text-black-blue",children:u.industry||"-"})]}),e.jsxs("span",{children:["Size: ",e.jsx("span",{className:"text-black-blue",children:u.size||"-"})]}),e.jsxs("span",{children:["Website: ",e.jsx("span",{className:"text-black-blue",children:u.website||"-"})]}),e.jsxs("span",{children:["Contacts: ",e.jsx("span",{className:"text-black-blue",children:u.contactCount})]})]})]}),e.jsxs("button",{onClick:()=>a(l,p,u),disabled:p.length===0||d,className:"px-4 py-2 bg-custom-blue text-white text-[12px] font-medium rounded-full hover:bg-custom-blue/90 disabled:opacity-40 flex-shrink-0",children:["Merge ",p.length>0?`(${p.length})`:""]})]})]})},R=({title:t,count:a,previewLabel:d,onCancel:n,onConfirm:l,isMerging:x})=>e.jsx("div",{className:"fixed inset-0 bg-black/30 z-50 flex items-center justify-center",onClick:n,children:e.jsxs("div",{className:"bg-surface rounded-2xl w-[440px] p-6",onClick:c=>c.stopPropagation(),children:[e.jsx("h2",{className:"text-base font-semibold text-black-blue mb-2",children:t}),e.jsxs("p",{className:"text-[13px] text-text-secondary",children:["This will merge ",a," duplicate record",a>1?"s":""," into"," ",e.jsx("span",{className:"font-medium text-black-blue",children:d}
1),". Related deals, tasks, activities and meetings will be reassigned, and the duplicates will be removed. This cannot be undone."]}),e.jsxs("div",{className:"flex justify-end gap-3 mt-5",children:[e.jsx("button",{onClick:n,disabled:x,className:"px-4 py-2 text-[13px] text-text-secondary border border-border rounded-full hover:bg-surface-muted disabled:opacity-50",children:"Cancel"}),e.jsx("button",{onClick:l,disabled:x,className:"px-4 py-2 text-[13px] text-white bg-custom-blue rounded-full hover:bg-custom-blue/90 disabled:opacity-50",children:x?"Merging...":"Confirm Merge"})]})]})});export{ne 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.