determine-basal.js 21 KB

1
  1. var freeaps_determineBasal;(()=>{var e={5546:(e,r,a)=>{var t=a(6880);function o(e,r){r||(r=0);var a=Math.pow(10,r);return Math.round(e*a)/a}function n(e,r){return"mmol/L"===r.out_units?o(e/18,1).toFixed(1):Math.round(e)}var s=1;e.exports=function(e,r,a,i,l,u,m,d,c,g){var h={},p=new Date;if(g&&(p=g),void 0===i||void 0===i.current_basal)return h.error="Error: could not get current basal rate",h;var f=t(i.current_basal,i),b=f,v=new Date;g&&(v=g);var B,M=new Date(e.date),_=o((v-M)/60/1e3,1),x=e.glucose,S=e.noise;B=e.delta>-.5?"+"+o(e.delta,0):o(e.delta,0);var G=Math.min(e.delta,e.short_avgdelta),y=Math.min(e.short_avgdelta,e.long_avgdelta),C=Math.max(e.delta,e.short_avgdelta,e.long_avgdelta);(x<=10||38===x||S>=3)&&(h.reason="CGM is calibrating, in ??? state, or noise is high");if(x>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 ("+x+"+"+e.delta+") for 5m w/ "+e.short_avgdelta+" mg/dL ~15m change & "+e.long_avgdelta+" mg/dL ~45m change"),console.error("Simulator mode detected (",e.device,"): continuing anyway")):!0),_>12||_<-5?h.reason="If current system time "+v+" is correct, then BG data is too old. The last BG data was read "+_+"m ago at "+M:0===e.short_avgdelta&&0===e.long_avgdelta&&(e.last_cal&&e.last_cal<3?h.reason="CGM was just calibrated":h.reason="CGM data is unchanged ("+x+"+"+e.delta+") for 5m w/ "+e.short_avgdelta+" mg/dL ~15m change & "+e.long_avgdelta+" mg/dL ~45m change"),x<=10||38===x||S>=3||_>12||_<-5||0===e.short_avgdelta&&0===e.long_avgdelta)return r.rate>=b?(h.reason+=". Canceling high temp basal of "+r.rate,h.deliverAt=p,h.temp="absolute",h.duration=0,h.rate=0,h):0===r.rate&&r.duration>30?(h.reason+=". Shortening "+r.duration+"m long zero temp to 30m. ",h.deliverAt=p,h.temp="absolute",h.duration=30,h.rate=0,h):(h.reason+=". Temp "+r.rate+" <= current basal "+b+"U/hr; doing nothing. ",h);var w,O,T,I=i.max_iob;if(void 0!==i.min_bg&&(O=i.min_bg),void 0!==i.max_bg&&(T=i.max_bg),void 0===i.min_bg||void 0===i.max_bg)return h.error="Error: could not determine target_bg. ",h;w=(i.min_bg+i.max_bg)/2;var A=i.exercise_mode||i.high_temptarget_raises_sensitivity,F=100;if(i.half_basal_exercise_target)var U=i.half_basal_exercise_target;else U=160;if(A&&i.temptargetSet&&w>F||i.low_temptarget_lowers_sensitivity&&i.temptargetSet&&w<F){var j=U-F;j+w-F>0?(s=j/(j+w-F),s=o(s=Math.min(s,i.autosens_max),2)):s=i.autosens_max,process.stderr.write("Sensitivity ratio set to "+s+" based on temp target of "+w+"; ")}else void 0!==l&&l&&(s=l.ratio,process.stderr.write("Autosens ratio: "+s+"; "));if(s&&(b=i.current_basal*s,(b=t(b,i))!==f?process.stderr.write("Adjusting basal from "+f+" to "+b+"; "):process.stderr.write("Basal unchanged: "+b+"; ")),i.temptargetSet);else if(void 0!==l&&l&&(i.sensitivity_raises_target&&l.ratio<1||i.resistance_lowers_target&&l.ratio>1)){O=o((O-60)/l.ratio)+60,T=o((T-60)/l.ratio)+60;var D=o((w-60)/l.ratio)+60;w===(D=Math.max(80,D))?process.stderr.write("target_bg unchanged: "+D+"; "):process.stderr.write("target_bg from "+w+" to "+D+"; "),w=D}if(e.noise>=2){var E=Math.max(1.1,i.noisyCGMTargetMultiplier),q=(Math.min(250,i.maxRaw),o(Math.min(200,O*E))),W=o(Math.min(200,w*E)),R=o(Math.min(200,T*E));process.stderr.write("Raising target_bg for noisy / raw CGM data, from "+w+" to "+W+"; "),O=q,w=W,T=R}var z=O-.5*(O-40),L=o(i.sens,1),P=i.sens;if(void 0!==l&&l&&((P=o(P=i.sens/s,1))!==L?process.stderr.write("ISF from "+L+" to "+P):process.stderr.write("ISF unchanged: "+P)),console.error("; CR:",i.carb_ratio),P=function(e,r,a,t,n,s,i){if(!1===a.auto_isf)return console.error("autoISF disabled in Preferences"),e;if(void 0===a.autoisf_max)return console.error("autoISF_max not defined"),e;if(a.autoisf_max){console.error("autoISF enabled in Preferences"),console.error("autoISF hourly change: "+a.autoisf_hourlychange),console.error("autoISF max: "+a.autoisf_max),console.error("SMB Range Extension: "+a.smb_max_range_extension),console.error("SMB fraction of InsReq: "+a.smb_delivery_ratio);var l=t.autoISF_duration,u=t.autoISF_average,m=a.autoisf_hourlychange;if(0==n.mealCOB&&l>=10)if(u>r){var d=a.autoisf_max,c=l/60,g=m/r,h=1+c*g*(u-r),p=Math.min(d,h);console.error("ISFreductionLevel = "+p+" ;");var f=Math.max(p,i);console.error("dura05_weight = "+c+"; avg05_weight = "+g+"; levelISF = "+h+"; liftISF = "+f),console.error("autoISF reports "+e+" did not do it for "+l+"min; go more aggressive by "+o(h,2)),d<h&&console.error("autoISF reduction "+o(h,2)+" limited by autoisf_max "+d),e=o(a.sens/f,1),console.error("autoISF reports sens is now "+e)}else console.error("autoISF by-passed; avg. glucose "+u+" below target "+r);else n.mealCOB>0?console.error("autoISF by-passed; mealCOB of "+o(n.mealCOB,1)):console.error("autoISF by-passed; BG is only "+l+"min at level "+u);return console.error("autoISF has run. Sens is now "+e),e}}(P,w,i,e,u,0,s),void 0===a)return h.error="Error: iob_data undefined. ",h;var k,N=a;if(a.length,a.length>1&&(a=N[0]),void 0===a.activity||void 0===a.iob)return h.error="Error: iob_data missing some property. ",h;var Z=((k=void 0!==a.lastTemp?o((new Date(v).getTime()-a.lastTemp.date)/6e4):0)+r.duration)%30;if(console.error("currenttemp:",r,"lastTempAge:",k,"m","tempModulus:",Z,"m"),h.temp="absolute",h.deliverAt=p,d&&r&&a.lastTemp&&r.rate!==a.lastTemp.rate&&k>10&&r.duration)return h.reason="Warning: currenttemp rate "+r.rate+" != lastTemp rate "+a.lastTemp.rate+" from pumphistory; canceling temp",m.setTempBasal(0,0,i,h,r);if(r&&a.lastTemp&&r.duration>0){var $=k-a.lastTemp.duration;if($>5&&k>10)return h.reason="Warning: currenttemp running but lastTemp from pumphistory ended "+$+"m ago; canceling temp",m.setTempBasal(0,0,i,h,r)}var H=o(-a.activity*P*5,2),J=o(6*(G-H));if(J<0&&(J=o(6*(y-H)))<0&&(J=o(6*(e.long_avgdelta-H))),a.iob>0)var K=o(x-a.iob*P);else K=o(x-a.iob*Math.min(P,i.sens));var Q=K+J;if(void 0===Q||isNaN(Q))return h.error="Error: could not calculate eventualBG. ",h;var V=function(e,r,a){return o(a+(e-r)/24,1)}(w,Q,H);h={temp:"absolute",bg:x,tick:B,eventualBG:Q,insulinReq:0,reservoir:c,deliverAt:p,sensitivityRatio:s};var X=[],Y=[],ee=[],re=[];X.push(x),Y.push(x),re.push(x),ee.push(x);var ae=function(e,r,a,t){return r?!e.allowSMB_with_high_temptarget&&e.temptargetSet&&t>100?(console.error("SMB disabled due to high temptarget of",t),!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&&t<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(t,e)),!0):(console.error("SMB disabled (no enableSMB preferences active or no condition satisfied)"),!1):(console.error("SMB disabled (!microBolusAllowed)"),!1)}(i,d,u,w),te=i.enableUAM,oe=0,ne=0;oe=o(G-H,1);var se=o(G-H,1);csf=P/i.carb_ratio,console.error("profile.sens:",i.sens,"sens:",P,"CSF:",csf);var ie=o(30*csf*5/60,1);oe>ie&&(console.error("Limiting carb impact from",oe,"to",ie,"mg/dL/5m (",30,"g/h )"),oe=ie);var le=3;s&&(le/=s);var ue=le;if(u.carbs){le=Math.max(le,u.mealCOB/20);var me=o((new Date(v).getTime()-u.lastCarbTime)/6e4),de=(u.carbs-u.mealCOB)/u.carbs;ue=o(ue=le+1.5*me/60,1),console.error("Last carbs",me,"minutes ago; remainingCATime:",ue,"hours;",o(100*de)+"% carbs absorbed")}var ce=Math.max(0,oe/5*60*ue/2)/csf,ge=90,he=1;i.remainingCarbsCap&&(ge=Math.min(90,i.remainingCarbsCap)),i.remainingCarbsFraction&&(he=Math.min(1,i.remainingCarbsFraction));var pe=1-he,fe=Math.max(0,u.mealCOB-ce-u.carbs*pe),be=(fe=Math.min(ge,fe))*csf*5/60/(ue/2),ve=o(u.slopeFromMaxDeviation,2),Be=o(u.slopeFromMinDeviation,2),Me=Math.min(ve,-Be/3);ne=0===oe?0:Math.min(60*ue/5/2,Math.max(0,u.mealCOB*csf/oe)),console.error("Carb Impact:",oe,"mg/dL per 5m; CI Duration:",o(5*ne/60*2,1),"hours; remaining CI (",ue," peak):",o(be,1),"mg/dL per 5m");var _e,xe,Se,Ge,ye,Ce=999,we=999,Oe=999,Te=x,Ie=999,Ae=999,Fe=999,Ue=999,je=Q,De=x,Ee=x,qe=0,We=[],Re=[];try{N.forEach((function(e){var r=o(-e.activity*P*5,2),a=o(-e.iobWithZeroTemp.activity*P*5,2),t=oe*(1-Math.min(1,Y.length/12));je=Y[Y.length-1]+r+t;var n=re[re.length-1]+a,s=Math.max(0,Math.max(0,oe)*(1-X.length/Math.max(2*ne,1))),i=Math.min(X.length,12*ue-X.length),l=Math.max(0,i/(ue/2*12)*be);s+l,We.push(o(l,0)),Re.push(o(s,0)),COBpredBG=X[X.length-1]+r+Math.min(0,t)+s+l;var u=Math.max(0,se+ee.length*Me),m=Math.max(0,se*(1-ee.length/Math.max(36,1))),d=Math.min(u,m);d>0&&(qe=o(5*(ee.length+1)/60,1)),UAMpredBG=ee[ee.length-1]+r+Math.min(0,t)+d,Y.length<48&&Y.push(je),X.length<48&&X.push(COBpredBG),ee.length<48&&ee.push(UAMpredBG),re.length<48&&re.push(n),COBpredBG<Ie&&(Ie=o(COBpredBG)),UAMpredBG<Ae&&(Ae=o(UAMpredBG)),je<Fe&&(Fe=o(je)),n<Ue&&(Ue=o(n));Y.length>18&&je<Ce&&(Ce=o(je)),je>De&&(De=je),(ne||be>0)&&X.length>18&&COBpredBG<we&&(we=o(COBpredBG)),(ne||be>0)&&COBpredBG>De&&(Ee=COBpredBG),te&&ee.length>12&&UAMpredBG<Oe&&(Oe=o(UAMpredBG)),te&&UAMpredBG>De&&UAMpredBG}))}catch(e){console.error("Problem with iobArray. Optional feature Advanced Meal Assist disabled")}u.mealCOB&&(console.error("predCIs (mg/dL/5m):",Re.join(" ")),console.error("remainingCIs: ",We.join(" "))),h.predBGs={},Y.forEach((function(e,r,a){a[r]=o(Math.min(401,Math.max(39,e)))}));for(var ze=Y.length-1;ze>12&&Y[ze-1]===Y[ze];ze--)Y.pop();for(h.predBGs.IOB=Y,Se=o(Y[Y.length-1]),re.forEach((function(e,r,a){a[r]=o(Math.min(401,Math.max(39,e)))})),ze=re.length-1;ze>6&&!(re[ze-1]>=re[ze]||re[ze]<=w);ze--)re.pop();if(h.predBGs.ZT=re,o(re[re.length-1]),u.mealCOB>0&&(oe>0||be>0)){for(X.forEach((function(e,r,a){a[r]=o(Math.min(401,Math.max(39,e)))})),ze=X.length-1;ze>12&&X[ze-1]===X[ze];ze--)X.pop();h.predBGs.COB=X,Ge=o(X[X.length-1]),Q=Math.max(Q,o(X[X.length-1]))}if(oe>0||be>0){if(te){for(ee.forEach((function(e,r,a){a[r]=o(Math.min(401,Math.max(39,e)))})),ze=ee.length-1;ze>12&&ee[ze-1]===ee[ze];ze--)ee.pop();h.predBGs.UAM=ee,ye=o(ee[ee.length-1]),ee[ee.length-1]&&(Q=Math.max(Q,o(ee[ee.length-1])))}h.eventualBG=Q}console.error("UAM Impact:",se,"mg/dL per 5m; UAM Duration:",qe,"hours"),Ce=Math.max(39,Ce),we=Math.max(39,we),Oe=Math.max(39,Oe),_e=o(Ce);var Le=u.mealCOB/u.carbs;xe=o(Oe<999&&we<999?(1-Le)*UAMpredBG+Le*COBpredBG:we<999?(je+COBpredBG)/2:Oe<999?(je+UAMpredBG)/2:je),Ue>xe&&(xe=Ue),Te=o(Te=ne||be>0?te?Le*Ie+(1-Le)*Ae:Ie:te?Ae:Fe);var Pe=Oe;if(Ue<z)Pe=(Oe+Ue)/2;else if(Ue<w){var ke=(Ue-z)/(w-z);Pe=(Oe+(Oe*ke+Ue*(1-ke)))/2}else Ue>Oe&&(Pe=(Oe+Ue)/2);if(Pe=o(Pe),u.carbs)if(!te&&we<999)_e=o(Math.max(Ce,we));else if(we<999){var Ne=Le*we+(1-Le)*Pe;_e=o(Math.max(Ce,we,Ne))}else _e=te?Pe:Te;else te&&(_e=o(Math.max(Ce,Pe)));_e=Math.min(_e,xe),process.stderr.write("minPredBG: "+_e+" minIOBPredBG: "+Ce+" minZTGuardBG: "+Ue),we<999&&process.stderr.write(" minCOBPredBG: "+we),Oe<999&&process.stderr.write(" minUAMPredBG: "+Oe),console.error(" avgPredBG:",xe,"COB:",u.mealCOB,"/",u.carbs),Ee>x&&(_e=Math.min(_e,Ee)),h.COB=u.mealCOB,h.IOB=a.iob,h.BGI=H,h.deviation=n(J,i),h.ISF=n(P,i),h.CR=o(i.carb_ratio,2),h.target_bg=n(w,i),h.reason="minPredBG "+n(_e,i)+", minGuardBG "+n(Te,i)+", IOBpredBG "+n(Se,i),Ge>0&&(h.reason+=", COBpredBG "+n(Ge,i)),ye>0&&(h.reason+=", UAMpredBG "+n(ye,i)),h.reason+="; ";var Ze=K;Ze<40&&(Ze=Math.min(Te,Ze));var $e,He=z-Ze,Je=240,Ke=240;if(u.mealCOB>0&&(oe>0||be>0)){for(ze=0;ze<X.length;ze++)if(X[ze]<O){Je=5*ze;break}for(ze=0;ze<X.length;ze++)if(X[ze]<z){Ke=5*ze;break}}else{for(ze=0;ze<Y.length;ze++)if(Y[ze]<O){Je=5*ze;break}for(ze=0;ze<Y.length;ze++)if(Y[ze]<z){Ke=5*ze;break}}ae&&Te<z&&(console.error("minGuardBG",n(Te,i),"projected below",n(z,i),"- disabling SMB"),ae=!1),void 0===i.maxDelta_bg_threshold&&($e=.2),void 0!==i.maxDelta_bg_threshold&&($e=Math.min(i.maxDelta_bg_threshold,.3)),C>$e*x&&(console.error("maxDelta "+n(C,i)+" > "+100*$e+"% of BG "+n(x,i)+" - disabling SMB"),h.reason+="maxDelta "+n(C,i)+" > "+100*$e+"% of BG "+n(x,i)+": SMB disabled; ",ae=!1),console.error("BG projected to remain above",n(O,i),"for",Je,"minutes"),(Ke<240||Je<60)&&console.error("BG projected to remain above",n(z,i),"for",Ke,"minutes");var Qe=Ke,Ve=i.current_basal*P*Qe/60,Xe=Math.max(0,u.mealCOB-.25*u.carbs),Ye=(He-Ve)/csf-Xe;if(Ve=o(Ve),Ye=o(Ye),console.error("naive_eventualBG:",K,"bgUndershoot:",He,"zeroTempDuration:",Qe,"zeroTempEffect:",Ve,"carbsReq:",Ye),Ye>=i.carbsReqThreshold&&Ke<=45&&(h.carbsReq=Ye,h.reason+=Ye+" add'l carbs req w/in "+Ke+"m; "),x<z&&a.iob<20*-i.current_basal/60&&G>0&&G>V)h.reason+="IOB "+a.iob+" < "+o(20*-i.current_basal/60,2),h.reason+=" and minDelta "+n(G,i)+" > expectedDelta "+n(V,i)+"; ";else if(x<z||Te<z){h.reason+="minGuardBG "+n(Te,i)+"<"+n(z,i);var er=(He=w-Te)/P,rr=o(60*er/i.current_basal);return rr=30*o(rr/30),rr=Math.min(120,Math.max(30,rr)),m.setTempBasal(0,rr,i,h,r)}if(i.skip_neutral_temps&&h.deliverAt.getMinutes()>=55)return h.reason+="; Canceling temp at "+h.deliverAt.getMinutes()+"m past the hour. ",m.setTempBasal(0,0,i,h,r);if(Q<O){if(h.reason+="Eventual BG "+n(Q,i)+" < "+n(O,i),G>V&&G>0&&!Ye)return K<40?(h.reason+=", naive_eventualBG < 40. ",m.setTempBasal(0,30,i,h,r)):(e.delta>G?h.reason+=", but Delta "+n(B,i)+" > expectedDelta "+n(V,i):h.reason+=", but Min. Delta "+G.toFixed(2)+" > Exp. Delta "+n(V,i),r.duration>15&&t(b,i)===t(r.rate,i)?(h.reason+=", temp "+r.rate+" ~ req "+b+"U/hr. ",h):(h.reason+="; setting current basal of "+b+" as temp. ",m.setTempBasal(b,30,i,h,r)));var ar=2*Math.min(0,(Q-w)/P);ar=o(ar,2);var tr=Math.min(0,(K-w)/P);if(tr=o(tr,2),G<0&&G>V)ar=o(ar*(G/V),2);var or=b+2*ar;or=t(or,i);var nr=r.duration*(r.rate-b)/60;if(nr<Math.min(ar,tr)-.3*b)return h.reason+=", "+r.duration+"m@"+r.rate.toFixed(2)+" is a lot less than needed. ",m.setTempBasal(or,30,i,h,r);if(void 0!==r.rate&&r.duration>5&&or>=.8*r.rate)return h.reason+=", temp "+r.rate+" ~< req "+or+"U/hr. ",h;if(or<=0){if((rr=o(60*(er=(He=w-K)/P)/i.current_basal))<0?rr=0:(rr=30*o(rr/30),rr=Math.min(120,Math.max(0,rr))),rr>0)return h.reason+=", setting "+rr+"m zero temp. ",m.setTempBasal(or,rr,i,h,r)}else h.reason+=", setting "+or+"U/hr. ";return m.setTempBasal(or,30,i,h,r)}if(G<V&&(!d||!ae))return e.delta<G?h.reason+="Eventual BG "+n(Q,i)+" > "+n(O,i)+" but Delta "+n(B,i)+" < Exp. Delta "+n(V,i):h.reason+="Eventual BG "+n(Q,i)+" > "+n(O,i)+" but Min. Delta "+G.toFixed(2)+" < Exp. Delta "+n(V,i),r.duration>15&&t(b,i)===t(r.rate,i)?(h.reason+=", temp "+r.rate+" ~ req "+b+"U/hr. ",h):(h.reason+="; setting current basal of "+b+" as temp. ",m.setTempBasal(b,30,i,h,r));if(Math.min(Q,_e)<T&&(!d||!ae))return h.reason+=n(Q,i)+"-"+n(_e,i)+" in range: no temp required",r.duration>15&&t(b,i)===t(r.rate,i)?(h.reason+=", temp "+r.rate+" ~ req "+b+"U/hr. ",h):(h.reason+="; setting current basal of "+b+" as temp. ",m.setTempBasal(b,30,i,h,r));if(Q>=T&&(h.reason+="Eventual BG "+n(Q,i)+" >= "+n(T,i)+", "),a.iob>I)return h.reason+="IOB "+o(a.iob,2)+" > max_iob "+I,r.duration>15&&t(b,i)===t(r.rate,i)?(h.reason+=", temp "+r.rate+" ~ req "+b+"U/hr. ",h):(h.reason+="; setting current basal of "+b+" as temp. ",m.setTempBasal(b,30,i,h,r));(ar=o((Math.min(_e,Q)-w)/P,2))>I-a.iob&&(h.reason+="max_iob "+I+", ",ar=I-a.iob),or=t(or=b+2*ar,i),ar=o(ar,3),h.insulinReq=ar;var sr=o((new Date(v).getTime()-a.lastBolusTime)/6e4,1);if(d&&ae&&x>z){var ir=o(u.mealCOB/i.carb_ratio,3);if(!1===i.auto_isf){console.error("autoISF disabled, don't adjust SMB range");var lr=1}else lr=i.smb_max_range_extension;if(lr>1&&console.error("SMB max range extended from default by factor "+lr),void 0===i.maxSMBBasalMinutes){var ur=o(lr*i.current_basal*30/60,1);console.error("profile.maxSMBBasalMinutes undefined: defaulting to 30m")}else a.iob>ir&&a.iob>0?(console.error("IOB",a.iob,"> COB",u.mealCOB+"; mealInsulinReq =",ir),i.maxUAMSMBBasalMinutes?(console.error("profile.maxUAMSMBBasalMinutes:",i.maxUAMSMBBasalMinutes,"profile.current_basal:",i.current_basal),ur=o(lr*i.current_basal*i.maxUAMSMBBasalMinutes/60,1)):(console.error("profile.maxUAMSMBBasalMinutes undefined: defaulting to 30m"),ur=o(30*i.current_basal/60,1))):(console.error("profile.maxSMBBasalMinutes:",i.maxSMBBasalMinutes,"profile.current_basal:",i.current_basal),ur=o(lr*i.current_basal*i.maxSMBBasalMinutes/60,1));var mr=i.bolus_increment,dr=1/mr;if(!1===i.auto_isf)console.error("autoISF disabled, don't adjust SMB ratio"),cr=.5;else var cr=i.smb_delivery_ratio;cr>.5&&console.error("SMB ratio increased from default 0.5 to "+cr);var gr=Math.floor(Math.min(ar*cr,ur)*dr)/dr;rr=o(60*(er=(w-(K+Ce)/2)/P)/i.current_basal),ar>0&&gr<mr&&(rr=0);var hr=0;rr<=0?rr=0:rr>=30?(rr=30*o(rr/30),rr=Math.min(60,Math.max(0,rr))):(hr=o(b*rr/30,2),rr=30),h.reason+=" insulinReq "+ar,gr>=ur&&(h.reason+="; maxBolus "+ur),rr>0&&(h.reason+="; setting "+rr+"m low temp of "+hr+"U/h"),h.reason+=". ";var pr=3;i.SMBInterval&&(pr=Math.min(10,Math.max(1,i.SMBInterval)));var fr=o(pr-sr,0),br=o(60*(pr-sr),0)%60;if(console.error("naive_eventualBG",K+",",rr+"m "+hr+"U/h temp needed; last bolus",sr+"m ago; maxBolus: "+ur),sr>pr?gr>0&&(h.units=gr,h.reason+="Microbolusing "+gr+"U. "):h.reason+="Waiting "+fr+"m "+br+"s to microbolus again. ",rr>0)return h.rate=hr,h.duration=rr,h}var vr=m.getMaxSafeBasal(i);return or>vr&&(h.reason+="adj. req. rate: "+or+" to maxSafeBasal: "+vr+", ",or=t(vr,i)),(nr=r.duration*(r.rate-b)/60)>=2*ar?(h.reason+=r.duration+"m@"+r.rate.toFixed(2)+" > 2 * insulinReq. Setting temp basal of "+or+"U/hr. ",m.setTempBasal(or,30,i,h,r)):void 0===r.duration||0===r.duration?(h.reason+="no temp, setting "+or+"U/hr. ",m.setTempBasal(or,30,i,h,r)):r.duration>5&&t(or,i)<=t(r.rate,i)?(h.reason+="temp "+r.rate+" >~ req "+or+"U/hr. ",h):(h.reason+="temp "+r.rate+"<"+or+"U/hr. ",m.setTempBasal(or,30,i,h,r))}},6880:(e,r,a)=>{var t=a(6654);e.exports=function(e,r){var a=20;void 0!==r&&"string"==typeof r.model&&(t(r.model,"54")||t(r.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,r,a)=>{var t=a(5639).Symbol;e.exports=t},9932:e=>{e.exports=function(e,r){for(var a=-1,t=null==e?0:e.length,o=Array(t);++a<t;)o[a]=r(e[a],a,e);return o}},9750:e=>{e.exports=function(e,r,a){return e==e&&(void 0!==a&&(e=e<=a?e:a),void 0!==r&&(e=e>=r?e:r)),e}},4239:(e,r,a)=>{var t=a(2705),o=a(9607),n=a(2333),s=t?t.toStringTag:void 0;e.exports=function(e){return null==e?void 0===e?"[object Undefined]":"[object Null]":s&&s in Object(e)?o(e):n(e)}},531:(e,r,a)=>{var t=a(2705),o=a(9932),n=a(1469),s=a(3448),i=t?t.prototype:void 0,l=i?i.toString:void 0;e.exports=function e(r){if("string"==typeof r)return r;if(n(r))return o(r,e)+"";if(s(r))return l?l.call(r):"";var a=r+"";return"0"==a&&1/r==-Infinity?"-0":a}},7561:(e,r,a)=>{var t=a(7990),o=/^\s+/;e.exports=function(e){return e?e.slice(0,t(e)+1).replace(o,""):e}},1957:(e,r,a)=>{var t="object"==typeof a.g&&a.g&&a.g.Object===Object&&a.g;e.exports=t},9607:(e,r,a)=>{var t=a(2705),o=Object.prototype,n=o.hasOwnProperty,s=o.toString,i=t?t.toStringTag:void 0;e.exports=function(e){var r=n.call(e,i),a=e[i];try{e[i]=void 0;var t=!0}catch(e){}var o=s.call(e);return t&&(r?e[i]=a:delete e[i]),o}},2333:e=>{var r=Object.prototype.toString;e.exports=function(e){return r.call(e)}},5639:(e,r,a)=>{var t=a(1957),o="object"==typeof self&&self&&self.Object===Object&&self,n=t||o||Function("return this")();e.exports=n},7990:e=>{var r=/\s/;e.exports=function(e){for(var a=e.length;a--&&r.test(e.charAt(a)););return a}},6654:(e,r,a)=>{var t=a(9750),o=a(531),n=a(554),s=a(9833);e.exports=function(e,r,a){e=s(e),r=o(r);var i=e.length,l=a=void 0===a?i:t(n(a),0,i);return(a-=r.length)>=0&&e.slice(a,l)==r}},1469:e=>{var r=Array.isArray;e.exports=r},3218:e=>{e.exports=function(e){var r=typeof e;return null!=e&&("object"==r||"function"==r)}},7005:e=>{e.exports=function(e){return null!=e&&"object"==typeof e}},3448:(e,r,a)=>{var t=a(4239),o=a(7005);e.exports=function(e){return"symbol"==typeof e||o(e)&&"[object Symbol]"==t(e)}},8601:(e,r,a)=>{var t=a(4841),o=1/0;e.exports=function(e){return e?(e=t(e))===o||e===-1/0?17976931348623157e292*(e<0?-1:1):e==e?e:0:0===e?e:0}},554:(e,r,a)=>{var t=a(8601);e.exports=function(e){var r=t(e),a=r%1;return r==r?a?r-a:r:0}},4841:(e,r,a)=>{var t=a(7561),o=a(3218),n=a(3448),s=/^[-+]0x[0-9a-f]+$/i,i=/^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 r="function"==typeof e.valueOf?e.valueOf():e;e=o(r)?r+"":r}if("string"!=typeof e)return 0===e?e:+e;e=t(e);var a=i.test(e);return a||l.test(e)?u(e.slice(2),a?2:8):s.test(e)?NaN:+e}},9833:(e,r,a)=>{var t=a(531);e.exports=function(e){return null==e?"":t(e)}}},r={};function a(t){var o=r[t];if(void 0!==o)return o.exports;var n=r[t]={exports:{}};return e[t](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 t=a(5546);freeaps_determineBasal=t})();