var freeaps_determineBasal;(()=>{var e={5546:(e,t,a)=>{var r=a(6880);function o(e,t){t||(t=0);var a=Math.pow(10,t);return Math.round(e*a)/a}function n(e,t){return"mmol/L"===t.out_units?o(.0555*e,1):Math.round(e)}e.exports=function(e,t,a,i,s,l,m,u,d,c,g,h,p,v){var f=i.min_bg,b=v.overrideTarget;const B=v.smbIsOff,M=v.advancedSettings,_=v.isfAndCr,y=v.isf,x=v.cr,w=v.smbIsAlwaysOff,D=v.start,S=v.end,T=v.smbMinutes,C=v.uamMinutes;var U=0,G="",O="",R="",A="",I="",F=0,j=0,P=0,E=0,q=0,W=0;const k=v.weightedAverage;var L=1,z=i.sens,N=i.carb_ratio;v.useOverride&&(L=v.overridePercentage/100,_?(z/=L,N/=L):(x&&(N/=L),y&&(z/=L)));const H=i.weightPercentage,Z=v.average_total_data;function $(e,t){var a=e.getTime();return new Date(a+36e5*t)}function J(e){var t=i.bolus_increment;.025!=t&&(t=.05);var a=e/t;return a>=1?o(Math.floor(a)*t,5):0}function K(e){function t(e){return e<10&&(e="0"+e),e}return t(e.getHours())+":"+t(e.getMinutes())+":00"}function Q(e,t){var a=new Date("1/1/1999 "+e),r=new Date("1/1/1999 "+t);return(a.getTime()-r.getTime())/36e5}function V(e,t){var a=0,r=t,o=(e-t)/36e5,n=0,i=o,s=0;do{if(o>0){var l=K(r),m=p[0].rate;for(let e=0;e
=(s=Q(p[e+1].start,p[e].start))?n=s:o=s?n=s:ou)if(e+1=(s=Q(d,l))?n=s:o=(s=Q("23:59:59",l))?n=s:o0&&o21)q=V(X,(ee=24-U,te=X.getTime(),new Date(te-36e5*ee))),A="24 hours of data is required for an accurate tdd calculation. Currently only "+U.toPrecision(3)+" hours of pump history data are available. Using your pump scheduled basals to fill in the missing hours. Scheduled basals added: "+q.toPrecision(5)+" U. ";else U<21?(ge=!1,enableDynamicCR=!1):A=""}else console.log("Pumphistory is empty!"),ge=!1,enableDynamicCR=!1;var ee,te;for(let e=0;e0){F=e,W=g[e].rate;var ae=g[e-1]["duration (min)"]/60,re=ae,oe=new Date(g[e-1].timestamp),ne=oe,ie=0;do{if(e--,0==e){ne=new Date;break}if("TempBasal"==g[e]._type||"PumpSuspend"==g[e]._type){ne=new Date(g[e].timestamp);break}var se=e-2;if(se>=0&&"Rewind"==g[se]._type){let e=g[se].timestamp;for(;se-1>=0&&"Prime"==g[se-=1]._type;)ie=(g[se].timestamp-e)/36e5;ie>=ae&&(ne=e,ie=0)}}while(e>0);var le=(ne-oe)/36e5;le0&&(--r,"TempBasal"==g[r]._type)){a=new Date(g[r].timestamp);break}}while(r>0);(a-t)/36e5>0&&(q+=V(a,t))}for(let e=g.length-1;e>0;e--)if("TempBasalDuration"==g[e]._type){let t=g[e]["duration (min)"]/60,a=new Date(g[e].timestamp);var me=a;let r=e;do{if(--r,r>=0&&("TempBasal"==g[r]._type||"PumpSuspend"==g[r]._type)){me=new Date(g[r].timestamp);break}}while(r>0);if(0==e&&"TempBasalDuration"==g[0]._type&&(me=new Date,t=g[e]["duration (min)"]/60),(me-a)/36e5-t>0){q+=V(me,$(a,t))}}var ue,de={TDD:o(j=E+P+q,5),bolus:o(E,5),temp_basal:o(P,5),scheduled_basal:o(q,5)};U>21?(O=". Bolus insulin: "+E.toPrecision(5)+" U",R=". Temporary basal insulin: "+P.toPrecision(5)+" U",G=". Insulin with scheduled basal rate: "+q.toPrecision(5)+" U",I=A+(" TDD past 24h is: "+j.toPrecision(5)+" U")+O+R+G,tddReason=", Total insulin: "+o(j,2)+" U, "+o(E/j*100,0)+"% Bolus "+o((P+q)/j*100,0)+"% Basal"):tddReason=", TDD: Not enough pumpData (< 21h)";const ce=e.glucose;var ge=h.useNewFormula;const he=h.enableDynamicCR,pe=Math.min(i.autosens_min,i.autosens_max),ve=Math.max(i.autosens_min,i.autosens_max);(ve==pe||ve<1||pe>1)&&(ge=!1,console.log("Dynamic ISF disabled due to current autosens settings"));const fe=h.adjustmentFactor,be=i.min_bg;var Be=!1,Me="",_e=1,ye="";Z>0&&(_e=k/Z),ye=_e>1?"Basal adjustment with a 24 hour to total average (up to 14 days of data) TDD ratio (limited by Autosens max setting). Basal Ratio: "+(_e=o(_e=Math.min(_e,i.autosens_max),2))+". Upper limit = Autosens max ("+i.autosens_max+")":_e<1?"Basal adjustment with a 24 hour to to total average (up to 14 days of data) TDD ratio (limited by Autosens min setting). Basal Ratio: "+(_e=o(_e=Math.max(_e,i.autosens_min),2))+". Lower limit = Autosens min ("+i.autosens_min+")":"Basal adjusted with a 24 hour to total average (up to 14 days of data) TDD ratio: "+_e,ye=", Basal ratio: "+_e,(i.high_temptarget_raises_sensitivity||i.exercise_mode||v.isEnabled)&&(Be=!0),be>=118&&Be&&(ge=!1,Me="Dynamic ISF temporarily off due to a high temp target/exercising. Current min target: "+be);var xe=", Dynamic ratios log: ",we=", AF: "+fe,De="BG: "+ce+" mg/dl ("+(.0555*ce).toPrecision(2)+" mmol/l)",Se="",Te="";const Ce=h.curve,Ue=h.insulinPeakTime,Ge=h.useCustomPeakTime;var Oe=55,Re=65;switch(Ce){case"rapid-acting":Re=65;break;case"ultra-rapid":Re=50}Ge?(Oe=120-Ue,console.log("Custom insulinpeakTime set to :"+Ue+", insulinFactor: "+Oe)):(Oe=120-Re,console.log("insulinFactor set to : "+Oe)),ue=j,H<1&&k>0&&(j=k,console.log("Using weighted TDD average: "+o(j,2)+" U, instead of past 24 h ("+o(ue,2)+" U), weight: "+H),Te=", Weighted TDD: "+o(j,2)+" U");const Ae=h.sigmoid;var Ie="";if(ge){var Fe=z*fe*j*Math.log(ce/Oe+1)/1800;Se=", Logarithmic formula"}if(ge&&Ae){const e=pe,t=ve-e,a=.0555*(ce-i.min_bg);var je=_e,Pe=ve-1;1==ve&&(Pe=ve+.01-1);const r=Math.log10(1/Pe-e/Pe)/Math.log10(Math.E),o=a*fe*je+r;Fe=t/(1+Math.exp(-o))+e,Se=", Sigmoid function"}var Ee=N;const qe=o(N,1);var We="",ke="";if(ge&&j>0){if(We=", Dynamic ISF/CR: On/",Fe>ve?(Me=", Dynamic ISF limited by autosens_max setting: "+ve+" ("+o(Fe,2)+"), ",ke=", Autosens/Dynamic Limit: "+ve+" ("+o(Fe,2)+")",Fe=ve):Fe-.5?"+"+o(e.delta,0):o(e.delta,0);var Ye=Math.min(e.delta,e.short_avgdelta),et=Math.min(e.short_avgdelta,e.long_avgdelta),tt=Math.max(e.delta,e.short_avgdelta,e.long_avgdelta);(Ve<=10||38===Ve||Xe>=3)&&(ze.reason="CGM is calibrating, in ??? state, or noise is high");if(Ve>60&&0==e.delta&&e.short_avgdelta>-1&&e.short_avgdelta<1&&e.long_avgdelta>-1&&e.long_avgdelta<1&&("fakecgm"==e.device?(console.error("CGM data is unchanged ("+n(Ve,i)+"+"+n(e.delta,i)+") for 5m w/ "+n(e.short_avgdelta,i)+" mg/dL ~15m change & "+n(e.long_avgdelta,2)+" mg/dL ~45m change"),console.error("Simulator mode detected ("+e.device+"): continuing anyway")):!0),Qe>12||Qe<-5?ze.reason="If current system time "+$e+" is correct, then BG data is too old. The last BG data was read "+Qe+"m ago at "+Ke:0===e.short_avgdelta&&0===e.long_avgdelta&&(e.last_cal&&e.last_cal<3?ze.reason="CGM was just calibrated":ze.reason="CGM data is unchanged ("+n(Ve,i)+"+"+n(e.delta,i)+") for 5m w/ "+n(e.short_avgdelta,i)+" mg/dL ~15m change & "+n(e.long_avgdelta,i)+" mg/dL ~45m change"),Ve<=10||38===Ve||Xe>=3||Qe>12||Qe<-5||0===e.short_avgdelta&&0===e.long_avgdelta)return t.rate>=Ze?(ze.reason+=". Canceling high temp basal of "+t.rate,ze.deliverAt=Ne,ze.temp="absolute",ze.duration=0,ze.rate=0,ze):0===t.rate&&t.duration>30?(ze.reason+=". Shortening "+t.duration+"m long zero temp to 30m. ",ze.deliverAt=Ne,ze.temp="absolute",ze.duration=30,ze.rate=0,ze):(ze.reason+=". Temp "+t.rate+" <= current basal "+Ze+"U/hr; doing nothing. ",ze);var at,rt,ot,nt,it=i.max_iob;if(void 0!==i.min_bg&&(rt=i.min_bg),void 0!==i.max_bg&&(ot=i.max_bg),void 0!==i.enableSMB_high_bg_target&&(nt=i.enableSMB_high_bg_target),void 0===i.min_bg||void 0===i.max_bg)return ze.error="Error: could not determine target_bg. ",ze;at=(i.min_bg+i.max_bg)/2;var st=i.exercise_mode||i.high_temptarget_raises_sensitivity||v.isEnabled,lt=100,mt=160;if(mt=i.half_basal_exercise_target,v.isEnabled){const e=v.hbt;console.log("Half Basal Target used: "+n(e,i)+" "+i.out_units),mt=e}else console.log("Default Half Basal Target used: "+n(mt,i)+" "+i.out_units);if(st&&i.temptargetSet&&at>lt||i.low_temptarget_lowers_sensitivity&&i.temptargetSet&&at=at&&sensitivityRatio0&&(process.stderr.write("TDD-adjustment of basals activated, using tdd24h_14d_Ratio "+o(_e,2)+", TDD 24h = "+o(ue,2)+"U, Weighted average TDD = "+o(k,2)+"U, (Weight percentage = "+H+"), Total data of TDDs (up to 14 days) average = "+o(Z,2)+"U. "),Ze!==He*L?process.stderr.write("Adjusting basal from "+He*L+" U/h to "+Ze+" U/h; "):process.stderr.write("Basal unchanged: "+Ze+" U/h; "))),i.temptargetSet);else if(void 0!==s&&s&&(i.sensitivity_raises_target&&s.ratio<1||i.resistance_lowers_target&&s.ratio>1)){rt=o((rt-60)/s.ratio)+60,ot=o((ot-60)/s.ratio)+60;var dt=o((at-60)/s.ratio)+60;at===(dt=Math.max(80,dt))?process.stderr.write("target_bg unchanged: "+n(dt,i)+"; "):process.stderr.write("target_bg from "+n(dt,i)+" to "+n(dt,i)+"; "),at=dt}var ct=n(at,i);at!=f&&(ct=0!==b&&b!==at?n(f,i)+"→"+n(b,i)+"→"+n(at,i):n(f,i)+"→"+n(at,i));var gt=200,ht=200,pt=200;if(e.noise>=2){var vt=Math.max(1.1,i.noisyCGMTargetMultiplier);Math.min(250,i.maxRaw);gt=o(Math.min(200,rt*vt)),ht=o(Math.min(200,at*vt)),pt=o(Math.min(200,ot*vt)),process.stderr.write("Raising target_bg for noisy / raw CGM data, from "+n(dt,i)+" to "+n(ht,i)+"; "),rt=gt,at=ht,ot=pt}var ft=rt-.5*(rt-40),bt=i.threshold_setting;bt>ft&&bt<=120&&bt>=65?(console.error("Threshold changed in settings from "+n(ft,i)+" to "+n(bt,i)+". "),ft=bt):console.error("Current threshold: "+n(ft,i));var Bt="",Mt=(o(z,1),z);if(void 0!==s&&s&&((Mt=o(Mt=z/sensitivityRatio,1))!==z?process.stderr.write("ISF from "+n(z,i)+" to "+n(Mt,i)):process.stderr.write("ISF unchanged: "+n(Mt,i)),Bt+="Autosens ratio: "+o(sensitivityRatio,2)+", ISF: "+n(z,i)+"→"+n(Mt,i)),console.error("CR:"+N),void 0===a)return ze.error="Error: iob_data undefined. ",ze;var _t,yt=a;if(a.length,a.length>1&&(a=yt[0]),void 0===a.activity||void 0===a.iob)return ze.error="Error: iob_data missing some property. ",ze;var xt=((_t=void 0!==a.lastTemp?o((new Date($e).getTime()-a.lastTemp.date)/6e4):0)+t.duration)%30;if(console.error("currenttemp:"+t.rate+" lastTempAge:"+_t+"m, tempModulus:"+xt+"m"),ze.temp="absolute",ze.deliverAt=Ne,u&&t&&a.lastTemp&&t.rate!==a.lastTemp.rate&&_t>10&&t.duration)return ze.reason="Warning: currenttemp rate "+t.rate+" != lastTemp rate "+a.lastTemp.rate+" from pumphistory; canceling temp",m.setTempBasal(0,0,i,ze,t);if(t&&a.lastTemp&&t.duration>0){var wt=_t-a.lastTemp.duration;if(wt>5&&_t>10)return ze.reason="Warning: currenttemp running but lastTemp from pumphistory ended "+wt+"m ago; canceling temp",m.setTempBasal(0,0,i,ze,t)}var Dt=o(-a.activity*Mt*5,2),St=o(6*(Ye-Dt));St<0&&(St=o(6*(et-Dt)))<0&&(St=o(6*(e.long_avgdelta-Dt)));var Tt=Ve,Ct=(Tt=a.iob>0?o(Ve-a.iob*Mt):o(Ve-a.iob*Math.min(Mt,z)))+St;if(void 0===Ct||isNaN(Ct))return ze.error="Error: could not calculate eventualBG. Sensitivity: "+Mt+" Deviation: "+St,ze;var Ut=function(e,t,a){return o(a+(e-t)/24,1)}(at,Ct,Dt);ze={temp:"absolute",bg:Ve,tick:Je,eventualBG:Ct,insulinReq:0,reservoir:d,deliverAt:Ne,sensitivityRatio,TDD:ue,insulin:de,current_target:at,insulinForManualBolus:Fa};var Gt=[],Ot=[],Rt=[],At=[];Gt.push(Ve),Ot.push(Ve),At.push(Ve),Rt.push(Ve);var It=function(e,t,a,r,o,i){return t?!e.allowSMB_with_high_temptarget&&e.temptargetSet&&o>100?(console.error("SMB disabled due to high temptarget of "+o),!1):!0===a.bwFound&&!1===e.A52_risk_enable?(console.error("SMB disabled due to Bolus Wizard activity in the last 6 hours."),!1):!0===e.enableSMB_always?(a.bwFound?console.error("Warning: SMB enabled within 6h of using Bolus Wizard: be sure to easy bolus 30s before using Bolus Wizard"):console.error("SMB enabled due to enableSMB_always"),!0):!0===e.enableSMB_with_COB&&a.mealCOB?(a.bwCarbs?console.error("Warning: SMB enabled with Bolus Wizard carbs: be sure to easy bolus 30s before using Bolus Wizard"):console.error("SMB enabled for COB of "+a.mealCOB),!0):!0===e.enableSMB_after_carbs&&a.carbs?(a.bwCarbs?console.error("Warning: SMB enabled with Bolus Wizard carbs: be sure to easy bolus 30s before using Bolus Wizard"):console.error("SMB enabled for 6h after carb entry"),!0):!0===e.enableSMB_with_temptarget&&e.temptargetSet&&o<100?(a.bwFound?console.error("Warning: SMB enabled within 6h of using Bolus Wizard: be sure to easy bolus 30s before using Bolus Wizard"):console.error("SMB enabled for temptarget of "+n(o,e)),!0):!0===e.enableSMB_high_bg&&null!==i&&r>=i?(console.error("Checking BG to see if High for SMB enablement."),console.error("Current BG",r," | High BG ",i),a.bwFound?console.error("Warning: High BG SMB enabled within 6h of using Bolus Wizard: be sure to easy bolus 30s before using Bolus Wizard"):console.error("High BG detected. Enabling SMB."),!0):(console.error("SMB disabled (no enableSMB preferences active or no condition satisfied)"),!1):(console.error("SMB disabled (!microBolusAllowed)"),!1)}(i,u,l,Ve,at,nt);if(B)if(w){let e=c.getHours();e>=D&&e<=S&&(console.error("SMB disabled by schedule (a Profile is active with SMBs disabled)"),It=!1)}else console.error("SMBs are disabled (a Profile is active with SMBs disabled)"),It=!1;var Ft=i.enableUAM,jt=0,Pt=0;jt=o(Ye-Dt,1);var Et=o(Ye-Dt,1);csf=Mt/N,console.error("profile.sens:"+n(z,i)+", sens:"+n(Mt,i)+", CSF:"+o(csf,1));var qt=o(30*csf*5/60,1);jt>qt&&(console.error("Limiting carb impact from "+jt+" to "+qt+"mg/dL/5m (30g/h)"),jt=qt);var Wt=3;sensitivityRatio&&(Wt/=sensitivityRatio);var kt=Wt;if(l.carbs){Wt=Math.max(Wt,l.mealCOB/20);var Lt=o((new Date($e).getTime()-l.lastCarbTime)/6e4),zt=(l.carbs-l.mealCOB)/l.carbs;kt=o(kt=Wt+1.5*Lt/60,1),console.error("Last carbs "+Lt+" minutes ago; remainingCATime:"+kt+"hours; "+o(100*zt,1)+"% carbs absorbed")}var Nt=Math.max(0,jt/5*60*kt/2)/csf,Ht=90,Zt=1;i.remainingCarbsCap&&(Ht=Math.min(90,i.remainingCarbsCap)),i.remainingCarbsFraction&&(Zt=Math.min(1,i.remainingCarbsFraction));var $t=1-Zt,Jt=Math.max(0,l.mealCOB-Nt-l.carbs*$t),Kt=(Jt=Math.min(Ht,Jt))*csf*5/60/(kt/2),Qt=o(l.slopeFromMaxDeviation,2),Vt=o(l.slopeFromMinDeviation,2),Xt=Math.min(Qt,-Vt/3);Pt=0===jt?0:Math.min(60*kt/5/2,Math.max(0,l.mealCOB*csf/jt)),console.error("Carb Impact:"+jt+"mg/dL per 5m; CI Duration:"+o(5*Pt/60*2,1)+"hours; remaining CI ("+kt/2+"h peak):"+o(Kt,1)+"mg/dL per 5m");var Yt,ea,ta,aa,ra,oa=999,na=999,ia=999,sa=Ve,la=999,ma=999,ua=999,da=999,ca=Ct,ga=Ve,ha=Ve,pa=0,va=[],fa=[];try{yt.forEach((function(e){var t=o(-e.activity*Mt*5,2),a=o(-e.iobWithZeroTemp.activity*Mt*5,2),r=Tt,n=jt*(1-Math.min(1,Ot.length/12));if(!0===(ge&&!Ae))ca=Ot[Ot.length-1]+o(-e.activity*(1800/(j*fe*Math.log(Math.max(Ot[Ot.length-1],39)/Oe+1)))*5,2)+n,r=At[At.length-1]+o(-e.iobWithZeroTemp.activity*(1800/(j*fe*Math.log(Math.max(At[At.length-1],39)/Oe+1)))*5,2),console.log("Dynamic ISF (Logarithmic Formula) )adjusted predictions for IOB and ZT: IOBpredBG: "+o(ca,2)+" , ZTpredBG: "+o(r,2));else ca=Ot[Ot.length-1]+t+n,r=At[At.length-1]+a;var i=Math.max(0,Math.max(0,jt)*(1-Gt.length/Math.max(2*Pt,1))),s=Math.min(Gt.length,12*kt-Gt.length),l=Math.max(0,s/(kt/2*12)*Kt);i+l,va.push(o(l,0)),fa.push(o(i,0)),COBpredBG=Gt[Gt.length-1]+t+Math.min(0,n)+i+l;var m=Math.max(0,Et+Rt.length*Xt),u=Math.max(0,Et*(1-Rt.length/Math.max(36,1))),d=Math.min(m,u);if(d>0&&(pa=o(5*(Rt.length+1)/60,1)),!0===(ge&&!Ae))UAMpredBG=Rt[Rt.length-1]+o(-e.activity*(1800/(j*fe*Math.log(Math.max(Rt[Rt.length-1],39)/Oe+1)))*5,2)+Math.min(0,n)+d,console.log("Dynamic ISF (Logarithmic Formula) adjusted prediction for UAM: UAMpredBG: "+o(UAMpredBG,2));else UAMpredBG=Rt[Rt.length-1]+t+Math.min(0,n)+d;Ot.length<48&&Ot.push(ca),Gt.length<48&&Gt.push(COBpredBG),Rt.length<48&&Rt.push(UAMpredBG),At.length<48&&At.push(r),COBpredBG18&&caga&&(ga=ca),(Pt||Kt>0)&&Gt.length>18&&COBpredBG0)&&COBpredBG>ga&&(ha=COBpredBG),Ft&&Rt.length>12&&UAMpredBGga&&UAMpredBG}))}catch(e){console.error("Problem with iobArray. Optional feature Advanced Meal Assist disabled")}l.mealCOB&&(console.error("predCIs (mg/dL/5m):"+fa.join(" ")),console.error("remainingCIs: "+va.join(" "))),ze.predBGs={},Ot.forEach((function(e,t,a){a[t]=o(Math.min(401,Math.max(39,e)))}));for(var ba=Ot.length-1;ba>12&&Ot[ba-1]===Ot[ba];ba--)Ot.pop();for(ze.predBGs.IOB=Ot,ta=o(Ot[Ot.length-1]),At.forEach((function(e,t,a){a[t]=o(Math.min(401,Math.max(39,e)))})),ba=At.length-1;ba>6&&!(At[ba-1]>=At[ba]||At[ba]<=at);ba--)At.pop();if(ze.predBGs.ZT=At,o(At[At.length-1]),l.mealCOB>0&&(jt>0||Kt>0)){for(Gt.forEach((function(e,t,a){a[t]=o(Math.min(401,Math.max(39,e)))})),ba=Gt.length-1;ba>12&&Gt[ba-1]===Gt[ba];ba--)Gt.pop();ze.predBGs.COB=Gt,aa=o(Gt[Gt.length-1]),Ct=Math.max(Ct,o(Gt[Gt.length-1]))}if(jt>0||Kt>0){if(Ft){for(Rt.forEach((function(e,t,a){a[t]=o(Math.min(401,Math.max(39,e)))})),ba=Rt.length-1;ba>12&&Rt[ba-1]===Rt[ba];ba--)Rt.pop();ze.predBGs.UAM=Rt,ra=o(Rt[Rt.length-1]),Rt[Rt.length-1]&&(Ct=Math.max(Ct,o(Rt[Rt.length-1])))}ze.eventualBG=Ct}console.error("UAM Impact:"+Et+"mg/dL per 5m; UAM Duration:"+pa+"hours"),oa=Math.max(39,oa),na=Math.max(39,na),ia=Math.max(39,ia),Yt=o(oa);var Ba=l.mealCOB/l.carbs;ea=o(ia<999&&na<999?(1-Ba)*UAMpredBG+Ba*COBpredBG:na<999?(ca+COBpredBG)/2:ia<999?(ca+UAMpredBG)/2:ca),da>ea&&(ea=da),sa=o(sa=Pt||Kt>0?Ft?Ba*la+(1-Ba)*ma:la:Ft?ma:ua);var Ma=ia;if(daia&&(Ma=(ia+da)/2);if(Ma=o(Ma),l.carbs)if(!Ft&&na<999)Yt=o(Math.max(oa,na));else if(na<999){var ya=Ba*na+(1-Ba)*Ma;Yt=o(Math.max(oa,na,ya))}else Yt=Ft?Ma:sa;else Ft&&(Yt=o(Math.max(oa,Ma)));Yt=Math.min(Yt,ea),process.stderr.write("minPredBG: "+Yt+" minIOBPredBG: "+oa+" minZTGuardBG: "+da),na<999&&process.stderr.write(" minCOBPredBG: "+na),ia<999&&process.stderr.write(" minUAMPredBG: "+ia),console.error(" avgPredBG:"+ea+" COB/Carbs:"+l.mealCOB+"/"+l.carbs),ha>Ve&&(Yt=Math.min(Yt,ha)),ze.COB=l.mealCOB,ze.IOB=a.iob,ze.BGI=n(Dt,i),ze.deviation=n(St,i),ze.ISF=n(Mt,i),ze.CR=o(N,1),ze.target_bg=n(at,i),ze.TDD=o(ue,2),ze.current_target=o(at,0);var xa=ze.CR;qe!=ze.CR&&(xa=qe+"→"+ze.CR),ze.reason=Bt+", COB: "+ze.COB+", Dev: "+ze.deviation+", BGI: "+ze.BGI+", CR: "+xa+", Target: "+ct+", minPredBG "+n(Yt,i)+", minGuardBG "+n(sa,i)+", IOBpredBG "+n(ta,i),aa>0&&(ze.reason+=", COBpredBG "+n(aa,i)),ra>0&&(ze.reason+=", UAMpredBG "+n(ra,i)),ze.reason+=tddReason,ze.reason+="; ";var wa=Tt;wa<40&&(wa=Math.min(sa,wa));var Da,Sa=ft-wa,Ta=240,Ca=240;if(l.mealCOB>0&&(jt>0||Kt>0)){for(ba=0;baDa*Ve&&(console.error("maxDelta "+n(tt,i)+" > "+100*Da+"% of BG "+n(Ve,i)+" - disabling SMB"),ze.reason+="maxDelta "+n(tt,i)+" > "+100*Da+"% of BG "+n(Ve,i)+" - SMB disabled!, ",It=!1),console.error("BG projected to remain above "+n(rt,i)+" for "+Ta+"minutes"),(Ca<240||Ta<60)&&console.error("BG projected to remain above "+n(ft,i)+" for "+Ca+"minutes");var Ua=Ca,Ga=i.current_basal*L*Mt*Ua/60,Oa=Math.max(0,l.mealCOB-.25*l.carbs),Ra=(Sa-Ga)/csf-Oa;Ga=o(Ga),Ra=o(Ra),console.error("naive_eventualBG:",Tt,"bgUndershoot:",Sa,"zeroTempDuration:",Ua,"zeroTempEffect:",Ga,"carbsReq:",Ra),"Could not parse clock data"==l.reason?console.error("carbsReq unknown: Could not parse clock data"):Ra>=i.carbsReqThreshold&&Ca<=45&&(ze.carbsReq=Ra,ze.reason+=Ra+" add'l carbs req w/in "+Ca+"m; ");var Aa=0;if(Ve0&&Ye>Ut)ze.reason+="IOB "+a.iob+" < "+o(-i.current_basal*L*20/60,2),ze.reason+=" and minDelta "+n(Ye,i)+" > expectedDelta "+n(Ut,i)+"; ";else if(Ve=55)return ze.reason+="; Canceling temp at "+ze.deliverAt.getMinutes()+"m past the hour. ",m.setTempBasal(0,0,i,ze,t);var Ia=0,Fa=0,ja=Ze,Pa=0;if(Ct