1import{r as f,j as e,I as V,a9 as Ge,b as R,W as Me,X as le,ah as Ye,L as q,w as Ue,ae as Xe,D as Y,y as Ne,Y as Qe,C as je,ac as Je,F as $e,aU as Ze,f as et,T as tt,aV as st,aW as at,aX as Fe,aY as lt,a2 as nt,aZ as rt,a_ as Ae,a$ as Pe,b0 as Te,b1 as ot,b2 as it,b3 as ct,b4 as dt,b5 as xt,b6 as mt}from"./index-Df108JlE.js";import{C as ht}from"./CloseButton-CAKvuKo3.js";import{u as ve}from"./useModalA11y-B5_bs4oH.js";import{Q as Ie}from"./QueryError-Csi7m0zt.js";import{A as pt,a as ut}from"./AnalysisRequestModal-Dkb7uKyY.js";import{C as gt}from"./chevron-right-C5-qd-S8.js";import{S as ft}from"./Sankey-IHob80vp.js";import{ai as U,af as Q,ag as J,ah as G,au as bt}from"./CartesianChart-CxTC1H5D.js";import{M as be}from"./maximize-2-C1hFM99k.js";import{a as ce,L as W}from"./LineChart-BhL0DOVD.js";import{C as Z}from"./CartesianGrid-CgHY_Blv.js";import{L as he}from"./Legend-DQKG7AK5.js";import{B as Ee,c as Le}from"./BarChart-C3zlDDul.js";import{C as jt}from"./chevron-left-BG1_bPN5.js";function yt({mix:u,plans:d,title:b="Plan Type Mix â HMO vs PPO",subtitle:r}){const[x,o]=f.useState(!1),m=ve(()=>o(!1));if(!u||!u.total)return null;const{hmo_pct:A=0,ppo_pct:a=0,other_pct:M=0,hmo:j,ppo:w,other:E}=u,S=h=>h!=null?h.toLocaleString():"â",k=Array.isArray(d)&&d.length>0,D=k&&d.reduce((h,l)=>h+(l.enrolled||0),0)<(u.total||0),P=a>=45?`${a}% PPO â strong out-of-network opportunity; even un-contracted SNFs can admit these residents (at higher member cost-share).`:a>=25?`${a}% PPO â a meaningful out-of-network opening, but the HMO majority still rewards in-network contracts.`:`HMO-dominant (${A}%) â in-network contracting is essential to capture these MA residents; little out-of-network access.`,v=(h,l,C,O="#fff")=>h>0&&e.jsx("div",{style:{width:`${h}%`,backgroundColor:l,color:O},className:"flex items-center justify-center overflow-hidden whitespace-nowrap",title:`${C} ${h}%`,children:h>=9?`${C} ${h}%`:""}),y={HMO:j,PPO:w,Other:E},s=h=>(d||[]).filter(l=>l.type===h).sort((l,C)=>C.enrolled-l.enrolled),g=(h,l,C,O)=>{const c=s(h);if(!c.length)return null;const _=c.reduce((I,$)=>I+($.enrolled||0),0),i=y[h]??_,z=i>_;return e.jsxs("div",{className:"min-w-0",children:[e.jsxs("div",{className:"flex items-baseline gap-2 mb-1.5 sticky top-0 bg-white py-1",children:[e.jsx("span",{className:"inline-block w-2.5 h-2.5 rounded-sm",style:{backgroundColor:l}}),e.jsx("span",{className:"text-xs font-bold text-slate-800",children:C}),e.jsxs("span",{className:"text-[11px] text-slate-400",children:[z?"top ":"",c.length," plan",c.length!==1?"s":""," · ",S(i)," · ",O]})]}),e.jsx("div",{className:"space-y-0.5",children:c.map((I,$)=>e.jsxs("div",{className:"flex items-center justify-between gap-3 text-xs py-0.5 border-b border-slate-50",children:[e.jsx("span",{className:"text-slate-700 truncate",title:I.name,children:I.name}),e.jsx("span",{className:"text-slate-500 tabular-nums shrink-0",children:S(I.enrolled)})]},$))})]})};return e.jsxs("div",{className:"bg-white rounded-xl border border-slate-200 shadow-sm p-5",children:[e.jsxs("div",{className:"flex items-baseline justify-between mb-1",children:[e.jsx("h3",{className:"text-sm font-bold text-slate-800",children:b}),e.jsxs("div",{className:"flex items-center gap-2",children:[k&&e.jsxs("button",{onClick:()=>o(!0),className:"text-xs text-sky-600 hover:text-sky-700 font-medium",children:["View ",D?"top ":"",d.length," plans"]}),e.jsx(V,{text:"HMO plans (incl. HMO-POS) generally have NO out-of-network benefits â residents must use in-network SNFs. PPO plans (Local + Regional) carry out-of-network benefits, so a PPO-heavy market is more accessible to SNFs that aren't contracted. Weighted by MA enrollment, latest CMS month."})]})]}),r&&e.jsx("p",{className:"text-xs text-slate-400 mb-2",children:r}),e.jsxs("div",{className:`flex h-7 rounded-md overflow-hidden text-[11px] font-semibold mb-2 ${k?"cursor-pointer":""}`,onClick:k?()=>o(!0):void 0,title:k?"Click to see the plans behind this mix":void 0,children:[v(A,"#0284c7","HMO"),v(a,"#475569","PPO"),v(M,"#cbd5e1","Other","#475569")]}),e.jsxs("p",{className:"text-xs text-slate-500",children:[e.jsxs("span",{className:"font-medium text-slate-700",children:["HMO ",S(j)]})," ",e.jsx("span",{className:"text-slate-400",children:"in-network only"})," · ",e.jsxs("span",{className:"font-medium text-slate-700",children:["PPO ",S(w)]})," ",e.jsx("span",{className:"text-slate-400",children:"out-of-network benefits"}),E>0&&e.jsxs(e.Fragment,{children:[" "," · ","Other ",S(E)]})]}),e.jsx("p",{className:"text-xs text-slate-500 mt-1.5 italic",children:P}),x&&k&&e.jsx("div",{className:"fixed inset-0 z-50 flex items-center justify-center bg-black/40 p-4",onClick:()=>o(!1),children:e.jsxs("div",{ref:m,role:"dialog","aria-modal":"true","aria-label":"Plans by type",className:"bg-white rounded-lg shadow-lg w-full max-w-3xl max-h-[80vh] flex flex-col",onClick:h=>h.stopPropagation(),children:[e.jsxs("div",{className:"flex items-start justify-between px-5 py-3 border-b border-slate-200",children:[e.jsxs("div",{children:[e.jsx("h3",{className:"text-sm font-bold text-slate-800",children:"Plans by Type â HMO vs PPO"}),r&&e.jsx("p",{className:"text-xs text-slate-400",children:r})]}),e.jsx(ht,{onClick:()=>o(!1)})]}),e.jsxs("div",{className:"overflow-y-auto p-5 grid grid-cols-1 sm:grid-cols-2 gap-6",children:[g("HMO","#0284c7","HMO","in-network only"),g("PPO","#475569","PPO","out-of-network benefits")]}),s("Other").length>0&&e.jsx("div",{className:"px-5 pb-4",children:g("Other","#cbd5e1","Other","PFFS / PACE / Cost / MSA")}),D&&e.jsx("p",{className:"px-5 pb-4 text-[11px] text-slate-400",children:"Listing the largest plans by enrollment. Each column total is the full market for that type, so it exceeds the plans shown."})]})})]})}function Nt(u=640){const d=`(max-width: ${u}px)`,[b,r]=f.useState(()=>typeof window<"u"&&window.matchMedia(d).matches);return f.useEffect(()=>{const x=window.matchMedia(d),o=m=>r(m.matches);
1return x.addEventListener("change",o),r(x.matches),()=>x.removeEventListener("change",o)},[d]),b}const Ke=["AL","AK","AZ","AR","CA","CO","CT","DE","DC","FL","GA","HI","ID","IL","IN","IA","KS","KY","LA","ME","MD","MA","MI","MN","MS","MO","MT","NE","NV","NH","NJ","NM","NY","NC","ND","OH","OK","OR","PA","RI","SC","SD","TN","TX","UT","VT","VA","WA","WV","WI","WY"],pe=["#0ea5e9","#f97316","#10b981","#8b5cf6","#ef4444","#06b6d4","#f59e0b","#ec4899","#14b8a6","#6366f1"],Oe=[{key:"State Overview",label:"State Overview",tip:"Statewide Medicare Advantage penetration rates and enrollment trends from CMS monthly data. See which states have the highest MA adoption, track growth over time, and click any state to drill into county-level detail."},{key:"County Explorer",label:"County Explorer",tip:"County-level MA penetration, top plan share, and market concentration. Identify which MA plans dominate each county and understand the competitive landscape for SNF referral sources in your market."},{key:"Plan Benefits",label:"Plan Benefits",tip:"CY2026 CMS Medicare Plan Finder benefit summary. Look up any MA plan by contract+plan ID and see SNF cost share, rehab coverage, out-of-pocket cap, authorization requirements. Built for intake/business office."},{key:"Plan Explorer",label:"Plan Explorer",tip:"Search and analyze individual MA plan organizations (H-contracts) and their enrollment footprint. View plan details, geographic reach, and enrollment volume to understand which organizations control MA beneficiaries in your area."},{key:"Enrollment Flow",label:"Enrollment Flow",tip:"Visualizes how MA enrollment is distributed across plan organizations within a state or county. Understand market concentration, identify dominant plans, and track how enrollment flows between organizations over time."}];function Ut(){const[u]=Ge(),d=u.get("state"),b=u.get("county"),r=u.get("org"),x=u.get("tab"),m=Oe.map(y=>y.key).includes(x)?x:r?"Plan Explorer":d?"County Explorer":"State Overview",[A,a]=f.useState(m),[M,j]=f.useState(d),[w,E]=f.useState(b),[S,k]=f.useState(r),[D,P]=f.useState(!1),v=y=>{j(y),a("County Explorer")};return e.jsxs("div",{className:"p-6 max-w-7xl mx-auto space-y-6",children:[e.jsx("div",{children:e.jsxs("h1",{className:"text-2xl font-bold text-slate-800 flex items-center gap-2",children:["Medicare Advantage Explorer ",e.jsx(V,{text:"Analyze Medicare Advantage enrollment and market share using CMS monthly enrollment data. MA penetration rates indicate how many Medicare beneficiaries have chosen private plans over traditional fee-for-service â a critical metric for SNF operators since MA plans control referrals, authorization, and reimbursement in high-penetration markets.",size:14,position:"below"})]})}),e.jsx("div",{className:"flex gap-1 border-b border-slate-200",children:Oe.map(y=>e.jsxs("button",{onClick:()=>a(y.key),className:`flex items-center gap-1 px-4 py-2 text-sm font-medium border-b-2 transition-colors ${A===y.key?"border-sky-600 text-sky-700":"border-transparent text-slate-500 hover:text-slate-700 hover:border-slate-300"}`,children:[y.label,e.jsx(V,{text:y.tip,size:10,position:"below",portal:!0})]},y.key))}),A==="County Explorer"&&e.jsx(vt,{jumpToState:M,jumpToCounty:w,clearJump:()=>{j(null),E(null)}}),A==="State Overview"&&e.jsxs(e.Fragment,{children:[e.jsx(wt,{onStateClick:v}),e.jsx(pt,{onRequestClick:()=>P(!0),context:"medicare_advantage"})]}),A==="Plan Benefits"&&e.jsx(Tt,{onOpenPlanExplorer:y=>{k(y),a("Plan Explorer")}}),A==="Plan Explorer"&&e.jsx(Ct,{initialOrg:S,clearInitialOrg:()=>k(null)}),A==="Enrollment Flow"&&e.jsx(kt,{}),e.jsx(ut,{isOpen:D,onClose:()=>P(!1),pageContext:"medicare_advantage"})]})}function vt({jumpToState:u,jumpToCounty:d,clearJump:b}){const[r,x]=f.useState(""),[o,m]=f.useState([]),[A,a]=f.useState(!1),M=f.useRef(d);f.useEffect(()=>{u&&(x(u),d||(m([]),a(!1)),b())},[u,d,b]);const{data:j,isLoading:w,isError:E,error:S}=R({queryKey:["ma-counties",r],queryFn:()=>Fe({state:r}),enabled:!!r,retry:(c,_)=>!_?.upgrade&&c<2}),{data:k}=R({queryKey:["ma-concentration-for-counties",r],queryFn:()=>lt({state:r}),enabled:!!r,staleTime:3600*1e3}),D=c=>String(c??"").padStart(5,"0"),P=f.useMemo(()=>{const c=new Map;for(const _ of k?.counties||[])c.set(D(_.ssa_code),_);return c},[k]);f.useEffect(()=>{if(M.current&&j?.results){const c=parseInt(M.current);M.current=null;const _=j.results.find(i=>i.ssa===c);_&&(m([{ssa:_.ssa,county_name:_.county_name}]),a(!0))}},[j]);const v=o.map(c=>c.ssa).join(","),{data:y,isLoading:s,isFetching:g}=R({queryKey:["ma-trends",v],queryFn:()=>nt({ssa_codes:v}),enabled:A&&v.length>0&&o.length<=50}),h=c=>{a(!1),m(_=>_.find(z=>z.ssa===c.ssa)?_.filter(z=>z.ssa!==c.ssa):[..._,{ssa:c.ssa,county_name:c.county_name}])},l=()=>{j?.results&&(a(!1),m(j.results.map(c=>({ssa:c.ssa,county_name:c.county_name}))))},C=()=>{a(!1),m([])},O=o.length>50;return e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"grid grid-cols-1 lg:grid-cols-3 gap-6",children:[e.jsxs("div",{className:"bg-white rounded-xl border border-slate-200 shadow-sm p-4",children:[e.jsx("h2",{className:"text-sm font-semibold text-slate-700 mb-3",children:"Select State"}),e.jsxs("select",{value:r,onChange:c=>{x(c.target.value),m([]),a(!1)},className:"w-full border border-slate-300 rounded-lg px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-sky-500",children:[e.jsx("option",{value:"",children:"Choose a state..."}),Ke.map(c=>e.jsx("option",{value:c,children:c},c))]}),o.length>0&&e.jsxs("div",{className:"mt-4",children:[e.jsxs("div",{className:"flex justify-between items-center mb-2",children:[e.jsxs("p",{className:"text-xs font-semibold text-slate-500 uppercase tracking-wide",children:["Selected (",o.length,")"]}),e.jsx("button",{onClick:C,className:"text-xs text-slate-400 hover:text-slate-700",children:"Clear all"})]}),O&&e.jsxs("div",{className:"flex items-start gap-2 bg-amber-50 border border-amber-200 rounded-lg p-2.5 mb-2",children:[e.jsx(Me,{size:14,className:"text-amber-500 flex-shrink-0 mt-0.5"}),e.jsxs("p",{className:"text-xs text-amber-700",children:["Too many counties selected (",o.length,"). Maximum is 50 for county-level trends. Use the ",e.jsx("strong",{children:"State Overview"})," tab instead for full-state analysis."]})]}),e.jsx("div",{className:"flex flex-wrap gap-1.5 max-h-40 overflow-y-auto",children:o.map(c=>e.jsxs("span",{className:"inline-flex items-center gap-1 text-xs bg-sky-50 text-sky-700 px-2 py-1 rounded-full border border-sky-200",children:[c.county_name,e.jsx("button",{onClick:()=>h(c),"aria-label":`Remove ${c.county_name}`,className:"hover:text-sky-900",children:e.jsx(le,{size:10})})]},c.ssa))}),e.jsx("button",{onClick:()=>a(!0),disabled:g||O,className:"mt-3 w-full bg-sky-600 hover:bg-sky-700 text-white text-sm font-medium py-2 rounded-lg transition-colors disabled:opacity-50",children:g?"Loading...":O?"Too many counties (max 50)":"Load Trends"})]})]}),e.jsxs("div",{className:"lg:col-span-2 bg-white rounded-xl border border-slate-200 shadow-sm overflow-hidden",children:[e.jsxs("div",{className:"px-4 py-3 border-b border-slate-200 flex justify-between items-center",children:[e.jsxs("div",{children:[e.jsxs("p",{className:"text-sm font-semibold text-slate-700",children:[r?`Counties in ${r}`:"Select a state to view counties",j&&` (${j.total})`]}),e.jsx(Ye,{label:"MA enrollment data as of",date:j?.report_date})]}),j?.results?.length>0&&e.jsx("button",{onClick:l,className:"text-xs text-sky-600 hover:text-sky-800 font-medium",children:"Select all"})]}),j?.penetration_unavailable&&e.jsxs("div",{className:"flex items-start gap-2 bg-amber-50 border border-amber-200 rounded-lg p-2.5 mb-3",children:[e.jsx(Me,{size:14,className:"text-amber-500 flex-shrink-0 mt-0.5"}),e.jsx("p",{className:"text-xs text-amber-700",children:j.note})]}),E?e.jsx("div",{className:"py-4",children:e.jsx(Ie,{error:S})}):w?e.jsxs("div",{className:"flex items-center gap-2 text-slate-400 py-12 justify-center",children:[e.jsx(q,{size:18,className:"animate-spin"})," Loading counties..."]}):j?.results?.length>0?e.jsx("div",{className:"max-h-80 overflow-y-auto",children:e.jsxs("table",{className:"w-full text-sm",children:[e.jsx("thead",{className:"bg-slate-50 border-b border-slate-200 sticky top-0",children:e.jsxs("tr",{children:[e.jsx("th",{scope:"col",className:"px-3 py-2 text-left text-xs font-semibold text-slate-500 uppercase w-8"}),e.jsx("th",{scope:"col",className:"px-3 py-2 text-left text-xs font-semibold text-slate-500 uppercase",children:"County"}),e.jsx("th",{scope:"col",className:"px-3 py-2 text-right text-xs font-semibold text-slate-500 uppercase",children:e.jsxs("span",{className:"inline-flex items-center gap-1 justify-end",children:["Penetration ",e.jsx(V,{text:"Percentage of Medicare beneficiaries enrolled in Medicare Advantage plans.",position:"below"})]})}),e.jsx("th",{scope:"col",className:"px-3 py-2 text-right text-xs font-semibold text-slate-500 uppercase",children:e.jsxs("span",{className:"inline-flex items-center gap-1 justify-end",children:["Enrolled ",e.jsx(V,{text:"Total MA plan enrollees in the county.",position:"below"})]})}),e.jsx("th",{scope:"col",className:"px-3 py-2 text-right text-xs font-semibold text-slate-500 uppercase w-16",children:e.jsxs("span",{className:"inline-flex items-center gap-1 justify-end",children:["Plans ",e.jsx(V,{text:"Number of distinct MA plan organizations active in the county.",position:"below"})]})}),e.jsx("th",{scope:"col",className:"px-3 py-2 text-left text-xs font-semibold text-slate-500 uppercase",children:e.jsxs("span",{className:"inline-flex items-center gap-1",children:["Top Plan ",e.jsx(V,{text:"Plan organization with the highest enrollment share in the county. Hover the Top Share % for the concentration label (Competitive / Moderate / Highly Concentrated).",position:"below"})]})}),e.jsx("th",{scope:"col",className:"px-3 py-2 text-right text-xs font-semibold text-slate-500 uppercase w-20",children:e.jsxs("span",{className:"inline-flex items-center gap-1 justify-end",children:["Top Share ",e.jsx(V,{text:"Top plan's share of MA enrollees in this county. â¥50% means one plan dominates the market.",position:"below"})]})}),e.jsx("th",{scope:"col",className:"px-3 py-2 text-center text-xs font-semibold text-slate-500 uppercase w-24",children:"SNFs"})]})}),e.jsx("tbody",{className:"divide-y divide-slate-100",children:j.results.map(c=>{const _=o.some(z=>z.ssa===c.ssa),i=P.get(D(c.ssa));return e.jsxs("tr",{onClick:()=>
1h(c),className:`cursor-pointer transition-colors ${_?"bg-sky-50":"hover:bg-slate-50"}`,children:[e.jsx("td",{className:"px-3 py-2",children:e.jsx("input",{type:"checkbox",checked:_,readOnly:!0,className:"rounded border-slate-300 text-sky-600 pointer-events-none"})}),e.jsx("td",{className:"px-3 py-2 text-slate-700 font-medium",children:c.county_name}),e.jsx("td",{className:"px-3 py-2 text-right text-slate-600",children:c.penetration!=null?c.penetration.toFixed(1)+"%":"â"}),e.jsx("td",{className:"px-3 py-2 text-right text-slate-600",children:c.enrolled?.toLocaleString()??"â"}),e.jsx("td",{className:"px-3 py-2 text-right text-slate-600 text-xs",children:i?.plan_count??e.jsx("span",{className:"text-slate-300",children:"\\u2014"})}),e.jsx("td",{className:"px-3 py-2 text-slate-700",children:i?.top_plan?e.jsx("span",{className:"truncate inline-block max-w-[12rem] align-middle",title:i.top_plan,children:i.top_plan}):e.jsx("span",{className:"text-slate-300",children:"\\u2014"})}),e.jsx("td",{className:"px-3 py-2 text-right text-slate-600",title:i?.concentration?`${i.concentration} (HHI ${i.hhi?.toLocaleString()||"â"})`:"",children:i?.top_plan_share!=null?i.top_plan_share.toFixed(0)+"%":e.jsx("span",{className:"text-slate-300",children:"\\u2014"})}),e.jsx("td",{className:"px-3 py-2 text-center",onClick:z=>z.stopPropagation(),children:e.jsxs(Ue,{to:`/?state=${r}&county=${encodeURIComponent(c.county_name)}`,target:"_blank",rel:"noopener noreferrer",className:"inline-flex items-center gap-1 text-xs text-sky-600 hover:text-sky-800 hover:underline",title:`View SNFs in ${c.county_name} County (opens in new tab)`,children:[e.jsx(Xe,{size:12})," View"]})})]},c.ssa)})})]})}):r?e.jsxs("p",{className:"text-sm text-slate-400 text-center py-12",children:["No county data found for ",r]}):e.jsx("p",{className:"text-sm text-slate-400 text-center py-12",children:"Select a state to browse counties"})]})]}),s&&e.jsxs("div",{className:"flex items-center gap-2 text-slate-400 py-12 justify-center",children:[e.jsx(q,{size:18,className:"animate-spin"})," Loading trends..."]}),y&&A&&e.jsx(ye,{data:y})]})}function wt({onStateClick:u}){const[d,b]=f.useState("penetration"),[r,x]=f.useState("desc"),[o,m]=f.useState(""),{data:A,isLoading:a,isError:M,error:j}=R({queryKey:["ma-states"],queryFn:()=>rt(),retry:(s,g)=>!g?.upgrade&&s<2}),{data:w,isLoading:E}=R({queryKey:["ma-state-trends",o],queryFn:()=>Ae({state:o}),enabled:!!o}),{data:S,isLoading:k}=R({queryKey:["ma-state-trends","NATIONAL"],queryFn:()=>Ae(),staleTime:1440*60*1e3}),D=s=>{d===s?x(g=>g==="asc"?"desc":"asc"):(b(s),x("desc"))},P=[{key:"state_abbr",label:"State",getter:s=>s.state_abbr??"",align:"left"},{key:"penetration",label:"Penetration",getter:s=>s.penetration??-1,fmt:s=>s==null||s<0?"â":s.toFixed(1)+"%"},{key:"total_enrolled",label:"Enrolled",getter:s=>s.total_enrolled??0,fmt:s=>s?.toLocaleString()},{key:"total_eligibles",label:"Eligibles",getter:s=>s.total_eligibles??-1,fmt:s=>s==null||s<0?"â":s.toLocaleString()},{key:"county_count",label:"Counties",getter:s=>s.county_count??0,fmt:s=>s}],v=f.useMemo(()=>{if(!A?.results)return[];const s=P.find(g=>g.key===d);return s?[...A.results].sort((g,h)=>{const l=s.getter(g),C=s.getter(h),O=l<C?-1:l>C?1:0;return r==="asc"?O:-O}):A.results},[A,d,r]);if(a)return e.jsxs("div",{className:"flex items-center gap-2 text-slate-400 py-12 justify-center",children:[e.jsx(q,{size:18,className:"animate-spin"})," Loading state data..."]});if(M)return e.jsx(Ie,{error:j});const y=Math.max(...A?.results?.map(s=>s.penetration||0)||[1]);return e.jsx("div",{className:"space-y-6",children:e.jsxs("div",{className:"grid grid-cols-1 lg:grid-cols-2 gap-6 items-start lg:[grid-auto-rows:1fr]",children:[e.jsxs("div",{className:"bg-white rounded-xl border border-slate-200 shadow-sm overflow-hidden flex flex-col h-full max-h-[60vh] lg:max-h-none lg:min-h-[500px]",children:[e.jsxs("div",{className:"px-4 py-3 border-b border-slate-200 flex-shrink-0 flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsxs("p",{className:"text-sm font-semibold text-slate-700",children:["All States (",v.length,")"]}),e.jsx("p",{className:"text-xs text-slate-400",children:"Click a state to view trends or drill into counties"})]}),e.jsxs("button",{onClick:()=>Ne(v.map(s=>({State:s.state_abbr,"State Name":s.state_name,"Penetration %":s.penetration?.toFixed(1),Enrolled:s.total_enrolled,Eligibles:s.total_eligibles,Counties:s.county_count})),["State","State Name","Penetration %","Enrolled","Eligibles","Counties"],"ma-states-overview.csv"),className:"flex items-center gap-1 text-xs text-slate-500 hover:text-sky-600 font-medium transition-colors",children:[e.jsx(Y,{size:13})," CSV"]})]}),e.jsx("div",{className:"flex-1 overflow-y-auto",children:e.jsxs("table",{className:"w-full text-sm",children:[e.jsx("thead",{className:"bg-slate-50 border-b border-slate-200 sticky top-0",children:e.jsxs("tr",{children:[P.map(s=>e.jsxs("th",{scope:"col",onClick:()=>D(s.key),className:`px-3 py-2 text-xs font-semibold text-slate-500 uppercase cursor-pointer hover:text-slate-700 select-none whitespace-nowrap ${s.align==="left"?"text-left":"text-right"}`,children:[s.label,d===s.key&&(r==="asc"?e.jsx(Qe,{size:11,className:"inline ml-0.5"}):e.jsx(je,{size:11,className:"inline ml-0.5"}))]},s.key)),e.jsx("th",{scope:"col",className:"px-3 py-2 text-xs font-semibold text-slate-500 uppercase text-center w-20"})]})}),e.jsx("tbody",{className:"divide-y divide-slate-100",children:v.map(s=>{const g=o===s.state_abbr,h=(s.penetration||0)/y*100;return e.jsxs("tr",{onClick:()=>
1m(g?null:s.state_abbr),className:`cursor-pointer transition-colors ${g?"bg-sky-50":"hover:bg-slate-50"}`,children:[e.jsxs("td",{className:"px-3 py-2",children:[e.jsx("span",{className:"font-medium text-slate-800",children:s.state_abbr}),e.jsx("span",{className:"text-xs text-slate-400 ml-1.5",children:s.state_name})]}),e.jsx("td",{className:"px-3 py-2 text-right",children:e.jsxs("div",{className:"flex items-center justify-end gap-2",children:[e.jsx("div",{className:"w-16 h-2 bg-slate-100 rounded-full overflow-hidden",children:e.jsx("div",{className:"h-full rounded-full",style:{width:h+"%",backgroundColor:_t(s.penetration||0)}})}),e.jsx("span",{className:"text-slate-700 font-medium w-12 text-right",title:s.penetration==null?"CMS has not reported usable eligible counts for this state this month":void 0,children:s.penetration!=null?s.penetration.toFixed(1)+"%":"â"})]})}),e.jsx("td",{className:"px-3 py-2 text-right text-slate-600",children:s.total_enrolled?.toLocaleString()}),e.jsx("td",{className:"px-3 py-2 text-right text-slate-400",children:s.total_eligibles!=null?s.total_eligibles.toLocaleString():"â"}),e.jsx("td",{className:"px-3 py-2 text-right text-slate-500",children:s.county_count}),e.jsx("td",{className:"px-3 py-2 text-center",children:e.jsx("button",{onClick:l=>{l.stopPropagation(),u(s.state_abbr)},className:"text-xs text-sky-600 hover:text-sky-800 font-medium",title:"View counties",children:"Counties"})})]},s.state_abbr)})})]})})]}),e.jsxs("div",{children:[!o&&k&&e.jsxs("div",{className:"flex items-center gap-2 text-slate-400 py-12 justify-center",children:[e.jsx(q,{size:18,className:"animate-spin"})," Loading national trends..."]}),!o&&S&&!k&&e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("h3",{className:"text-lg font-bold text-slate-800",children:"National Trends"}),e.jsx("p",{className:"text-xs text-slate-400",children:"Click a state above to compare it against this"})]}),e.jsx(ye,{data:S,compact:!0})]}),!o&&!S&&!k&&e.jsxs("div",{className:"bg-slate-50 border border-slate-200 rounded-xl p-12 text-center",children:[e.jsx(Je,{size:32,className:"mx-auto text-slate-300 mb-3"}),e.jsx("p",{className:"text-sm text-slate-500",children:"Click a state in the table to view its MA trends"})]}),o&&E&&e.jsxs("div",{className:"flex items-center gap-2 text-slate-400 py-12 justify-center",children:[e.jsx(q,{size:18,className:"animate-spin"})," Loading ",o," trends..."]}),o&&w&&!E&&e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-baseline gap-3",children:[e.jsxs("h3",{className:"text-lg font-bold text-slate-800",children:[o," State Trends"]}),(()=>{const s=w?.penetration_trend?.at?.(-1)?.penetration,g=S?.penetration_trend?.at?.(-1)?.penetration;if(s==null||g==null)return null;const h=s-g;return e.jsxs("span",{className:"text-xs text-slate-500",children:[s.toFixed(1),"% vs ",g.toFixed(1),"% national",e.jsxs("span",{className:`ml-1 font-semibold ${h>=0?"text-slate-700":"text-slate-500"}`,children:["(",h>=0?"+":"",h.toFixed(1)," pts)"]})]})})()]}),e.jsx("button",{onClick:()=>u(o),className:"text-xs text-sky-600 hover:text-sky-800 font-medium",children:"View counties â"})]}),e.jsx(ye,{data:w,compact:!0})]})]})]})})}function ye({data:u,compact:d=!1}){const[b,r]=f.useState(!1),[x,o]=f.useState(null),m=f.useCallback(()=>o(null),[]),A=ve(m),a=Nt(),M=d?220:280,j=u.penetration_trend.map(t=>({date:t.report_date?.slice(0,7),"Penetration %":t.penetration,"MA Enrolled":t.enrolled,Traditional:t.eligibles!=null&&t.enrolled!=null?t.eligibles-t.enrolled:null,"Total Medicare":t.eligibles})),w=u.plan_trend??[],E=w.length>0?w.reduce((t,N)=>N.report_date>t?N.report_date:t,""):"",S={"Health Care Service Corporation":"Blue Cross Blue Shield (HCSC)","Elevance Health, Inc.":"Anthem Blue Cross Blue Shield","Blue Cross Blue Shield of Michigan Mutual Ins. Co.":"Blue Cross Blue Shield of Michigan","California Physicians' Service":"Blue Shield of California","Highmark Health":"Highmark Blue Cross Blue Shield","UnitedHealth Group, Inc.":"UnitedHealthcare","Humana Inc.":"Humana","CVS Health Corporation":"Aetna (CVS Health)","Centene Corporation":"Centene (WellCare)","Molina Healthcare, Inc.":"Molina Healthcare"},k=t=>{const N=t.parent_organization;return N?S[N]||N:null},D={};w.filter(t=>
1t.report_date===E).forEach(t=>{const T=k(t)||"__other__";D[T]=(D[T]||0)+(t.enrolled||0)});const P=Object.entries(D).filter(([t])=>t!=="__other__").sort((t,N)=>N[1]-t[1]).map(([t])=>t),v=P.slice(0,10),y=(D.__other__||0)+P.slice(10).reduce((t,N)=>t+(D[N]||0),0),s=[...v.map(t=>({name:t.length>30?t.slice(0,28)+"...":t,fullName:t,enrolled:D[t]||0})),...y>0?[{name:"Other",fullName:"Other",enrolled:y}]:[]],g=P.slice(0,4),h=[...g,"Other"],l=Object.values(D).reduce((t,N)=>t+N,0)||1,C=P.filter(t=>(D[t]||0)/l>=.03),O=[...C,"Other"],c={},_={};w.forEach(t=>{const T=k(t)||"Other",F=t.report_date?.slice(0,7);_[F]||(_[F]=0),_[F]+=t.enrolled||0,c[F]||(c[F]={date:F}),c[F][T]=(c[F][T]||0)+(t.enrolled||0)});const i=Object.values(c).sort((t,N)=>t.date.localeCompare(N.date)),z=i.map(t=>{const N={date:t.date};return g.forEach(T=>{N[T]=t[T]||null}),N.Other=(t.Other||0)+P.filter(T=>!g.includes(T)).reduce((T,F)=>T+(t[F]||0),0),N}),I=b?z.map(t=>{const N=_[t.date]||1,T={date:t.date};return h.forEach(F=>{T[F]=t[F]!=null?Math.round(t[F]/N*1e3)/10:null}),T}):z,$=i.map(t=>{const N={date:t.date};
1return C.forEach(T=>{N[T]=t[T]||null}),N.Other=(t.Other||0)+P.filter(T=>!C.includes(T)).reduce((T,F)=>T+(t[F]||0),0),N}),H=b?$.map(t=>{const N=_[t.date]||1,T={date:t.date};return O.forEach(F=>{T[F]=t[F]!=null?Math.round(t[F]/N*1e3)/10:null}),T}):$,L={};u.penetration_trend.forEach(t=>{const N=t.report_date?.slice(0,7);t.eligibles!=null&&t.enrolled!=null&&(L[N]={count:t.eligibles-t.enrolled,pct:t.penetration!=null?Math.round((100-t.penetration)*10)/10:null})});const K=Object.keys(L).length>0;return e.jsxs("div",{className:"space-y-6",children:[!d&&e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("h2",{className:"text-lg font-bold text-slate-800",children:u.county_names?.join(", ")}),e.jsxs("span",{className:"text-xs text-slate-400",children:["SSA: ",u.ssa_codes?.join(", ")]})]}),!d&&e.jsx(yt,{mix:u.plan_type_mix,plans:u.plans_by_type,subtitle:`${u.county_names?.join(", ")||"County"} â weighted by MA enrollment`}),e.jsxs("div",{className:"bg-white rounded-xl border border-slate-200 shadow-sm p-3 sm:p-5",children:[e.jsxs("div",{className:"flex items-center justify-between mb-0.5",children:[e.jsx("h3",{className:"text-sm font-semibold text-slate-700",children:"MA Penetration Trend"}),e.jsx("button",{onClick:()=>o("penetration"),className:"text-slate-400 hover:text-sky-600 transition-colors",title:"Expand",children:e.jsx(be,{size:14})})]}),e.jsx("p",{className:"text-xs text-slate-400 mb-3",children:"MA penetration % (left) vs. MA, Traditional, and total Medicare enrollment (right)"}),e.jsx(Q,{width:"100%",height:M,children:e.jsxs(ce,{data:j,margin:{left:a?-8:0,right:a?-4:0,top:4,bottom:0},children:[e.jsx(Z,{strokeDasharray:"3 3",stroke:"#f1f5f9"}),e.jsx(J,{dataKey:"date",tick:{fontSize:a?9:11},interval:"preserveStartEnd",minTickGap:a?28:12}),e.jsx(G,{yAxisId:"left",domain:[0,100],tickFormatter:t=>t+"%",tick:{fontSize:a?9:11},width:a?30:40}),e.jsx(G,{yAxisId:"right",orientation:"right",tick:{fontSize:a?9:11},width:a?32:48,tickFormatter:t=>t>=1e6?`${(t/1e6).toFixed(1)}M`:t>=1e3?`${Math.round(t/1e3)}K`:t}),e.jsx(U,{formatter:(t,N)=>N==="Penetration %"?t+"%":t?.toLocaleString()}),e.jsx(he,{wrapperStyle:{fontSize:a?10:12}}),e.jsx(W,{yAxisId:"left",type:"monotone",dataKey:"Penetration %",stroke:"#0ea5e9",strokeWidth:2,dot:!1}),e.jsx(W,{yAxisId:"right",type:"monotone",dataKey:"MA Enrolled",stroke:"#10b981",strokeWidth:2,dot:!1}),e.jsx(W,{yAxisId:"right",type:"monotone",dataKey:"Traditional",stroke:"#475569",strokeWidth:2,dot:!1}),e.jsx(W,{yAxisId:"right",type:"monotone",dataKey:"Total Medicare",stroke:"#94a3b8",strokeWidth:1.5,strokeDasharray:"4 3",dot:!1})]})})]}),s.length>0&&e.jsxs("div",{className:"bg-white rounded-xl border border-slate-200 shadow-sm p-3 sm:p-5",children:[e.jsxs("div",{className:"flex items-center justify-between mb-0.5",children:[e.jsx("h3",{className:"text-sm font-semibold text-slate-700",children:"Current Enrollment by Plan"}),e.jsx("button",{onClick:()=>o("enrollment"),className:"text-slate-400 hover:text-sky-600 transition-colors",title:"Expand",children:e.jsx(be,{size:14})})]}),e.jsxs("p",{className:"text-xs text-slate-400 mb-3",children:["Latest month ","â"," top ",Math.min(10,P.length)," plans by parent organization",a?" (tap a bar for the full name)":""]}),e.jsx(Q,{width:"100%",height:Math.max(220,s.length*(a?30:34)),children:e.jsxs(Ee,{data:s,layout:"vertical",margin:{left:a?0:160,right:a?44:60},children:[e.jsx(Z,{strokeDasharray:"3 3",stroke:"#f1f5f9"}),e.jsx(J,{type:"number",tick:{fontSize:a?9:11},tickFormatter:t=>t>=1e6?`${(t/1e6).toFixed(1)}M`:t>=1e3?`${(t/1e3).toFixed(0)}K`:t}),e.jsx(G,{type:"category",dataKey:"name",tick:{fontSize:a?9:11},width:a?92:155,tickFormatter:a?t=>t&&t.length>13?t.slice(0,12)+"â¦":t:void 0}),e.jsx(U,{formatter:t=>t?.toLocaleString(),labelFormatter:(t,N)=>N?.[0]?.payload?.fullName}),e.jsx(Le,{dataKey:"enrolled",fill:"#0ea5e9",radius:[0,4,4,0],children:e.jsx(bt,{dataKey:"enrolled",position:"right",formatter:t=>t>=1e6?`${(t/1e6).toFixed(1)}M`:t>=1e3?`${Math.round(t/1e3)}K`:t,style:{fontSize:a?9:10,fill:"#64748b"}})})]})})]}),I.length>0&&e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"flex items-center justify-between flex-wrap gap-2",children:[e.jsxs("div",{children:[e.jsx("h3",{className:"text-sm font-semibold text-slate-700",children:"Enrollment Trend by Plan"}),e.jsxs("p",{className:"text-xs text-slate-400",children:["Top 4 plans + Other ","â"," ",b?"% of total MA enrolled":"monthly enrollment"]})]}),e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("button",{onClick:()=>o("trend"),className:"text-slate-400 hover:text-sky-600 transition-colors",title:"Expand",children:e.jsx(be,{size:14})}),e.jsxs("div",{className:"flex items-center gap-0 bg-white border border-slate-200 rounded-lg p-0.5",children:[e.jsx("button",{onClick:()=>r(!1),className:`px-3 py-1 text-xs font-medium rounded-md transition-colors ${b?"text-slate-500 hover:text-slate-700":"bg-sky-600 text-white"}`,children:"Enrolled"}),e.jsx("button",{onClick:()=>r(!0),className:`px-3 py-1 text-xs font-medium rounded-md transition-colors ${b?"bg-sky-600 text-white":"text-slate-500 hover:text-slate-700"}`,children:"% Share"})]})]})]}),e.jsx("div",{className:"bg-white rounded-xl border border-slate-200 shadow-sm p-3 sm:p-5",children:e.jsx(Q,{width:"100%",height:d?250:320,children:e.jsxs(ce,{data:I,margin:{left:a?-10:0,right:a?4:0,top:4,bottom:0},children:[e.jsx(Z,{strokeDasharray:"3 3",stroke:"#f1f5f9"}),e.jsx(J,{dataKey:"date",tick:{fontSize:a?9:10},interval:"preserveStartEnd",minTickGap:a?28:12}),e.jsx(G,{tick:{fontSize:a?9:10},width:a?34:56,tickFormatter:b?t=>t+"%":t=>t>=1e6?`${(t/1e6).toFixed(1)}M`:t>=1e3?`${Math.round(t/1e3)}K`:t}),e.jsx(U,{formatter:t=>
1b?t+"%":t?.toLocaleString()}),e.jsx(he,{wrapperStyle:{fontSize:a?10:11}}),h.map((t,N)=>e.jsx(W,{type:"monotone",dataKey:t,stroke:t==="Other"?"#94a3b8":pe[N%pe.length],strokeWidth:t==="Other"?1:1.5,strokeDasharray:t==="Other"?"4 3":void 0,dot:!1},t))]})})})]}),!d&&H.length>0&&e.jsxs("div",{className:"bg-white rounded-xl border border-slate-200 shadow-sm overflow-hidden",children:[e.jsxs("div",{className:"flex items-center justify-between px-4 py-3 border-b border-slate-100",children:[e.jsxs("p",{className:"text-xs font-semibold text-slate-600 uppercase tracking-wide",children:["Plan ",b?"Market Share":"Enrollment"," History"]}),e.jsxs("button",{onClick:()=>Ne(H.slice().reverse().map(t=>{const N={Date:t.date};O.forEach(F=>{N[F]=t[F]!=null?b?t[F].toFixed(1)+"%":t[F]:""});const T=L[t.date];return N["Traditional Medicare"]=T?b?T.pct!=null?T.pct.toFixed(1)+"%":"":T.count:"",N}),["Date",...O,...K?["Traditional Medicare"]:[]],`ma-plan-enrollment-${u.ssa_codes?.join("-")||"export"}.csv`),className:"flex items-center gap-1 text-xs text-slate-500 hover:text-sky-600 font-medium transition-colors",children:[e.jsx(Y,{size:13})," Export CSV"]})]}),e.jsx("div",{className:"overflow-x-auto max-h-[400px] overflow-y-auto",children:e.jsxs("table",{className:"w-full text-sm",children:[e.jsx("thead",{className:"bg-slate-50 border-b border-slate-200 sticky top-0",children:e.jsxs("tr",{children:[e.jsx("th",{scope:"col",className:"px-3 py-2 text-left text-xs font-semibold text-slate-500 uppercase tracking-wide sticky left-0 bg-slate-50",children:"Date"}),O.map(t=>e.jsx("th",{scope:"col",className:"px-3 py-2 text-right text-xs font-semibold text-slate-500 uppercase tracking-wide max-w-[120px]",title:t,children:e.jsx("span",{className:"block truncate",children:t})},t)),K&&e.jsx("th",{scope:"col",className:"px-3 py-2 text-right text-xs font-semibold text-slate-500 uppercase tracking-wide max-w-[120px] border-l border-slate-200",title:b?"Traditional (non-MA) Medicare as a share of all Medicare beneficiaries":"Traditional (non-MA) Medicare enrollees = total eligibles minus MA-enrolled",children:e.jsx("span",{className:"block truncate",children:"Traditional Medicare"})})]})}),e.jsx("tbody",{className:"divide-y divide-slate-100",children:H.slice().reverse().map((t,N)=>e.jsxs("tr",{className:"hover:bg-slate-50",children:[e.jsx("td",{className:"px-3 py-2 font-mono text-slate-600 sticky left-0 bg-white",children:t.date}),O.map(T=>e.jsx("td",{className:"px-3 py-2 text-right text-slate-600",children:t[T]!=null?b?t[T].toFixed(1)+"%":t[T].toLocaleString():"â"},T)),K&&e.jsx("td",{className:"px-3 py-2 text-right text-slate-600 border-l border-slate-100",children:L[t.date]?b?L[t.date].pct!=null?L[t.date].pct.toFixed(1)+"%":"â":L[t.date].count.toLocaleString():"â"})]},N))})]})})]}),x&&e.jsx("div",{className:"fixed inset-0 z-50 bg-black/50 flex items-center justify-center p-2 sm:p-8",onClick:()=>o(null),children:e.jsxs("div",{ref:A,role:"dialog","aria-modal":"true","aria-labelledby":"ma-chart-expanded-title",className:"bg-white rounded-lg shadow-lg w-full max-w-6xl max-h-[90vh] overflow-auto p-4 sm:p-8",onClick:t=>t.stopPropagation(),children:[e.jsxs("div",{className:"flex items-center justify-between mb-6",children:[e.jsxs("h3",{id:"ma-chart-expanded-title",className:"text-lg font-bold text-slate-800",children:[x==="penetration"&&"MA Penetration Trend",x==="enrollment"&&"Current Enrollment by Plan",x==="trend"&&`Enrollment Trend by Plan â ${b?"% share":"enrolled"}`]}),e.jsx("button",{onClick:()=>o(null),"aria-label":"Close",className:"text-slate-400 hover:text-slate-600 transition-colors",children:e.jsx(le,{size:20})})]}),x==="penetration"&&e.jsx(Q,{width:"100%",height:500,children:e.jsxs(ce,{data:j,children:[e.jsx(Z,{strokeDasharray:"3 3",stroke:"#f1f5f9"}),e.jsx(J,{dataKey:"date",tick:{fontSize:12}}),e.jsx(G,{yAxisId:"left",domain:[0,100],tickFormatter:t=>t+"%",tick:{fontSize:12}}),e.jsx(G,{yAxisId:"right",orientation:"right",tick:{fontSize:12},tickFormatter:t=>t>=1e6?`${(t/1e6).toFixed(1)}M`:t>=1e3?`${Math.round(t/1e3)}K`:t}),e.jsx(U,{formatter:(t,N)=>
1N==="Penetration %"?t+"%":t?.toLocaleString()}),e.jsx(he,{}),e.jsx(W,{yAxisId:"left",type:"monotone",dataKey:"Penetration %",stroke:"#0ea5e9",strokeWidth:2.5,dot:!1}),e.jsx(W,{yAxisId:"right",type:"monotone",dataKey:"MA Enrolled",stroke:"#10b981",strokeWidth:2.5,dot:!1}),e.jsx(W,{yAxisId:"right",type:"monotone",dataKey:"Traditional",stroke:"#475569",strokeWidth:2.5,dot:!1}),e.jsx(W,{yAxisId:"right",type:"monotone",dataKey:"Total Medicare",stroke:"#94a3b8",strokeWidth:2,strokeDasharray:"4 3",dot:!1})]})}),x==="enrollment"&&s.length>0&&e.jsx(Q,{width:"100%",height:Math.max(400,s.length*42),children:e.jsxs(Ee,{data:s,layout:"vertical",margin:{left:a?0:220,right:a?44:0},children:[e.jsx(Z,{strokeDasharray:"3 3",stroke:"#f1f5f9"}),e.jsx(J,{type:"number",tick:{fontSize:a?9:12},tickFormatter:a?(t=>t>=1e6?`${(t/1e6).toFixed(1)}M`:t>=1e3?`${Math.round(t/1e3)}K`:t):void 0}),e.jsx(G,{type:"category",dataKey:"name",tick:{fontSize:a?9:12},width:a?96:210,tickFormatter:a?t=>t&&t.length>13?t.slice(0,12)+"â¦":t:void 0}),e.jsx(U,{formatter:t=>t?.toLocaleString(),labelFormatter:(t,N)=>N?.[0]?.payload?.fullName}),e.jsx(Le,{dataKey:"enrolled",fill:"#0ea5e9",radius:[0,4,4,0]})]})}),x==="trend"&&I.length>0&&e.jsx(Q,{width:"100%",height:500,children:e.jsxs(ce,{data:I,children:[e.jsx(Z,{strokeDasharray:"3 3",stroke:"#f1f5f9"}),e.jsx(J,{dataKey:"date",tick:{fontSize:12}}),e.jsx(G,{tick:{fontSize:12},tickFormatter:b?t=>t+"%":void 0}),e.jsx(U,{formatter:t=>b?t+"%":t?.toLocaleString()}),e.jsx(he,{wrapperStyle:{fontSize:12}}),h.map((t,N)=>e.jsx(W,{type:"monotone",dataKey:t,stroke:t==="Other"?"#94a3b8":pe[N%pe.length],strokeWidth:t==="Other"?1.5:2,strokeDasharray:t==="Other"?"4 3":void 0,dot:!1},t))]})})]})})]})}const ae=["#0ea5e9","#f97316","#10b981","#8b5cf6","#ef4444","#06b6d4","#f59e0b","#ec4899","#14b8a6","#6366f1","#84cc16","#a855f7","#fb923c","#22d3ee","#f43f5e","#4ade80","#c084fc","#fbbf24","#2dd4bf","#818cf8","#a3e635","#e879f9","#fb7185","#34d399","#facc15"];function kt(){const u=f.useRef(null),[d,b]=f.useState("ALL"),[r,x]=f.useState(""),[o,m]=f.useState(""),[A,a]=f.useState(5),{data:M}=R({queryKey:["enrollment-flow-init",d],queryFn:()=>Pe({state:d,date_from:"2020-01-01",date_to:"2020-01-01",top_n:3}),enabled:!!d}),j=M?.available_dates||[];f.useEffect(()=>{if(j.length>=2&&!r){const s=j[j.length-1],g=new Date(new Date(s).setFullYear(new Date(s).getFullYear()-1));let h=j[0],l=1/0;for(const C of j){const O=Math.abs(new Date(C)-g);O<l&&(l=O,h=C)}x(h),m(s)}},[j]);const{data:w,isLoading:E}=R({queryKey:["enrollment-flow",d,r,o,A],queryFn:()=>Pe({state:d,date_from:r,date_to:o,top_n:A}),enabled:!!d&&!!r&&!!o&&r!==o}),S=f.useMemo(()=>{if(!w?.nodes?.length||!w?.links?.length)return null;const s=w.nodes.length/2,g=w.nodes.map((l,C)=>({name:l.name,enrollment:l.enrollment,share_pct:l.share_pct,fill:ae[C%s%ae.length]})),h=w.links.map(l=>({...l}));return{nodes:g,links:h}},[w]),k=f.useMemo(()=>{if(!S)return null;const s=S.nodes.length/2,g=S.links.filter(c=>c.source===c.target-s),h=S.links.filter(c=>c.source!==c.target-s),l=g.reduce((c,_)=>c+_.value,0),C=h.reduce((c,_)=>c+_.value,0),O=l+C;return{retained:l,migrated:C,total:O,pctRetained:O?(l/O*100).toFixed(1):0}},[S]),D=({x:s,y:g,width:h,height:l,index:C,payload:O})=>{if(!S)return null;const c=S.nodes.length/2,_=C<c,i=ae[C%c%ae.length],z=w?.nodes?.[C],I=O.name,$=_?s-4:s+h+4,H=_?"end":"start",L=z?.enrollment,K=z?.share_pct;return e.jsxs("g",{children:[e.jsx("rect",{x:s,y:g,width:h,height:l,fill:i,rx:2}),l>14&&e.jsxs(e.Fragment,{children:[e.jsx("text",{x:$,y:g+l/2-7,textAnchor:H,dominantBaseline:"central",fontSize:11,fill:"#334155",fontWeight:600,children:I}),e.jsxs("text",{x:$,y:g+l/2+7,textAnchor:H,dominantBaseline:"central",fontSize:9,fill:"#64748b",children:[L?.toLocaleString()," (",K,"%)"]})]}),l>8&&l<=14&&e.jsxs("text",{x:$,y:g+l/2,textAnchor:H,dominantBaseline:"central",fontSize:10,fill:"#334155",fontWeight:500,children:[I," ",L?`${(L/1e3).toFixed(0)}k`:""]})]})},P=s=>{if(!S)return null;const{sourceX:g,targetX:h,sourceY:l,targetY:C,sourceControlX:O,targetControlX:c,linkWidth:_,index:i,payload:z}=s,I=S.nodes.length/2,$=z.source?.depth!=null?w.links[i]?.source:0,H=ae[$%I%ae.length],L=z.source?.name===z.target?.name;return e.jsx("path",{d:`M${g},${l} 2 C${O},${l} ${c},${C} ${h},${C}`,fill:"none",stroke:H,strokeWidth:_,strokeOpacity:L?.25:.45,onMouseEnter:K=>K.target.style.strokeOpacity=.7,onMouseLeave:K=>K.target.style.strokeOpacity=L?.25:.45})},v=s=>s?new Date(s+"T00:00:00").toLocaleDateString("en-US",{month:"short",year:"numeric"}):"",y=()=>{const s=u.current?.querySelector("svg");if(!s)return;const g=new XMLSerializer().serializeToString(s),h=document.createElement("canvas"),l=2;h.width=s.width.baseVal.value*l,h.height=s.height.baseVal.value*l;const C=h.getContext("2d");C.scale(l,l),C.fillStyle="#ffffff",C.fillRect(0,0,h.width,h.height);const O=new Image;O.onload=()=>{C.drawImage(O,0,0);const c=document.createElement("a");c.download=`enrollment-flow-${d}-${r}-to-${o}.png`,c.href=h.toDataURL("image/png"),c.click()},O.src="data:image/svg+xml;base64,"+btoa(unescape(encodeURIComponent(g)))};return e.jsxs("div",{className:"space-y-4",children:[e.jsx("p",{className:"text-xs text-slate-500",children:"Visualize how MA enrollment shifted between plans over time. Wider flows
2= more members. Cross-plan flows are approximated from county-level enrollment changes."}),e.jsx("div",{className:"bg-white rounded-xl border border-slate-200 shadow-sm p-4",children:e.jsxs("div",{className:"flex flex-wrap gap-4 items-end",children:[e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-slate-600 mb-1",children:"State"}),e.jsxs("select",{value:d,onChange:s=>{b(s.target.value),x(""),m("")},className:"border border-slate-300 rounded-lg px-3 py-1.5 text-sm",children:[e.jsx("option",{value:"ALL",children:"All States (National)"}),Ke.map(s=>e.jsx("option",{value:s,children:s},s))]})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-slate-600 mb-1",children:"From"}),e.jsxs("select",{value:r,onChange:s=>x(s.target.value),className:"border border-slate-300 rounded-lg px-3 py-1.5 text-sm",children:[e.jsx("option",{value:"",children:"Select date"}),j.map(s=>e.jsx("option",{value:s,children:v(s)},s))]})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-slate-600 mb-1",children:"To"}),e.jsxs("select",{value:o,onChange:s=>m(s.target.value),className:"border border-slate-300 rounded-lg px-3 py-1.5 text-sm",children:[e.jsx("option",{value:"",children:"Select date"}),j.filter(s=>s>r).map(s=>e.jsx("option",{value:s,children:v(s)},s))]})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-slate-600 mb-1",children:"Top Plans"}),e.jsx("select",{value:A,onChange:s=>a(+s.target.value),className:"border border-slate-300 rounded-lg px-3 py-1.5 text-sm",children:[5,8,10,12,15,20].map(s=>e.jsx("option",{value:s,children:s},s))})]})]})}),k&&e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-3 gap-4",children:[e.jsxs("div",{className:"bg-white rounded-xl border border-slate-200 shadow-sm p-4 text-center",children:[e.jsx("div",{className:"text-xs text-slate-500 uppercase tracking-wide",children:"Total Enrollment"}),e.jsx("div",{className:"text-2xl font-bold text-slate-800 mt-1",children:k.total?.toLocaleString()})]}),e.jsxs("div",{className:"bg-white rounded-xl border border-slate-200 shadow-sm p-4 text-center group relative",children:[e.jsxs("div",{className:"text-xs text-slate-500 uppercase tracking-wide cursor-help",children:["Retained in Same Plan",e.jsx(V,{text:"Approximation. For each county, the minimum of a plan's enrollment across both periods is counted as 'retained.' This assumes those members stayed, but some may have left and been replaced by new enrollees. Actual individual-level switching data is not available."})]}),e.jsxs("div",{className:"text-2xl font-bold text-emerald-600 mt-1",children:[k.pctRetained,"%"]}),e.jsxs("div",{className:"text-xs text-slate-400",children:[k.retained?.toLocaleString()," members"]})]}),e.jsxs("div",{className:"bg-white rounded-xl border border-slate-200 shadow-sm p-4 text-center group relative",children:[e.jsxs("div",{className:"text-xs text-slate-500 uppercase tracking-wide cursor-help",children:["Migrated Between Plans",e.jsx(V,{text:"Estimated member movement between plan organizations. Within each county, when a plan loses enrollment and another gains, the lost members are proportionally distributed to the gaining plans based on their relative growth. This is an approximation â actual individual member-level switching data is not available."})]}),e.jsx("div",{className:"text-2xl font-bold text-amber-600 mt-1",children:k.migrated?.toLocaleString()}),e.jsxs("div",{className:"text-xs text-slate-400",children:[k.total?(k.migrated/k.total*100).toFixed(1):0,"% of total"]})]})]}),e.jsxs("div",{className:"bg-white rounded-xl border border-slate-200 shadow-sm p-4 overflow-x-auto",style:{WebkitOverflowScrolling:"touch"},children:[!E&&S&&e.jsx("div",{className:"flex justify-end mb-1",children:e.jsxs("button",{onClick:y,className:"flex items-center gap-1 text-xs text-slate-500 hover:text-sky-600 transition-colors",children:[e.jsx(Y,{size:14})," Download PNG"]})}),E&&e.jsxs("div",{className:"flex items-center justify-center py-20",children:[e.jsx(q,{className:"animate-spin text-sky-500",size:28}),e.jsxs("span",{className:"ml-2 text-sm text-slate-500",children:["Building enrollment flow",d==="ALL"?" (national view may take a moment)":"","..."]}
2)]}),!E&&!S&&r&&o&&e.jsx("div",{className:"text-center py-20 text-slate-400",children:e.jsx("p",{children:"No enrollment flow data for this selection."})}),!E&&!r&&e.jsx("div",{className:"text-center py-20 text-slate-400",children:e.jsx("p",{children:"Select a state and date range to view enrollment flows."})}),!E&&S&&e.jsxs("div",{className:"flex flex-col items-center",ref:u,style:{minWidth:"1100px"},children:[e.jsxs("div",{className:"flex justify-between items-center mb-2 w-full max-w-5xl",children:[e.jsx("div",{className:"text-sm font-medium text-slate-600",children:v(r)}),e.jsxs("div",{className:"text-xs text-slate-400 flex items-center gap-2",children:[e.jsx("span",{className:"inline-block w-16 h-0.5 bg-slate-300"}),"Enrollment Migration",e.jsx("span",{className:"inline-block w-16 h-0.5 bg-slate-300"})]}),e.jsx("div",{className:"text-sm font-medium text-slate-600",children:v(o)})]}),e.jsx(ft,{width:1100,height:Math.max(550,S.nodes.length/2*42),data:S,node:D,link:P,nodePadding:10,nodeWidth:14,linkCurvature:.5,iterations:64,margin:{top:10,right:220,bottom:10,left:220},children:e.jsx(U,{content:({payload:s})=>{if(!s?.[0])return null;const g=s[0].payload;if(g.source&&g.target){const l=g.source.name===g.target.name;return e.jsx("div",{className:"bg-white border border-slate-200 rounded-lg shadow-lg p-3 text-xs max-w-xs",children:l?e.jsxs("div",{children:[e.jsx("strong",{children:g.source.name})," retained ",e.jsx("strong",{children:g.value?.toLocaleString()})," members"]}):e.jsxs("div",{children:[e.jsx("strong",{children:g.source.name})," â ",e.jsx("strong",{children:g.target.name}),": ",e.jsx("strong",{children:g.value?.toLocaleString()})," estimated members migrated"]})})}const h=w?.nodes?.find(l=>
2l.name===g.name);return e.jsxs("div",{className:"bg-white border border-slate-200 rounded-lg shadow-lg p-3 text-xs",children:[e.jsx("div",{className:"font-bold text-slate-800",children:g.name}),h&&e.jsxs("div",{className:"text-slate-600 mt-1",children:["Enrollment: ",h.enrollment?.toLocaleString()," (",h.share_pct,"% share)"]})]})}})})]})]}),S&&e.jsxs("div",{className:"bg-white rounded-xl border border-slate-200 shadow-sm p-4",children:[e.jsx("h3",{className:"text-xs font-semibold text-slate-600 uppercase tracking-wide mb-2",children:"Reading the Chart"}),e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-3 gap-3 text-xs text-slate-600",children:[e.jsxs("div",{children:[e.jsx("span",{className:"font-medium text-slate-800",children:"Left blocks"})," â Plan enrollment and market share at ",v(r)]}),e.jsxs("div",{children:[e.jsx("span",{className:"font-medium text-slate-800",children:"Right blocks"})," â Plan enrollment and market share at ",v(o)]}),e.jsxs("div",{children:[e.jsx("span",{className:"font-medium text-slate-800",children:"Flows"})," â Thicker = more members. Cross-plan flows show approximate migration based on county-level changes."]})]})]})]})}const ie=[[0,[21,94,89]],[.2,[13,148,136]],[.35,[34,197,94]],[.5,[250,204,21]],[.65,[251,146,60]],[.8,[220,38,38]],[1,[127,29,29]]];function St(u,d,b){const r=b-d,x=Math.max(0,Math.min(1,(u-d)/r));for(let m=0;m<ie.length-1;m++){const[A,a]=ie[m],[M,j]=ie[m+1];if(x<=M){const w=(x-A)/(M-A),E=Math.round(a[0]+w*(j[0]-a[0])),S=Math.round(a[1]+w*(j[1]-a[1])),k=Math.round(a[2]+w*(j[2]-a[2]));return`rgb(${E},${S},${k})`}}return`rgb(${ie[ie.length-1][1].join(",")})`}function _t(u){return St(u,0,65)}function Ct({initialOrg:u=null,clearInitialOrg:d=()=>{}}={}){const[b,r]=f.useState(""),[x,o]=f.useState(""),[m,A]=f.useState(u);f.useEffect(()=>{u&&d()},[]);const[a,M]=f.useState(null),[j,w]=f.useState({col:"enrolled",dir:"desc"}),[E,S]=f.useState({col:"enrolled",dir:"desc"}),[k,D]=f.useState({col:"total_enrollment",dir:"desc"}),[P,v]=f.useState(""),[y,s]=f.useState("yoy"),[g,h]=f.useState(!1),{data:l,isLoading:C}=R({queryKey:["plan-org-top20",y],queryFn:()=>Te({page_size:20,compare:y})}),{data:O,isLoading:c}=R({queryKey:["plan-org-search",x,y],queryFn:()=>Te({q:x,page_size:50,compare:y}),enabled:x.length>0}),_=x?O:l,i=x?c:C,{data:z,isLoading:I}=R({queryKey:["plan-org-detail",m,y],queryFn:()=>ot(m,{compare:y}),enabled:!!m}),{data:$,isLoading:H}=R({queryKey:["plan-detail",a,y],queryFn:()=>it(a,{compare:y}),enabled:!!a}),L=a?$:z;function K(n){n.preventDefault(),o(b),A(null),M(null)}function t(n){A(n),M(null),v(""),w({col:"enrolled",dir:"desc"}),S({col:"enrolled",dir:"desc"})}function N(n){M(n),v(""),w({col:"enrolled",dir:"desc"}),S({col:"enrolled",dir:"desc"})}function T(){M(null),v("")}function F(){A(null),M(null),v("")}const ke=5;function ne(n){return n==null?"text-slate-400":n>=ke?"text-emerald-600":n<=-ke?"text-red-600":"text-slate-500"}function de({pct:n}){if(n==null)return e.jsx("span",{className:"text-xs text-slate-400",children:"â"});const p=n>0,te=n<0;return e.jsxs("span",{className:`inline-flex items-center gap-0.5 text-xs font-medium ${ne(n)}`,children:[p?e.jsx(et,{size:12}):te?e.jsx(tt,{size:12}):e.jsx(st,{size:12}),p?"+":"",n,"%"]})}function ue(n,p){const te=[...n];return te.sort((We,Ve)=>{let X=We[p.col],se=Ve[p.col];return X==null&&(X=-1/0),se==null&&(se=-1/0),typeof X=="string"?p.dir==="desc"?se.localeCompare(X):X.localeCompare(se):p.dir==="desc"?se-X:X-se}),te}function Se(n){return n.map(p=>({...p,growth_num:p.enrolled!=null&&p.enrolled_prior!=null?p.enrolled-p.enrolled_prior:null}))}const ge=f.useMemo(()=>ue(Se(L?.states||[]),j),[L?.states,j]),fe=f.useMemo(()=>{let n=L?.top_counties||[];return P&&(n=n.filter(p=>p.state===P)),ue(Se(n),E)},[L?.top_counties,E,P]),_e=f.useMemo(()=>ue(z?.contracts||[],k),[z?.contracts,k]);function re(n){w(p=>({col:n,dir:p.col===n&&p.dir==="desc"?"asc":"desc"}))}function oe(n){S(p=>({col:n,dir:p.col===n&&p.dir==="desc"?"asc":"desc"}))}function ee(n){D(p=>({col:n,dir:p.col===n&&p.dir==="desc"?"asc":"desc"}))}function B({col:n,sort:p}){return p.col!==n?null:e.jsx("span",{className:"ml-0.5 text-sky-500",children:p.dir==="asc"?"â²":"â¼"})}const xe=Ne;function Re(){xe(ge,[{label:"State",get:p=>p.state},{label:"Enrolled",get:p=>p.enrolled},{label:"Counties",get:p=>p.counties},{label:"Growth #",get:p=>p.growth_num},{label:"Growth %",get:p=>p.growth_pct}],`${a||m||"plan"}_states.csv`)}function He(){xe(fe,[{label:"County",get:p=>p.county},{label:"State",get:p=>p.state},{label:"Enrolled",get:p=>p.enrolled},{label:y==="mom"?"Prior Month":"Prior Year",get:p=>p.enrolled_prior},{label:"Growth #",get:p=>p.growth_num},{label:"Growth %",get:p=>p.growth_pct}],`${a||m||"plan"}_counties.csv`)}function Be(){xe(_e,[{label:"Contract ID",get:p=>p.contract_id},{label:"Marketing Name",get:p=>p.organization_marketing_name},{label:"Type",get:p=>p.plan_type},{label:"Enrollment",get:p=>p.total_enrollment},{label:"States",get:p=>p.states_served},{label:"Counties",get:p=>p.counties_served}],`${m||"org"}_contracts.csv`)}const me=m?"detail":"search",qe=m&&(I||a&&H),Ce=n=>n?new Date(n+"T00:00:00").toLocaleDateString("en-US",{month:"long",year:"numeric"}):"";return e.jsxs("div",{className:"space-y-6",children:[_?.enrollment_date&&e.jsxs("div",{className:"bg-sky-50 border border-sky-200 rounded-lg px-4 py-2 text-xs text-sky-800",children:[e.jsxs("span",{className:"font-semibold",children:["Data as of ",Ce(_.enrollment_date)]})," ","â CMS Medicare Advantage enrollment and plan directory data (abridged monthly release).",_.directory_date&&_.directory_date!==_.enrollment_date&&e.jsxs(e.Fragment,{children:[" Plan directory updated ",Ce(_.directory_date),"."]})]}),e.jsxs("div",{className:"flex flex-col sm:flex-row gap-3 sm:items-center sm:justify-between",children:[e.jsxs("form",{onSubmit:K,className:"flex gap-3 flex-1",children:[e.jsxs("div",{className:"relative flex-1 max-w-lg",children:[e.jsx($e,{size:15,className:"absolute left-3 top-1/2 -translate-y-1/2 text-slate-400"}),e.jsx("input",{value:b,onChange:n=>r(n.target.value),placeholder:"Search by plan name, parent organization, or contract ID...","aria-label":"Search MA plans",className:"w-full pl-9 pr-3 py-2 border border-slate-300 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-sky-500"})]}),e.jsx("button",{type:"submit",className:"bg-sky-600 hover:bg-sky-700 text-white px-5 py-2 rounded-lg text-sm font-medium transition-colors",children:"Search"})]}),e.jsxs("div",{className:"flex items-center bg-slate-100 rounded-lg p-0.5 text-xs font-medium",children:[e.jsx("button",{onClick:()=>s("yoy"),className:`px-3 py-1.5 rounded-md transition-colors ${y==="yoy"?"bg-white text-sky-700 shadow-sm":"text-slate-500 hover:text-slate-700"}`,children:"Year over Year"}),e.jsx("button",{onClick:()=>s("mom"),className:`px-3 py-1.5 rounded-md transition-colors ${y==="mom"?"bg-white text-sky-700 shadow-sm":"text-slate-500 hover:text-slate-700"}
2`,children:"vs Prior Month"})]})]}),m&&e.jsxs("div",{className:"flex items-center gap-1.5 text-sm text-slate-500",children:[e.jsx("button",{onClick:F,className:"hover:text-sky-600 transition-colors",children:"Search Results"}),e.jsx(je,{size:12,className:"rotate-[-90deg]"}),e.jsx("button",{onClick:T,className:`transition-colors ${a?"hover:text-sky-600":"text-slate-800 font-medium"}`,children:m}),a&&e.jsxs(e.Fragment,{children:[e.jsx(je,{size:12,className:"rotate-[-90deg]"}),e.jsx("span",{className:"text-slate-800 font-medium font-mono",children:a})]})]}),me==="search"&&i&&e.jsxs("div",{className:"flex items-center gap-2 text-slate-500 py-8 justify-center",children:[e.jsx(q,{size:18,className:"animate-spin"})," ",x?"Searching organizations...":"Loading top organizations..."]}),me==="search"&&_&&e.jsxs("div",{className:"bg-white rounded-xl border border-slate-200 shadow-sm overflow-hidden",children:[e.jsxs("div",{className:"px-4 py-3 border-b border-slate-100 bg-slate-50 flex items-center justify-between",children:[e.jsx("p",{className:"text-sm text-slate-600",children:x?`${_.total.toLocaleString()} organizations found`:"Top 20 organizations by enrollment"}),e.jsxs("div",{className:"flex items-center gap-3",children:[!x&&e.jsx("p",{className:"text-xs text-slate-400",children:"Use the search above to find more"}),e.jsxs("button",{onClick:()=>xe(_.results,[{label:"Parent Organization",get:n=>n.parent_organization},{label:"Plan Types",get:n=>n.plan_types},{label:"Contracts",get:n=>n.contracts},{label:"Enrollment",get:n=>n.total_enrollment},{label:"Growth #",get:n=>n.growth_num},{label:"Growth %",get:n=>n.growth_pct},{label:"States",get:n=>n.states_served},{label:"Counties",get:n=>n.counties_served}],"ma_organizations.csv"),className:"text-xs text-slate-400 hover:text-sky-600 flex items-center gap-1 transition-colors",children:[e.jsx(Y,{size:12})," CSV"]})]})]}),e.jsx("div",{className:"overflow-x-auto",children:e.jsxs("table",{className:"w-full text-sm",children:[e.jsx("thead",{className:"bg-slate-50 border-b border-slate-200",children:e.jsxs("tr",{children:[e.jsx("th",{scope:"col",className:"text-left px-3 py-2.5 text-xs font-semibold text-slate-600 uppercase",children:"Parent Organization"}),e.jsx("th",{scope:"col",className:"text-left px-3 py-2.5 text-xs font-semibold text-slate-600 uppercase",children:"Plan Types"}),e.jsx("th",{scope:"col",className:"text-right px-3 py-2.5 text-xs font-semibold text-slate-600 uppercase",children:"Contracts"}),e.jsx("th",{scope:"col",className:"text-right px-3 py-2.5 text-xs font-semibold text-slate-600 uppercase",children:"Enrollment"}),e.jsx("th",{scope:"col",className:"text-right px-3 py-2.5 text-xs font-semibold text-slate-600 uppercase",children:"Growth #"}),e.jsx("th",{scope:"col",className:"text-right px-3 py-2.5 text-xs font-semibold text-slate-600 uppercase",children:"Growth %"}),e.jsx("th",{scope:"col",className:"text-right px-3 py-2.5 text-xs font-semibold text-slate-600 uppercase",children:"States"}),e.jsx("th",{scope:"col",className:"text-right px-3 py-2.5 text-xs font-semibold text-slate-600 uppercase",children:"Counties"})]})}),e.jsx("tbody",{className:"divide-y divide-slate-100",children:_.results.map(n=>e.jsxs("tr",{onClick:()=>t(n.parent_organization),className:"hover:bg-sky-50 cursor-pointer transition-colors",children:[e.jsx("td",{className:"px-3 py-2.5 font-medium text-sky-700",children:n.parent_organization}),e.jsx("td",{className:"px-3 py-2.5 text-slate-500 text-xs max-w-xs truncate",children:n.plan_types||"â"}),e.jsx("td",{className:"px-3 py-2.5 text-right text-slate-700 font-medium",children:n.contracts}),e.jsx("td",{className:"px-3 py-2.5 text-right text-slate-700 font-medium",children:n.total_enrollment?.toLocaleString()||"â"}),e.jsx("td",{className:`px-3 py-2.5 text-right font-medium ${ne(n.growth_pct)}`,children:n.growth_num!=null?`${n.growth_num>0?"+":""}${n.growth_num.toLocaleString()}`:"â"}),e.jsx("td",{className:"px-3 py-2.5 text-right",children:e.jsx(de,{pct:n.growth_pct})}),e.jsx("td",{className:"px-3 py-2.5 text-right text-slate-600",children:n.states_served||"â"}),e.jsx("td",{className:"px-3 py-2.5 text-right text-slate-600",children:n.counties_served?.toLocaleStr
2ing()||"â"})]},n.parent_organization))})]})})]}),me==="detail"&&qe&&e.jsxs("div",{className:"flex items-center gap-2 text-slate-500 py-8 justify-center",children:[e.jsx(q,{size:18,className:"animate-spin"})," Loading..."]}),me==="detail"&&L&&e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"bg-white rounded-xl border border-slate-200 shadow-sm p-5",children:[e.jsxs("div",{className:"flex items-start justify-between",children:[e.jsxs("div",{children:[e.jsx("h2",{className:"text-xl font-bold text-slate-800 mb-1",children:a?$?.organization_marketing_name||a:m}),e.jsx("p",{className:"text-sm text-slate-500",children:a?[$?.parent_organization,$?.plan_type,$?.organization_type,$?.tax_status].filter(Boolean).join(" · "):[z?.plan_types,`${z?.total_contracts} contract${z?.total_contracts!==1?"s":""}`].filter(Boolean).join(" · ")}),a&&e.jsx("span",{className:"inline-block mt-1 text-xs font-mono bg-slate-100 px-2 py-0.5 rounded text-slate-500",children:a})]}),e.jsx("button",{onClick:a?T:F,className:"text-slate-400 hover:text-slate-600 transition-colors",title:a?"Back to organization":"Back to search",children:e.jsx(le,{size:18})})]}),e.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-5 gap-4 mt-5",children:[e.jsxs("div",{className:"bg-slate-50 rounded-lg p-3 text-center",children:[e.jsx("p",{className:"text-[10px] text-slate-500 uppercase tracking-wide",children:"Total Enrollment"}),e.jsx("p",{className:"text-2xl font-bold text-slate-800",children:L.total_enrollment?.toLocaleString()||"â"}),e.jsxs("button",{onClick:()=>h(!0),className:"mt-1 inline-flex items-center gap-0.5 text-xs font-medium text-sky-600 hover:text-sky-700 transition-colors",children:["View trend ",e.jsx(gt,{size:11})]})]}),e.jsxs("div",{className:"bg-slate-50 rounded-lg p-3 text-center",children:[e.jsx("p",{className:"text-[10px] text-slate-500 uppercase tracking-wide",children:"Member Growth"}),L.total_enrollment!=null&&L.total_enrollment_prior!=null?e.jsxs(e.Fragment,{children:[e.jsxs("p",{className:`text-2xl font-bold ${ne(L.growth_pct)}`,children:[L.total_enrollment-L.total_enrollment_prior>0?"+":"",(L.total_enrollment-L.total_enrollment_prior).toLocaleString()]}),L.growth_pct!=null&&e.jsx(de,{pct:L.growth_pct})]}):e.jsx("p",{className:"text-2xl font-bold text-slate-800",children:"â"}),e.jsx("p",{className:"text-xs text-slate-400",children:y==="mom"?"vs prior month":"Year over year"})]}),e.jsxs("div",{className:"bg-slate-50 rounded-lg p-3 text-center",children:[e.jsx("p",{className:"text-[10px] text-slate-500 uppercase tracking-wide",children:y==="mom"?"Prior Month":"Prior Year"}),e.jsx("p",{className:"text-2xl font-bold text-slate-800",children:L.total_enrollment_prior?.toLocaleString()||"â"})]}),e.jsxs("div",{className:"bg-slate-50 rounded-lg p-3 text-center",children:[e.jsx("p",{className:"text-[10px] text-slate-500 uppercase tracking-wide",children:"States"}),e.jsx("p",{className:"text-2xl font-bold text-slate-800",children:L.states_served||"â"})]}),e.jsxs("div",{className:"bg-slate-50 rounded-lg p-3 text-center",children:[e.jsx("p",{className:"text-[10px] text-slate-500 uppercase tracking-wide",children:"Counties"}),e.jsx("p",{className:"text-2xl font-bold text-slate-800",children:L.counties_served?.toLocaleString()||"â"})]})]})]}),g&&e.jsx(Mt,{params:a?{contract_id:a}:{parent_org:m},title:a?$?.organization_marketing_name||a:m,subtitle:a||"All contracts",onClose:()=>h(!1)}),e.jsxs("div",{className:"grid grid-cols-1 lg:grid-cols-2 gap-6",children:[e.jsxs("div",{className:"bg-white rounded-xl border border-slate-200 shadow-sm overflow-hidden",children:[e.jsxs("div",{className:"px-4 py-3 border-b border-slate-100 bg-slate-50 flex items-center justify-between",children:[e.jsx("h3",{className:"text-sm font-semibold text-slate-700",children:"Enrollment by State"}),e.jsxs("button",{onClick:Re,className:"text-xs text-slate-400 hover:text-sky-600 flex items-center gap-1 transition-colors",children:[e.jsx(Y,{size:12})," CSV"]})]}),e.jsx("div",{className:"overflow-y-auto",style:{maxHeight:500},children:e.jsxs("table",{className:"w-full text-sm",children:[e.jsx("thead",{className:"bg-slate-50 border-b border-slate-200 sticky top-0 z-10",children:e.jsxs("tr",{children:[e.jsxs("th",{scope:"col",onClick:()=>re("state"),className:"text-left px-3 py-2 text-xs font-semibold text-slate-600 uppercase cursor-pointer hover:text-sky-600",children:["State",e.jsx(B,{col:"state",sort:j})]}),e.jsxs("th",{scope:"col",onClick:()=>re("enrolled"),className:"text-right px-3 py-2 text-xs font-semibold text-slate-600 uppercase cursor-pointer hover:text-sky-600",children:["Enrolled",e.jsx(B,{col:"enrolled",sort:j})]}),e.jsxs("th",{scope:"col",onClick:()=>re("counties"),className:"text-right px-3 py-2 text-xs font-semibold text-slate-600 uppercase cursor-pointer hover:text-sky-600",children:["Counties",e.jsx(B,{col:"counties",sort:j})]}),e.jsxs("th",{scope:"col",onClick:()=>re("growth_num"),className:"text-right px-3 py-2 text-xs font-semibold text-slate-600 uppercase cursor-pointer hover:text-sky-600",children:["Growth #",e.jsx(B,{col:"growth_num",sort:j})]}),e.jsxs("th",{scope:"col",onClick:()=>re("growth_pct"),className:"text-right px-3 py-2 text-xs font-semibold text-slate-600 uppercase cursor-pointer hover:text-sky-600",children:["Growth %",e.jsx(B,{col:"growth_pct",sort:j})]})]})}),e.jsx("tbody",{className:"divide-y divide-slate-100",children:ge.map(n=>
2{const p=ge[0]?.enrolled||1;return e.jsxs("tr",{onClick:()=>v(te=>te===n.state?"":n.state),className:`cursor-pointer transition-colors ${P===n.state?"bg-sky-50":"hover:bg-slate-50"}`,children:[e.jsx("td",{className:"px-3 py-2 font-medium text-slate-800",children:n.state}),e.jsx("td",{className:"px-3 py-2 text-right",children:e.jsxs("div",{className:"flex items-center justify-end gap-2",children:[e.jsx("div",{className:"w-16 h-2 bg-slate-100 rounded-full overflow-hidden",children:e.jsx("div",{className:"h-full bg-sky-400 rounded-full",style:{width:`${n.enrolled/p*100}%`}})}),e.jsx("span",{className:"text-slate-700 font-medium w-16 text-right",children:n.enrolled.toLocaleString()})]})}),e.jsx("td",{className:"px-3 py-2 text-right text-slate-600",children:n.counties}),e.jsx("td",{className:`px-3 py-2 text-right font-medium ${ne(n.growth_pct)}`,children:n.growth_num!=null?`${n.growth_num>0?"+":""}${n.growth_num.toLocaleString()}`:"â"}),e.jsx("td",{className:"px-3 py-2 text-right",children:e.jsx(de,{pct:n.growth_pct})})]},n.state)})})]})})]}),e.jsxs("div",{className:"bg-white rounded-xl border border-slate-200 shadow-sm overflow-hidden",children:[e.jsxs("div",{className:"px-4 py-3 border-b border-slate-100 bg-slate-50 flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs("h3",{className:"text-sm font-semibold text-slate-700",children:["Top Counties ",P&&e.jsxs("span",{className:"text-sky-600 ml-1",children:["â ",P]})]}),P&&e.jsxs("button",{onClick:()=>v(""),className:"text-xs text-slate-400 hover:text-slate-600 flex items-center gap-1",children:[e.jsx(le,{size:12})," Clear"]})]}),e.jsxs("button",{onClick:He,className:"text-xs text-slate-400 hover:text-sky-600 flex items-center gap-1 transition-colors",children:[e.jsx(Y,{size:12})," CSV"]})]}),e.jsx("div",{className:"overflow-y-auto",style:{maxHeight:500},children:e.jsxs("table",{className:"w-full text-sm",children:[e.jsx("thead",{className:"bg-slate-50 border-b border-slate-200 sticky top-0 z-10",children:e.jsxs("tr",{children:[e.jsxs("th",{scope:"col",onClick:()=>oe("county"),className:"text-left px-3 py-2 text-xs font-semibold text-slate-600 uppercase cursor-pointer hover:text-sky-600",children:["County",e.jsx(B,{col:"county",sort:E})]}),e.jsx("th",{scope:"col",className:"text-left px-3 py-2 text-xs font-semibold text-slate-600 uppercase",children:"ST"}),e.jsxs("th",{scope:"col",onClick:()=>oe("enrolled"),className:"text-right px-3 py-2 text-xs font-semibold text-slate-600 uppercase cursor-pointer hover:text-sky-600",children:["Enrolled",e.jsx(B,{col:"enrolled",sort:E})]}),e.jsxs("th",{scope:"col",onClick:()=>oe("enrolled_prior"),className:"text-right px-3 py-2 text-xs font-semibold text-slate-600 uppercase cursor-pointer hover:text-sky-600",children:["Prior",e.jsx(B,{col:"enrolled_prior",sort:E})]}),e.jsxs("th",{scope:"col",onClick:()=>oe("growth_num"),className:"text-right px-3 py-2 text-xs font-semibold text-slate-600 uppercase cursor-pointer hover:text-sky-600",children:["Growth #",e.jsx(B,{col:"growth_num",sort:E})]}),e.jsxs("th",{scope:"col",onClick:()=>oe("growth_pct"),className:"text-right px-3 py-2 text-xs font-semibold text-slate-600 uppercase cursor-pointer hover:text-sky-600",children:["Growth %",e.jsx(B,{col:"growth_pct",sort:E})]})]})}),e.jsxs("tbody",{className:"divide-y divide-slate-100",children:[fe.map((n,p)=>e.jsxs("tr",{className:"hover:bg-slate-50",children:[e.jsx("td",{className:"px-3 py-2 text-slate-800",children:n.county}),e.jsx("td",{className:"px-3 py-2 text-slate-500",children:n.state}),e.jsx("td",{className:"px-3 py-2 text-right font-medium text-slate-700",children:n.enrolled.toLocaleString()}),e.jsx("td",{className:"px-3 py-2 text-right text-slate-500",children:n.enrolled_prior?.toLocaleString()||"â"}),e.jsx("td",{className:`px-3 py-2 text-right font-medium ${ne(n.growth_pct)}`,children:n.growth_num!=null?`${n.growth_num>0?"+":""}${n.growth_num.toLocaleString()}`:"â"}),e.jsx("td",{className:"px-3 py-2 text-right",children:e.jsx(de,{pct:n.growth_pct})})]},`${n.state}-${n.county}-${p}`)),fe.length===0&&e.jsx("tr",{children:e.jsx("td",{colSpan:6,className:"px-4 py-8 text-center text-slate-400",children:"No counties to show"})})]})]})})]})]}),!a&&z?.contracts&&e.jsxs("div",{className:"bg-white rounded-xl border border-slate-200 shadow-sm overflow-hidden",children:[e.jsxs("div",{className:"px-4 py-3 border-b border-slate-100 bg-slate-50 flex items-center justify-between",children:[e.jsxs("h3",{className:"text-sm font-semibold text-slate-700",children:["Contracts ",e.jsx("span",{className:"text-slate-400 font-normal ml-1",children:"â click to dive deeper into a specific contract"})]}),e.jsxs("button",{onClick:Be,className:"flex items-center gap-1.5 px-3 py-1.5 text-xs font-medium text-slate-600 bg-white border border-slate-200 rounded-lg hover:bg-slate-50 transition-colors",title:"Export contracts to CSV",children:[e.jsx(Y,{size:13})," CSV"]})]}),e.jsx("div",{className:"overflow-x-auto",children:e.jsxs("table",{className:"w-full text-sm",children:[e.jsx("thead",{className:"bg-slate-50 border-b border-slate-200",children:e.jsxs("tr",{children:[e.jsxs("th",{scope:"col",onClick:()=>ee("contract_id"),className:"text-left px-4 py-2.5 text-xs font-semibold text-slate-600 uppercase cursor-pointer hover:text-sky-600 select-none",children:["Contract ID",e.jsx(B,{col:"contract_id",sort:k})]}),e.jsxs("th",{scope:"col",onClick:()=>ee("organization_marketing_name"),className:"text-left px-4 py-2.5 text-xs font-semibold text-slate-600 uppercase cursor-pointer hover:text-sky-600 select-none",children:["Marketing Name",e.jsx(B,{col:"organization_marketing_name",sort:k})]}),e.jsxs("th",{scope:"col",onClick:()=>ee("plan_type"),className:"text-left px-4 py-2.5 text-xs font-semibold text-slate-600 uppercase cursor-pointer hover:text-sky-600 select-none",children:["Type",e.jsx(B,{col:"plan_type",sort:k})]}),e.jsxs("th",{scope:"col",onClick:()=>ee("total_enrollment"),className:"text-right px-4 py-2.5 text-xs font-semibold text-slate-600 uppercase cursor-pointer hover:text-sky-600 select-none",children:["Enrollment",e.jsx(B,{col:"total_enrollment",sort:k})]}),e.jsxs("th",{scope:"col",onClick:()=>ee("states_served"),className:"text-right px-4 py-2.5 text-xs font-semibold text-slate-600 uppercase cursor-pointer hover:text-sky-600 select-none",children:["States",e.jsx(B,{col:"states_served",sort:k})]}),e.jsxs("th",{scope:"col",onClick:()=>ee("counties_served"),className:"text-right px-4 py-2.5 text-xs font-semibold text-slate-600 uppercase cursor-pointer hover:text-sky-600 select-none",children:["Counties",e.jsx(B,{col:"counties_served",sort:k})]})]})}),e.jsx("tbody",{className:"divide-y divide-slate-100",children:_e.map(n=>e.jsxs("tr",{onClick:()=>N(n.contract_id),className:"hover:bg-sky-50 cursor-pointer transition-colors",children:[e.jsx("td",{className:"px-4 py-2.5 font-mono text-sky-600 font-medium",children:n.contract_id}),e.jsx("td",{className:"px-4 py-2.5 font-medium text-slate-800",children:n.organization_marketing_name||"â"}),e.jsx("td",{className:"px-4 py-2.5 text-slate-500 text-xs",children:n.plan_type||"â"}),e.jsx("td",{className:"px-4 py-2.5 text-right text-slate-700 font-medium",children:n.total_enrollment?.toLocaleStr
2ing()||"â"}),e.jsx("td",{className:"px-4 py-2.5 text-right text-slate-600",children:n.states_served||"â"}),e.jsx("td",{className:"px-4 py-2.5 text-right text-slate-600",children:n.counties_served?.toLocaleString()||"â"})]},n.contract_id))})]})})]})]})]})}function Mt({params:u,title:d,subtitle:b,onClose:r}){const x=f.useRef(null),o=ve(r),{data:m,isLoading:A,isError:a,error:M}=R({queryKey:["plan-enrollment-history",u],queryFn:()=>ct(u),retry:!1});f.useEffect(()=>{const i=z=>{z.key==="Escape"&&r()};return document.addEventListener("keydown",i),()=>document.removeEventListener("keydown",i)},[r]);const j=m?.series||[],w=j.map(i=>i.report_date),[E,S]=f.useState(""),[k,D]=f.useState("");f.useEffect(()=>{w.length&&!E&&!k&&(S(w[0]),D(w[w.length-1]))},[w,E,k]);const P=i=>i?new Date(i+"T00:00:00").toLocaleDateString("en-US",{month:"short",year:"numeric"}):"",v=E||w[0],y=k||w[w.length-1],s=j.filter(i=>i.report_date>=v&&i.report_date<=y).map(i=>({date:i.report_date.slice(0,7),Enrolled:i.enrolled})),g=s[0]?.Enrolled,h=s[s.length-1]?.Enrolled,l=g!=null&&h!=null?h-g:null,C=l!=null&&g>0?l/g*100:null,O=s.map(i=>i.Enrolled).filter(i=>i!=null);let c=["auto","auto"];if(O.length){const i=Math.min(...O),z=Math.max(...O),$=(z-i||Math.max(1,z*.1))*.06,H=K=>{const t=Math.pow(10,Math.max(0,String(Math.round(K)).length-2));return Math.max(0,Math.floor(K/t)*t)},L=K=>{const t=Math.pow(10,Math.max(0,String(Math.round(K)).length-2));return Math.ceil(K/t)*t};c=[H(i-$),L(z+$)]}const _=()=>{const i=x.current?.querySelector("svg");if(!i)return;const z=new XMLSerializer().serializeToString(i),I=document.createElement("canvas"),$=2,H=i.width?.baseVal?.value||i.clientWidth||800,L=i.height?.baseVal?.value||i.clientHeight||340;I.width=H*$,I.height=L*$;const K=I.getContext("2d");K.scale($,$),K.fillStyle="#ffffff",K.fillRect(0,0,H,L);const t=new Image;t.onload=()=>{K.drawImage(t,0,0);const N=document.createElement("a"),T=String(d||"plan").replace(/[^a-z0-9]+/gi,"-").toLowerCase().slice(0,40);N.download=`enrollment-trend-${T}-${v}-to-${y}.png`,N.href=I.toDataURL("image/png"),N.click()},t.src="data:image/svg+xml;base64,"+btoa(unescape(encodeURIComponent(z)))};return e.jsx("div",{className:"fixed inset-0 z-50 bg-black/50 flex items-center justify-center p-6",onClick:r,children:e.jsxs("div",{ref:o,role:"dialog","aria-modal":"true","aria-labelledby":"enrollment-trend-modal-title",className:"bg-white rounded-lg shadow-lg w-full max-w-3xl max-h-[88vh] overflow-auto",onClick:i=>i.stopPropagation(),children:[e.jsxs("div",{className:"flex items-start justify-between px-6 pt-5 pb-3 border-b border-slate-100",children:[e.jsxs("div",{className:"min-w-0",children:[e.jsx("h3",{id:"enrollment-trend-modal-title",className:"text-base font-semibold text-slate-800 truncate",children:d}),e.jsxs("p",{className:"text-xs text-slate-400 mt-0.5",children:["Total enrollment over time",b?` · ${b}`:""]})]}),e.jsx("button",{onClick:r,"aria-label":"Close",className:"text-slate-400 hover:text-slate-600 transition-colors shrink-0 ml-4",children:e.jsx(le,{size:18})})]}),e.jsxs("div",{className:"px-6 py-5",children:[A&&e.jsxs("div",{className:"flex items-center gap-2 text-slate-400 py-16 justify-center",children:[e.jsx(q,{size:18,className:"animate-spin"})," Loading enrollment historyâ¦"]}),a&&e.jsx("p",{className:"text-sm text-slate-500 py-12 text-center",children:M?.message||"Could not load enrollment history."}),!A&&!a&&s.length>0&&e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"flex items-end justify-between flex-wrap gap-4 mb-4",children:[l!=null&&e.jsxs("div",{className:"flex items-baseline gap-4",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-[10px] text-slate-400 uppercase tracking-wide",children:"Latest in range"}),e.jsx("p",{className:"text-xl font-bold text-slate-800",children:h?.toLocaleString()})]}),e.jsxs("div",{children:[e.jsxs("p",{className:"text-[10px] text-slate-400 uppercase tracking-wide",children:["Net change (",s.length," mo)"]}),e.jsxs("p",{className:`text-xl font-bold ${l>0?"text-emerald-600":l<0?"text-red-600":"text-slate-700"}`,children:[l>
20?"+":"",l.toLocaleString(),C!=null&&e.jsxs("span",{className:"text-sm font-medium ml-1",children:["(",C>0?"+":"",C.toFixed(1),"%)"]})]})]})]}),e.jsxs("div",{className:"flex items-end gap-2",children:[e.jsxs("div",{children:[e.jsx("label",{className:"block text-[10px] font-medium text-slate-500 uppercase tracking-wide mb-1",children:"From"}),e.jsx("select",{value:E,onChange:i=>S(i.target.value),className:"border border-slate-300 rounded-lg px-2 py-1 text-xs bg-white",children:w.filter(i=>i<=y).map(i=>e.jsx("option",{value:i,children:P(i)},i))})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-[10px] font-medium text-slate-500 uppercase tracking-wide mb-1",children:"To"}),e.jsx("select",{value:k,onChange:i=>D(i.target.value),className:"border border-slate-300 rounded-lg px-2 py-1 text-xs bg-white",children:w.filter(i=>i>=v).map(i=>e.jsx("option",{value:i,children:P(i)},i))})]}),e.jsxs("button",{onClick:_,title:"Download chart as PNG",className:"flex items-center gap-1 border border-slate-200 rounded-lg px-2.5 py-1.5 text-xs text-slate-500 hover:text-sky-600 transition-colors",children:[e.jsx(Y,{size:13})," PNG"]})]})]}),e.jsx("div",{ref:x,children:e.jsx(Q,{width:"100%",height:340,children:e.jsxs(ce,{data:s,margin:{top:5,right:20,left:10,bottom:5},children:[e.jsx(Z,{strokeDasharray:"3 3",stroke:"#f1f5f9"}),e.jsx(J,{dataKey:"date",tick:{fontSize:11},minTickGap:24}),e.jsx(G,{tick:{fontSize:11},width:60,domain:c,allowDecimals:!1,tickFormatter:i=>i>=1e6?`${(i/1e6).toFixed(1)}M`:i>=1e3?`${Math.round(i/1e3)}K`:i}),e.jsx(U,{formatter:i=>i?.toLocaleString(),labelFormatter:i=>i}),e.jsx(W,{type:"monotone",dataKey:"Enrolled",stroke:"#0ea5e9",strokeWidth:2,dot:!1})]})})}),e.jsxs("p",{className:"text-[11px] text-slate-400 italic mt-3",children:["Source: CMS Medicare Advantage enrollment by contract (monthly), available from ",P(w[0]),". Org-level series sums all contracts under the current parent organization. Y-axis is scaled to the data range shown, not from zero."]})]}),!A&&!a&&s.length===0&&e.jsx("p",{className:"text-sm text-slate-400 py-12 text-center",children:"No enrollment history available."})]})]})})}const At={AL:"Alabama",AK:"Alaska",AZ:"Arizona",AR:"Arkansas",CA:"California",CO:"Colorado",CT:"Connecticut",DE:"Delaware",DC:"District of Columbia",FL:"Florida",GA:"Georgia",HI:"Hawaii",ID:"Idaho",IL:"Illinois",IN:"Indiana",IA:"Iowa",KS:"Kansas",KY:"Kentucky",LA:"Louisiana",ME:"Maine",MD:"Maryland",MA:"Massachusetts",MI:"Michigan",MN:"Minnesota",MS:"Mississippi",MO:"Missouri",MT:"Montana",NE:"Nebraska",NV:"Nevada",NH:"New Hampshire",NJ:"New Jersey",NM:"New Mexico",NY:"New York",NC:"North Carolina",ND:"North Dakota",OH:"Ohio",OK:"Oklahoma",OR:"Oregon",PA:"Pennsylvania",RI:"Rhode Island",SC:"South Carolina",SD:"South Dakota",TN:"Tennessee",TX:"Texas",UT:"Utah",VT:"Vermont",VA:"Virginia",WA:"Washington",WV:"West Virginia",WI:"Wisconsin",WY:"Wyoming"},we=[{key:"snf",label:"Skilled Nursing",defaultOn:!0,matches:["Skilled Nursing Facility"]},{key:"rehab",label:"Rehab (PT/OT/ST)",defaultOn:!0,matches:["Rehabilitation services"]},{key:"oop",label:"Max OOP",defaultOn:!0,matches:["Maximum out-of-pocket enrollee responsibility (does not include prescription drugs)"]},{key:"deductible",label:"Deductible",defaultOn:!0,matches:["Health plan deductible"]},{key:"hospital",label:"Hospital",defaultOn:!0,matches:["Inpatient hospital coverage","Outpatient hospital coverage"]},{key:"doctor",label:"Doctor visits",defaultOn:!1,matches:["Doctor visits"]},{key:"er",label:"ER / Urgent care",defaultOn:!1,matches:["Emergency care/Urgent care","Emergency care/urgent care"]},{key:"dme",label:"DME / supplies",defaultOn:!1,matches:["Medical equipment/supplies"]},{key:"ambulance",label:"Ambulance",defaultOn:!1,matches:["Ground Ambulance","Ground ambulance"]},{key:"partb",label:"Part B drugs",defaultOn:!1,matches:["Medicare Part B drugs"]},{key:"diag",label:"Diagnostics / lab",defaultOn:!1,matches:["Diagnostic procedures/lab services/imaging"]},{key:"mental",label:"Mental health",defaultOn:!1,matches:["Mental health services"]}],Pt=new Set(we.filter(u=>u.defaultOn).map(u=>u.key));function ze(u){for(const d of we)if(d.matches.includes(u))return d.key;return null}function Tt({onOpenPlanExplorer:u}){const[d,b]=f.useState("lookup"),[r,x]=f.useState(null),[o,m]=f.useState(""),[A,a]=f.useState(!1),M=f.useRef(null),[j,w]=f.useState("");f.useEffect(()=>{if(o.trim().length<1){w("");return}const l=setTimeout(()=>w(o.trim()),200);return()=>clearTimeout(l)},[o]);const{data:E,isFetching:S}=R({queryKey:["ma-plan-search",j],queryFn:()=>dt({q:j,limit:20}),enabled:j.length>=1,staleTime:300*1e3,keepPreviousData:!0}),k=E?.results||[];f.useEffect(()=>{if(!A)return;const l=C=>{M.current&&!M.current.contains(C.target)&&a(!1)};return document.addEventListener("mousedown",l),()=>document.removeEventListener("mousedown",l)},[A]);const D=l=>{m(`${l.contract_id}
2-${String(l.plan_id).padStart(3,"0")}`),a(!1),x({contractId:l.contract_id,planId:l.plan_id,segmentId:l.segment_id})},[P,v]=f.useState(""),[y,s]=f.useState(""),{data:g}=R({queryKey:["ma-counties-for-state",P],queryFn:()=>Fe({state:P}),enabled:!!P,staleTime:3600*1e3}),h=g?.results||[];return e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"bg-white rounded-xl border border-slate-200 p-4",children:[e.jsxs("div",{className:"flex items-start justify-between gap-4 flex-wrap",children:[e.jsxs("div",{children:[e.jsx("h2",{className:"text-base font-semibold text-slate-900",children:"CY2026 Plan Benefits â CMS-published summary"}),e.jsx("p",{className:"text-xs text-slate-500 mt-0.5",children:"Informational reference for SNF cost share, rehab coverage, OOP, and authorization. Always confirm with the payer."})]}),e.jsx("span",{className:"text-[11px] text-slate-400 whitespace-nowrap",children:"Source: CMS MPF Benefit Summary · Dec 2025"})]}),e.jsx("p",{className:"mt-2 text-[11px] text-slate-400 italic",title:"These numbers come from CMS's published benefit-summary file and are not a direct quote from the plan's contract or evidence of coverage. Authorization rules, networks, and member-specific cost share can differ from what is shown here. Always confirm before billing or counseling the family.",children:"Reference summary from CMS â verify with the payer before billing."}),e.jsxs("details",{className:"mt-2 group",children:[e.jsx("summary",{className:"cursor-pointer text-[11px] font-medium text-sky-700 hover:text-sky-800 select-none",children:"Where do I find the contract / plan number?"}),e.jsxs("div",{className:"mt-2 grid md:grid-cols-2 gap-3 text-xs text-slate-600 bg-slate-50 rounded p-3 border border-slate-200",children:[e.jsxs("div",{children:[e.jsx("p",{className:"font-semibold text-slate-700 mb-1",children:"Best â exact match"}),e.jsxs("ul",{className:"list-disc ml-4 space-y-0.5",children:[e.jsxs("li",{children:[e.jsx("strong",{children:"Medicare Common Working File (CWF)"})," â Plan ID field"]}),e.jsxs("li",{children:[e.jsx("strong",{children:"Patient's MA ID card"})," â printed as ",e.jsx("code",{className:"text-[11px] bg-white px-1 rounded",children:"H1234-005"})]}),e.jsxs("li",{children:[e.jsx("strong",{children:"Hospital discharge facesheet"})," â insurance section"]})]})]}),e.jsxs("div",{children:[e.jsx("p",{className:"font-semibold text-slate-700 mb-1",children:"Contract prefix"}),e.jsxs("p",{className:"text-slate-600",children:[e.jsx("strong",{children:"H"})," HMO / Local PPO · ",e.jsx("strong",{children:"R"})," Regional PPO · ",e.jsx("strong",{children:"S"})," Stand-alone PDP · ",e.jsx("strong",{children:"E"})," Employer / Union"]}),e.jsxs("p",{className:"mt-2 text-slate-500",children:["Don't know it? Browse by state + county instead, or search by name on the ",e.jsx("button",{type:"button",onClick:()=>u(null),className:"text-sky-700 underline hover:text-sky-800",children:"Plan Explorer"}),"."]})]})]})]}),e.jsx("div",{className:"flex gap-4 mt-3 border-b border-slate-200 -mx-1 px-1",children:[{k:"lookup",label:"By contract + plan ID"},{k:"county",label:"Browse by state + county"}].map(l=>e.jsx("button",{onClick:()=>{b(l.k),x(null)},className:`px-1 py-1.5 text-xs font-medium border-b-2 transition-colors ${d===l.k?"border-sky-600 text-sky-700":"border-transparent text-slate-500 hover:text-slate-700"}`,children:l.label},l.k))}),d==="lookup"&&e.jsxs("div",{className:"mt-3 relative",ref:M,children:[e.jsx("label",{className:"block text-[11px] font-medium text-slate-500 mb-1",children:"Search plans"}),e.jsxs("div",{className:"relative max-w-xl",children:[e.jsx($e,{size:14,className:"absolute left-2.5 top-1/2 -translate-y-1/2 text-slate-400 pointer-events-none"}),e.jsx("input",{value:o,onChange:l=>{m(l.target.value),a(!0),r?.contractId&&x(null)},onFocus:()=>{j.length>=1&&a(!0)},placeholder:"Type contract ID (H1234), plan (H0028-007), or org name (Humana, Kaiserâ¦)","aria-label":"Search plan benefits by contract, plan, or organization",className:"w-full border border-slate-300 rounded pl-8 pr-16 py-1.5 text-sm",autoComplete:"off"}),S&&e.jsx(q,{size:14,className:"absolute right-9 top-1/2 -translate-y-1/2 text-slate-400 animate-spin"}),o&&e.jsx("button",{type:"button",onClick:()=>{m(""),x(null),a(!1),M.current?.querySelector("input")?.focus()},title:"Clear search",className:"absolute right-2 top-1/2 -translate-y-1/2 text-slate-400 hover:text-slate-700 inline-flex items-center justify-center w-6 h-6 rounded hover:bg-slate-100",children:e.jsx(le,{size:13})})]}),A&&j.length>=1&&e.jsxs("div",{className:"absolute z-30 mt-1 w-full max-w-xl bg-white border border-slate-200 rounded-lg shadow-lg max-h-80 overflow-y-auto",children:[k.length===0&&!S&&e.jsxs("div",{className:"px-3 py-2 text-xs text-slate-400",children:['No plans match "',j,'".']}),k.map(l=>e.jsxs("button",{type:"button",onClick:()=>D(l),className:"w-full text-left px-3 py-2 text-xs hover:bg-sky-50 border-b border-slate-100 last:border-b-0",children:[e.jsx("div",{className:"font-medium text-slate-800",children:l.org_name}),e.jsxs("div",{className:"text-[11px] text-slate-500 font-mono",children:[l.contract_id,"-",String(l.plan_id).padStart(3,"0"),l.segment_id?` · seg ${l.segment_id}`:"",e.jsx("span",{className:"ml-2 font-sans text-slate-400",children:l.plan_type})]})]},`${l.contract_id}-${l.plan_id}-${l.segment_id}`))]})]}),d==="county"&&e.jsxs("form",{onSubmit:l=>{l.preventDefault(),y&&x({ssa:y,stateAbbr:P,countyName:h.find(C=>String(C.ssa)===String(y))?.county_name})},className:"mt-3 flex flex-wrap gap-2 items-end",children:[e.jsxs("div",{children:[e.jsx("label",{className:"block text-[11px] font-medium text-slate-500 mb-1",children:"State"}),e.jsxs("select",{value:P,onChange:l=>{v(l.target.value),s("")},className:"border border-slate-300 rounded px-2 py-1.5 text-sm w-44 bg-white",children:[e.jsx("option",{value:"",children:"Select stateâ¦"}),Ze.map(l=>e.jsx("option",{value:l,children:At[l]||l},l))]})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-[11px] font-medium text-slate-500 mb-1",children:"County"}),e.jsxs("select",{value:y,onChange:l=>s(l.target.value),disabled:!P||!h.length,className:"border border-slate-300 rounded px-2 py-1.5 text-sm min-w-[12rem] bg-white disabled:bg-slate-50 disabled:text-slate-400",children:[e.jsx("option",{value:"",children:P?h.length?"Select countyâ¦":"Loadingâ¦":"Pick a state first"}),h.map(l=>e.jsx("option",{value:l.ssa,children:l.county_name},l.ssa))]})]}),e.jsx("button",{type:"submit",disabled:!y,className:"bg-sky-600 hover:bg-sky-700 disabled:bg-slate-300 text-white text-sm font-medium px-4 py-1.5 rounded",children:"Show plans"})]})]}),d==="county"&&r?.ssa&&!r?.contractId&&e.jsx(Ot,{ssa:r.ssa,stateAbbr:r.stateAbbr,countyName:r.countyName,onPick:l=>x({...r,contractId:l.contract_id,planId:l.plan_id,segmentId:l.segment_id})}),r?.contractId&&e.jsx(Et,{contractId:r.contractId,planId:r.planId,segmentId:r.segmentId,onOpenPlanExplorer:u,backLink:d==="county"&&r?.ssa?{label:`Back to plans in ${r.countyName||`SSA ${r.ssa}`}`,onClick:()=>x({ssa:r.ssa,stateAbbr:r.stateAbbr,countyName:r.countyName})}:null})]})}function Et({contractId:u,planId:d,segmentId:b,onOpenPlanExplorer:r,backLink:x=null}){const{data:o,isLoading:m,isError:A,error:a}=R({queryKey:["plan-benefits",u,d,b],queryFn:()=>xt(u,d,{segment_id:b}),retry:!1}),[M,j]=f.useState(new Set(Pt)),w=v=>j(y=>{const s=new Set(y);return s.has(v)?s.delete(v):s.add(v),s}),[E,S]=f.useState(!1),k=async()=>{S(!0);try{await at(u,d,{segment_id:b,year:o?.contract_year})}catch(v){alert(`PDF download failed: ${v.message}`)}finally{S(!1)}};if(m)return e.jsxs("div",{className:"bg-white rounded-xl border border-slate-200 p-6 text-center text-sm text-slate-500",children:[e.jsx(q,{className:"inline animate-spin mr-2",size:14}
2),"Loading benefitsâ¦"]});if(A){const v=(a?.message||"").includes("404")||(a?.message||"").toLowerCase().includes("no benefit data");return e.jsxs("div",{className:"bg-white rounded-xl border border-slate-200 p-6 text-center",children:[e.jsx("p",{className:"text-sm font-medium text-slate-800 mb-1",children:v?"Couldn't find that plan in the CY2026 benefit summary.":"Lookup error"}),e.jsx("p",{className:"text-xs text-slate-500",children:v?"Try the search above â it accepts org name (e.g. 'Humana'), contract prefix ('H00'), or full plan (H0028-007). Some PDP / employer / regional plans (R-, S-, E-) may not be in the standard MA benefit-summary file.":a?.message||"Try again or pick a different plan from the search."})]})}if(!o)return null;const D=o.items.filter(v=>{const y=ze(v.category);return y&&M.has(y)}),P=o.items.filter(v=>!ze(v.category));return e.jsxs("div",{className:"space-y-2",children:[x&&e.jsxs("button",{type:"button",onClick:x.onClick,className:"text-[11px] text-sky-700 hover:text-sky-800 hover:underline inline-flex items-center gap-1",children:[e.jsx(jt,{size:11})," ",x.label]}),e.jsxs("div",{className:"bg-white rounded-xl border border-slate-200 overflow-hidden",children:[e.jsxs("div",{className:"px-5 py-3.5 border-b border-slate-200 flex items-center justify-between flex-wrap gap-2",children:[e.jsxs("div",{className:"min-w-0",children:[e.jsx("button",{onClick:()=>r(o.org_name),className:"block text-base font-semibold text-slate-900 hover:text-sky-700 transition-colors text-left truncate",children:o.org_name}),e.jsxs("p",{className:"text-[11px] text-slate-500 mt-0.5",children:[e.jsxs("span",{className:"font-mono",children:[o.contract_id,"-",String(o.plan_id).padStart(3,"0")]}),o.segment_id?e.jsxs("span",{className:"font-mono",children:[" · seg ",o.segment_id]}):null,e.jsx("span",{className:"mx-1.5 text-slate-300",children:"·"}),o.plan_type,e.jsx("span",{className:"mx-1.5 text-slate-300",children:"·"}),"CY",o.contract_year]})]}),e.jsx("div",{className:"flex items-center gap-2 shrink-0",children:e.jsxs("button",{type:"button",onClick:k,disabled:E,className:"text-xs font-medium bg-slate-900 text-white hover:bg-slate-800 disabled:bg-slate-400 px-3 py-1.5 rounded flex items-center gap-1.5",children:[E?e.jsx(q,{size:12,className:"animate-spin"}):e.jsx(Y,{size:12}),E?"Preparingâ¦":"Intake PDF"]})})]}),e.jsxs("div",{className:"px-5 py-3 bg-slate-50/60 border-b border-slate-100 flex items-center gap-2 flex-wrap",children:[e.jsx("span",{className:"text-[11px] font-medium text-slate-500 uppercase tracking-wide mr-1",children:"Show:"}),we.map(v=>{const y=M.has(v.key);return e.jsx("button",{type:"button",onClick:()=>w(v.key),className:`text-[11px] font-medium px-2 py-0.5 rounded-full border transition-colors ${y?"bg-sky-100 text-sky-800 border-sky-300":"bg-white text-slate-500 border-slate-200 hover:border-slate-300 hover:text-slate-700"}`,children:v.label},v.key)})]}),e.jsx(Lt,{items:D,restItems:P})]})]})}function De(u){const d=[];for(const b of u)(b.variants&&b.variants.length?b.variants:[{}]).forEach((x,o)=>d.push({key:`${b.category}-${b.service_desc||""}-${o}`,category:b.category.replace(/ \(does not include prescription drugs\)/,""),service:b.service_desc||"",isFirstVariant:o===0,network:x.network||"",cost_share:x.cost_share||"â",auth:(x.authorization_flag||"").toLowerCase()==="yes",referral:(x.referral_flag||"").toLowerCase()==="yes",limit:(x.limit_flag||"").toLowerCase()==="yes"}));return d}function Lt({items:u,restItems:d}){const b=De(u),r=De(d||[]),x=b.some(m=>/\bor\b/i.test(m.cost_share||""));if(b.length===0&&(!d||!d.length))return e.jsx("div",{className:"px-5 py-8 text-center text-sm text-slate-400",children:"Select at least one category above."});const o=({r:m})=>e.jsxs("tr",{className:"hover:bg-slate-50",children:[e.jsx("td",{className:"px-4 py-2 align-top",children:m.isFirstVariant?e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"text-sm font-medium text-slate-900",children:m.category}),m.service&&e.jsx("div",{className:"text-[11px] text-slate-500 mt-0.5",children:m.service})]}):e.jsx("span",{className:"sr-only",children:m.category})}),e.jsx("td",{className:"px-3 py-2 align-top text-xs text-slate-600 whitespace-nowrap",children:m.network||e.jsx("span",{className:"text-slate-300",children:"â"})}),e.jsx("td",{className:"px-3 py-2 align-top text-sm text-slate-700",children:e.jsx("span",{className:"whitespace-pre-line",children:m.cost_share})}),e.jsx("td",{className:"px-3 py-2 align-top text-xs text-center whitespace-nowrap",children:m.auth?e.jsx("span",{className:"text-rose-700 font-medium",children:"Yes"}):e.jsx("span",{className:"text-slate-300",children:"â"})}),e.jsx("td",{className:"px-3 py-2 align-top text-xs text-center whitespace-nowrap",children:m.referral?e.jsx("span",{className:"text-amber-700 font-medium",children:"Yes"}):e.jsx("span",{className:"text-slate-300",children:"â"})}),e.jsx("td",{className:"px-3 py-2 align-top text-xs text-center whitespace-nowrap",children:m.limit?e.jsx("span",{className:"text-slate-600 font-medium",children:"Yes"}):e.jsx("span",{className:"text-slate-300",children:"â"})})]});return e.jsxs("div",{className:"overflow-x-auto",children:[e.jsxs("table",{className:"w-full",children:[e.jsx("thead",{className:"bg-slate-50 border-y border-slate-200",children:e.jsxs("tr",{className:"text-left text-[11px] font-semibold text-slate-500 uppercase tracking-wide",children:[e.jsx("th",{scope:"col",className:"px-4 py-2 w-[28%]",children:"Benefit"}),e.jsx("th",{scope:"col",className:"px-3 py-2 w-[12%]",children:"Network"}),e.jsx("th",{scope:"col",className:"px-3 py-2",children:"Cost share"}),e.jsx("th",{scope:"col",className:"px-3 py-2 text-center w-[8%]",children:"Auth"}),e.jsx("th",{scope:"col",className:"px-3 py-2 text-center w-[8%]",children:"Referral"}),e.jsx("th",{scope:"col",className:"px-3 py-2 text-center w-[8%]",children:"Limit"})]})}),e.jsx("tbody",{className:"divide-y divide-slate-100",children:b.map(m=>e.jsx(o,{r:m},m.key))})]}),r.length>0&&e.jsxs("details",{children:[e.jsxs("summary",{className:"cursor-pointer px-4 py-2.5 text-[11px] font-medium text-slate-500 hover:text-slate-700 select-none border-t border-slate-100 bg-slate-50/40",children:["Show ",r.length," other supplemental row",r.length>
21?"s":""," (dental, vision, hearing, etc.)"]}),e.jsx("table",{className:"w-full",children:e.jsx("tbody",{className:"divide-y divide-slate-100",children:r.map(m=>e.jsx(o,{r:m},m.key))})})]}),x&&e.jsx("p",{className:"px-4 py-2 text-[11px] text-slate-400 italic border-t border-slate-100",children:'"A or B" cost share means the plan publishes tiered pricing â for example a preferred-provider rate vs a standard in-network rate, or different copays for different facility types. The actual amount depends on the provider tier used.'})]})}function Ot({ssa:u,stateAbbr:d,countyName:b,onPick:r}){const{data:x,isLoading:o,isError:m,error:A}=R({queryKey:["county-plan-benefits",u],queryFn:()=>mt({ssa:u}),retry:!1});return o?e.jsxs("div",{className:"bg-white rounded-xl border border-slate-200 p-6 text-center text-sm text-slate-500",children:[e.jsx(q,{className:"inline animate-spin mr-2",size:14}),"Loading plansâ¦"]}):m?e.jsx("div",{className:"bg-amber-50 border border-amber-200 rounded-xl p-4 text-sm text-amber-800",children:A?.message}):x?.contracts?.length?e.jsxs("div",{className:"bg-white rounded-xl border border-slate-200 overflow-hidden",children:[e.jsxs("div",{className:"px-5 py-3 border-b border-slate-200 flex items-center justify-between flex-wrap gap-2",children:[e.jsxs("p",{className:"text-sm text-slate-700",children:[e.jsx("strong",{children:x.n_contracts.toLocaleString()})," contract",x.n_contracts===1?"":"s"," ·",e.jsx("strong",{className:"ml-1",children:x.n_plans.toLocaleString()})," plan",x.n_plans===1?"":"s"," in ",b?`${b}, ${d}`:`SSA ${u}`,e.jsx("span",{className:"text-slate-400 ml-2",children:"· sorted by contract enrollment"})]}),e.jsx("span",{className:"text-[11px] text-slate-400",children:"Pick a plan to see benefit details"})]}),e.jsx("ul",{className:"divide-y divide-slate-100 max-h-[60vh] overflow-y-auto",children:x.contracts.map(a=>e.jsxs("li",{className:"px-5 py-3 hover:bg-slate-50/60",children:[e.jsxs("div",{className:"flex items-start justify-between gap-4 mb-2",children:[e.jsxs("div",{className:"min-w-0",children:[e.jsx("div",{className:"text-sm font-medium text-slate-900 truncate",children:a.org_name}),e.jsxs("div",{className:"text-[11px] text-slate-500 font-mono mt-0.5",children:[a.contract_id,e.jsx("span",{className:"ml-2 font-sans text-slate-400",children:a.plan_type})]})]}),e.jsx("div",{className:"shrink-0 text-right",children:a.enrolled!=null?e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"text-sm font-medium text-slate-700",children:a.enrolled.toLocaleString()}),e.jsx("div",{className:"text-[10px] text-slate-400 uppercase tracking-wide whitespace-nowrap",children:"contract enrollees"})]}):e.jsx("span",{className:"text-slate-300 text-xs",children:"No enrollment data"})})]}),e.jsxs("div",{className:"text-xs text-slate-500",children:[e.jsx("span",{className:"text-slate-400",children:"Plans: "}),a.plans.map((M,j)=>e.jsxs("span",{children:[j>0&&e.jsx("span",{className:"text-slate-300 mx-1.5",children:"·"}),e.jsxs("button",{type:"button",onClick:()=>r({contract_id:a.contract_id,plan_id:M.plan_id,segment_id:M.segment_id,org_name:a.org_name,plan_type:M.plan_type}),title:`Open benefits for ${a.contract_id}-${String(M.plan_id).padStart(3,"0")}${M.segment_id?` seg ${M.segment_id}`:""}`,className:"font-mono text-sky-700 hover:text-sky-900 hover:underline",children:[String(M.plan_id).padStart(3,"0"),M.segment_id?`·${M.segment_id}`:""]})]},`${M.plan_id}-${M.segment_id}`))]})]},a.contract_id))})]}):e.jsxs("div",{className:"bg-white rounded-xl border border-slate-200 p-6 text-sm text-slate-500",children:["No plans found for ",b?`${b}, ${d}`:`SSA ${u}`,"."]})}export{Ut 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.