determine-basal.js 23 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):Math.round(e)}var i="",s="";function l(e,r){var a=[2,7,12,16,20,50,60,80,90,100,110,150,180,200],t=[0,0,.4,.7,.7,-.5,-.5,-.3,-.2,0,0,.5,.7,.7],o=a.length-1,n=a[0],i=t[0],s=a[o],l=t[o],m=1,d=1,u=1,c=n;if(n>e)m=(d=i)+((l=t[1])-d)/((s=a[1])-(u=n))*(e-u);else if(s<e)m=(d=i=t[o-1])+(l-d)/(s-(u=n=a[o-1]))*(e-u);else for(var g=0;g<=o;g++){if(i=t[g],(n=a[g])==e){m=i;break}if(n>e){m=d+(i-d)/(n-(u=c))*(e-u);break}d=i,c=n}return m*=e>100?r.higher_ISFrange_weight:e>40?r.lower_ISFrange_weight:r.delta_ISFrange_weight}e.exports=function(e,r,a,m,d,u,c,g,p,b){var h={},v=new Date;if(b&&(v=b),void 0===m||void 0===m.current_basal)return h.error="Error: could not get current basal rate",h;var f=t(m.current_basal,m),B=f,M=new Date;b&&(M=b);var _,x=new Date(e.date),y=o((M-x)/60/1e3,1),S=e.glucose,C=e.noise;_=e.delta>-.5?"+"+o(e.delta,0):o(e.delta,0);var G=Math.min(e.delta,e.short_avgdelta),w=Math.min(e.short_avgdelta,e.long_avgdelta),O=Math.max(e.delta,e.short_avgdelta,e.long_avgdelta);(S<=10||38===S||C>=3)&&(h.reason="CGM is calibrating, in ??? state, or noise is high");if(S>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 ("+S+"+"+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),y>12||y<-5?h.reason="If current system time "+M+" is correct, then BG data is too old. The last BG data was read "+y+"m ago at "+x: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 ("+S+"+"+e.delta+") for 5m w/ "+e.short_avgdelta+" mg/dL ~15m change & "+e.long_avgdelta+" mg/dL ~45m change"),S<=10||38===S||C>=3||y>12||y<-5||0===e.short_avgdelta&&0===e.long_avgdelta)return r.rate>=B?(h.reason+=". Canceling high temp basal of "+r.rate,h.deliverAt=v,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=v,h.temp="absolute",h.duration=30,h.rate=0,h):(h.reason+=". Temp "+r.rate+" <= current basal "+B+"U/hr; doing nothing. ",h);var I,F,T,A=m.max_iob;if(void 0!==m.min_bg&&(F=m.min_bg),void 0!==m.max_bg&&(T=m.max_bg),void 0===m.min_bg||void 0===m.max_bg)return h.error="Error: could not determine target_bg. ",h;I=(m.min_bg+m.max_bg)/2;var U=m.exercise_mode||m.high_temptarget_raises_sensitivity,R=100;if(m.half_basal_exercise_target)var D=m.half_basal_exercise_target;else D=160;if(U&&m.temptargetSet&&I>R||m.low_temptarget_lowers_sensitivity&&m.temptargetSet&&I<R){var j=D-R;j+I-R>0?(sensitivityRatio=j/(j+I-R),sensitivityRatio=Math.min(sensitivityRatio,m.autosens_max),sensitivityRatio=o(sensitivityRatio,2)):sensitivityRatio=m.autosens_max,process.stderr.write("Sensitivity ratio set to "+sensitivityRatio+" based on temp target of "+I+"; ")}else void 0!==d&&d&&(sensitivityRatio=d.ratio,process.stderr.write("Autosens ratio: "+sensitivityRatio+"; "));if(sensitivityRatio&&(B=m.current_basal*sensitivityRatio,(B=t(B,m))!==f?process.stderr.write("Adjusting basal from "+f+" to "+B+"; "):process.stderr.write("Basal unchanged: "+B+"; ")),m.temptargetSet);else if(void 0!==d&&d&&(m.sensitivity_raises_target&&d.ratio<1||m.resistance_lowers_target&&d.ratio>1)){F=o((F-60)/d.ratio)+60,T=o((T-60)/d.ratio)+60;var E=o((I-60)/d.ratio)+60;I===(E=Math.max(80,E))?process.stderr.write("target_bg unchanged: "+E+"; "):process.stderr.write("target_bg from "+I+" to "+E+"; "),I=E}if(e.noise>=2){var q=Math.max(1.1,m.noisyCGMTargetMultiplier),W=(Math.min(250,m.maxRaw),o(Math.min(200,F*q))),z=o(Math.min(200,I*q)),L=o(Math.min(200,T*q));process.stderr.write("Raising target_bg for noisy / raw CGM data, from "+I+" to "+z+"; "),F=W,I=z,T=L}var k=F-.5*(F-40),N=o(m.sens,1),P=m.sens;if(void 0!==d&&d&&((P=o(P=m.sens/sensitivityRatio,1))!==N?process.stderr.write("ISF from "+n(N,m)+" to "+n(P,m)):process.stderr.write("ISF unchanged: "+n(P,m)),i+="Autosense: Ratio "+sensitivityRatio+", ISF "+n(N,m)+" -> "+n(P,m)+"; "),console.error("CR:"+m.carb_ratio),P=function(e,r,a,t,s,m,d,u){if(!a.use_autoisf)return console.error("autoISF disabled in Preferences"),e;var c=t.autoISF_duration,g=t.autoISF_average,p=a.autoisf_max,b=!1;if(s.mealCOB>0&&!a.enableautoisf_with_COB)console.error("BG dependant autoISF by-passed; preferences disabled mealCOB of "+o(s.mealCOB,1));else{var h=1,v=1,f=r+10-g,B=1+l(100-f,a);if(console.error("bg_ISF adaptation is "+o(B,2)),p<B&&console.error("bg_ISF adaptation "+o(B,2)+" limited by autoisf_max "+p),B<1)return Math.min(720,o(a.sens/B,1));B>1&&(b=!0);var M=t.delta;f>0?console.error("delta_ISF adaptation by-passed as average glucose < "+n(r,a)+"+10"):t.short_avgdelta<0?console.error("delta_ISF adaptation by-passed as no rise or too short lived"):a.enableppisf_always||a.postmeal_ISF_duration>=(m-s.lastCarbTime)/1e3/3600?1!=(h=1+Math.max(0,M*a.postmeal_ISF_weight))&&(b=!0,p<h?console.error("pp_ISF adaptation"+o(h,2)+"limited by autoisf_max",p):console.error("pp_ISF adaptation is"+n(h,a))):(v=l(M,a),f>-20&&(v*=.5),1!=(v=1+v)&&(b=!0,p<v?console.error("delta_ISF adaptation "+o(v,2)+" limited by autoisf_max ",p):console.error("delta_ISF adaptation is "+n(v,a))))}var _=1,x=a.autoisf_hourlychange;s.mealCOB>0&&!a.enableautoisf_with_COB?console.error("autoISF by-passed; preferences disabled mealCOB of "+o(s.mealCOB,1)):c<10?console.error("autoISF by-passed; BG is only "+c+"m at level "+g):g<=r?console.error("autoISF by-passed; avg. glucose "+g+" below target "+n(r,a)):(_+=c/60*(x/r)*(g-r),b=!0,console.error("autoISF reports ISF "+n(e,a)+" did not do it for "+c+"m; go more aggressive by "+o(_,2)),p<_&&console.error("autoISF adaptation "+o(_,2)+" limited by autoisf_max "+p));if(b){var y=Math.max(Math.min(p,Math.max(_,B,v,h)),u);e=o(a.sens/y,1),i+="autoISF -> "+n(e,a)+", Ratio "+o(y,2)+"; "}return e}(P,I,m,e,u,b,0,sensitivityRatio),void 0===a)return h.error="Error: iob_data undefined. ",h;var Z,$=a;if(a.length,a.length>1&&(a=$[0]),void 0===a.activity||void 0===a.iob)return h.error="Error: iob_data missing some property. ",h;var H=((Z=void 0!==a.lastTemp?o((new Date(M).getTime()-a.lastTemp.date)/6e4):0)+r.duration)%30;if(console.error("currenttemp:",r,"lastTempAge:",Z,"m","tempModulus:",H,"m"),h.temp="absolute",h.deliverAt=v,g&&r&&a.lastTemp&&r.rate!==a.lastTemp.rate&&Z>10&&r.duration)return h.reason="Warning: currenttemp rate "+r.rate+" != lastTemp rate "+a.lastTemp.rate+" from pumphistory; canceling temp",c.setTempBasal(0,0,m,h,r);if(r&&a.lastTemp&&r.duration>0){var J=Z-a.lastTemp.duration;if(J>5&&Z>10)return h.reason="Warning: currenttemp running but lastTemp from pumphistory ended "+J+"m ago; canceling temp",c.setTempBasal(0,0,m,h,r)}var K=o(-a.activity*P*5,2),Q=o(6*(G-K));if(Q<0&&(Q=o(6*(w-K)))<0&&(Q=o(6*(e.long_avgdelta-K))),a.iob>0)var V=o(S-a.iob*P);else V=o(S-a.iob*Math.min(P,m.sens));var X=V+Q;if(void 0===X||isNaN(X))return h.error="Error: could not calculate eventualBG. ",h;var Y=function(e,r,a){return o(a+(e-r)/24,1)}(I,X,K);h={temp:"absolute",bg:S,tick:_,eventualBG:X,insulinReq:0,reservoir:p,deliverAt:v,sensitivityRatio};var ee=[],re=[],ae=[],te=[];ee.push(S),re.push(S),te.push(S),ae.push(S);var oe=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)}(m,g,u,I),ne=m.enableUAM,ie=0,se=0;ie=o(G-K,1);var le=o(G-K,1);csf=P/m.carb_ratio,console.error("profile.sens:"+n(m.sens,m)+", sens:"+n(P,m)+", CSF:"+o(csf,1));var me=o(30*csf*5/60,1);ie>me&&(console.error("Limiting carb impact from",ie,"to",me,"mg/dL/5m (",30,"g/h )"),ie=me);var de=3;sensitivityRatio&&(de/=sensitivityRatio);var ue=de;if(u.carbs){de=Math.max(de,u.mealCOB/20);var ce=o((new Date(M).getTime()-u.lastCarbTime)/6e4),ge=(u.carbs-u.mealCOB)/u.carbs;ue=o(ue=de+1.5*ce/60,1),console.error("Last carbs",ce,"minutes ago; remainingCATime:",ue,"hours;",o(100*ge)+"% carbs absorbed")}var pe=Math.max(0,ie/5*60*ue/2)/csf,be=90,he=1;m.remainingCarbsCap&&(be=Math.min(90,m.remainingCarbsCap)),m.remainingCarbsFraction&&(he=Math.min(1,m.remainingCarbsFraction));var ve=1-he,fe=Math.max(0,u.mealCOB-pe-u.carbs*ve),Be=(fe=Math.min(be,fe))*csf*5/60/(ue/2),Me=o(u.slopeFromMaxDeviation,2),_e=o(u.slopeFromMinDeviation,2),xe=Math.min(Me,-_e/3),ye=0;0===ie?se=0:!0===m.floating_carbs?(se=Math.min(60*ue/5/2,Math.max(0,u.carbs*csf/ie)),ye=Math.min(60*ue/5/2,Math.max(0,u.mealCOB*csf/ie)),u.carbs>0&&(i+="Floating Carbs CID:"+o(se,1)+" / MealCarbs:"+o(u.carbs,1)+" vs. Not Floating:"+o(ye,1)+" / MealCOB:"+o(u.mealCOB,1)+"; ",console.error("Floating Carbs CID:"+o(se,1)+" / MealCarbs:"+o(u.carbs,1)+" vs. Not Floating:"+o(ye,1)+" / MealCOB:"+o(u.mealCOB,1)))):se=Math.min(60*ue/5/2,Math.max(0,u.mealCOB*csf/ie)),console.error("Carb Impact:",ie,"mg/dL per 5m; CI Duration:",o(5*se/60*2,1),"hours; remaining CI (",ue/2,"h peak):",o(Be,1),"mg/dL per 5m");var Se,Ce,Ge,we,Oe,Ie=999,Fe=999,Te=999,Ae=S,Ue=999,Re=999,De=999,je=999,Ee=X,qe=S,We=S,ze=0,Le=[],ke=[];try{$.forEach((function(e){var r=o(-e.activity*P*5,2),a=o(-e.iobWithZeroTemp.activity*P*5,2),t=ie*(1-Math.min(1,re.length/12));Ee=re[re.length-1]+r+t;var n=te[te.length-1]+a,i=Math.max(0,Math.max(0,ie)*(1-ee.length/Math.max(2*se,1))),s=Math.min(ee.length,12*ue-ee.length),l=Math.max(0,s/(ue/2*12)*Be);i+l,Le.push(o(l,0)),ke.push(o(i,0)),COBpredBG=ee[ee.length-1]+r+Math.min(0,t)+i+l;var m=Math.max(0,le+ae.length*xe),d=Math.max(0,le*(1-ae.length/Math.max(36,1))),u=Math.min(m,d);u>0&&(ze=o(5*(ae.length+1)/60,1)),UAMpredBG=ae[ae.length-1]+r+Math.min(0,t)+u,re.length<48&&re.push(Ee),ee.length<48&&ee.push(COBpredBG),ae.length<48&&ae.push(UAMpredBG),te.length<48&&te.push(n),COBpredBG<Ue&&(Ue=o(COBpredBG)),UAMpredBG<Re&&(Re=o(UAMpredBG)),Ee<De&&(De=o(Ee)),n<je&&(je=o(n));re.length>18&&Ee<Ie&&(Ie=o(Ee)),Ee>qe&&(qe=Ee),(se||Be>0)&&ee.length>18&&COBpredBG<Fe&&(Fe=o(COBpredBG)),(se||Be>0)&&COBpredBG>qe&&(We=COBpredBG),ne&&ae.length>12&&UAMpredBG<Te&&(Te=o(UAMpredBG)),ne&&UAMpredBG>qe&&UAMpredBG}))}catch(e){console.error("Problem with iobArray. Optional feature Advanced Meal Assist disabled")}u.mealCOB&&(console.error("predCIs (mg/dL/5m):",ke.join(" ")),console.error("remainingCIs: ",Le.join(" "))),h.predBGs={},re.forEach((function(e,r,a){a[r]=o(Math.min(401,Math.max(39,e)))}));for(var Ne=re.length-1;Ne>12&&re[Ne-1]===re[Ne];Ne--)re.pop();for(h.predBGs.IOB=re,Ge=o(re[re.length-1]),te.forEach((function(e,r,a){a[r]=o(Math.min(401,Math.max(39,e)))})),Ne=te.length-1;Ne>6&&!(te[Ne-1]>=te[Ne]||te[Ne]<=I);Ne--)te.pop();if(h.predBGs.ZT=te,o(te[te.length-1]),u.mealCOB>0&&(ie>0||Be>0)){for(ee.forEach((function(e,r,a){a[r]=o(Math.min(401,Math.max(39,e)))})),Ne=ee.length-1;Ne>12&&ee[Ne-1]===ee[Ne];Ne--)ee.pop();h.predBGs.COB=ee,we=o(ee[ee.length-1]),X=Math.max(X,o(ee[ee.length-1]))}if(ie>0||Be>0){if(ne){for(ae.forEach((function(e,r,a){a[r]=o(Math.min(401,Math.max(39,e)))})),Ne=ae.length-1;Ne>12&&ae[Ne-1]===ae[Ne];Ne--)ae.pop();h.predBGs.UAM=ae,Oe=o(ae[ae.length-1]),ae[ae.length-1]&&(X=Math.max(X,o(ae[ae.length-1])))}h.eventualBG=X}console.error("UAM Impact:",le,"mg/dL per 5m; UAM Duration:",ze,"hours"),Ie=Math.max(39,Ie),Fe=Math.max(39,Fe),Te=Math.max(39,Te),Se=o(Ie);var Pe=u.mealCOB/u.carbs;Ce=o(Te<999&&Fe<999?(1-Pe)*UAMpredBG+Pe*COBpredBG:Fe<999?(Ee+COBpredBG)/2:Te<999?(Ee+UAMpredBG)/2:Ee),je>Ce&&(Ce=je),Ae=o(Ae=se||Be>0?ne?Pe*Ue+(1-Pe)*Re:Ue:ne?Re:De);var Ze=Te;if(je<k)Ze=(Te+je)/2;else if(je<I){var $e=(je-k)/(I-k);Ze=(Te+(Te*$e+je*(1-$e)))/2}else je>Te&&(Ze=(Te+je)/2);if(Ze=o(Ze),u.carbs)if(!ne&&Fe<999)Se=o(Math.max(Ie,Fe));else if(Fe<999){var He=Pe*Fe+(1-Pe)*Ze;Se=o(Math.max(Ie,Fe,He))}else Se=ne?Ze:Ae;else ne&&(Se=o(Math.max(Ie,Ze)));Se=Math.min(Se,Ce),process.stderr.write("minPredBG: "+Se+" minIOBPredBG: "+Ie+" minZTGuardBG: "+je),Fe<999&&process.stderr.write(" minCOBPredBG: "+Fe),Te<999&&process.stderr.write(" minUAMPredBG: "+Te),console.error(" avgPredBG:",Ce,"COB:",u.mealCOB,"/",u.carbs),We>S&&(Se=Math.min(Se,We)),h.COB=u.mealCOB,h.IOB=a.iob,h.BGI=K,h.deviation=n(Q,m),h.ISF=n(P,m),h.CR=o(m.carb_ratio,2),h.target_bg=n(I,m),h.reason=i+"minPredBG "+n(Se,m)+", minGuardBG "+n(Ae,m)+", IOBpredBG "+n(Ge,m),we>0&&(h.reason+=", COBpredBG "+n(we,m)),Oe>0&&(h.reason+=", UAMpredBG "+n(Oe,m)),h.reason+="; ";var Je=V;Je<40&&(Je=Math.min(Ae,Je));var Ke,Qe=k-Je,Ve=240,Xe=240;if(u.mealCOB>0&&(ie>0||Be>0)){for(Ne=0;Ne<ee.length;Ne++)if(ee[Ne]<F){Ve=5*Ne;break}for(Ne=0;Ne<ee.length;Ne++)if(ee[Ne]<k){Xe=5*Ne;break}}else{for(Ne=0;Ne<re.length;Ne++)if(re[Ne]<F){Ve=5*Ne;break}for(Ne=0;Ne<re.length;Ne++)if(re[Ne]<k){Xe=5*Ne;break}}oe&&Ae<k&&(console.error("minGuardBG",n(Ae,m),"projected below",n(k,m),"- disabling SMB"),oe=!1),void 0===m.maxDelta_bg_threshold&&(Ke=.2),void 0!==m.maxDelta_bg_threshold&&(Ke=Math.min(m.maxDelta_bg_threshold,.3)),O>Ke*S&&(console.error("maxDelta "+n(O,m)+" > "+100*Ke+"% of BG "+n(S,m)+" - disabling SMB"),h.reason+="maxDelta "+n(O,m)+" > "+100*Ke+"% of BG "+n(S,m)+": SMB disabled; ",oe=!1),console.error("BG projected to remain above",n(F,m),"for",Ve,"minutes"),(Xe<240||Ve<60)&&console.error("BG projected to remain above",n(k,m),"for",Xe,"minutes");var Ye=Xe,er=m.current_basal*P*Ye/60,rr=Math.max(0,u.mealCOB-.25*u.carbs),ar=(Qe-er)/csf-rr;if(er=o(er),ar=o(ar),console.error("naive_eventualBG:",V,"bgUndershoot:",Qe,"zeroTempDuration:",Ye,"zeroTempEffect:",er,"carbsReq:",ar),ar>=m.carbsReqThreshold&&Xe<=45&&(h.carbsReq=ar,h.reason+=ar+" add'l carbs req w/in "+Xe+"m; "),S<k&&a.iob<20*-m.current_basal/60&&G>0&&G>Y)h.reason+="IOB "+a.iob+" < "+o(20*-m.current_basal/60,2),h.reason+=" and minDelta "+n(G,m)+" > expectedDelta "+n(Y,m)+"; ";else if(S<k||Ae<k){h.reason+="minGuardBG "+n(Ae,m)+"<"+n(k,m);var tr=(Qe=I-Ae)/P,or=o(60*tr/m.current_basal);return or=30*o(or/30),or=Math.min(120,Math.max(30,or)),c.setTempBasal(0,or,m,h,r)}if(m.skip_neutral_temps&&h.deliverAt.getMinutes()>=55)return h.reason+="; Canceling temp at "+h.deliverAt.getMinutes()+"m past the hour. ",c.setTempBasal(0,0,m,h,r);if(X<F){if(h.reason+="Eventual BG "+n(X,m)+" < "+n(F,m),G>Y&&G>0&&!ar)return V<40?(h.reason+=", naive_eventualBG < 40. ",c.setTempBasal(0,30,m,h,r)):(e.delta>G?h.reason+=", but Delta "+n(_,m)+" > expectedDelta "+n(Y,m):h.reason+=", but Min. Delta "+G.toFixed(2)+" > Exp. Delta "+n(Y,m),r.duration>15&&t(B,m)===t(r.rate,m)?(h.reason+=", temp "+r.rate+" ~ req "+B+"U/hr. ",h):(h.reason+="; setting current basal of "+B+" as temp. ",c.setTempBasal(B,30,m,h,r)));var nr=2*Math.min(0,(X-I)/P);nr=o(nr,2);var ir=Math.min(0,(V-I)/P);if(ir=o(ir,2),G<0&&G>Y)nr=o(nr*(G/Y),2);var sr=B+2*nr;sr=t(sr,m);var lr=r.duration*(r.rate-B)/60;if(lr<Math.min(nr,ir)-.3*B)return h.reason+=", "+r.duration+"m@"+r.rate.toFixed(2)+" is a lot less than needed. ",c.setTempBasal(sr,30,m,h,r);if(void 0!==r.rate&&r.duration>5&&sr>=.8*r.rate)return h.reason+=", temp "+r.rate+" ~< req "+sr+"U/hr. ",h;if(sr<=0){if((or=o(60*(tr=(Qe=I-V)/P)/m.current_basal))<0?or=0:(or=30*o(or/30),or=Math.min(120,Math.max(0,or))),or>0)return h.reason+=", setting "+or+"m zero temp. ",c.setTempBasal(sr,or,m,h,r)}else h.reason+=", setting "+sr+"U/hr. ";return c.setTempBasal(sr,30,m,h,r)}if(G<Y&&(!g||!oe))return e.delta<G?h.reason+="Eventual BG "+n(X,m)+" > "+n(F,m)+" but Delta "+n(_,m)+" < Exp. Delta "+n(Y,m):h.reason+="Eventual BG "+n(X,m)+" > "+n(F,m)+" but Min. Delta "+G.toFixed(2)+" < Exp. Delta "+n(Y,m),r.duration>15&&t(B,m)===t(r.rate,m)?(h.reason+=", temp "+r.rate+" ~ req "+B+"U/hr. ",h):(h.reason+="; setting current basal of "+B+" as temp. ",c.setTempBasal(B,30,m,h,r));if(Math.min(X,Se)<T&&(!g||!oe))return h.reason+=n(X,m)+"-"+n(Se,m)+" in range: no temp required",r.duration>15&&t(B,m)===t(r.rate,m)?(h.reason+=", temp "+r.rate+" ~ req "+B+"U/hr. ",h):(h.reason+="; setting current basal of "+B+" as temp. ",c.setTempBasal(B,30,m,h,r));if(X>=T&&(h.reason+="Eventual BG "+n(X,m)+" >= "+n(T,m)+", "),a.iob>A)return h.reason+="IOB "+o(a.iob,2)+" > max_iob "+A,r.duration>15&&t(B,m)===t(r.rate,m)?(h.reason+=", temp "+r.rate+" ~ req "+B+"U/hr. ",h):(h.reason+="; setting current basal of "+B+" as temp. ",c.setTempBasal(B,30,m,h,r));(nr=o((Math.min(Se,X)-I)/P,2))>A-a.iob&&(h.reason+="max_iob "+A+", ",nr=A-a.iob),sr=t(sr=B+2*nr,m),nr=o(nr,3),h.insulinReq=nr;var mr=o((new Date(M).getTime()-a.lastBolusTime)/6e4,1);if(g&&oe&&S>k){var dr=o(u.mealCOB/m.carb_ratio,3);if(m.use_autoisf)ur=m.smb_max_range_extension;else{console.error("autoISF disabled, SMB range extension disabled");var ur=1}if(ur>1&&console.error("SMB max range extended from default by factor "+ur),void 0===m.maxSMBBasalMinutes){var cr=o(ur*m.current_basal*30/60,1);console.error("profile.maxSMBBasalMinutes undefined: defaulting to 30m")}else a.iob>dr&&a.iob>0?(console.error("IOB",a.iob,"> COB",u.mealCOB+"; mealInsulinReq =",dr),m.maxUAMSMBBasalMinutes?(console.error("profile.maxUAMSMBBasalMinutes:",m.maxUAMSMBBasalMinutes,"profile.current_basal:",m.current_basal),cr=o(ur*m.current_basal*m.maxUAMSMBBasalMinutes/60,1)):(console.error("profile.maxUAMSMBBasalMinutes undefined: defaulting to 30m"),cr=o(30*m.current_basal/60,1))):(console.error("profile.maxSMBBasalMinutes:",m.maxSMBBasalMinutes,"profile.current_basal:",m.current_basal),cr=o(ur*m.current_basal*m.maxSMBBasalMinutes/60,1));var gr=m.bolus_increment,pr=1/gr;if(m.use_autoisf){var br=function(e,r,a){if(void 0===e.smb_delivery_ratio_bg_range||0===e.smb_delivery_ratio_bg_range)return console.error("SMB delivery ratio set to fixed value "+e.smb_delivery_ratio),e.smb_delivery_ratio;var t=Math.min(e.smb_delivery_ratio_min,e.smb_delivery_ratio_max);if(r<=a)return console.error("SMB delivery ratio limited by minimum value "+t),t;var n=Math.max(e.smb_delivery_ratio_min,e.smb_delivery_ratio_max);if(r>=a+e.smb_delivery_ratio_bg_range)return console.error("SMB delivery ratio limited by maximum value "+n),n;var i=t+(n-t)*(r-a)/e.smb_delivery_ratio_bg_range;return console.error("SMB delivery ratio set to interpolated value "+o(i,2)),i}(m,S,I);s="SMB Delivery Ratio "+o(br,2)+", ",h.reason+=s}else console.error("autoISF disabled, don't adjust SMB Delivery Ratio"),br=.5;br>.5&&console.error("SMB Delivery Ratio increased from default 0.5 to "+o(br,2));var hr=Math.min(nr*br,cr);hr=Math.floor(hr*pr)/pr,or=o(60*(tr=(I-(V+Ie)/2)/P)/m.current_basal),nr>0&&hr<gr&&(or=0);var vr=0;or<=0?or=0:or>=30?(or=30*o(or/30),or=Math.min(60,Math.max(0,or))):(vr=o(B*or/30,2),or=30),h.reason+=" insulinReq "+nr,hr>=cr&&(h.reason+="; maxBolus "+cr),or>0&&(h.reason+="; setting "+or+"m low temp of "+vr+"U/h"),h.reason+=". ";var fr=3;m.SMBInterval&&(fr=Math.min(10,Math.max(1,m.SMBInterval)));var Br=o(fr-mr,0),Mr=o(60*(fr-mr),0)%60;if(console.error("naive_eventualBG",V+",",or+"m "+vr+"U/h temp needed; last bolus",mr+"m ago; maxBolus: "+cr),mr>fr?hr>0&&(h.units=hr,h.reason+="Microbolusing "+hr+"U. "):h.reason+="Waiting "+Br+"m "+Mr+"s to microbolus again. ",or>0)return h.rate=vr,h.duration=or,h}var _r=c.getMaxSafeBasal(m);return sr>_r&&(h.reason+="adj. req. rate: "+sr+" to maxSafeBasal: "+_r+", ",sr=t(_r,m)),(lr=r.duration*(r.rate-B)/60)>=2*nr?(h.reason+=r.duration+"m@"+r.rate.toFixed(2)+" > 2 * insulinReq. Setting temp basal of "+sr+"U/hr. ",c.setTempBasal(sr,30,m,h,r)):void 0===r.duration||0===r.duration?(h.reason+="no temp, setting "+sr+"U/hr. ",c.setTempBasal(sr,30,m,h,r)):r.duration>5&&t(sr,m)<=t(r.rate,m)?(h.reason+="temp "+r.rate+" >~ req "+sr+"U/hr. ",h):(h.reason+="temp "+r.rate+"<"+sr+"U/hr. ",c.setTempBasal(sr,30,m,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),i=t?t.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,r,a)=>{var t=a(2705),o=a(9932),n=a(1469),i=a(3448),s=t?t.prototype:void 0,l=s?s.toString:void 0;e.exports=function e(r){if("string"==typeof r)return r;if(n(r))return o(r,e)+"";if(i(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,i=o.toString,s=t?t.toStringTag:void 0;e.exports=function(e){var r=n.call(e,s),a=e[s];try{e[s]=void 0;var t=!0}catch(e){}var o=i.call(e);return t&&(r?e[s]=a:delete e[s]),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),i=a(9833);e.exports=function(e,r,a){e=i(e),r=o(r);var s=e.length,l=a=void 0===a?s:t(n(a),0,s);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),i=/^[-+]0x[0-9a-f]+$/i,s=/^0b[01]+$/i,l=/^0o[0-7]+$/i,m=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=s.test(e);return a||l.test(e)?m(e.slice(2),a?2:8):i.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})();