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,u,m,d,c,g,h,p,v,f){var B=i.min_bg,b=v.overrideTarget;0!=b&&6!=b&&v.useOverride&&!i.temptargetSet&&(B=b);const M=v.smbIsOff,_=v.advancedSettings,y=v.isfAndCr,x=v.isf,S=v.cr,D=v.smbIsAlwaysOff,w=v.start;var G=v.end;const T=v.smbMinutes,C=v.uamMinutes;var U=h.useNewFormula,O=0,A=B,R=0,I="",F="",j="",P="",E="",q="",W=0,k=0,L=0,z=0,N=0,H=0;const Z=v.weightedAverage;var $=1,J=i.sens,K=i.carb_ratio;v.useOverride&&($=v.overridePercentage/100,y?(J/=$,K/=$):(S&&(K/=$),x&&(J/=$)));const Q=i.weightPercentage,V=v.average_total_data;function X(e,t){var a=e.getTime();return new Date(a+36e5*t)}function Y(e){var t=i.bolus_increment;.1!=t&&(t=.05);var a=e/t;return a>=1?o(Math.floor(a)*t,5):0}function ee(e){function t(e){return e<10&&(e="0"+e),e}return t(e.getHours())+":"+t(e.getMinutes())+":00"}function te(e,t){var a=new Date("1/1/1999 "+e),r=new Date("1/1/1999 "+t);return(a.getTime()-r.getTime())/36e5}const ae=Math.min(i.autosens_min,i.autosens_max),re=Math.max(i.autosens_min,i.autosens_max);function oe(e,t){var a=0,r=t,o=(e-t)/36e5,n=0,i=o,s=0;do{if(o>0){var l=ee(r),u=p[0].rate;for(let e=0;e=(s=te(p[e+1].start,p[e].start))?n=s:o=s?n=s:om)if(e+1=(s=te(d,l))?n=s:o=(s=te("23:59:59",l))?n=s:o0&&o1)&&(U=!1,console.log("Dynamic ISF disabled due to current autosens settings")),g.length){if(U){let e=g.length-1;var ne=new Date(g[e].timestamp),ie=new Date(g[0].timestamp);if("TempBasalDuration"==g[0]._type&&(ie=new Date),(R=(ie-ne)/36e5)<23.9&&R>21)N=oe(ne,(se=24-R,le=ne.getTime(),new Date(le-36e5*se))),P="24 hours of data is required for an accurate tdd calculation. Currently only "+R.toPrecision(3)+" hours of pump history data are available. Using your pump scheduled basals to fill in the missing hours. Scheduled basals added: "+N.toPrecision(5)+" U. ";else R<21?(U=!1,enableDynamicCR=!1):P=""}}else console.log("Pumphistory is empty!"),U=!1,enableDynamicCR=!1;var se,le;if(U){for(let e=0;e0){W=e,H=g[e].rate;var ue=g[e-1]["duration (min)"]/60,me=ue,de=new Date(g[e-1].timestamp),ce=de,ge=0;do{if(e--,0==e){ce=new Date;break}if("TempBasal"==g[e]._type||"PumpSuspend"==g[e]._type){ce=new Date(g[e].timestamp);break}var he=e-2;if(he>=0&&"Rewind"==g[he]._type){let e=g[he].timestamp;for(;he-1>=0&&"Prime"==g[he-=1]._type;)ge=(g[he].timestamp-e)/36e5;ge>=ue&&(ce=e,ge=0)}}while(e>0);var pe=(ce-de)/36e5;pe0&&(--r,"TempBasal"==g[r]._type)){a=new Date(g[r].timestamp);break}}while(r>0);(a-t)/36e5>0&&(N+=oe(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 ve=a;let r=e;do{if(--r,r>=0&&("TempBasal"==g[r]._type||"PumpSuspend"==g[r]._type)){ve=new Date(g[r].timestamp);break}}while(r>0);if(0==e&&"TempBasalDuration"==g[0]._type&&(ve=new Date,t=g[e]["duration (min)"]/60),(ve-a)/36e5-t>0){N+=oe(ve,X(a,t))}}var fe={TDD:o(k=z+L+N,5),bolus:o(z,5),temp_basal:o(L,5),scheduled_basal:o(N,5)};R>21?(F=". Bolus insulin: "+z.toPrecision(5)+" U",j=". Temporary basal insulin: "+L.toPrecision(5)+" U",I=". Insulin with scheduled basal rate: "+N.toPrecision(5)+" U",E=P+(" TDD past 24h is: "+k.toPrecision(5)+" U")+F+j+I,q=", TDD: "+o(k,2)+" U, "+o(z/k*100,0)+"% Bolus "+o((L+N)/k*100,0)+"% Basal"):q=", TDD: Not enough pumpData (< 21h)"}var Be;const be=e.glucose,Me=h.enableDynamicCR,_e=h.adjustmentFactor,ye=B;var xe=!1,Se="",De=1,we="";V>0&&(De=Z/V),we=De>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: "+(De=o(De=Math.min(De,i.autosens_max),2))+". Upper limit = Autosens max ("+i.autosens_max+") ":De<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: "+(De=o(De=Math.max(De,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: "+De+" ",we=", Basal ratio: "+De+" ",(i.high_temptarget_raises_sensitivity||i.exercise_mode||v.isEnabled)&&(xe=!0),ye>=118&&xe&&(U=!1,Se="Dynamic ISF temporarily off due to a high temp target/exercising. Current min target: "+ye);var Ge=", Dynamic ratios log: ",Te=", AF: "+_e,Ce="BG: "+be+" mg/dl ("+(.0555*be).toPrecision(2)+" mmol/l)",Ue="",Oe="";const Ae=h.curve,Re=i.insulinPeakTime,Ie=h.useCustomPeakTime;var Fe=55,je=65;switch(Ae){case"rapid-acting":je=65;break;case"ultra-rapid":je=50}Ie?(Fe=120-Re,console.log("Custom insulinpeakTime set to :"+Re+", insulinFactor: "+Fe)):(Fe=120-je,console.log("insulinFactor set to : "+Fe)),Be=k,Q<1&&Z>0&&(k=Z,console.log("Using weighted TDD average: "+o(k,2)+" U, instead of past 24 h ("+o(Be,2)+" U), weight: "+Q),Oe=", Weighted TDD: "+o(k,2)+" U");const Pe=h.sigmoid;var Ee="";if(U){var qe=J*_e*k*Math.log(be/Fe+1)/1800;Ue=", Logarithmic formula"}if(U&&Pe){const e=ae,t=re-e,a=.0555*(be-B);var We=De,ke=re-1;1==re&&(ke=re+.01-1);const r=Math.log10(1/ke-e/ke)/Math.log10(Math.E),o=a*_e*We+r;qe=t/(1+Math.exp(-o))+e,Ue=", Sigmoid function"}var Le=K;const ze=o(K,1);var Ne="",He="";if(U&&k>0){if(Ne=", Dynamic ISF/CR: On/",qe>re?(Se=", Dynamic ISF limited by autosens_max setting: "+re+" ("+o(qe,2)+"), ",He=", Autosens/Dynamic Limit: "+re+" ("+o(qe,2)+")",qe=re):qe-.5?"+"+o(e.delta,0):o(e.delta,0);var rt=Math.min(e.delta,e.short_avgdelta),ot=Math.min(e.short_avgdelta,e.long_avgdelta),nt=Math.max(e.delta,e.short_avgdelta,e.long_avgdelta);(tt<=10||38===tt||at>=3)&&($e.reason="CGM is calibrating, in ??? state, or noise is high");if(tt>60&&0==e.delta&&e.short_avgdelta>-1&&e.short_avgdelta<1&&e.long_avgdelta>-1&&e.long_avgdelta<1&&400!=tt&&("fakecgm"==e.device?(console.error("CGM data is unchanged ("+n(tt,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")):400!=tt&&!0),et>12||et<-5?$e.reason="If current system time "+Ve+" is correct, then BG data is too old. The last BG data was read "+et+"m ago at "+Ye:0===e.short_avgdelta&&0===e.long_avgdelta&&400!=tt&&(e.last_cal&&e.last_cal<3?$e.reason="CGM was just calibrated":$e.reason="CGM data is unchanged ("+n(tt,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"),400!=tt&&(tt<=10||38===tt||at>=3||et>12||et<-5||0===e.short_avgdelta&&0===e.long_avgdelta))return t.rate>=Qe?($e.reason+=". Canceling high temp basal of "+t.rate,$e.deliverAt=Je,$e.temp="absolute",$e.duration=0,$e.rate=0,$e):0===t.rate&&t.duration>30?($e.reason+=". Shortening "+t.duration+"m long zero temp to 30m. ",$e.deliverAt=Je,$e.temp="absolute",$e.duration=30,$e.rate=0,$e):($e.reason+=". Temp "+t.rate+" <= current basal "+Qe+"U/hr; doing nothing. ",$e);var it,st,lt,ut,mt=i.max_iob;if(void 0!==B&&(st=B),void 0!==i.max_bg&&(lt=B),void 0!==i.enableSMB_high_bg_target&&(ut=i.enableSMB_high_bg_target),void 0===B)return $e.error="Error: could not determine target_bg. ",$e;it=B;var dt=i.exercise_mode||i.high_temptarget_raises_sensitivity||v.isEnabled,ct=100,gt=160;if(gt=i.half_basal_exercise_target,v.isEnabled){const e=v.hbt;console.log("Half Basal Target used: "+n(e,i)+" "+i.out_units),gt=e}else console.log("Default Half Basal Target used: "+n(gt,i)+" "+i.out_units);if(dt&&i.temptargetSet&&it>ct||i.low_temptarget_lowers_sensitivity&&i.temptargetSet&&it=it&&sensitivityRatio0&&(process.stderr.write("TDD-adjustment of basals activated, using tdd24h_14d_Ratio "+o(De,2)+", TDD 24h = "+o(Be,2)+"U, Weighted average TDD = "+o(Z,2)+"U, (Weight percentage = "+Q+"), Total data of TDDs (up to 14 days) average = "+o(V,2)+"U. "),Qe!==Ke*$?process.stderr.write("Adjusting basal from "+Ke*$+" U/h to "+Qe+" U/h; "):process.stderr.write("Basal unchanged: "+Qe+" U/h; "))),i.temptargetSet);else if(void 0!==s&&s&&(i.sensitivity_raises_target&&s.ratio<1||i.resistance_lowers_target&&s.ratio>1)){st=o((st-60)/s.ratio)+60,lt=o((lt-60)/s.ratio)+60;var pt=o((it-60)/s.ratio)+60;it===(pt=Math.max(80,pt))?process.stderr.write("target_bg unchanged: "+n(pt,i)+"; "):process.stderr.write("target_bg from "+n(pt,i)+" to "+n(pt,i)+"; "),it=pt}var vt=n(it,i);it!=B&&(vt=0!==b&&6!==b&&b!==it?n(B,i)+"→"+n(b,i)+"→"+n(it,i):n(B,i)+"→"+n(it,i));var ft=200,Bt=200,bt=200;if(e.noise>=2){var Mt=Math.max(1.1,i.noisyCGMTargetMultiplier);Math.min(250,i.maxRaw);ft=o(Math.min(200,st*Mt)),Bt=o(Math.min(200,it*Mt)),bt=o(Math.min(200,lt*Mt)),process.stderr.write("Raising target_bg for noisy / raw CGM data, from "+n(pt,i)+" to "+n(Bt,i)+"; "),st=ft,it=Bt,lt=bt}A=st-.5*(st-40);var _t=i.threshold_setting;_t>A&&_t<=120&&_t>=65?(console.error("Threshold changed in settings from "+n(A,i)+" to "+n(_t,i)+". "),A=_t):console.error("Current threshold: "+n(A,i));var yt="",xt=(o(J,1),J);if(void 0!==s&&s&&((xt=o(xt=J/sensitivityRatio,1))!==J?process.stderr.write("ISF from "+n(J,i)+" to "+n(xt,i)):process.stderr.write("ISF unchanged: "+n(xt,i)),yt+="Autosens ratio: "+o(sensitivityRatio,2)+", ISF: "+n(J,i)+"→"+n(xt,i)),console.error("CR:"+K),void 0===a)return $e.error="Error: iob_data undefined. ",$e;var St,Dt=a;if(a.length,a.length>1&&(a=Dt[0]),void 0===a.activity||void 0===a.iob)return $e.error="Error: iob_data missing some property. ",$e;var wt=((St=void 0!==a.lastTemp?o((new Date(Ve).getTime()-a.lastTemp.date)/6e4):0)+t.duration)%30;if(console.error("currenttemp:"+t.rate+" lastTempAge:"+St+"m, tempModulus:"+wt+"m"),$e.temp="absolute",$e.deliverAt=Je,m&&t&&a.lastTemp&&t.rate!==a.lastTemp.rate&&St>10&&t.duration)return $e.reason="Warning: currenttemp rate "+t.rate+" != lastTemp rate "+a.lastTemp.rate+" from pumphistory; canceling temp",u.setTempBasal(0,0,i,$e,t);if(t&&a.lastTemp&&t.duration>0){var Gt=St-a.lastTemp.duration;if(Gt>5&&St>10)return $e.reason="Warning: currenttemp running but lastTemp from pumphistory ended "+Gt+"m ago; canceling temp",u.setTempBasal(0,0,i,$e,t)}var Tt=o(-a.activity*xt*5,2),Ct=o(6*(rt-Tt));Ct<0&&(Ct=o(6*(ot-Tt)))<0&&(Ct=o(6*(e.long_avgdelta-Tt)));var Ut=tt,Ot=(Ut=a.iob>0?o(tt-a.iob*xt):o(tt-a.iob*Math.min(xt,J)))+Ct;if(void 0===Ot||isNaN(Ot))return $e.error="Error: could not calculate eventualBG. Sensitivity: "+xt+" Deviation: "+Ct,$e;var At,Rt,It=function(e,t,a){return o(a+(e-t)/24,1)}(it,Ot,Tt);$e={temp:"absolute",bg:tt,tick:Xe,eventualBG:Ot,insulinReq:0,reservoir:d,deliverAt:Je,sensitivityRatio,CR:o(K,1),TDD:Be,insulin:fe,current_target:it,insulinForManualBolus:O,manualBolusErrorString:0,minDelta:rt,expectedDelta:It,minGuardBG:Rt,minPredBG:At,threshold:n(A,i)};var Ft=[],jt=[],Pt=[],Et=[];Ft.push(tt),jt.push(tt),Et.push(tt),Pt.push(tt);var qt=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):400==r?(console.error("Invalid CGM (HIGH). SMBs disabled."),!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,m,l,tt,it,ut);if(M)if(D){let e=c.getHours();Gw&&(G+=24),e>=w&&e<=G&&(console.error("SMB disabled by profile override"),qt=!1),GNt&&(console.error("Limiting carb impact from "+kt+" to "+Nt+"mg/dL/5m (30g/h)"),kt=Nt);var Ht=3;sensitivityRatio&&(Ht/=sensitivityRatio);var Zt=Ht;if(l.carbs){Ht=Math.max(Ht,l.mealCOB/20);var $t=o((new Date(Ve).getTime()-l.lastCarbTime)/6e4),Jt=(l.carbs-l.mealCOB)/l.carbs;Zt=o(Zt=Ht+1.5*$t/60,1),console.error("Last carbs "+$t+" minutes ago; remainingCATime:"+Zt+"hours; "+o(100*Jt,1)+"% carbs absorbed")}var Kt=Math.max(0,kt/5*60*Zt/2)/csf,Qt=90,Vt=1;i.remainingCarbsCap&&(Qt=Math.min(90,i.remainingCarbsCap)),i.remainingCarbsFraction&&(Vt=Math.min(1,i.remainingCarbsFraction));var Xt=1-Vt,Yt=Math.max(0,l.mealCOB-Kt-l.carbs*Xt),ea=(Yt=Math.min(Qt,Yt))*csf*5/60/(Zt/2),ta=o(l.slopeFromMaxDeviation,2),aa=o(l.slopeFromMinDeviation,2),ra=Math.min(ta,-aa/3);Lt=0===kt?0:Math.min(60*Zt/5/2,Math.max(0,l.mealCOB*csf/kt)),console.error("Carb Impact:"+kt+"mg/dL per 5m; CI Duration:"+o(5*Lt/60*2,1)+"hours; remaining CI ("+Zt/2+"h peak):"+o(ea,1)+"mg/dL per 5m");var oa,na,ia,sa,la=999,ua=999,ma=999,da=999,ca=999,ga=999,ha=999,pa=Ot,va=tt,fa=tt,Ba=0,ba=[],Ma=[];try{Dt.forEach((function(e){var t=o(-e.activity*xt*5,2),a=o(-e.iobWithZeroTemp.activity*xt*5,2),r=Ut,n=kt*(1-Math.min(1,jt.length/12));if(!0===(U&&!Pe))pa=jt[jt.length-1]+o(-e.activity*(1800/(k*_e*Math.log(Math.max(jt[jt.length-1],39)/Fe+1)))*5,2)+n,r=Et[Et.length-1]+o(-e.iobWithZeroTemp.activity*(1800/(k*_e*Math.log(Math.max(Et[Et.length-1],39)/Fe+1)))*5,2),console.log("Dynamic ISF (Logarithmic Formula) )adjusted predictions for IOB and ZT: IOBpredBG: "+o(pa,2)+" , ZTpredBG: "+o(r,2));else pa=jt[jt.length-1]+t+n,r=Et[Et.length-1]+a;var i=Math.max(0,Math.max(0,kt)*(1-Ft.length/Math.max(2*Lt,1))),s=Math.min(Ft.length,12*Zt-Ft.length),l=Math.max(0,s/(Zt/2*12)*ea);i+l,ba.push(o(l,0)),Ma.push(o(i,0)),COBpredBG=Ft[Ft.length-1]+t+Math.min(0,n)+i+l;var u=Math.max(0,zt+Pt.length*ra),m=Math.max(0,zt*(1-Pt.length/Math.max(36,1))),d=Math.min(u,m);if(d>0&&(Ba=o(5*(Pt.length+1)/60,1)),!0===(U&&!Pe))UAMpredBG=Pt[Pt.length-1]+o(-e.activity*(1800/(k*_e*Math.log(Math.max(Pt[Pt.length-1],39)/Fe+1)))*5,2)+Math.min(0,n)+d,console.log("Dynamic ISF (Logarithmic Formula) adjusted prediction for UAM: UAMpredBG: "+o(UAMpredBG,2));else UAMpredBG=Pt[Pt.length-1]+t+Math.min(0,n)+d;jt.length<48&&jt.push(pa),Ft.length<48&&Ft.push(COBpredBG),Pt.length<48&&Pt.push(UAMpredBG),Et.length<48&&Et.push(r),COBpredBG18&&pava&&(va=pa),(Lt||ea>0)&&Ft.length>18&&COBpredBG0)&&COBpredBG>va&&(fa=COBpredBG),Wt&&Pt.length>12&&UAMpredBGva&&UAMpredBG}))}catch(e){console.error("Problem with iobArray. Optional feature Advanced Meal Assist disabled")}l.mealCOB&&(console.error("predCIs (mg/dL/5m):"+Ma.join(" ")),console.error("remainingCIs: "+ba.join(" "))),$e.predBGs={},jt.forEach((function(e,t,a){a[t]=o(Math.min(401,Math.max(39,e)))}));for(var _a=jt.length-1;_a>12&&jt[_a-1]===jt[_a];_a--)jt.pop();for($e.predBGs.IOB=jt,na=o(jt[jt.length-1]),Et.forEach((function(e,t,a){a[t]=o(Math.min(401,Math.max(39,e)))})),_a=Et.length-1;_a>6&&!(Et[_a-1]>=Et[_a]||Et[_a]<=it);_a--)Et.pop();if($e.predBGs.ZT=Et,o(Et[Et.length-1]),l.mealCOB>0&&(kt>0||ea>0)){for(Ft.forEach((function(e,t,a){a[t]=o(Math.min(1500,Math.max(39,e)))})),_a=Ft.length-1;_a>12&&Ft[_a-1]===Ft[_a];_a--)Ft.pop();$e.predBGs.COB=Ft,ia=o(Ft[Ft.length-1]),Ot=Math.max(Ot,o(Ft[Ft.length-1])),console.error("COBpredBG: "+o(Ft[Ft.length-1]))}if(kt>0||ea>0){if(Wt){for(Pt.forEach((function(e,t,a){a[t]=o(Math.min(401,Math.max(39,e)))})),_a=Pt.length-1;_a>12&&Pt[_a-1]===Pt[_a];_a--)Pt.pop();$e.predBGs.UAM=Pt,sa=o(Pt[Pt.length-1]),Pt[Pt.length-1]&&(Ot=Math.max(Ot,o(Pt[Pt.length-1])))}$e.eventualBG=Ot}console.error("UAM Impact:"+zt+"mg/dL per 5m; UAM Duration:"+Ba+"hours"),la=Math.max(39,la),ua=Math.max(39,ua),ma=Math.max(39,ma),At=o(la);var ya=l.mealCOB/l.carbs;oa=o(ma<999&&ua<999?(1-ya)*UAMpredBG+ya*COBpredBG:ua<999?(pa+COBpredBG)/2:ma<999?(pa+UAMpredBG)/2:pa),ha>oa&&(oa=ha),Rt=o(Rt=Lt||ea>0?Wt?ya*da+(1-ya)*ca:da:Wt?ca:ga);var xa=ma;if(hama&&(xa=(ma+ha)/2);if(xa=o(xa),l.carbs)if(!Wt&&ua<999)At=o(Math.max(la,ua));else if(ua<999){var Da=ya*ua+(1-ya)*xa;At=o(Math.max(la,ua,Da))}else At=Wt?xa:Rt;else Wt&&(At=o(Math.max(la,xa)));At=Math.min(At,oa),process.stderr.write("minPredBG: "+At+" minIOBPredBG: "+la+" minZTGuardBG: "+ha),ua<999&&process.stderr.write(" minCOBPredBG: "+ua),ma<999&&process.stderr.write(" minUAMPredBG: "+ma),console.error(" avgPredBG:"+oa+" COB/Carbs:"+l.mealCOB+"/"+l.carbs),fa>tt&&(At=Math.min(At,fa)),$e.COB=l.mealCOB,$e.IOB=a.iob,$e.BGI=n(Tt,i),$e.deviation=n(Ct,i),$e.ISF=n(xt,i),$e.CR=o(K,1),$e.target_bg=n(it,i),$e.TDD=o(Be,2),$e.current_target=o(it,0);var wa=$e.CR;ze!=$e.CR&&(wa=ze+"→"+$e.CR),$e.reason=yt+", COB: "+$e.COB+", Dev: "+$e.deviation+", BGI: "+$e.BGI+", CR: "+wa+", Target: "+vt+", minPredBG "+n(At,i)+", minGuardBG "+n(Rt,i)+", IOBpredBG "+n(na,i),ia>0&&($e.reason+=", COBpredBG "+n(ia,i)),sa>0&&($e.reason+=", UAMpredBG "+n(sa,i)),$e.reason+=q,$e.reason+="; ";var Ga=Ut;Ga<40&&(Ga=Math.min(Rt,Ga));var Ta,Ca=A-Ga,Ua=240,Oa=240;if(l.mealCOB>0&&(kt>0||ea>0)){for(_a=0;_aTa*tt&&(console.error("maxDelta "+n(nt,i)+" > "+100*Ta+"% of BG "+n(tt,i)+" - disabling SMB"),$e.reason+="maxDelta "+n(nt,i)+" > "+100*Ta+"% of BG "+n(tt,i)+" - SMB disabled!, ",qt=!1),console.error("BG projected to remain above "+n(st,i)+" for "+Ua+"minutes"),(Oa<240||Ua<60)&&console.error("BG projected to remain above "+n(A,i)+" for "+Oa+"minutes");var Aa=Oa,Ra=i.current_basal*$*xt*Aa/60,Ia=Math.max(0,l.mealCOB-.25*l.carbs),Fa=(Ca-Ra)/csf-Ia;Ra=o(Ra),Fa=o(Fa),console.error("naive_eventualBG:",Ut,"bgUndershoot:",Ca,"zeroTempDuration:",Aa,"zeroTempEffect:",Ra,"carbsReq:",Fa),"Could not parse clock data"==l.reason?console.error("carbsReq unknown: Could not parse clock data"):Fa>=i.carbsReqThreshold&&Oa<=45&&($e.carbsReq=Fa,$e.reason+=Fa+" add'l carbs req w/in "+Oa+"m; ");var ja=0;if(tt0&&rt>It)$e.reason+="IOB "+a.iob+" < "+o(-i.current_basal*$*20/60,2),$e.reason+=" and minDelta "+n(rt,i)+" > expectedDelta "+n(It,i)+"; ";else if(tt=55)return $e.reason+="; Canceling temp at "+$e.deliverAt.getMinutes()+"m past the hour. ",u.setTempBasal(0,0,i,$e,t);var Pa=0,Ea=Qe,qa=0;if(OtIt&&rt>0&&!Fa)return Ut<40?($e.reason+=", naive_eventualBG < 40. ",u.setTempBasal(0,30,i,$e,t)):(e.delta>rt?$e.reason+=", but Delta "+n(Xe,i)+" > expectedDelta "+n(It,i):$e.reason+=", but Min. Delta "+rt.toFixed(2)+" > Exp. Delta "+n(It,i),t.duration>15&&r(Qe,i)===r(t.rate,i)?($e.reason+=", temp "+t.rate+" ~ req "+Qe+"U/hr. ",$e):($e.reason+="; setting current basal of "+Qe+" as temp. ",u.setTempBasal(Qe,30,i,$e,t)));Pa=o(Pa=2*Math.min(0,(Ot-it)/xt),2);var Wa=Math.min(0,(Ut-it)/xt);if(Wa=o(Wa,2),rt<0&&rt>It)Pa=o(Pa*(rt/It),2);Ea=r(Ea=Qe+2*Pa,i),qa=t.duration*(t.rate-Qe)/60;var ka=Math.min(Pa,Wa);if(console.log("naiveInsulinReq:"+Wa),qa5&&Ea>=.8*t.rate)return $e.reason+=", temp "+t.rate+" ~< req "+Ea+"U/hr. ",$e;if(Ea<=0){if((ja=o(60*((Ca=it-Ut)/xt)/i.current_basal*$))<0?ja=0:(ja=30*o(ja/30),ja=Math.min(120,Math.max(0,ja))),ja>0)return $e.reason+=", setting "+ja+"m zero temp. ",u.setTempBasal(Ea,ja,i,$e,t)}else $e.reason+=", setting "+Ea+"U/hr. ";return u.setTempBasal(Ea,30,i,$e,t)}if(rt=2||It+-1*rt>=2)&&($e.manualBolusErrorString=rt>=0&&It>0?3:rt<0&&It<=0||rt<0&&It>=0?4:5),$e.insulinForManualBolus=o(($e.eventualBG-$e.target_bg)/xt,2),!m||!qt))return e.delta "+n(st,i)+" but Delta "+n(Xe,i)+" < Exp. Delta "+n(It,i):$e.reason+="Eventual BG "+n(Ot,i)+" > "+n(st,i)+" but Min. Delta "+rt.toFixed(2)+" < Exp. Delta "+n(It,i),t.duration>15&&r(Qe,i)===r(t.rate,i)?($e.reason+=", temp "+t.rate+" ~ req "+Qe+"U/hr. ",$e):($e.reason+="; setting current basal of "+Qe+" as temp. ",u.setTempBasal(Qe,30,i,$e,t));if(Math.min(Ot,At)st&&($e.manualBolusErrorString=6,$e.insulinForManualBolus=o(($e.eventualBG-$e.target_bg)/xt,2),$e.minPredBG=At),!m||!qt))return $e.reason+=n(Ot,i)+"-"+n(At,i)+" in range: no temp required",t.duration>15&&r(Qe,i)===r(t.rate,i)?($e.reason+=", temp "+t.rate+" ~ req "+Qe+"U/hr. ",$e):($e.reason+="; setting current basal of "+Qe+" as temp. ",u.setTempBasal(Qe,30,i,$e,t));if(Ot>=lt&&($e.reason+="Eventual BG "+n(Ot,i)+" >= "+n(lt,i)+", ",Ot>lt&&($e.insulinForManualBolus=o((Ot-it)/xt,2))),a.iob>mt)return $e.reason+="IOB "+o(a.iob,2)+" > max_iob "+mt,t.duration>15&&r(Qe,i)===r(t.rate,i)?($e.reason+=", temp "+t.rate+" ~ req "+Qe+"U/hr. ",$e):($e.reason+="; setting current basal of "+Qe+" as temp. ",u.setTempBasal(Qe,30,i,$e,t));Pa=o((Math.min(At,Ot)-it)/xt,2),O=o((Ot-it)/xt,2),Pa>mt-a.iob?(console.error("SMB limited by maxIOB: "+mt-a.iob+" (. insulinReq: "+Pa+" U)"),$e.reason+="max_iob "+mt+", ",Pa=mt-a.iob):console.error("SMB not limited by maxIOB ( insulinReq: "+Pa+" U)."),O>mt-a.iob?(console.error("Ev. Bolus limited by maxIOB: "+mt-a.iob+" (. insulinForManualBolus: "+O+" U)"),$e.reason+="max_iob "+mt+", "):console.error("Ev. Bolus would not be limited by maxIOB ( insulinForManualBolus: "+O+" U)."),Ea=r(Ea=Qe+2*Pa,i),Pa=o(Pa,3),$e.insulinReq=Pa;var La=o((new Date(Ve).getTime()-a.lastBolusTime)/6e4,1);if(m&&qt&&tt>A){var za=30;void 0!==i.maxSMBBasalMinutes&&(za=i.maxSMBBasalMinutes);var Na=30;void 0!==i.maxUAMSMBBasalMinutes&&(Na=i.maxUAMSMBBasalMinutes),v.useOverride&&_&&T!==za&&(console.error("SMB Max Minutes - setting overriden from "+za+" to "+T),za=T),v.useOverride&&_&&C!==Na&&(console.error("UAM Max Minutes - setting overriden from "+Na+" to "+C),Na=C);var Ha=o(l.mealCOB/K,3),Za=0;void 0===za?(Za=o(i.current_basal*$*30/60,1),console.error("smbMinutesSetting undefined: defaulting to 30m"),Pa>Za&&console.error("SMB limited by maxBolus: "+Za+" ( "+Pa+" U)")):a.iob>Ha&&a.iob>0?(console.error("IOB"+a.iob+"> COB"+l.mealCOB+"; mealInsulinReq ="+Ha),Na?(console.error("maxUAMSMBBasalMinutes: "+Na+", profile.current_basal: "+i.current_basal*$),Za=o(i.current_basal*$*Na/60,1)):(console.error("maxUAMSMBBasalMinutes undefined: defaulting to 30m"),Za=o(i.current_basal*$*30/60,1)),Pa>Za?console.error("SMB limited by maxUAMSMBBasalMinutes [ "+Na+"m ]: "+Za+"U ( "+Pa+"U )"):console.error("SMB is not limited by maxUAMSMBBasalMinutes. ( insulinReq: "+Pa+"U )")):(console.error(".maxSMBBasalMinutes: "+za+", profile.current_basal: "+i.current_basal*$),Pa>(Za=o(i.current_basal*za/60,1))?console.error("SMB limited by maxSMBBasalMinutes: "+za+"m ]: "+Za+"U ( insulinReq: "+Pa+"U )"):console.error("SMB is not limited by maxSMBBasalMinutes. ( insulinReq: "+Pa+"U )"));var $a=i.bolus_increment,Ja=1/$a,Ka=i.smb_delivery_ratio;Ka>.5&&console.error("SMB Delivery Ratio increased from default 0.5 to "+o(Ka,2));var Qa=Math.min(Pa*Ka,Za);Qa=Math.floor(Qa*Ja)/Ja,ja=o(60*((it-(Ut+la)/2)/xt)/i.current_basal*$),Pa>0&&Qa<$a&&(ja=0);var Va=0;ja<=0?ja=0:ja>=30?(ja=30*o(ja/30),ja=Math.min(60,Math.max(0,ja))):(Va=o(Qe*ja/30,2),ja=30),$e.reason+=" insulinReq "+Pa,Qa>=Za&&($e.reason+="; maxBolus "+Za),ja>0&&($e.reason+="; setting "+ja+"m low temp of "+Va+"U/h"),$e.reason+=". ";var Xa=3;i.SMBInterval&&(Xa=Math.min(10,Math.max(1,i.SMBInterval)));var Ya=o(Xa-La,0),er=o(60*(Xa-La),0)%60;if(console.error("naive_eventualBG "+Ut+","+ja+"m "+Va+"U/h temp needed; last bolus "+La+"m ago; maxBolus: "+Za),La>Xa?Qa>0&&($e.units=Qa,$e.reason+="Microbolusing "+Qa+"U. "):$e.reason+="Waiting "+Ya+"m "+er+"s to microbolus again. ",ja>0)return $e.rate=Va,$e.duration=ja,$e}var tr=u.getMaxSafeBasal(i);return 400==tt?u.setTempBasal(i.current_basal,30,i,$e,t):(Ea>tr&&($e.reason+="adj. req. rate: "+Ea+" to maxSafeBasal: "+o(tr,2)+", ",Ea=r(tr,i)),(qa=t.duration*(t.rate-Qe)/60)>=2*Pa?($e.reason+=t.duration+"m@"+t.rate.toFixed(2)+" > 2 * insulinReq. Setting temp basal of "+Ea+"U/hr. ",u.setTempBasal(Ea,30,i,$e,t)):void 0===t.duration||0===t.duration?($e.reason+="no temp, setting "+Ea+"U/hr. ",u.setTempBasal(Ea,30,i,$e,t)):t.duration>5&&r(Ea,i)<=r(t.rate,i)?($e.reason+="temp "+t.rate+" >~ req "+Ea+"U/hr. ",$e):($e.reason+="temp "+t.rate+"<"+Ea+"U/hr. ",u.setTempBasal(Ea,30,i,$e,t)))}},6880:(e,t,a)=>{var r=a(6654);e.exports=function(e,t){var a=20;void 0!==t&&"string"==typeof t.model&&(r(t.model,"54")||r(t.model,"23"))&&(a=40);return e<1?Math.round(e*a)/a:e<10?Math.round(20*e)/20:Math.round(10*e)/10}},2705:(e,t,a)=>{var r=a(5639).Symbol;e.exports=r},9932:e=>{e.exports=function(e,t){for(var a=-1,r=null==e?0:e.length,o=Array(r);++a{e.exports=function(e,t,a){return e==e&&(void 0!==a&&(e=e<=a?e:a),void 0!==t&&(e=e>=t?e:t)),e}},4239:(e,t,a)=>{var r=a(2705),o=a(9607),n=a(2333),i=r?r.toStringTag:void 0;e.exports=function(e){return null==e?void 0===e?"[object Undefined]":"[object Null]":i&&i in Object(e)?o(e):n(e)}},531:(e,t,a)=>{var r=a(2705),o=a(9932),n=a(1469),i=a(3448),s=r?r.prototype:void 0,l=s?s.toString:void 0;e.exports=function e(t){if("string"==typeof t)return t;if(n(t))return o(t,e)+"";if(i(t))return l?l.call(t):"";var a=t+"";return"0"==a&&1/t==-Infinity?"-0":a}},7561:(e,t,a)=>{var r=a(7990),o=/^\s+/;e.exports=function(e){return e?e.slice(0,r(e)+1).replace(o,""):e}},1957:(e,t,a)=>{var r="object"==typeof a.g&&a.g&&a.g.Object===Object&&a.g;e.exports=r},9607:(e,t,a)=>{var r=a(2705),o=Object.prototype,n=o.hasOwnProperty,i=o.toString,s=r?r.toStringTag:void 0;e.exports=function(e){var t=n.call(e,s),a=e[s];try{e[s]=void 0;var r=!0}catch(e){}var o=i.call(e);return r&&(t?e[s]=a:delete e[s]),o}},2333:e=>{var t=Object.prototype.toString;e.exports=function(e){return t.call(e)}},5639:(e,t,a)=>{var r=a(1957),o="object"==typeof self&&self&&self.Object===Object&&self,n=r||o||Function("return this")();e.exports=n},7990:e=>{var t=/\s/;e.exports=function(e){for(var a=e.length;a--&&t.test(e.charAt(a)););return a}},6654:(e,t,a)=>{var r=a(9750),o=a(531),n=a(554),i=a(9833);e.exports=function(e,t,a){e=i(e),t=o(t);var s=e.length,l=a=void 0===a?s:r(n(a),0,s);return(a-=t.length)>=0&&e.slice(a,l)==t}},1469:e=>{var t=Array.isArray;e.exports=t},3218:e=>{e.exports=function(e){var t=typeof e;return null!=e&&("object"==t||"function"==t)}},7005:e=>{e.exports=function(e){return null!=e&&"object"==typeof e}},3448:(e,t,a)=>{var r=a(4239),o=a(7005);e.exports=function(e){return"symbol"==typeof e||o(e)&&"[object Symbol]"==r(e)}},8601:(e,t,a)=>{var r=a(4841),o=1/0;e.exports=function(e){return e?(e=r(e))===o||e===-1/0?17976931348623157e292*(e<0?-1:1):e==e?e:0:0===e?e:0}},554:(e,t,a)=>{var r=a(8601);e.exports=function(e){var t=r(e),a=t%1;return t==t?a?t-a:t:0}},4841:(e,t,a)=>{var r=a(7561),o=a(3218),n=a(3448),i=/^[-+]0x[0-9a-f]+$/i,s=/^0b[01]+$/i,l=/^0o[0-7]+$/i,u=parseInt;e.exports=function(e){if("number"==typeof e)return e;if(n(e))return NaN;if(o(e)){var t="function"==typeof e.valueOf?e.valueOf():e;e=o(t)?t+"":t}if("string"!=typeof e)return 0===e?e:+e;e=r(e);var a=s.test(e);return a||l.test(e)?u(e.slice(2),a?2:8):i.test(e)?NaN:+e}},9833:(e,t,a)=>{var r=a(531);e.exports=function(e){return null==e?"":r(e)}}},t={};function a(r){var o=t[r];if(void 0!==o)return o.exports;var n=t[r]={exports:{}};return e[r](n,n.exports,a),n.exports}a.g=function(){if("object"==typeof globalThis)return globalThis;try{return this||new Function("return this")()}catch(e){if("object"==typeof window)return window}}();var r=a(5546);freeaps_determineBasal=r})();