determine-basal.js 26 KB

1
  1. var freeaps_determineBasal;(()=>{var e={5546:(e,a,r)=>{var t=r(6880);function o(e,a){a||(a=0);var r=Math.pow(10,a);return Math.round(e*r)/r}function i(e,a){return"mmol/L"===a.out_units?o(e/18,1):Math.round(e)}var n="",s="",l="",m="",d="";function u(e,a){var r=[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=r.length-1,i=r[0],n=t[0],s=r[o],l=t[o],m=1,d=1,u=1,c=i;if(i>e)m=(d=n)+((l=t[1])-d)/((s=r[1])-(u=i))*(e-u);else if(s<e)m=(d=n=t[o-1])+(l-d)/(s-(u=i=r[o-1]))*(e-u);else for(var g=0;g<=o;g++){if(n=t[g],(i=r[g])==e){m=n;break}if(i>e){m=d+(n-d)/(i-(u=c))*(e-u);break}d=n,c=i}return m*=e>100?a.higher_ISFrange_weight:e>40?a.lower_ISFrange_weight:a.delta_ISFrange_weight}function c(e,a,r){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(a<=r)return console.error("SMB delivery ratio limited by minimum value "+t),t;var i=Math.max(e.smb_delivery_ratio_min,e.smb_delivery_ratio_max);if(a>=r+e.smb_delivery_ratio_bg_range)return console.error("SMB delivery ratio limited by maximum value "+i),i;var n=t+(i-t)*(a-r)/e.smb_delivery_ratio_bg_range;return console.error("SMB delivery ratio set to interpolated value "+o(n,2)),n}e.exports=function(e,a,r,g,b,f,p,v,h,_){var B={},M=new Date;if(_&&(M=_),void 0===g||void 0===g.current_basal)return B.error="Error: could not get current basal rate",B;var x=t(g.current_basal,g),y=x,S=new Date;_&&(S=_);var C,G=new Date(e.date),w=o((S-G)/60/1e3,1),I=e.glucose,F=e.noise;C=e.delta>-.5?"+"+o(e.delta,0):o(e.delta,0);var O=Math.min(e.delta,e.short_avgdelta),A=Math.min(e.short_avgdelta,e.long_avgdelta),T=Math.max(e.delta,e.short_avgdelta,e.long_avgdelta);(I<=10||38===I||F>=3)&&(B.reason="CGM is calibrating, in ??? state, or noise is high");if(I>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 ("+i(I,g)+"+"+i(e.delta,g)+") for 5m w/ "+i(e.short_avgdelta,g)+" mg/dL ~15m change & "+i(e.long_avgdelta,2)+" mg/dL ~45m change"),console.error("Simulator mode detected ("+e.device+"): continuing anyway")):!0),w>12||w<-5?B.reason="If current system time "+S+" is correct, then BG data is too old. The last BG data was read "+w+"m ago at "+G:0===e.short_avgdelta&&0===e.long_avgdelta&&(e.last_cal&&e.last_cal<3?B.reason="CGM was just calibrated":B.reason="CGM data is unchanged ("+i(I,g)+"+"+i(e.delta,g)+") for 5m w/ "+i(e.short_avgdelta,g)+" mg/dL ~15m change & "+i(e.long_avgdelta,g)+" mg/dL ~45m change"),I<=10||38===I||F>=3||w>12||w<-5||0===e.short_avgdelta&&0===e.long_avgdelta)return a.rate>=y?(B.reason+=". Canceling high temp basal of "+a.rate,B.deliverAt=M,B.temp="absolute",B.duration=0,B.rate=0,B):0===a.rate&&a.duration>30?(B.reason+=". Shortening "+a.duration+"m long zero temp to 30m. ",B.deliverAt=M,B.temp="absolute",B.duration=30,B.rate=0,B):(B.reason+=". Temp "+a.rate+" <= current basal "+y+"U/hr; doing nothing. ",B);var R,D,U,j=g.max_iob;if(void 0!==g.min_bg&&(D=g.min_bg),void 0!==g.max_bg&&(U=g.max_bg),void 0===g.min_bg||void 0===g.max_bg)return B.error="Error: could not determine target_bg. ",B;R=(g.min_bg+g.max_bg)/2;var P=g.exercise_mode||g.high_temptarget_raises_sensitivity,q=100;if(g.half_basal_exercise_target)var E=g.half_basal_exercise_target;else E=160;if(P&&g.temptargetSet&&R>q||g.low_temptarget_lowers_sensitivity&&g.temptargetSet&&R<q){var W=E-q;W+R-q>0?(sensitivityRatio=W/(W+R-q),sensitivityRatio=Math.min(sensitivityRatio,g.autosens_max),sensitivityRatio=o(sensitivityRatio,2)):sensitivityRatio=g.autosens_max,process.stderr.write("Sensitivity ratio set to "+sensitivityRatio+" based on temp target of "+R+"; ")}else void 0!==b&&b&&(sensitivityRatio=b.ratio,process.stderr.write("Autosens ratio: "+sensitivityRatio+"; "));if(sensitivityRatio&&(y=g.current_basal*sensitivityRatio,(y=t(y,g))!==x?process.stderr.write("Adjusting basal from "+x+" to "+y+"; "):process.stderr.write("Basal unchanged: "+y+"; ")),g.temptargetSet);else if(void 0!==b&&b&&(g.sensitivity_raises_target&&b.ratio<1||g.resistance_lowers_target&&b.ratio>1)){D=o((D-60)/b.ratio)+60,U=o((U-60)/b.ratio)+60;var k=o((R-60)/b.ratio)+60;R===(k=Math.max(80,k))?process.stderr.write("target_bg unchanged: "+k+"; "):process.stderr.write("target_bg from "+R+" to "+k+"; "),R=k}if(e.noise>=2){var z=Math.max(1.1,g.noisyCGMTargetMultiplier),L=(Math.min(250,g.maxRaw),o(Math.min(200,D*z))),N=o(Math.min(200,R*z)),Z=o(Math.min(200,U*z));process.stderr.write("Raising target_bg for noisy / raw CGM data, from "+R+" to "+N+"; "),D=L,R=N,U=Z}var $=D-.5*(D-40),H=o(g.sens,1),J=g.sens;if(void 0!==b&&b&&((J=o(J=g.sens/sensitivityRatio,1))!==H?process.stderr.write("ISF from "+i(H,g)+" to "+i(J,g)):process.stderr.write("ISF unchanged: "+i(J,g)),n+="Autosense, Ratio: "+sensitivityRatio+", ISF: "+i(H,g)+"→"+i(J,g)+m),console.error("CR:"+g.carb_ratio),J=function(e,a,r,t,g,b,f,p){if(!r.use_autoisf)return console.error("autoISF disabled in Preferences"),e;var v=t.autoISF_duration,h=t.autoISF_average,_=t.dura_p,B=t.delta_pl,M=t.delta_pn,x=t.r_squ,y=t.bg_acceleration,S=t.parabola_fit_a0,C=t.parabola_fit_a1,G=t.parabola_fit_a2,w=t.pp_debug;s+="BG-accel: "+o(y,3)+", PF-minutes: "+_+", PF-corr: "+o(x,3)+", PF-nextDelta: "+i(M,r)+", PF-lastDelta: "+i(B,r)+", regular Delta: "+i(t.delta,r),console.error(w+s+" , Weights Accel/Brake: "+r.bgAccel_ISF_weight+" / "+r.bgBrake_ISF_weight);var I=r.autoisf_max,F=!1;if(g.mealCOB>0&&!r.enableautoisf_with_COB)console.error("BG dependant autoISF by-passed; preferences disabled mealCOB of "+o(g.mealCOB,1));else{var O=1,A=1,T=a+10-h,R=1;if(r.enable_BG_acceleration){var D=y,U=-C/2/G*5,j=o(S-U*U/25*G,1);(U=o(U,1))<0&&D<0?(d="saw max of "+i(j,r)+", about "+-U+" min ago",console.error("Parabolic fit "+d)):U<0&&D>0?(d="saw min of "+i(j,r)+", about "+-U+" min ago",console.error("Parabolic fit "+d)):U>0&&D<0?(d="predicts max of "+i(j,r)+", in about "+U+"min",console.error("Parabolic fit "+d)):U>0&&D>0&&(d="predicts min of "+i(j,r)+", in about "+U+" min",console.error("Parabolic fit "+d));if(x<.9)d="by-passed accel_ISF, as correlation "+o(x,3)+" is too low",console.error("Parabolic fit "+d),l+=", Parabolic Fit, "+d;else{l+=", Parabolic Fit, "+d+", regΔ: "+i(t.delta,r)+", lastΔ: "+i(B,r)+", nextΔ: "+i(M,r)+", Corr "+o(x,3)+", BG-Accel: "+o(y,2);var P=1;if(t.glucose<r.target_bg&&D<1&&(P=.5),D<0)var q=r.bgBrake_ISF_weight;else q=r.bgAccel_ISF_weight;R=1+D*P*q,console.error("Original result for acce_ISF: "+o(R,2)),1!=R&&(F=!0),I<R?console.error("acce_ISF adaptation "+o(R,2)+" limited by autoisf_max = "+I):r.autoisf_min>R?(console.error("acce_ISF adaptation "+o(R,2)+" limited by autoisf_min = "+r.autoisf_min),R=r.autoisf_min):console.error("acce_ISF adaptation is "+o(R,2)),l+=", accel. Ratio: "+o(R,2)}}else console.error("autoISF BG accelertion adaption disabled in Preferences");var E=c(r,t.glucose,a);n+=", SMB Delivery Ratio:, "+o(E,2);var W=1+u(100-T,r);if(console.error("1. bg_ISF adaptation is "+o(W,2)),I<W&&(m=", bg_ISF adaptation "+o(W,2)+", limited by autoisf_max "+I,console.error(m)),W<1)return R>1&&(m=", bg_ISF adaptation lifted to "+o(W*=R,2)+", as bg accelerates already",console.error(m)),W<r.autoisf_min&&(m=", bg_ISF adaptation "+o(W,2)+" limited by autoisf_min "+r.autoisf_min,console.error(m)),e=Math.min(720,o(r.sens/Math.max(r.autoisf_min,W),1)),console.error("early Return autoISF: "+i(e,r)),e;W>1&&(F=!0);var k=t.delta;T>0?console.error("delta_ISF adaptation by-passed as average glucose < "+i(a+10,r)):t.short_avgdelta<0?console.error("delta_ISF adaptation by-passed as no rise or too short lived"):r.enableppisf_always||r.postmeal_ISF_duration>=(b-g.lastCarbTime)/1e3/3600?1!=(O=1+Math.max(0,k*r.postmeal_ISF_weight))&&(F=!0,I<O?console.error("pp_ISF adaptation"+i(O,r)+"limited by autoisf_max",I):console.error("pp_ISF adaptation is"+i(O,r))):(A=u(k,r),T>-20&&(A*=.5),1!=(A=1+A)&&(F=!0,I<A?console.error("delta_ISF adaptation "+i(A,r)+" limited by autoisf_max ",I):console.error("delta_ISF adaptation is "+i(A,r))))}var z=1,L=r.autoisf_hourlychange;g.mealCOB>0&&!r.enableautoisf_with_COB?console.error("autoISF by-passed; preferences disabled mealCOB of "+o(g.mealCOB,1)):v<10?console.error("autoISF by-passed; BG is only "+v+"m at level "+h):h<=a?console.error("autoISF by-passed; avg. glucose "+h+" below target "+i(a,r)):(z+=v/60*(L/a)*(h-a),F=!0,console.error("autoISF reports ISF "+i(e,r)+" did not do it for "+v+"m; go more aggressive by "+o(z,2)),I<z&&console.error("autoISF adaptation "+o(z,2)+" limited by autoisf_max "+I));if(F){var N=Math.max(Math.min(I,Math.max(z,W,A,R,O)),p);R<1&&N>1&&(N*=R,console.error("strongest ISF weakened by factor "+o(R,2)+" as bg decelerates already")),e=o(r.sens/N,1),n+=", autoISF, Duration: "+v+", Avg: "+i(h,r)+", Ratio: "+o(N,2)+", ISF: "+i(e,r)+l}return console.error("Inside autoISF result "+i(e,r)),e}(J,R,g,e,f,_,0,sensitivityRatio),void 0===r)return B.error="Error: iob_data undefined. ",B;var K,Q=r;if(r.length,r.length>1&&(r=Q[0]),void 0===r.activity||void 0===r.iob)return B.error="Error: iob_data missing some property. ",B;var V=((K=void 0!==r.lastTemp?o((new Date(S).getTime()-r.lastTemp.date)/6e4):0)+a.duration)%30;if(console.error("currenttemp:"+a.rate+" lastTempAge:"+K+"m, tempModulus:"+V+"m"),B.temp="absolute",B.deliverAt=M,v&&a&&r.lastTemp&&a.rate!==r.lastTemp.rate&&K>10&&a.duration)return B.reason="Warning: currenttemp rate "+a.rate+" != lastTemp rate "+r.lastTemp.rate+" from pumphistory; canceling temp",p.setTempBasal(0,0,g,B,a);if(a&&r.lastTemp&&a.duration>0){var X=K-r.lastTemp.duration;if(X>5&&K>10)return B.reason="Warning: currenttemp running but lastTemp from pumphistory ended "+X+"m ago; canceling temp",p.setTempBasal(0,0,g,B,a)}var Y=o(-r.activity*J*5,2),ee=o(6*(O-Y));if(ee<0&&(ee=o(6*(A-Y)))<0&&(ee=o(6*(e.long_avgdelta-Y))),r.iob>0)var ae=o(I-r.iob*J);else ae=o(I-r.iob*Math.min(J,g.sens));var re=ae+ee;if(void 0===re||isNaN(re))return B.error="Error: could not calculate eventualBG. Sensitivity: "+J+" Deviation: "+ee,B;var te=function(e,a,r){return o(r+(e-a)/24,1)}(R,re,Y);B={temp:"absolute",bg:I,tick:C,eventualBG:re,insulinReq:0,reservoir:h,deliverAt:M,sensitivityRatio};var oe=[],ie=[],ne=[],se=[];oe.push(I),ie.push(I),se.push(I),ne.push(I);var le=function(e,a,r,t){return a?!e.allowSMB_with_high_temptarget&&e.temptargetSet&&t>100?(console.error("SMB disabled due to high temptarget of",t),!1):!0===r.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?(r.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&&r.mealCOB?(r.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",r.mealCOB),!0):!0===e.enableSMB_after_carbs&&r.carbs?(r.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?(r.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",i(t,e)),!0):(console.error("SMB disabled (no enableSMB preferences active or no condition satisfied)"),!1):(console.error("SMB disabled (!microBolusAllowed)"),!1)}(g,v,f,R),me=g.enableUAM,de=0,ue=0;de=o(O-Y,1);var ce=o(O-Y,1);csf=J/g.carb_ratio,console.error("profile.sens:"+i(g.sens,g)+", sens:"+i(J,g)+", CSF:"+o(csf,1));var ge=o(30*csf*5/60,1);de>ge&&(console.error("Limiting carb impact from "+de+" to "+ge+"mg/dL/5m (30g/h)"),de=ge);var be=3;sensitivityRatio&&(be/=sensitivityRatio);var fe=be;if(f.carbs){be=Math.max(be,f.mealCOB/20);var pe=o((new Date(S).getTime()-f.lastCarbTime)/6e4),ve=(f.carbs-f.mealCOB)/f.carbs;fe=o(fe=be+1.5*pe/60,1),console.error("Last carbs "+pe+" minutes ago; remainingCATime:"+fe+"hours; "+o(100*ve)+"% carbs absorbed")}var he=Math.max(0,de/5*60*fe/2)/csf,_e=90,Be=1;g.remainingCarbsCap&&(_e=Math.min(90,g.remainingCarbsCap)),g.remainingCarbsFraction&&(Be=Math.min(1,g.remainingCarbsFraction));var Me=1-Be,xe=Math.max(0,f.mealCOB-he-f.carbs*Me),ye=(xe=Math.min(_e,xe))*csf*5/60/(fe/2),Se=o(f.slopeFromMaxDeviation,2),Ce=o(f.slopeFromMinDeviation,2),Ge=Math.min(Se,-Ce/3),we=0;0===de?ue=0:!0===g.floating_carbs?(ue=Math.min(60*fe/5/2,Math.max(0,f.carbs*csf/de)),we=Math.min(60*fe/5/2,Math.max(0,f.mealCOB*csf/de)),f.carbs>0&&(n+=", Floating Carbs:, CID: "+o(ue,1)+", MealCarbs: "+o(f.carbs,1)+", Not Floating:, CID: "+o(we,1)+", MealCOB: "+o(f.mealCOB,1),console.error("Floating Carbs CID: "+o(ue,1)+" / MealCarbs: "+o(f.carbs,1)+" vs. Not Floating:"+o(we,1)+" / MealCOB:"+o(f.mealCOB,1)))):ue=Math.min(60*fe/5/2,Math.max(0,f.mealCOB*csf/de)),console.error("Carb Impact:"+de+"mg/dL per 5m; CI Duration:"+o(5*ue/60*2,1)+"hours; remaining CI ("+fe/2+"h peak):",o(ye,1)+"mg/dL per 5m");var Ie,Fe,Oe,Ae,Te,Re=999,De=999,Ue=999,je=I,Pe=999,qe=999,Ee=999,We=999,ke=re,ze=I,Le=I,Ne=0,Ze=[],$e=[];try{Q.forEach((function(e){var a=o(-e.activity*J*5,2),r=o(-e.iobWithZeroTemp.activity*J*5,2),t=de*(1-Math.min(1,ie.length/12));ke=ie[ie.length-1]+a+t;var i=se[se.length-1]+r,n=Math.max(0,Math.max(0,de)*(1-oe.length/Math.max(2*ue,1))),s=Math.min(oe.length,12*fe-oe.length),l=Math.max(0,s/(fe/2*12)*ye);n+l,Ze.push(o(l,0)),$e.push(o(n,0)),COBpredBG=oe[oe.length-1]+a+Math.min(0,t)+n+l;var m=Math.max(0,ce+ne.length*Ge),d=Math.max(0,ce*(1-ne.length/Math.max(36,1))),u=Math.min(m,d);u>0&&(Ne=o(5*(ne.length+1)/60,1)),UAMpredBG=ne[ne.length-1]+a+Math.min(0,t)+u,ie.length<48&&ie.push(ke),oe.length<48&&oe.push(COBpredBG),ne.length<48&&ne.push(UAMpredBG),se.length<48&&se.push(i),COBpredBG<Pe&&(Pe=o(COBpredBG)),UAMpredBG<qe&&(qe=o(UAMpredBG)),ke<Ee&&(Ee=o(ke)),i<We&&(We=o(i));ie.length>18&&ke<Re&&(Re=o(ke)),ke>ze&&(ze=ke),(ue||ye>0)&&oe.length>18&&COBpredBG<De&&(De=o(COBpredBG)),(ue||ye>0)&&COBpredBG>ze&&(Le=COBpredBG),me&&ne.length>12&&UAMpredBG<Ue&&(Ue=o(UAMpredBG)),me&&UAMpredBG>ze&&UAMpredBG}))}catch(e){console.error("Problem with iobArray. Optional feature Advanced Meal Assist disabled")}f.mealCOB&&(console.error("predCIs (mg/dL/5m):"+$e.join(" ")),console.error("remainingCIs: "+Ze.join(" "))),B.predBGs={},ie.forEach((function(e,a,r){r[a]=o(Math.min(401,Math.max(39,e)))}));for(var He=ie.length-1;He>12&&ie[He-1]===ie[He];He--)ie.pop();for(B.predBGs.IOB=ie,Oe=o(ie[ie.length-1]),se.forEach((function(e,a,r){r[a]=o(Math.min(401,Math.max(39,e)))})),He=se.length-1;He>6&&!(se[He-1]>=se[He]||se[He]<=R);He--)se.pop();if(B.predBGs.ZT=se,o(se[se.length-1]),f.mealCOB>0&&(de>0||ye>0)){for(oe.forEach((function(e,a,r){r[a]=o(Math.min(401,Math.max(39,e)))})),He=oe.length-1;He>12&&oe[He-1]===oe[He];He--)oe.pop();B.predBGs.COB=oe,Ae=o(oe[oe.length-1]),re=Math.max(re,o(oe[oe.length-1]))}if(de>0||ye>0){if(me){for(ne.forEach((function(e,a,r){r[a]=o(Math.min(401,Math.max(39,e)))})),He=ne.length-1;He>12&&ne[He-1]===ne[He];He--)ne.pop();B.predBGs.UAM=ne,Te=o(ne[ne.length-1]),ne[ne.length-1]&&(re=Math.max(re,o(ne[ne.length-1])))}B.eventualBG=re}console.error("UAM Impact:"+ce+"mg/dL per 5m; UAM Duration:"+Ne+"hours"),Re=Math.max(39,Re),De=Math.max(39,De),Ue=Math.max(39,Ue),Ie=o(Re);var Je=f.mealCOB/f.carbs;Fe=o(Ue<999&&De<999?(1-Je)*UAMpredBG+Je*COBpredBG:De<999?(ke+COBpredBG)/2:Ue<999?(ke+UAMpredBG)/2:ke),We>Fe&&(Fe=We),je=o(je=ue||ye>0?me?Je*Pe+(1-Je)*qe:Pe:me?qe:Ee);var Ke=Ue;if(We<$)Ke=(Ue+We)/2;else if(We<R){var Qe=(We-$)/(R-$);Ke=(Ue+(Ue*Qe+We*(1-Qe)))/2}else We>Ue&&(Ke=(Ue+We)/2);if(Ke=o(Ke),f.carbs)if(!me&&De<999)Ie=o(Math.max(Re,De));else if(De<999){var Ve=Je*De+(1-Je)*Ke;Ie=o(Math.max(Re,De,Ve))}else Ie=me?Ke:je;else me&&(Ie=o(Math.max(Re,Ke)));Ie=Math.min(Ie,Fe),process.stderr.write("minPredBG: "+Ie+" minIOBPredBG: "+Re+" minZTGuardBG: "+We),De<999&&process.stderr.write(" minCOBPredBG: "+De),Ue<999&&process.stderr.write(" minUAMPredBG: "+Ue),console.error(" avgPredBG:"+Fe+" COB/Carbs:"+f.mealCOB+"/"+f.carbs),Le>I&&(Ie=Math.min(Ie,Le)),B.COB=f.mealCOB,B.IOB=r.iob,B.BGI=i(Y,g),B.deviation=i(ee,g),B.ISF=i(J,g),B.CR=o(g.carb_ratio,2),B.target_bg=i(R,g),B.reason=n+", Standard, COB: "+B.COB+", Dev: "+B.deviation+", BGI: "+B.BGI+", CR: "+B.CR+", Target: "+B.target_bg+", minPredBG "+i(Ie,g)+", minGuardBG "+i(je,g)+", IOBpredBG "+i(Oe,g),Ae>0&&(B.reason+=", COBpredBG "+i(Ae,g)),Te>0&&(B.reason+=", UAMpredBG "+i(Te,g)),B.reason+="; ";var Xe=ae;Xe<40&&(Xe=Math.min(je,Xe));var Ye,ea=$-Xe,aa=240,ra=240;if(f.mealCOB>0&&(de>0||ye>0)){for(He=0;He<oe.length;He++)if(oe[He]<D){aa=5*He;break}for(He=0;He<oe.length;He++)if(oe[He]<$){ra=5*He;break}}else{for(He=0;He<ie.length;He++)if(ie[He]<D){aa=5*He;break}for(He=0;He<ie.length;He++)if(ie[He]<$){ra=5*He;break}}le&&je<$&&(console.error("minGuardBG "+i(je,g)+" projected below "+i($,g)+" - disabling SMB"),le=!1),void 0===g.maxDelta_bg_threshold&&(Ye=.2),void 0!==g.maxDelta_bg_threshold&&(Ye=Math.min(g.maxDelta_bg_threshold,.3)),T>Ye*I&&(console.error("maxDelta "+i(T,g)+" > "+100*Ye+"% of BG "+i(I,g)+" - disabling SMB"),B.reason+="maxDelta "+i(T,g)+" > "+100*Ye+"% of BG "+i(I,g)+": SMB disabled; ",le=!1),console.error("BG projected to remain above "+i(D,g)+" for "+aa+"minutes"),(ra<240||aa<60)&&console.error("BG projected to remain above "+i($,g)+" for "+ra+"minutes");var ta=ra,oa=g.current_basal*J*ta/60,ia=Math.max(0,f.mealCOB-.25*f.carbs),na=(ea-oa)/csf-ia;if(oa=o(oa),na=o(na),console.error("naive_eventualBG:"+ae+" bgUndershoot:"+ea+" zeroTempDuration:"+ta+" zeroTempEffect:"+oa+" carbsReq:"+na),na>=g.carbsReqThreshold&&ra<=45&&(B.carbsReq=na,B.reason+=na+" add'l carbs req w/in "+ra+"m; "),I<$&&r.iob<20*-g.current_basal/60&&O>0&&O>te)B.reason+="IOB "+r.iob+" < "+o(20*-g.current_basal/60,2),B.reason+=" and minDelta "+i(O,g)+" > expectedDelta "+i(te,g)+"; ";else if(I<$||je<$){B.reason+="minGuardBG "+i(je,g)+"<"+i($,g);var sa=(ea=R-je)/J,la=o(60*sa/g.current_basal);return la=30*o(la/30),la=Math.min(120,Math.max(30,la)),p.setTempBasal(0,la,g,B,a)}if(g.skip_neutral_temps&&B.deliverAt.getMinutes()>=55)return B.reason+="; Canceling temp at "+B.deliverAt.getMinutes()+"m past the hour. ",p.setTempBasal(0,0,g,B,a);if(re<D){if(B.reason+="Eventual BG "+i(re,g)+" < "+i(D,g),O>te&&O>0&&!na)return ae<40?(B.reason+=", naive_eventualBG < 40. ",p.setTempBasal(0,30,g,B,a)):(e.delta>O?B.reason+=", but Delta "+i(C,g)+" > expectedDelta "+i(te,g):B.reason+=", but Min. Delta "+O.toFixed(2)+" > Exp. Delta "+i(te,g),a.duration>15&&t(y,g)===t(a.rate,g)?(B.reason+=", temp "+a.rate+" ~ req "+y+"U/hr. ",B):(B.reason+="; setting current basal of "+y+" as temp. ",p.setTempBasal(y,30,g,B,a)));var ma=2*Math.min(0,(re-R)/J);ma=o(ma,2);var da=Math.min(0,(ae-R)/J);if(da=o(da,2),O<0&&O>te)ma=o(ma*(O/te),2);var ua=y+2*ma;ua=t(ua,g);var ca=a.duration*(a.rate-y)/60;if(ca<Math.min(ma,da)-.3*y)return B.reason+=", "+a.duration+"m@"+a.rate.toFixed(2)+" is a lot less than needed. ",p.setTempBasal(ua,30,g,B,a);if(void 0!==a.rate&&a.duration>5&&ua>=.8*a.rate)return B.reason+=", temp "+a.rate+" ~< req "+ua+"U/hr. ",B;if(ua<=0){if((la=o(60*(sa=(ea=R-ae)/J)/g.current_basal))<0?la=0:(la=30*o(la/30),la=Math.min(120,Math.max(0,la))),la>0)return B.reason+=", setting "+la+"m zero temp. ",p.setTempBasal(ua,la,g,B,a)}else B.reason+=", setting "+ua+"U/hr. ";return p.setTempBasal(ua,30,g,B,a)}if(O<te&&(!v||!le))return e.delta<O?B.reason+="Eventual BG "+i(re,g)+" > "+i(D,g)+" but Delta "+i(C,g)+" < Exp. Delta "+i(te,g):B.reason+="Eventual BG "+i(re,g)+" > "+i(D,g)+" but Min. Delta "+O.toFixed(2)+" < Exp. Delta "+i(te,g),a.duration>15&&t(y,g)===t(a.rate,g)?(B.reason+=", temp "+a.rate+" ~ req "+y+"U/hr. ",B):(B.reason+="; setting current basal of "+y+" as temp. ",p.setTempBasal(y,30,g,B,a));if(Math.min(re,Ie)<U&&(!v||!le))return B.reason+=i(re,g)+"-"+i(Ie,g)+" in range: no temp required",a.duration>15&&t(y,g)===t(a.rate,g)?(B.reason+=", temp "+a.rate+" ~ req "+y+"U/hr. ",B):(B.reason+="; setting current basal of "+y+" as temp. ",p.setTempBasal(y,30,g,B,a));if(re>=U&&(B.reason+="Eventual BG "+i(re,g)+" >= "+i(U,g)+", "),r.iob>j)return B.reason+="IOB "+o(r.iob,2)+" > max_iob "+j,a.duration>15&&t(y,g)===t(a.rate,g)?(B.reason+=", temp "+a.rate+" ~ req "+y+"U/hr. ",B):(B.reason+="; setting current basal of "+y+" as temp. ",p.setTempBasal(y,30,g,B,a));(ma=o((Math.min(Ie,re)-R)/J,2))>j-r.iob&&(B.reason+="max_iob "+j+", ",ma=j-r.iob),ua=t(ua=y+2*ma,g),ma=o(ma,3),B.insulinReq=ma;var ga=o((new Date(S).getTime()-r.lastBolusTime)/6e4,1);if(v&&le&&I>$){var ba=o(f.mealCOB/g.carb_ratio,3);if(g.use_autoisf)fa=g.smb_max_range_extension;else{console.error("autoISF disabled, SMB range extension disabled");var fa=1}if(fa>1&&console.error("SMB max range extended from default by factor "+fa),void 0===g.maxSMBBasalMinutes){var pa=o(fa*g.current_basal*30/60,1);console.error("profile.maxSMBBasalMinutes undefined: defaulting to 30m")}else r.iob>ba&&r.iob>0?(console.error("IOB",r.iob,"> COB",f.mealCOB+"; mealInsulinReq =",ba),g.maxUAMSMBBasalMinutes?(console.error("profile.maxUAMSMBBasalMinutes:",g.maxUAMSMBBasalMinutes,"profile.current_basal:",g.current_basal),pa=o(fa*g.current_basal*g.maxUAMSMBBasalMinutes/60,1)):(console.error("profile.maxUAMSMBBasalMinutes undefined: defaulting to 30m"),pa=o(30*g.current_basal/60,1))):(console.error("profile.maxSMBBasalMinutes:",g.maxSMBBasalMinutes,"profile.current_basal:",g.current_basal),pa=o(fa*g.current_basal*g.maxSMBBasalMinutes/60,1));var va=g.bolus_increment,ha=1/va;if(g.use_autoisf){var _a=c(g,I,R);"SMB Delivery Ratio: "+o(_a,2)+", "}else console.error("autoISF disabled, don't adjust SMB Delivery Ratio"),_a=.5;_a>.5&&console.error("SMB Delivery Ratio increased from default 0.5 to "+o(_a,2));var Ba=Math.min(ma*_a,pa);Ba=Math.floor(Ba*ha)/ha,la=o(60*(sa=(R-(ae+Re)/2)/J)/g.current_basal),ma>0&&Ba<va&&(la=0);var Ma=0;la<=0?la=0:la>=30?(la=30*o(la/30),la=Math.min(60,Math.max(0,la))):(Ma=o(y*la/30,2),la=30),B.reason+=" insulinReq "+ma,Ba>=pa&&(B.reason+="; maxBolus "+pa),la>0&&(B.reason+="; setting "+la+"m low temp of "+Ma+"U/h"),B.reason+=". ";var xa=3;g.SMBInterval&&(xa=Math.min(10,Math.max(1,g.SMBInterval)));var ya=o(xa-ga,0),Sa=o(60*(xa-ga),0)%60;if(console.error("naive_eventualBG",ae+",",la+"m "+Ma+"U/h temp needed; last bolus",ga+"m ago; maxBolus: "+pa),ga>xa?Ba>0&&(B.units=Ba,B.reason+="Microbolusing "+Ba+"U. "):B.reason+="Waiting "+ya+"m "+Sa+"s to microbolus again. ",la>0)return B.rate=Ma,B.duration=la,B}var Ca=p.getMaxSafeBasal(g);return ua>Ca&&(B.reason+="adj. req. rate: "+ua+" to maxSafeBasal: "+Ca+", ",ua=t(Ca,g)),(ca=a.duration*(a.rate-y)/60)>=2*ma?(B.reason+=a.duration+"m@"+a.rate.toFixed(2)+" > 2 * insulinReq. Setting temp basal of "+ua+"U/hr. ",p.setTempBasal(ua,30,g,B,a)):void 0===a.duration||0===a.duration?(B.reason+="no temp, setting "+ua+"U/hr. ",p.setTempBasal(ua,30,g,B,a)):a.duration>5&&t(ua,g)<=t(a.rate,g)?(B.reason+="temp "+a.rate+" >~ req "+ua+"U/hr. ",B):(B.reason+="temp "+a.rate+"<"+ua+"U/hr. ",p.setTempBasal(ua,30,g,B,a))}},6880:(e,a,r)=>{var t=r(6654);e.exports=function(e,a){var r=20;void 0!==a&&"string"==typeof a.model&&(t(a.model,"54")||t(a.model,"23"))&&(r=40);return e<1?Math.round(e*r)/r:e<10?Math.round(20*e)/20:Math.round(10*e)/10}},2705:(e,a,r)=>{var t=r(5639).Symbol;e.exports=t},9932:e=>{e.exports=function(e,a){for(var r=-1,t=null==e?0:e.length,o=Array(t);++r<t;)o[r]=a(e[r],r,e);return o}},9750:e=>{e.exports=function(e,a,r){return e==e&&(void 0!==r&&(e=e<=r?e:r),void 0!==a&&(e=e>=a?e:a)),e}},4239:(e,a,r)=>{var t=r(2705),o=r(9607),i=r(2333),n=t?t.toStringTag:void 0;e.exports=function(e){return null==e?void 0===e?"[object Undefined]":"[object Null]":n&&n in Object(e)?o(e):i(e)}},531:(e,a,r)=>{var t=r(2705),o=r(9932),i=r(1469),n=r(3448),s=t?t.prototype:void 0,l=s?s.toString:void 0;e.exports=function e(a){if("string"==typeof a)return a;if(i(a))return o(a,e)+"";if(n(a))return l?l.call(a):"";var r=a+"";return"0"==r&&1/a==-Infinity?"-0":r}},7561:(e,a,r)=>{var t=r(7990),o=/^\s+/;e.exports=function(e){return e?e.slice(0,t(e)+1).replace(o,""):e}},1957:(e,a,r)=>{var t="object"==typeof r.g&&r.g&&r.g.Object===Object&&r.g;e.exports=t},9607:(e,a,r)=>{var t=r(2705),o=Object.prototype,i=o.hasOwnProperty,n=o.toString,s=t?t.toStringTag:void 0;e.exports=function(e){var a=i.call(e,s),r=e[s];try{e[s]=void 0;var t=!0}catch(e){}var o=n.call(e);return t&&(a?e[s]=r:delete e[s]),o}},2333:e=>{var a=Object.prototype.toString;e.exports=function(e){return a.call(e)}},5639:(e,a,r)=>{var t=r(1957),o="object"==typeof self&&self&&self.Object===Object&&self,i=t||o||Function("return this")();e.exports=i},7990:e=>{var a=/\s/;e.exports=function(e){for(var r=e.length;r--&&a.test(e.charAt(r)););return r}},6654:(e,a,r)=>{var t=r(9750),o=r(531),i=r(554),n=r(9833);e.exports=function(e,a,r){e=n(e),a=o(a);var s=e.length,l=r=void 0===r?s:t(i(r),0,s);return(r-=a.length)>=0&&e.slice(r,l)==a}},1469:e=>{var a=Array.isArray;e.exports=a},3218:e=>{e.exports=function(e){var a=typeof e;return null!=e&&("object"==a||"function"==a)}},7005:e=>{e.exports=function(e){return null!=e&&"object"==typeof e}},3448:(e,a,r)=>{var t=r(4239),o=r(7005);e.exports=function(e){return"symbol"==typeof e||o(e)&&"[object Symbol]"==t(e)}},8601:(e,a,r)=>{var t=r(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,a,r)=>{var t=r(8601);e.exports=function(e){var a=t(e),r=a%1;return a==a?r?a-r:a:0}},4841:(e,a,r)=>{var t=r(7561),o=r(3218),i=r(3448),n=/^[-+]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(i(e))return NaN;if(o(e)){var a="function"==typeof e.valueOf?e.valueOf():e;e=o(a)?a+"":a}if("string"!=typeof e)return 0===e?e:+e;e=t(e);var r=s.test(e);return r||l.test(e)?m(e.slice(2),r?2:8):n.test(e)?NaN:+e}},9833:(e,a,r)=>{var t=r(531);e.exports=function(e){return null==e?"":t(e)}}},a={};function r(t){var o=a[t];if(void 0!==o)return o.exports;var i=a[t]={exports:{}};return e[t](i,i.exports,r),i.exports}r.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=r(5546);freeaps_determineBasal=t})();