1import{j as a}from"./vendor-query-DUki8u-D.js";import{f as Ga,r as l}from"./vendor-react-DHU755GE.js";import{P as Ja}from"./PaginationToolbar-DHFtabo0.js";import{S as Ya}from"./SyncedTopScrollbar-JfL0pt5Z.js";import{supabase as C}from"./client-Dy2P3he-.js";import{w as Q,B as we,aE as ue,aF as me,aG as pe,g as Qa,r as Ka,o as ve,A as Za,C as Wa,E as Xa,F as et,aJ as at,I as tt,s as ke,h as ta}from"./App-DNURBdOM.js";import{C as sa}from"./card-B_TKe1rC.js";import{T as st,a as lt,b as la,c as b,d as nt,e as j}from"./table-BpuyeSw8.js";import{S as na,a as ia,b as ra,c as oa,d as ne}from"./select-BJ2LQ1BA.js";import{S as P}from"./searchable-select-DG_xrA70.js";import{T as it,a as rt,b as ot}from"./tabs-DO2x6V1p.js";import{R as ct}from"./RowActionsMenu-C4dUUzwJ.js";import{a as ga,P as ba,Q as dt,C as ut,L as mt,A as pt,S as ft,b as ht,c as xt,R as _t,E as gt,d as bt}from"./CrmMasterDialog-CJ4gjN9w.js";import{S as jt}from"./StatusBadge-BvY96cm0.js";import{L as vt}from"./LeadSourceCell-alR3Qu2s.js";import{V as wt}from"./ViewPartnerDialog-CTVmy7Wg.js";import{C as St}from"./CrmSubNav-DV7jUxDQ.js";import{C as Nt}from"./crm-vocab-DnrV6RXz.js";import{a as Ct}from"./lead-est-recovery-DzLSo3WB.js";import{i as ca,a as da,b as ua}from"./claim-buckets-Cl_CKEqx.js";import{A as yt,R as kt,n as At,a as Tt}from"./claim-constants-F6TZxwZ5.js";import{detectTestClaim as Ae}from"./test-claim-detector-C7yWOzH7.js";import{v as ma,b$ as Lt,cn as ja,cp as Dt,T as va,ae as wa,bH as Et,ca as Sa,cc as Pt,ap as pa,a0 as Ft,S as Rt,n as Mt,B as fa,j as Ot,cG as Bt,aB as It,aJ as Ut,J as qt,cm as zt,co as Ht,cj as $t,U as Vt,E as Gt}from"./vendor-icons-D7GmeyGG.js";import{f as K}from"./formatDistanceToNowStrict-B0_ltmaX.js";import{u as Jt}from"./useLeadCrmSummary-D5Voli55.js";import{c as Yt}from"./chunked-in-CCvlGUWb.js";import"./vendor-supabase-D2XPrbcR.js";import"./vendor-pdf-DHUBMtDJ.js";import"./error-reporter-EYiXaqJQ.js";import"./breadcrumbs-CN3GC8vy.js";import"./vendor-charts-DsctC1NR.js";import"./useTheme-DEq4C3Xf.js";import"./vendor-motion-DoyUfQ58.js";import"./index-BdQq_4o_.js";import"./index-BFcYZw7s.js";import"./popover-DAwJgSl1.js";import"./command-CBhwoYW6.js";import"./useCrmMutations-CTTMxhps.js";import"./label-CQkxHeEJ.js";import"./textarea-Pc8lpgfp.js";import"./calendar-BbCNGX3_.js";import"./isSameDay-BWv5-M0E.js";import"./format-D9x0XWGR.js";import"./en-US-BqCXNUOZ.js";import"./subDays-BK6vuqdz.js";import"./startOfMonth-D3_3nocY.js";import"./endOfMonth-B_n1vX73.js";import"./addMonths-Dw7XJecM.js";import"./differenceInCalendarMonths-BSRE4gQg.js";import"./separator-BGBKlkSh.js";import"./quo-deeplink-CB_LA5H-.js";import"./org-badge-CS0i4_0U.js";import"./async-partner-select-D7bPdIxE.js";import"./CommissionTreeView-Cgs93f7s.js";import"./useCommissionSettings-DzLA0olu.js";import"./CommissionWaterfallCard-DAdmpulL.js";import"./switch-D3dXx0E2.js";import"./commission-simulator-BnG1RJMf.js";import"./AgreementRowActions-BcX7NCD4.js";import"./agreement-pdf-B8dWl2Jr.js";import"./AgreementViewer-BacWGjDw.js";import"./checkbox-eIxR81FC.js";import"./scroll-area-C5uKDqac.js";import"./SignaturePad-Ca2_wxMG.js";import"./stored-agreement-pdf-CVgd7AlI.js";import"./agreement-filename-9SsE_bIj.js";import"./stored-partner-agreement-pdf--8-5SUD8.js";import"./agreement-tokens-HSUqObXi.js";import"./parseISO-CIZr9Bmv.js";import"./comp-display-COSnfmiM.js";import"./alert-DrXqJ_mR.js";import"./tra-house-D-y556jm.js";import"./alert-dialog-COU3YwVl.js";import"./radio-group-CRWAIIDJ.js";import"./slider-yFQijcgx.js";import"./date-picker-DXAtzP_K.js";import"./usePartnerRates-gt02cpVw.js";import"./friendly-password-Chc-6VHn.js";import"./getRoundingMethod-Cs3-IipQ.js";const ha={success:"bg-success/10 text-success border-success/20",info:"bg-primary/10 text-primary border-primary/20",warning:"bg-gold/15 text-gold border-gold/30",critical:"bg-destructive/10 text-destructive border-destructive/30"};function Qt({lead:r,summary:s,onOpenCrm:A,showStatus:x=!0,maxTags:M=3,className:ie}){var re;const T=r.next_follow_up_at?new Date(r.next_follow_up_at):null,_=T&&T<new Date,O=(s==null?void 0:s.pinnedNote)||(s==null?void 0:s.latestNote),V=(s==null?void 0:s.tags)??[],w=s==null?void 0:s.latestActivity;return!x&&!O&&!(s!=null&&s.openTasks)&&!T&&!V.length&&!w?a.jsx("div",{className:Q("max-w-[240px]",ie),children:a.jsxs(we,{size:"sm",variant:"ghost",className:"h-6 px-1.5 text-[10px] text-muted-foreground hover:text-primary",onClick:m=>{m.stopPropagation(),A()},children:[a.jsx(ma,{className:"h-3 w-3 mr-1"}),"Open CRM"]})}):a.jsxs("div",{className:Q("max-w-[240px] space-y-1",ie),children:[x&&(r.crm_status||r.priority)&&a.jsxs("div",{className:"flex flex-wrap items-center gap-1",children:[r.crm_status&&a.jsx(ga,{value:r.crm_status}),r.priority&&r.priority!=="normal"&&a.jsx(ba,{value:r.priority})]}),O&&a.jsxs(ue,{children:[a.jsx(me,{asChild:!0,children:a.jsxs("div",{className:"flex items-start gap-1 text-[11px] text-foreground
1/80",children:[s!=null&&s.pinnedNote?a.jsx(Lt,{className:"h-3 w-3 mt-0.5 text-amber-600 shrink-0"}):a.jsx(ja,{className:"h-3 w-3 mt-0.5 text-muted-foreground shrink-0"}),a.jsx("span",{className:"line-clamp-2 leading-tight",children:O.note})]})}),a.jsxs(pe,{side:"top",className:"text-xs max-w-[320px] whitespace-pre-wrap",children:[s!=null&&s.pinnedNote?"ð Pinned note · ":"Latest note · ",K(new Date(O.created_at),{addSuffix:!0}),a.jsx("div",{className:"mt-1",children:O.note})]})]}),a.jsxs("div",{className:"flex flex-wrap items-center gap-1 text-[10px]",children:[!!(s!=null&&s.openTasks)&&a.jsxs(ue,{children:[a.jsx(me,{asChild:!0,children:a.jsxs("span",{className:Q("inline-flex items-center gap-0.5 rounded-full border px-1.5 py-0.5 font-medium",s.overdueTasks?"bg-destructive/10 text-destructive border-destructive/30":"bg-primary/10 text-primary border-primary/20"),children:[a.jsx(Dt,{className:"h-2.5 w-2.5"}),s.openTasks,s.overdueTasks?a.jsx(va,{className:"h-2.5 w-2.5 ml-0.5"}):null]})}),a.jsxs(pe,{side:"top",className:"text-xs max-w-[260px]",children:[s.openTasks," open task",s.openTasks===1?"":"s",s.overdueTasks?` · ${s.overdueTasks} overdue`:"",((re=s.nextTask)==null?void 0:re.title)&&a.jsxs("div",{className:"mt-1",children:["Next: ",s.nextTask.title,s.nextTask.due_at&&` (${K(new Date(s.nextTask.due_at),{addSuffix:!0})})`]})]})]}),T&&a.jsxs(ue,{children:[a.jsx(me,{asChild:!0,children:a.jsxs("span",{className:Q("inline-flex items-center gap-0.5 rounded-full border px-1.5 py-0.5 font-medium",_?"bg-rose-50 text-rose-700 border-rose-200":"bg-emerald-50 text-emerald-700 border-emerald-200"),children:[a.jsx(wa,{className:"h-2.5 w-2.5"}),K(T,{addSuffix:!0})]})}),a.jsxs(pe,{side:"top",className:"text-xs",children:["Follow-up scheduled for ",T.toLocaleString(),_?" (overdue)":""]})]}),w&&a.jsxs(ue,{children:[a.jsx(me,{asChild:!0,children:a.jsxs("span",{className:"inline-flex items-center gap-0.5 rounded-full border border-border bg-muted px-1.5 py-0.5 text-muted-foreground",children:[a.jsx(Et,{className:"h-2.5 w-2.5"}),w.type.replace(/_/g," ")," ·"," ",K(new Date(w.created_at),{addSuffix:!0})]})}),a.jsxs(pe,{side:"top",className:"text-xs max-w-[280px] whitespace-pre-wrap",children:[a.jsx("div",{className:"font-semibold capitalize",children:w.type.replace(/_/g," ")}),a.jsx("div",{className:"text-[10px] text-muted-foreground",children:new Date(w.created_at).toLocaleString()}),w.notes&&a.jsx("div",{className:"mt-1",children:w.notes})]})]})]}),V.length>0&&a.jsxs("div",{className:"flex flex-wrap items-center gap-1",children:[a.jsx(Sa,{className:"h-2.5 w-2.5 text-muted-foreground"}),V.slice(0,M).map(m=>a.jsxs(ue,{children:[a.jsx(me,{asChild:!0,children:a.jsx("span",{className:Q("inline-flex items-center rounded-full border px-1.5 py-0.5 text-[9px] font-medium whitespace-nowrap max-w-[110px]",ha[m.severity]??ha.info),children:a.jsx("span",{className:"truncate",children:m.label})})}),a.jsxs(pe,{side:"top",className:"text-xs",children:[m.category," · ",m.label]})]},m.id)),V.length>M&&a.jsxs("span",{className:"text-[9px] text-muted-foreground",children:["+",V.length-M]})]}),a.jsxs(we,{size:"sm",variant:"ghost",className:"h-5 px-1 text-[10px] text-muted-foreground hover:text-primary",onClick:m=>{m.stopPropagation(),A()},children:[a.jsx(ma,{className:"h-2.5 w-2.5 mr-1"}),"Open CRM"]})]})}const Kt=[{key:"case",label:"Case #",toggleable:!1},{key:"company",label:"Company / Contact",toggleable:!1},{key:"status",label:"Status",toggleable:!0},{key:"crm_status",label:"CRM Status",toggleable:!0},{key:"owner",label:"Owner",toggleable:!0},{key:"est",label:"Est. Recovery",toggleable:!0},{key:"anchor",label:"Anchor",toggleable:!0},{key:"score",label:"Score",toggleable:!0},{key:"funding_stage",label:"Funding Stage",toggleable:!0},{key:"advance",label:"Advance",toggleable:!0},{key:"source",label:"Source",toggleable:!0},{key:"app_dates",label:"App Dates",toggleable:!0},{key:"created",label:"Created",toggleable:!0},{key:"next_followup",label:"Next Follow-Up",toggleable:!0},{key:"last_activity",label:"Last Activity",toggleable:!0},{key:"crm_snapshot",label:"CRM Snapshot",toggleable:!0}],xa=["bg-blue-100 text-blue-800 border-blue-200","bg-emerald-100 text-emerald-800 border-emerald-200","bg-amber-100 text-amber-800 border-amber-200","bg-vi
1olet-100 text-violet-800 border-violet-200","bg-rose-100 text-rose-800 border-rose-200","bg-cyan-100 text-cyan-800 border-cyan-200","bg-indigo-100 text-indigo-800 border-indigo-200","bg-teal-100 text-teal-800 border-teal-200","bg-fuchsia-100 text-fuchsia-800 border-fuchsia-200","bg-orange-100 text-orange-800 border-orange-200"];function Zt(r){let s=0;for(let A=0;A<r.length;A++)s=s*31+r.charCodeAt(A)>>>0;return xa[s%xa.length]}function _a(r){if(!r)return null;const s=r.first_name,A=r.last_name;if(s||A)return[s,A].filter(Boolean).join(" ");const x=r.full_name||r.display_name;return x&&x.trim()?x.trim():r.email?r.email.split("@")[0]:null}const Wt={all:"All",mine:"My Claims","due-today":"Due Today",overdue:"Overdue",unassigned:"Unassigned",stale:"Stale","high-priority":"High Priority",funding:"Funding","needs-admin":"Needs Admin",dead:"Dead","mine-stale":"My Stale","mine-dead":"My Dead"},Xt=yt.filter(r=>!kt[r]),es=["All","direct","partner","referral","broker"],as=["All","any_intent","standard","no_funding","active_deal"],ts=["All","Yes","No"],ss=["Hide Likely Test","All","Possible+","Likely Test"],ls=[{v:"presubmit",label:"Applying",sublabel:"Intake & onboarding"},{v:"presubmission",label:"Pre-Submission",sublabel:"ID verification & ACE"},{v:"anchor",label:"Processing",sublabel:"Handed off, pre-filing"},{v:"filed",label:"Filed",sublabel:"Protest · CIT · Refund"},{v:"not_ior",label:"Not IOR",sublabel:"Client is not the IOR"},{v:"funding",label:"Funded",sublabel:"Advance opportunities"},{v:"closed",label:"Closed",sublabel:"Paid · Lost · DQ"}];function Ll({preset:r="all"}){const{user:s,isAdmin:A}=Qa(),{dataSourceFilter:x}=Ka(),[M,ie]=Ga(),T=M.get("bucket")||"presubmit",[_,O]=l.useState(T);l.useEffect(()=>{T!==_&&O(T)},[T]),l.useEffect(()=>{const e=new URLSearchParams(M);e.set("bucket",_),ie(e,{replace:!0})},[_]);const V=M.get("open"),w=e=>{const t=new URLSearchParams(M);e?t.set("open",e):t.delete("open"),ie(t,{replace:!0})},[fe,re]=l.useState(null),[m,Te]=l.useState(null),[G,Na]=l.useState({}),[Ca,ya]=l.useState({}),[he,ka]=l.useState({}),[Z,Aa]=l.useState({}),[Se,Ta]=l.useState({}),[La,Da]=l.useState({}),[y,Ea]=l.useState({}),[Ne,Pa]=l.useState([]),[Fa,Ra]=l.useState({}),[Le,De]=l.useState({}),[oe,Ee]=l.useState(""),[W,Pe]=l.useState("All"),[X,Fe]=l.useState("__all__"),[B,Re]=l.useState("All"),[I,Me]=l.useState("All"),[ee,Oe]=l.useState("All"),[ae,Be]=l.useState("All"),[U,Ie]=l.useState("__none__"),[te,Ue]=l.useState("All"),[q,qe]=l.useState("All"),[se,ze]=l.useState("All"),[g,He]=l.useState("Hide Likely Test"),[z,Ma]=l.useState("newest"),[F,Oa]=l.useState(null),[H,R]=l.useState(null),[Ba,Ia]=l.useState({}),$e="tra_admin_crm_hidden_cols",[c,Ve]=l.useState(()=>{try{const e=typeof window<"u"?window.localStorage.getItem($e):null;return e?new Set(JSON.parse(e)):new Set}catch{return new Set}});l.useEffect(()=>{try{window.localStorage.setItem($e,JSON.stringify([...c]))}catch{}},[c]),l.useEffect(()=>{Ve(e=>{const t=new Set(e);return _==="presubmit"&&(t.add("score"),t.add("anchor")),t})},[_]);const L=async()=>{var ea;Te(null);let e=C.from("leads").select("*").order("last_activity_at",{ascending:!1,nullsFirst:!1}).range(0,2999);if(x&&(e=e.eq("data_source",x)),r==="mine"&&(s!=null&&s.id))e=e.eq("assigned_case_manager_id",s.id);else if(r==="unassigned")e=e.is("assigned_case_manager_id",null);else if(r==="due-today"){const n=new Date;n.setHours(0,0,0,0);const u=new Date;u.setHours(23,59,59,999),e=e.gte("next_follow_up_at",n.toISOString()).lte("next_follow_up_at",u.toISOString())}else r==="overdue"?(e=e.not("next_follow_up_at","is",null).order("next_follow_up_at",{ascending:!0}),s!=null&&s.id&&(e=e.eq("assigned_case_manager_id",s.id))):r==="stale"?(e=e.eq("is_stale",!0).neq("is_dead",!0),s!=null&&s.id&&(e=e.eq("assigned_case_manager_id",s.id))):r==="high-priority"?e=e.in("priority",["high","vip"]):r==="needs-admin"?e=e.eq("crm_status","needs_admin_review"):r==="dead"?(e=e.eq("is_dead",!0),s!=null&&s.id&&(e=e.eq("assigned_case_manager_id",s.id))):r==="mine-stale"&&(s!=null&&s.id)?e=e.eq("assigned_case_manager_id",s.id).eq("is_stale",!0).neq("is_dead",!0):r==="mine-dead"&&(s!=null&&s.id)&&(e=e.eq("assigned_case_manager_id",s.id).eq("is_dead",!0));const[t,o,i,d,f,h,k,v,S,p]=await Promise.all([e,C.from("claim_details").select("lead_id, estimated_recovery, qualification_score, funding_stage, refund_amount, external_case_numbers, engagement_fee_pct_override, funding_only").eq("data_source",x),C.from("claim_entries").select("lead_id, ieepa_dut
1ies, estimated_refund").eq("data_source",x),C.from("partner_claim_links").select("lead_id, external_claim_id, partner_id"),C.from("advance_funding").select("*").eq("data_source",x),C.from("anchor_claim_snapshots").select("lead_id").limit(5e3),C.from("brokers").select("id, full_name, company_name, email").eq("data_source",x),C.from("affiliates").select("id, full_name, company, email, phone, slug, organization_id, comp_model, pool_pct, upline_affiliate_id, created_at").eq("data_source",x).limit(2e4),C.from("organizations").select("id, company_short_name, slug, internal_logo_url, internal_icon_url, favicon_url, data_source, is_default").eq("data_source",x),C.from("organization_domains").select("organization_id, domain, is_primary")]);k.error&&console.warn("[CRM] brokers load error",k.error),v.error&&console.warn("[CRM] affiliates load error",v.error),S.error&&console.warn("[CRM] orgs load error",S.error);const D=t.data??[],E={};(o.data??[]).forEach(n=>E[n.lead_id]=n);const J={};(i.data??[]).forEach(n=>{var u;(J[u=n.lead_id]??(J[u]=[])).push(n)});const de={};(d.data??[]).forEach(n=>de[n.lead_id]=n);const Qe={};(f.data??[]).forEach(n=>Qe[n.lead_id]=n);const Ke={};(h.data??[]).forEach(n=>Ke[n.lead_id]=!0);const Ze={};(k.data??[]).forEach(n=>Ze[n.id]=n);const je={};(v.data??[]).forEach(n=>je[n.id]=n);{const n=Array.from(new Set(D.map(u=>u.affiliate_id).filter(u=>u&&!je[u])));n.length&&(await Yt(n,N=>C.from("affiliates").select("id, full_name, company, email, phone, slug, organization_id, comp_model, pool_pct, upline_affiliate_id, created_at").in("id",N))).forEach(N=>{je[N.id]=N})}const ye={};(p.data??[]).forEach(n=>{(!ye[n.organization_id]||n.is_primary)&&(ye[n.organization_id]=n.domain)});const We=D.map(n=>n.id),Y={};if(We.length){const{data:n}=await C.from("lead_activity_log").select("lead_id, new_status, created_at").in("lead_id",We).in("new_status",["submitted_to_processor","filed_with_cbp"]).order("created_at",{ascending:!1});(n??[]).forEach(u=>{var aa;if(!(u!=null&&u.lead_id))return;const N=Y[aa=u.lead_id]||(Y[aa]={});u.new_status==="submitted_to_processor"?N.submitted=u.created_at:u.new_status==="filed_with_cbp"&&(N.filed=u.created_at)});for(const u of D){const N=Y[ea=u.id]||(Y[ea]={});!N.filed&&u.anchor_backed_at&&(N.filed=u.anchor_backed_at)}}Ia(Y),Te(D),Na(E),ya(J),ka(de),Aa(Qe),Ta(Ke),Da(Ze),Ea(je),Pa(S.data??[]),Ra(ye);const Xe=Array.from(new Set(D.map(n=>n.assigned_case_manager_id).filter(Boolean)));if(Xe.length){const{data:n}=await C.from("profiles").select("user_id, full_name, display_name, email").in("user_id",Xe),u={};(n??[]).forEach(N=>{u[N.user_id]=N}),De(u)}else De({})};l.useEffect(()=>{L()},[r,x,s==null?void 0:s.id]);const ce=l.useMemo(()=>m?m.filter(e=>{var v,S,p,D;if(ca(e,G[e.id]))return!1;const t=!!((v=he[e.id])!=null&&v.external_claim_id);if(_==="funding"){if(!da(e,G[e.id],Z[e.id]))return!1}else if(ua(e.status,t)!==_)return!1;if(oe){const E=oe.toLowerCase();if(!((S=e.company_name)!=null&&S.toLowerCase().includes(E)||(p=e.contact_name)!=null&&p.toLowerCase().includes(E)||(D=e.contact_email)!=null&&D.toLowerCase().includes(E)||String(e.case_number??"").includes(E)))return!1}const o=At(e.status||"");if(W!=="All"&&o!==W||X!=="__all__"&&e.crm_status!==X||B!=="All"&&!(B==="referral"&&!!e.affiliate_id||B==="broker"&&!!e.assisted_by_broker_id||e.source===B))return!1;const i=String(e.funding_intent||"").trim(),d=!!Z[e.id];if(I==="any_intent"&&!(i&&i!=="standard"&&i!=="no_funding")||I==="standard"&&i!=="standard"||I==="no_funding"&&!(!i||i==="no_funding")||I==="active_deal"&&!d)return!1;const f=!!Se[e.id];if(ee==="Yes"&&!f||ee==="No"&&f)return!1;const h=e.affiliate_id?y[e.affiliate_id]:null,k=e.organization_id||(h==null?void 0:h.organization_id)||null;if(ae!=="All"&&k!==ae||U!=="All"&&(U==="__none__"&&e.assisted_by_broker_id||U!=="__none__"&&e.assisted_by_broker_id!==U)||te!=="All"&&e.affiliate_id!==te||q!=="All"&&(q==="__none__"&&e.assigned_case_manager_id||q!=="__none__"&&e.assigned_case_manager_id!==q)||se!=="All"&&e.priority!==se)return!1;if(g!=="All"){const E=e.affiliate_id?y[e.affiliate_id]:null,J=Ae(e,E),de=J.level==="strong"||!!e.admin_marked_test;if(g==="Hide Likely Test"&&de||g==="Likely Test"&&!de||g==="Possible+"&&!J.flagged&&!e.admin_marked_test)return!1}return!0}
1):[],[m,_,oe,W,X,B,I,ee,ae,U,te,q,se,g,G,Z,he,Se,y]),Ce=l.useMemo(()=>{const e=[...ce],t=o=>o?new Date(o).getTime():0;return z==="newest"?e.sort((o,i)=>t(i.created_at)-t(o.created_at)):z==="oldest"?e.sort((o,i)=>t(o.created_at)-t(i.created_at)):z==="activity"?e.sort((o,i)=>t(i.last_activity_at)-t(o.last_activity_at)):z==="followup"?e.sort((o,i)=>{const d=t(o.next_follow_up_at),f=t(i.next_follow_up_at);return!d&&!f?0:d?f?d-f:-1:1}):z==="company"&&e.sort((o,i)=>String(o.company_name||"").localeCompare(String(i.company_name||""))),e},[ce,z]),Ua=l.useMemo(()=>{const e={presubmit:0,presubmission:0,anchor:0,filed:0,not_ior:0,refund_received:0,tra_paid:0,funding:0,closed:0};return(m||[]).forEach(t=>{var d;if(ca(t,G[t.id]))return;if(g!=="All"){const f=t.affiliate_id?y[t.affiliate_id]:null,h=Ae(t,f),k=h.level==="strong"||!!t.admin_marked_test;if(g==="Hide Likely Test"&&k||g==="Likely Test"&&!k||g==="Possible+"&&!h.flagged&&!t.admin_marked_test)return}const o=!!((d=he[t.id])!=null&&d.external_claim_id),i=ua(t.status,o);e[i]++,da(t,G[t.id],Z[t.id])&&e.funding++}),e},[m,he,G,Z,g,y]),le=l.useMemo(()=>{const e={},t={},o={};let i=0;return(m||[]).forEach(d=>{if(g!=="All"){const k=d.affiliate_id?y[d.affiliate_id]:null,v=Ae(d,k),S=v.level==="strong"||!!d.admin_marked_test;if(g==="Hide Likely Test"&&S||g==="Likely Test"&&!S||g==="Possible+"&&!v.flagged&&!d.admin_marked_test)return}const f=d.affiliate_id?y[d.affiliate_id]:null,h=d.organization_id||(f==null?void 0:f.organization_id)||null;h&&(e[h]=(e[h]||0)+1),d.assisted_by_broker_id?t[d.assisted_by_broker_id]=(t[d.assisted_by_broker_id]||0)+1:i++,d.affiliate_id&&(o[d.affiliate_id]=(o[d.affiliate_id]||0)+1)}),{org:e,broker:t,brokerNone:i,partner:o}},[m,g,y]),$=(e,t)=>{Oa(e),R(t)},qa=l.useMemo(()=>{const e=new Set;return(m||[]).forEach(t=>{t.assigned_case_manager_id&&e.add(t.assigned_case_manager_id)}),Array.from(e)},[m]),za=()=>{Ee(""),Pe("All"),Fe("__all__"),Re("All"),Me("All"),Oe("All"),Be("All"),Ie("All"),Ue("All"),qe("All"),ze("All"),He("Hide Likely Test")},xe=_==="funding",Ge=_==="presubmit",_e=50,[ge,Je]=l.useState(1),Ha=Math.max(1,Math.ceil(Ce.length/_e));l.useEffect(()=>{Je(1)},[_,oe,W,X,B,I,ee,ae,U,te,q,se,g,z]);const be=l.useMemo(()=>Ce.slice((ge-1)*_e,ge*_e),[Ce,ge]),$a=l.useMemo(()=>be.map(e=>e.id),[be]),Va=Jt($a),Ye=l.useRef(null);return a.jsxs("div",{className:"space-y-4 min-w-0",children:[a.jsx(St,{title:`CRM â ${Wt[r]}`,right:a.jsxs(a.Fragment,{children:[a.jsxs(ve,{variant:"outline",children:[ce.length," of ",(m==null?void 0:m.length)??0]}),a.jsx(ve,{variant:x==="live"?"default":"secondary",children:x==="live"?"â Live":"𧪠Sandbox"}),a.jsxs(na,{value:z,onValueChange:e=>Ma(e),children:[a.jsx(ia,{className:"w-44 text-xs",children:a.jsx(ra,{})}),a.jsxs(oa,{children:[a.jsx(ne,{value:"newest",children:"Newest first"}),a.jsx(ne,{value:"oldest",children:"Oldest first"}),a.jsx(ne,{value:"activity",children:"Recent activity"}),a.jsx(ne,{value:"followup",children:"Follow-up soonest"}),a.jsx(ne,{value:"company",children:"Company AâZ"})]})]}),a.jsxs(na,{value:g,onValueChange:e=>He(e),children:[a.jsx(ia,{className:"w-40 text-xs",children:a.jsx(ra,{})}),a.jsx(oa,{children:ss.map(e=>a.jsx(ne,{value:e,children:e==="All"?"All (incl. test)":e},e))})]}),a.jsxs(Za,{children:[a.jsx(Wa,{asChild:!0,children:a.jsxs(we,{variant:"outline",size:"sm",className:"gap-1.5",children:[a.jsx(Pt,{className:"h-3.5 w-3.5"}),a.jsx("span",{className:"text-xs",children:"Columns"})]})}),a.jsxs(Xa,{align:"end",className:"w-52",children:[a.jsx(et,{className:"text-[11px] uppercase tracking-wider text-muted-foreground",children:"Show / hide columns"}),Kt.filter(e=>e.toggleable).map(e=>a.jsx(at,{checked:!c.has(e.key),onCheckedChange:t=>{Ve(o=>{const i=new Set(o);return t?i.delete(e.key):i.add(e.key),i})},onSelect:t=>t.preventDefault(),children:e.label},e.key))]})]})]})}),a.jsx(it,{value:_,onValueChange:e=>O(e),children:a.jsx(rt,{className:"h-auto flex-wrap gap-2 bg-transparent p-0",children:ls.map(e=>{const t=_===e.v;return a.jsxs(ot,{value:e.v,className:Q("group relative flex-col items-start gap-1 rounded-lg border bg-white px-4 py-2.5 text-left transition-all","hover:border-primary/40 hover:bg-white hover:shadow-sm","data-[state=active]:border-primary data-[state=active]:bg-white data-[state=active]:shadow-md","data-[state=active]:ring-1 data-[state=active]:ring-primary/30"),children:[a.jsxs("div",{className:"flex items-center gap-2",children:[e.v==="anchor"&&a.jsx(pa,{className:"h-3.5 w-3.5 text-primary"}),e.v==="presubmission"&&a.jsx(Ft,{className:"h-3.5 w-3.5 text-primary"}),a.jsx("span",{className:"font-semibold text-sm",children:e.label}),a.jsx(ve,{variant:t?"default":"secondary",className:"text-[10px] h-5 px-1.5 font-bold tabular-nums",children:Ua[e.v]})]}),a.jsx("div",{className:"text-[10px] text-muted-foreground font-normal",children:e.sublabel})]},e.v)})})}),a.jsx(sa,{className:"p-3 space-y-2",children:a.jsxs("div",{className:"flex flex-wrap gap-2 items-center",children:[a.jsxs("div",{className:"relative",children:[a.jsx(Rt,{className:"absolute left-2 top-1/2 -translate-y-1/2 h-3.5 w-3.5 text-muted-foreground"}),a.jsx(tt,{placeholder:"Search company, contact, email, case #â¦",value:oe,onChange:e=>Ee(e.target.value),className:"pl-7 w-64"})]}),a.jsx("div",{className:"w-40",children:a.jsx(P,{value:W==="All"?"":W,onChange:e=>Pe(e||"All"),placeholder:"All Statuses",searchPlaceholder:"Search statusâ¦",allowClear:!0,clearLabel:"All Statuses",options:Xt.map(e=>({value:e,label:Tt[e]}))})}),a.jsx("div",{className:"w-40",children:a.jsx(P,{value:X==="__all__"?"":X,onChange:e=>Fe(e||"__all__"),placeholder:"All CRM",searchPlaceholder:"Search CRM statusâ¦",allowClear:!0,clearLabel:"All CRM",options:Nt.map(e=>({value:e.value,label:e.label}))})}),a.jsx("div",{className:"w-32",children:a.jsx(P,{value:B==="All"?"":B,onChange:e=>Re(e||"All"),placeholder:"All Sources",searchPlaceholder:"Search sourceâ¦",allowClear:!0,clearLabel:"All Sources",options:es.filter(e=>e!=="All").map(e=>({value:e,label:e}))})}),a.jsx("div",{className:"w-36",children:a.jsx(P,{value:I==="All"?"":I,onChange:e=>Me(e||"All"),placeholder:"All Funding",searchPlaceholder:"Search fundingâ¦",allowClear:!0,clearLabel:"All Funding",options:as.filter(e=>e!=="All").map(e=>({value:e,label:e.replace("_"," ")}))})}),a.jsx("div",{className:"w-36",children:a.jsx(P,{value:ee==="All"?"":ee,onChange:e=>Oe(e||"All"),placeholder:"All (Anchor)",searchPlaceholder:"Searchâ¦",allowClear:!0,clearLabel:"All (Anchor)",options:ts.filter(e=>e!=="All").map(e=>({value:e,label:e==="Yes"?"Anchor-backed":"Preliminary"}))})}),a.jsx("div",{className:"w-48",children:a.jsx(P,{value:ae==="All"?"":ae,onChange:e=>Be(e||"All"),placeholder:"All Orgs",searchPlaceholder:"Search organizationsâ¦",allowClear:!0,clearLabel:"All Orgs",options:Ne.map(e=>{const t=e.favicon_url||e.internal_icon_url||e.internal_logo_url,o=Fa[e.id],i=le.org[e.id]||0;
1return{value:e.id,label:e.company_short_name??"â",sublabel:o||(e.slug?`/${e.slug}`:void 0),hint:i?`${i}`:void 0,icon:t?a.jsx("img",{src:t,alt:"",className:"h-4 w-4 rounded object-contain"}):a.jsx(Mt,{className:"h-3.5 w-3.5 text-muted-foreground"}),searchTokens:[e.slug,e.company_short_name,o].filter(Boolean)}}).sort((e,t)=>Number(t.hint||0)-Number(e.hint||0))})}),a.jsx("div",{className:"w-52",children:a.jsx(P,{value:U==="All"?"":U,onChange:e=>Ie(e||"All"),placeholder:"All Brokers",searchPlaceholder:"Search brokers by name, company, emailâ¦",allowClear:!0,clearLabel:"All Brokers",options:[{value:"__none__",label:"No broker",hint:le.brokerNone?`${le.brokerNone}`:void 0,icon:a.jsx(fa,{className:"h-3.5 w-3.5 text-muted-foreground/60"})},...Object.values(La).map(e=>({value:e.id,label:e.company_name??e.full_name??"â",sublabel:[e.full_name&&e.company_name?e.full_name:null,e.email].filter(Boolean).join(" · "),hint:le.broker[e.id]?`${le.broker[e.id]}`:void 0,icon:a.jsx(fa,{className:"h-3.5 w-3.5 text-primary"}),searchTokens:[e.full_name,e.company_name,e.email].filter(Boolean)})).sort((e,t)=>Number(t.hint||0)-Number(e.hint||0))]})}),a.jsx("div",{className:"w-52",children:a.jsx(P,{value:te==="All"?"":te,onChange:e=>Ue(e||"All"),placeholder:"All Partners",searchPlaceholder:"Search partners by name, email, companyâ¦",allowClear:!0,clearLabel:"All Partners",requireSearch:!0,minSearchChars:2,options:Object.values(y).map(e=>{const t=e.organization_id?Ne.find(i=>i.id===e.organization_id):null,o=le.partner[e.id]||0;return{value:e.id,label:e.full_name??e.company??"â",sublabel:[e.company,e.email,t==null?void 0:t.company_short_name].filter(Boolean).join(" · "),hint:o?`${o}`:void 0,searchTokens:[e.full_name,e.company,e.email,e.slug,t==null?void 0:t.company_short_name].filter(Boolean)}}).sort((e,t)=>Number(t.hint||0)-Number(e.hint||0))})}),a.jsx("div",{className:"w-40",children:a.jsx(P,{value:q==="All"?"":q,onChange:e=>qe(e||"All"),placeholder:"All Owners",searchPlaceholder:"Search ownersâ¦",allowClear:!0,clearLabel:"All Owners",options:[{value:"__none__",label:"Unassigned"},...qa.map(e=>({value:e,label:_a(Le[e])||`User ${e.slice(0,8)}`}))]})}),a.jsx("div",{className:"w-32",children:a.jsx(P,{value:se==="All"?"":se,onChange:e=>ze(e||"All"),placeholder:"Any Priority",searchPlaceholder:"Search priorityâ¦",allowClear:!0,clearLabel:"Any Priority",options:[{value:"vip",label:"VIP"},{value:"high",label:"High"},{value:"normal",label:"Normal"},{value:"low",label:"Low"}]})}),a.jsx(we,{size:"sm",variant:"ghost",onClick:za,children:"Clear"})]})}),a.jsx(sa,{className:"overflow-hidden",children:m===null?a.jsxs("div",{className:"p-4 space-y-2",children:[a.jsx(ke,{className:"h-8"}),a.jsx(ke,{className:"h-8"}),a.jsx(ke,{className:"h-8"})]}):ce.length===0?a.jsx("div",{className:"p-12 text-center text-sm text-muted-foreground",children:"No claims match this view."}):a.jsxs(a.Fragment,{children:[a.jsx(Ya,{targetRef:Ye,deps:[be.length,_]}),a.jsx("div",{ref:Ye,className:"overflow-x-auto",children:a.jsx("div",{children:a.jsxs(st,{children:[a.jsx(lt,{children:a.jsxs(la,{children:[a.jsx(b,{children:"Case #"}),a.jsx(b,{children:"Company / Contact"}),!c.has("status")&&a.jsx(b,{children:"Status"}),!c.has("crm_status")&&a.jsx(b,{children:"CRM Status"}),!c.has("owner")&&a.jsx(b,{children:"Owner"}),!c.has("est")&&a.jsx(b,{className:"text-right",children:"Est. Recovery"}),!c.has("anchor")&&a.jsx(b,{children:"Anchor"}),Ge&&!c.has("score")&&a.jsx(b,{children:"Score"}),xe&&!c.has("funding_stage")&&a.jsx(b,{children:"Funding Stage"}),xe&&!c.has("advance")&&a.jsx(b,{className:"text-right",children:"Advance"}),!c.has("source")&&a.jsx(b,{children:"Source"}),!c.has("app_dates")&&a.jsx(b,{children:"App Dates"}),!c.has("created")&&a.jsx(b,{children:"Created"}),!c.has("next_followup")&&a.jsx(b,{children:"Next Follow-Up"}),!c.has("last_activity")&&a.jsx(b,{children:"Last Activity"}),!c.has("crm_snapshot")&&a.jsx(b,{children:"CRM Snapshot"}),a.jsx(b,{})]})}),a.jsx(nt,{children:be.map(e=>{const t=G[e.id],o=Ca[e.id]||[],i=Ct(e,t,o),d=e.next_follow_up_at&&new Date(e.next_follow_up_at)<new Date,f=Z[e.id],h=e.affiliate_id?y[e.affiliate_id]:null,k=h!=null&&h.organization_id?Ne.find(p=>p.id===h.organization_id):null,v=Ba[e.id]||{},S=p=>p?new Date(p).toLocaleDateString(void 0,{month:"short",day:"numeric",year:"2-digit"}):null;return a.jsxs(la,{className:"cursor-pointer hover:bg-muted/40",onClick:()=>w(e.id),children:[a.jsxs(j,{className:"font-mono text-xs font-semibold",children:["#",e.case_number??e.id.slice(0,6)]}),a.jsx(j,{children:a.jsxs("div",{className:"max-w-[240px]",children:[a.jsx("div",{className:"font-medium text-sm truncate",children:e.company_name??"â"}),a.jsx("div",{className:"text-[11px] text-muted-foreground truncate",children:e.contact_name??"â"}),e.contact_email&&a.jsx("div",{className:"text-[10px] text-muted-foreground truncate",children:e.contact_email}),e.phone&&a.jsx("a",{href:`tel:${e.phone}`,onClick:p=>p.stopPropagation(),className:"text-[10px] text-muted-foreground truncate hover:text-primary hover:underline block",children:e.phone}),e.phone&&a.jsx(dt,{leadId:e.id,phone:e.phone,contactName:e.contact_name??e.company_name,compact:!0,stopPropagation:!0})]})}),!c.has("status")&&a.jsx(j,{children:a.jsx(jt,{status:e.status})}),!c.has("crm_status")&&a.jsx(j,{children:a.jsxs("div",{className:"flex flex-col gap-0.5",children:[a.jsx(ga,{value:e.crm_status}),a.jsx(ba,{value:e.priority})]})}),!c.has("owner")&&a.jsx(j,{className:"text-xs",children:e.assigned_case_manager_id?(()=>{const p=e.assigned_case_manager_id,D=_a(Le[p])||`User ${p.slice(0,6)}`;return a.jsxs("span",{className:Q("inline-flex items-center gap-1 rounded-full border px-2 py-0.5 text-[11px] font-medium max-w-[160px]",Zt(p)),children:[a.jsx(Ot,{className:"h-3 w-3 shrink-0"}),a.jsx("span",{className:"truncate",children:D})]})})():a.jsx("span",{className:"text-muted-foreground italic",children:"Unassigned"})}),!c.has("est")&&a.jsx(j,{className:"text-right font-mono text-xs",children:i!=null&&i.amount?ta(i.amount):"â"}),!c.has("anchor")&&a.jsx(j,{children:Se[e.id]?a.jsxs(ve,{variant:"default",className:"text-[10px]",children:[a.jsx(pa,{className:"h-2.5 w-2.5 mr-0.5"}),"Backed"]}):a.jsx("span",{className:"text-[11px] text-muted-foreground",children:"â"})}),Ge&&!c.has("score")&&a.jsx(j,{className:"text-xs",children:(t==null?void 0:t.qualification_score)??"â"}),xe&&!c.has("funding_stage")&&a.jsx(j,{className:"text-xs",children:(t==null?void 0:t.funding_stage)??"â"}),xe&&!c.has("advance")&&a.jsx(j,{className:"text-right font-mono text-xs",children:f!=null&&f.advance_amount?ta(Number(f.advance_amount)):"â"}),!c.has("source")&&a.jsx(j,{className:"text-[11px] align-top",children:a.jsx(vt,{lead:e,partner:h,org:k,onPartnerClick:h?p=>re(p):void 0})}),!c.has("app_dates")&&a.jsx(j,{className:"text-[11px] whitespace-nowrap align-top",children:a.jsxs("div",{className:"flex flex-col gap-0.5",children:[e.created_at&&a.jsxs("span",{className:"inline-flex items-center gap-1 text-muted-foreground",children:[a.jsx(Bt,{className:"h-3 w-3 text-blue-500"}),"App Started: ",a.jsx("span",{className:"text-foreground font-medium",children:S(e.created_at)})]}),v.submitted&&a.jsxs("span",{className:"inline-flex items-center gap-1 text-muted-foreground",children:[a.jsx(It,{className:"h-3 w-3 text-amber-500"}),"App Submitted: ",a.jsx("span",{className:"text-foreground font-medium",children:S(v.submitted)})]}),v.filed&&a.jsxs("span",{className:"inline-flex items-center gap-1 text-muted-foreground",children:[a.jsx(Ut,{className:"h-3 w-3 text-cyan-500"}),"Filed: ",a.jsx("span",{className:"text-foreground font-medium",children:S(v.filed)})]}),!e.created_at&&!v.submitted&&!v.filed&&a.jsx("span",{className:"text-muted-foreground",children:"â"})]})}),!c.has("created")&&a.jsx(j,{className:"text-[11px] text-muted-foreground whitespace-nowrap",children:e.created_at?a.jsxs("div",{className:"flex flex-col leading-tight",children:[a.jsx("span",{children:new Date(e.created_at).toLocaleDateString()}),a.jsx("span",{className:"text-[10px] opacity-70",children:K(new Date(e.created_at),{addSuffix:!0})})]}):"â"}),!c.has("next_followup")&&a.jsx(j,{className:`text-[11px] whitespace-nowrap ${d?"text-rose-600 font-semibold":"text-muted-foreground"}`,children:e.next_follow_up_at?a.jsxs("div",{className:"flex flex-col leading-tight",children:[a.jsx("span",{children:new Date(e.next_follow_up_at).toLocaleDateString()}),a.jsx("span",{className:"text-[10px] opacity-70",children:K(new Date(e.next_follow_up_at),{addSuffix:!0})})]}):"â"}),!c.has("last_activity")&&a.jsx(j,{className:"text-[11px] text-muted-foreground whitespace-nowrap",children:e.last_activity_at?a.jsxs("div",{className:"flex flex-col leading-tight",children:[a.jsx("span",{children:new Date(e.last_activity_at).toLocaleDateString()}),a.jsx("span",{className:"text-[10px] opacity-70",children:K(new Date(e.last_activity_at),{addSuffix:!0})})]}):"â"}),!c.has("crm_snapshot")&&a.jsx(j,{onClick:p=>p.stopPropagation(),children:a.jsx(Qt,{lead:e,summary:Va[e.id],onOpenCrm:()=>w(e.id),showStatus:!1})}),a.jsx(j,{onClick:p=>p.stopPropagation(),children:a.jsx(ct,{triggerLabel:"Manage",items:[{label:"Open Master View",icon:qt,onClick:()=>w(e.id)},{label:"Log Activity",icon:zt,onClick:()=>$(e,"log"),separator:!0},{label:"Add Note",icon:ja,onClick:()=>$(e,"note")}
1,{label:"Set Follow-Up",icon:wa,onClick:()=>$(e,"followup")},{label:"Create Task",icon:Ht,onClick:()=>$(e,"task")},{label:"Add Tag",icon:Sa,onClick:()=>$(e,"tag")},{label:"Change CRM Status",icon:$t,onClick:()=>$(e,"status"),separator:!0},{label:"Reassign Owner",icon:Vt,onClick:()=>$(e,"reassign"),show:A},{label:"Escalate to Admin",icon:va,onClick:()=>$(e,"escalate"),variant:"destructive"},{label:"Full Claim Page",icon:Gt,onClick:()=>window.location.assign(`/command/leads/${e.id}`),separator:!0}]})})]},e.id)})})]})})}),a.jsx("div",{className:"px-3 pb-3",children:a.jsx(Ja,{page:ge,totalPages:Ha,totalCount:ce.length,pageSize:_e,onPageChange:Je,label:"claims"})})]})}),a.jsx(ut,{leadId:V,onClose:()=>w(null),onChanged:L}),fe&&a.jsx(wt,{open:!!fe,onOpenChange:e=>{e||re(null)},affiliate:y[fe]||null,allAffiliates:Object.values(y),onChanged:L}),F&&a.jsxs(a.Fragment,{children:[a.jsx(mt,{open:H==="log",onOpenChange:()=>R(null),lead:F,onDone:L}),a.jsx(pt,{open:H==="note",onOpenChange:()=>R(null),lead:F,onDone:L}),a.jsx(ft,{open:H==="followup",onOpenChange:()=>R(null),lead:F,onDone:L}),a.jsx(ht,{open:H==="task",onOpenChange:()=>R(null),lead:F,onDone:L}),a.jsx(xt,{open:H==="tag",onOpenChange:()=>R(null),lead:F,onDone:L}),a.jsx(_t,{open:H==="reassign",onOpenChange:()=>R(null),lead:F,onDone:L}),a.jsx(gt,{open:H==="escalate",onOpenChange:()=>R(null),lead:F,onDone:L}),a.jsx(bt,{open:H==="status",onOpenChange:()=>R(null),lead:F,onDone:L})]})]})}export{Ll as CrmList,Ll 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.