1import{j as t,r as m,g as ae,h as re,c3 as O}from"./index.BCxWgvzH.js";import{s as de,F as ce}from"./followUpSuggestion.Jge6Duix.js";import{C as pe}from"./Combo.-xnbuyxg.js";import{D as Y}from"./DotPhraseTextarea.DM3FtUhK.js";import{A as G}from"./AayuDatePicker.j53RfbCj.js";import{F as ue,i as w,b as E,a as J,N as k}from"./FollowUpSelector.zN1ENoUZ.js";import"./chevron-down.h8skE8jE.js";import"./fonts.Dv3fxF0H.js";import"./trash-2.B3PLx4l8.js";const me={"1w":"1_week","2w":"2_weeks","1m":"1_month","6w":"6_weeks","2m":"2_months","3m":"3_months","6m":"6_months","12m":"1_year"},fe={"1_week":"1w","2_weeks":"2w","1_month":"1m","6_weeks":"6w","2_months":"2m","3_months":"3m","6_months":"6m","1_year":"12m"},Q=[{value:"office_visit",label:"Office Visit"},{value:"telehealth",label:"Telehealth"},{value:"phone",label:"Phone Call"},{value:"lab_only",label:"Lab Work Only"},{value:"imaging_only",label:"Imaging Only"}],P=[{value:"1_week",label:"1 Week"},{value:"2_weeks",label:"2 Weeks"},{value:"1_month",label:"1 Month"},{value:"6_weeks",label:"6 Weeks"},{value:"2_months",label:"2 Months"},{value:"3_months",label:"3 Months"},{value:"6_months",label:"6 Months"},{value:"1_year",label:"1 Year"},{value:"prn",label:"As Needed (PRN)"},{value:"specific",label:"Specific Date"}],he=["Routine follow-up","Medication check","Lab result review","Blood pressure check","Chronic disease management","Post-procedure check","Weight management","Mental health follow-up","Vaccine due","Annual wellness visit"];function X(n){return{id:`fu-chip-${n}-${Date.now()}-${Math.random().toString(36).slice(2,7)}`,type:"",reason:"",timeframe:me[n]||(w(n)?n:"1_month"),status:"pending",suggestion_state:"pending"}}function y(n){return fe[n.timeframe]||(w(n.timeframe)?n.timeframe:void 0)}function Z(n){const i=P.find(a=>a.value===n)?.label;return i||(w(n)?J(n):n)}function Re(n){return t.jsx(ve,{...n},n.encounterId)}function T(n,i){if(i.includes(k))return[];const a=n.filter(p=>{const g=y(p);return!g||i.includes(g)}),j=new Set(a.map(y));return[...a,...i.filter(p=>w(p)&&!j.has(p)).map(X)]}function q(n){if(!n||typeof n!="object")return!1;const i=n;return typeof i.id=="string"&&typeof i.reason=="string"&&(P.some(a=>a.value===i.timeframe)||typeof i.timeframe=="string"&&w(i.timeframe))&&(i.type===""||Q.some(a=>
1a.value===i.type))&&["pending","scheduled","completed"].includes(i.status)&&["instructions","provider"].every(a=>i[a]==null||typeof i[a]=="string")&&[i.specificDate,i.scheduledDate].every(a=>!a||B(a))&&(!i.scheduledTime||$(i.scheduledTime))}function B(n){if(typeof n!="string"||!/^\d{4}-\d{2}-\d{2}$/.test(n))return!1;const i=O(n);return i.getFullYear()===Number(n.slice(0,4))&&i.getMonth()+1===Number(n.slice(5,7))&&i.getDate()===Number(n.slice(8,10))}function $(n){return typeof n=="string"&&/^([01]\d|2[0-3]):[0-5]\d$/.test(n)}function ge(n){const i=[],a=Array.isArray(n?.intervals)?n.intervals:[],j=Array.isArray(n?.followUps)?n.followUps:[],p=[...new Set(a.filter(s=>typeof s=="string").map(s=>s.trim().toLowerCase()).filter(s=>s===k||w(s)))];(!Array.isArray(n?.intervals)||a.some(s=>typeof s!="string"||s.trim().toLowerCase()!==k&&!w(s.trim().toLowerCase())))&&i.push("Some saved intervals are unrecognized or outside the supported 1â730 day range."),(n?.followUps!=null&&!Array.isArray(n.followUps)||j.some(s=>!q(s)))&&i.push("Some saved visit details are incomplete or invalid. They will be excluded if you keep valid entries."),n?.patientInstructions!=null&&typeof n.patientInstructions!="string"&&i.push("Saved patient instructions could not be read.");const g=typeof n?.patientInstructions=="string"?n.patientInstructions:"",b=j.filter(q).map(s=>{const u=s.timeframe==="1_month"?"4w":s.timeframe==="2_months"?"8w":null;return u&&p.includes(u)&&!p.includes(y(s))?{...s,timeframe:u}:s}),f=b.map(y).filter(s=>!!s),h=p.includes(k)?[k]:[...new Set([...p,...f])],R={intervals:h,followUps:T(b,h),patientInstructions:g};let D=[{label:"Keep valid entries",draft:R}];if(p.includes(k)&&(j.length>0||p.length>1)){i.push("âNo follow-upâ conflicts with saved visits. Choose which plan to retain.");const s=[...new Set([...p.filter(u=>u!==k),...f])];D=[{label:"Keep no follow-up",draft:{intervals:[k],followUps:[],patientInstructions:g}},{label:"Keep recorded visits",draft:{intervals:s,followUps:T(b,s),patientInstructions:g}}]}else if(p.includes("3m")&&!p.includes("6m")&&b.some(s=>s.timeframe==="6_months")&&!b.some(s=>y(s)==="3m")){i.push("The selected interval is 3 months, but the recorded visit says 6 months. Confirm the intended duration.");const s=h.filter(v=>v!=="3m"),u=h.filter(v=>v!=="6m");D=[{label:"Use 6 months",draft:{intervals:s,followUps:T(b,s),patientInstructions:g}},{label:"Use 3 months",draft:{intervals:u,followUps:T(b.map(v=>v.timeframe==="6_months"?{...v,timeframe:"3_months"}:v),u),patientInstructions:g}}]}const S=j.map(s=>!s||typeof s!="object"?String(s):["reason","timeframe","type","status","specificDate","scheduledDate","scheduledTime","instructions"].map(u=>{const v=s[u];return u==="timeframe"&&typeof v=="string"?Z(v):v}).filter(u=>typeof u=="string").join(" · ")||"Incomplete saved visit");return a.length&&S.unshift(`Saved intervals: ${a.map(s=>typeof s=="string"?s:"unrecognized value").join(", ")}`),g&&S.push(g),{draft:R,recovery:i.length?{messages:i,entries:S,options:D}:null}}function ve({encounterId:n,isLocked:i,onDataChange:a,onStatusChange:j,onDataReady:p,onIntervalsChange:g,presentation:b="default"}){const[f,h]=m.useState([]),[R,D]=m.useState(!0),[S,s]=m.useState(!1),[u,v]=m.useState(0),[A,V]=m.useState(null),[L,ee]=m.useState(!1),[x,I]=m.useState(null),[N,H]=m.useState(!1),[o,U]=m.useState({type:"office_visit",status:"pending"}),[W,z]=m.useState(""),[_,F]=m.useState([]),[M,te]=m.useState(null);m.useEffect(()=>{let e=!1;if(D(!0),s(!1),g?.([]),!n||n==="new"){D(!1);return}return(async()=>{try{const l=await fetch(`${ae()}/api/encounters/${n}/follow-up-state`,{headers:re(!1)});if(!l.ok)throw new Error("Follow-up read failed");const d=await l.json();if(e)return;const{draft:c,recovery:r}=ge(d);te(d?.aiRecommended||null),V(r),F(c.intervals),h(de(c.followUps)),z(c.patientInstructions)}catch{e||s(!0)}finally{e||D(!1)}})(),()=>{e=!0}},[n,u]),m.useEffect(()=>{if(R||S||A)return;const e=f.length>0||_.length>0;j(e?"completed":"not_started")},[f,_,j,R,S,A]),m.useEffect(()=>{R||S||A||(p&&p({followUps:f,patientInstructions:W,intervals:_,_prefillOnly:!L}),g?.(_))},[f,W,_,R,S,A,L]);const C=()=>{ee(!0),a()},K=e=>{i||(F(e.intervals),h(e.followUps),z(e.patientInstructions),V(null),C())},ne=e=>{i||(F(e),h(l=>T(l,e)),C())},ie=()=>{if(i||!o.reason||!o.timeframe)return;const e={id:`fu-${Date.now()}`,type:o.type||"office_visit",reason:o.reason,timeframe:o.timeframe,specificDate:o.specificDate,provider:o.provider,instructions:o.instructions,status:"pending",suggestion_state:"pending"};h(d=>[...d,e]);const l=y(e);F(d=>[...new Set([...d.filter(c=>c!==k),...l?[l]:[]])].sort((c,r)=>E(c)-E(r))),U({type:"office_visit",status:"pending"}),H(!1),C()},se=(e,l,d)=>{i||!B(l)||d&&!$(d)||(I(null),h(c=>c.map(r=>r.id===e?{...r,status:"scheduled",scheduledDate:l,scheduledTime:d}:r)),C())},le=e=>{if(i)return;const l=f.find(r=>r.id===e);if(!l)return;const d=f.filter(r=>r.id!==e),c=y(l);h(d),c&&!d.some(r=>y(r)===c)&&F(r=>r.filter(oe=>oe!==c)),C()};return R?t.jsx("div",{style:{textAlign:"center",padding:40,color:"var(--blue-main)",opacity:.6},children:"Loading..."}):S?t.jsxs("div",{role:"alert",style:{padding:12},children:["Follow-up could not be loaded. Retry before making changes.",t.jsx("button",{type:"button",onClick:()=>v(e=>e+1),children:"Retry"})]}):A?t.jsxs("div",{role:"alert",style:{padding:12},children:[t.jsx("strong",{children:"Review saved follow-up"}),A.messages.map(e=>t.jsx("p",{children:e},e)),t.jsx("ul",{children:A.entries.map((e,l)=>t.jsx("li",{children:e},l))}),t.jsx("p",{children:"The saved plan remains unchanged until you choose how to continue."}),!i&&t.jsxs("div",{style:{display:"flex",flexWrap:"wrap",gap:8},children:[A.options.map(e=>t.jsx("button",{type:"button",onClick:()=>K(e.draft),children:e.label},e.label)),t.jsx("button",{type:"button",onClick:()=>K({intervals:[],followUps:[],patientInstructions:""}),children:"Replace saved follow-up plan"})]})]}):t.jsxs("div",{className:b==="note"?"enc-note-appointment":void 0,style:{display:"flex",flexDirection:"column",gap:10},children:[t.jsxs("div",{className:"enc-followup-picker",style:{background:"white",borderRadius:12,padding:"10px 12px",border:"1px solid rgba(41, 44, 97, 0.1)"},children:[t.jsxs("div",{className:"enc-followup-picker-row",style:{display:"flex",alignItems:"flex-start",ga
1p:8},children:[t.jsx("div",{style:{flex:1,minWidth:0},children:t.jsx(ue,{presentation:b,selected:_,onChange:ne,disabled:i,aiRecommended:M})}),!i&&t.jsx("button",{onClick:()=>H(!N),"data-testid":"followup-detailed-toggle",style:{flex:"none",padding:"2px 4px",border:"none",background:"transparent",color:"var(--blue-main)",fontSize:12,fontWeight:600,fontFamily:"inherit",cursor:"pointer",textDecoration:"underline",textUnderlineOffset:2,whiteSpace:"nowrap"},children:N?"Cancel":"Detailedâ¦"})]}),M&&t.jsxs("div",{style:{fontSize:11,color:"var(--blue-main)",opacity:.7,marginTop:6},children:["AI suggested ",M," based on the clinical note."]})]}),N&&!i&&t.jsxs("div",{className:"enc-followup-picker",style:{background:"white",borderRadius:12,padding:24,border:"1px solid rgba(41, 44, 97, 0.1)"},children:[t.jsx("h4",{style:{fontSize:14,fontWeight:600,color:"var(--blue-main)",margin:"0 0 20px"},children:"Schedule Follow-up"}),t.jsxs("div",{style:{marginBottom:20},children:[t.jsx("label",{style:{display:"block",fontSize:12,fontWeight:600,color:"var(--blue-main)",marginBottom:10},children:"Visit Type"}),t.jsx("div",{style:{display:"flex",gap:8,flexWrap:"wrap"},children:Q.map(e=>t.jsx("button",{onClick:()=>U(l=>({...l,type:e.value})),style:{padding:"10px 16px",borderRadius:8,border:o.type===e.value?"2px solid var(--blue-main)":"1px solid rgba(41, 44, 97, 0.15)",background:o.type===e.value?"var(--blue-soft)":"white",color:"var(--blue-main)",fontSize:13,fontWeight:o.type===e.value?600:400,cursor:"pointer"},children:e.label},e.value))})]}),t.jsxs("div",{style:{marginBottom:20},children:[t.jsx("label",{style:{display:"block",fontSize:12,fontWeight:600,color:"var(--blue-main)",marginBottom:10},children:"Reason for Visit"}),t.jsx("div",{style:{display:"flex",gap:8,flexWrap:"wrap",marginBottom:12},children:he.slice(0,6).map(e=>t.jsx("button",{onClick:()=>U(l=>({...l,reason:e})),style:{padding:"6px 14px",borderRadius:16,border:o.reason===e?"2px solid var(--teal)":"1px solid rgba(41, 44, 97, 0.15)",background:o.reason===e?"var(--teal-soft)":"white",color:o.reason===e?"var(--action-600)":"var(--blue-main)",fontSize:12,cursor:"pointer"},children:e},e))}),t.jsx("input",{type:"text",value:o.reason||"",onChange:e=>U(l=>({...l,reason:e.target.value})),placeholder:"Or enter custom reason",style:{width:"100%",padding:"10px 14px",border:"2px solid rgba(41, 44, 97, 0.12)",borderRadius:8,fontSize:14,color:"var(--blue-main)",background:"white"}})]}),t.jsxs("div",{style:{marginBottom:20},children:[t.jsx("label",{style:{display:"block",fontSize:12,fontWeight:600,color:"var(--blue-main)",marginBottom:10},children:"Timeframe"}),t.jsx("div",{style:{display:"flex",gap:8,flexWrap:"wrap"},children:P.map(e=>t.jsx("button",{onClick:()=>U(l=>({...l,timeframe:e.value})),style:{padding:"8px 16px",borderRadius:20,border:o.timeframe===e.value?"2px solid var(--blue-main)":"1px solid rgba(41, 44, 97, 0.15)",background:o.timeframe===e.value?"var(--blue-soft)":"white",color:"var(--blue-main)",fontSize:12,fontWeight:o.timeframe===e.value?600:400,cursor:"pointer"},children:e.label},e.value))}),o.timeframe==="specific"&&t.jsx(G,{value:o.specificDate||"",onChange:e=>U(l=>({...l,specificDate:e})),label:"Specific Date"})]}),t.jsxs("div",{style:{marginBottom:20},children:[t.jsx("label",{style:{display:"block",fontSize:12,fontWeight:600,color:"var(--blue-main)",marginBottom:6},children:"Instructions for Patient (Optional)"}),t.jsx(Y,{value:o.instructions||"",onChange:e=>U(l=>({...l,instructions:e.target.value})),placeholder:"Any special instructions before the visit...",rows:2,style:{width:"100%",padding:"10px 14px",border:"2px solid rgba(41, 44, 97, 0.12)",borderRadius:8,fontSize:14,color:"var(--blue-main)",background:"white",resize:"vertical"}})]}),t.jsx("button",{onClick:ie,disabled:!o.reason||!o.timeframe,style:{padding:"12px 24px",borderRadius:8,border:"none",background:o.reason&&o.timeframe?"var(--action-600)":"rgba(41, 44, 97, 0.2)",color:"white",fontSize:14,fontWeight:600,cursor:o.reason&&o.timeframe?"pointer":"not-allowed"},children:"Add Follow-up"})]}),f.length>0&&t.jsx(ce,{encounterId:n,rows:f,disabled:i,onChange:e=>{h(e),C()},onRemove:le,renderInterval:(e,l)=>t.jsx("div",{className:"fbd-interval",children:t.jsx(pe,{label:`Interval for follow-up ${l+1}`,searchable:!1,value:y(e)||null,options:[...new Set([..._.filter(w),"1w","2w","4w","6w","8w","3m","6m","12m"])].sort((d,c)=>E(d)-E(c)).map(d=>({value:d,label:`In ${J(d)}`})),disabled:i,onChange:d=>{const c=f.map(r=>r.id===e.id?{...r,timeframe:d,confirmed:!1}:r);h(c),F([...new Set(c.map(y).filter(r=>!!r))]),C()}})}),renderMetadata:e=>t.jsxs(t.Fragment,{children:[(e.scheduledDate||e.instructions||e.specificDate)&&t.jsxs("div",{className:"fbd-row-meta",children:[e.specificDate&&t.jsx("span",{children:O(e.specificDate).toLocaleDateString()}),e.scheduledDate&&t.jsxs("span",{children:[O(e.scheduledDate).toLocaleDateString()," ",e.scheduledTime&&`at ${e.scheduledTime}`]}),e.instructions&&t.jsx("span",{children:e.instructions})]}),!i&&e.status==="pending"&&t.jsx("button",{type:"button",onClick:()=>I({id:e.id,date:"",time:""}),style:{padding:"3px 8px",marginTop:7},children:"Record scheduled visit"}),!i&&x?.id===e.id&&t.jsxs("div",{style:{display:"flex",flexWrap:"wrap",alignItems:"end",gap:8,paddingTop:8},children:[t.jsx(G,{label:"Scheduled date",value:x.date,onChange:l=>I({...x,date:l})}),t.jsxs("label",{children:["Scheduled time (optional)",t.jsx("input",{type:"time",value:x.time,onChange:l=>I({...x,time:l.target.value}),style:{background:"white",color:"#292C61"}})]}),t.jsx("button",{type:"button",disabled:!B(x.date)||!!x.time&&!$(x.time),onClick:()=>se(e.id,x.date,x.time),children:"Save scheduled visit"}),t.jsx("button",{type:"button",onClick:()=>I(null),children:"Cancel"})]})]})}),!i&&_.some(w)&&t.jsx("button",{type:"button",className:"np2-btn",style:{padding:"5px 8px",alignSelf:"flex-start"},onClick:()=>{const e=_.find(w);e&&(h(l=>[...l,X(e)]),C())},children:"+ Add another follow-up"}),t.jsxs("label",{style:{display:"flex",flexDirection:"column",gap:4,fontSize:14},children:["Additional patient instructions",t.jsx(Y,{rows:1,value:W,disabled:i,onChange:e=>{z(e.target.value),C()},style:{fontSize:16,lineHeight:1.5,background:"white",color:"#292C61"}})]}),f.length>0&&t.jsxs("div",{style:{background:"linear-gradient(135deg, var(--blue-soft) 0%, rgba(183, 232, 233, 0.3) 100%)",borderRadius:8,padding:"10px 12px",border:"1px solid rgba(183, 232, 233, 0.5)"},children:[t.jsx("div",{className:"os-eyebrow",style:{marginBottom:4},children:"Patient Discharge Instructions"}),t.jsx("div",{style:{fontSize:12.5,color:"var(--blue-main)",lineHeight:1.55},children:f.map((e,l)=>t.jsxs("div",{children:[l+1,". ",e.type==="office_visit"?"Return to office":e.type==="telehealth"?"Schedule telehealth visit":e.type==="phone"?"Expect phone call":e.type==="lab_only"?"Get lab work done":e.type==="imaging_only"?"Schedule imaging":e.modality==="video"?"Schedule video visit":e.modality==="in_person"?"Schedule in-person visit":"Schedule a visit"," in ",Z(e.timeframe).toLowerCase()," for ",(e.reason||"follow-up").toLowerCase(),".",e.instructions&&` ${e.instructions}`]},e.id))})]})]})}export{me as INTERVAL_TO_TIMEFRAME,fe as TIMEFRAME_TO_INTERVAL,Re 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.