determine-basal.js 26 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="",l="",m="",d="",u="",c="",g="",b="",f="";function p(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}function h(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}e.exports=function(e,r,a,v,_,B,M,x,S,y){var C={},I=new Date;if(y&&(I=y),void 0===v||void 0===v.current_basal)return C.error="Error: could not get current basal rate",C;var G=t(v.current_basal,v),F=G,w=new Date;y&&(w=y);var O,A=new Date(e.date),T=o((w-A)/60/1e3,1),R=e.glucose,D=e.noise;O=e.delta>-.5?"+"+o(e.delta,0):o(e.delta,0);var U=Math.min(e.delta,e.short_avgdelta),j=Math.min(e.short_avgdelta,e.long_avgdelta),P=Math.max(e.delta,e.short_avgdelta,e.long_avgdelta);(R<=10||38===R||D>=3)&&(C.reason="CGM is calibrating, in ??? state, or noise is high");if(R>60&&0==e.delta&&e.short_avgdelta>-1&&e.short_avgdelta<1&&e.long_avgdelta>-1&&e.long_avgdelta<1&&("fakecgm"==e.device?(console.error("CGM data is unchanged ("+n(R,v)+"+"+n(e.delta,v)+") for 5m w/ "+n(e.short_avgdelta,v)+" mg/dL ~15m change & "+n(e.long_avgdelta,2)+" mg/dL ~45m change"),console.error("Simulator mode detected ("+e.device+"): continuing anyway")):!0),T>12||T<-5?C.reason="If current system time "+w+" is correct, then BG data is too old. The last BG data was read "+T+"m ago at "+A:0===e.short_avgdelta&&0===e.long_avgdelta&&(e.last_cal&&e.last_cal<3?C.reason="CGM was just calibrated":C.reason="CGM data is unchanged ("+n(R,v)+"+"+n(e.delta,v)+") for 5m w/ "+n(e.short_avgdelta,v)+" mg/dL ~15m change & "+n(e.long_avgdelta,v)+" mg/dL ~45m change"),R<=10||38===R||D>=3||T>12||T<-5||0===e.short_avgdelta&&0===e.long_avgdelta)return r.rate>=F?(C.reason+=". Canceling high temp basal of "+r.rate,C.deliverAt=I,C.temp="absolute",C.duration=0,C.rate=0,C):0===r.rate&&r.duration>30?(C.reason+=". Shortening "+r.duration+"m long zero temp to 30m. ",C.deliverAt=I,C.temp="absolute",C.duration=30,C.rate=0,C):(C.reason+=". Temp "+r.rate+" <= current basal "+F+"U/hr; doing nothing. ",C);var q,E,W,k=v.max_iob;if(void 0!==v.min_bg&&(E=v.min_bg),void 0!==v.max_bg&&(W=v.max_bg),void 0===v.min_bg||void 0===v.max_bg)return C.error="Error: could not determine target_bg. ",C;q=(v.min_bg+v.max_bg)/2;var z=v.exercise_mode||v.high_temptarget_raises_sensitivity,L=100,N=160;if(v.half_basal_exercise_target&&(N=v.half_basal_exercise_target),z&&v.temptargetSet&&q>L||v.low_temptarget_lowers_sensitivity&&v.temptargetSet&&q<L){var Z=N-L;Z+q-L>0?(sensitivityRatio=Z/(Z+q-L),sensitivityRatio=Math.min(sensitivityRatio,v.autosens_max),sensitivityRatio=o(sensitivityRatio,2)):sensitivityRatio=v.autosens_max,process.stderr.write("Sensitivity ratio set to "+sensitivityRatio+" based on temp target of "+q+"; ")}else void 0!==_&&_&&(sensitivityRatio=_.ratio,process.stderr.write("Autosens ratio: "+sensitivityRatio+"; "));if(sensitivityRatio&&(F=v.current_basal*sensitivityRatio,(F=t(F,v))!==G?process.stderr.write("Adjusting basal from "+G+" to "+F+"; "):process.stderr.write("Basal unchanged: "+F+"; ")),v.temptargetSet);else if(void 0!==_&&_&&(v.sensitivity_raises_target&&_.ratio<1||v.resistance_lowers_target&&_.ratio>1)){E=o((E-60)/_.ratio)+60,W=o((W-60)/_.ratio)+60;var $=o((q-60)/_.ratio)+60;q===($=Math.max(80,$))?process.stderr.write("target_bg unchanged: "+$+"; "):process.stderr.write("target_bg from "+q+" to "+$+"; "),q=$}var H=200,J=200,K=200;if(e.noise>=2){var Q=Math.max(1.1,v.noisyCGMTargetMultiplier);Math.min(250,v.maxRaw);H=o(Math.min(200,E*Q)),J=o(Math.min(200,q*Q)),K=o(Math.min(200,W*Q)),process.stderr.write("Raising target_bg for noisy / raw CGM data, from "+q+" to "+J+"; "),E=H,q=J,W=K}var V=E-.5*(E-40),X=o(v.sens,1),Y=v.sens;if(void 0!==_&&_&&((Y=o(Y=v.sens/sensitivityRatio,1))!==X?process.stderr.write("ISF from "+n(X,v)+" to "+n(Y,v)):process.stderr.write("ISF unchanged: "+n(Y,v)),i+="Autosens, Ratio: "+sensitivityRatio+", ISF: "+n(X,v)+"→"+n(Y,v)),console.error("CR:"+v.carb_ratio),Y=function(e,r,a,t,v,_,B,M){if(!a.use_autoisf)return console.error("autoISF disabled in Preferences"),e;var x=t.dura_p,S=t.delta_pl,y=t.delta_pn,C=t.r_squ,I=t.bg_acceleration,G=t.parabola_fit_a0,F=t.parabola_fit_a1,w=t.parabola_fit_a2,O=t.autoISF_duration,A=t.autoISF_average,T=a.autoisf_max,R=!1,D=1,U=1,j=1,P=r+10-A;if(!(v.mealCOB>0)||a.enableautoisf_with_COB){var q=t.pp_debug;if(c+="BG-accel: "+o(I,3)+", PF-minutes: "+x+", PF-corr: "+o(C,3)+", PF-nextDelta: "+n(y,a)+", PF-lastDelta: "+n(S,a)+", regular Delta: "+n(t.delta,a),console.error(q+c+" , Weights Accel/Brake: "+a.bgAccel_ISF_weight+" / "+a.bgBrake_ISF_weight),a.enable_BG_acceleration){var E=I;if(0!=t.parabola_fit_a2){var W=-F/2/w*5,k=o(G-W*W/25*w,1);(W=o(W,1))<0&&E<0?(f="saw max of "+n(k,a)+", about "+-W+" min ago",console.error("Parabolic fit "+f)):W<0&&E>0?(f="saw min of "+n(k,a)+", about "+-W+" min ago",console.error("Parabolic fit "+f)):W>0&&E<0?(f="predicts max of "+n(k,a)+", in about "+W+"min",console.error("Parabolic fit "+f)):W>0&&E>0&&(f="predicts min of "+n(k,a)+", in about "+W+" min",console.error("Parabolic fit "+f))}var z=C;if(z<=.9)f="acce_ISF by-passed, as correlation, "+o(z,3)+", is too low",console.error("Parabolic fit "+f),g+=", Parabolic Fit, "+f;else{g+=", Parabolic Fit, "+f+", regΔ: "+n(t.delta,a)+", lastΔ: "+n(S,a)+", nextΔ: "+n(y,a)+", Corr "+o(C,3)+", BG-Accel: "+o(E,2);var L=10*(z-.9),N=1;t.glucose<a.target_bg&&E>1&&(N=.5),j=1+E*N*(E<0?a.bgBrake_ISF_weight:a.bgAccel_ISF_weight)*L,console.error("Original result for acce_ISF: "+o(j,2)),1!=j&&(R=!0,g+=", acce-ISF Ratio: "+o(j,2))}}else console.error("autoISF BG accelertion adaption disabled in Preferences");var Z=h(a,t.glucose,r);i+=", SMB Delivery Ratio:, "+o(Z,2)+g+", autoISF";var $=1+p(100-P,a);console.error("bg_ISF adaptation is "+o($,2)),$<1&&j>1&&(b="bg_ISF adaptation lifted to "+o($*=j,2)+", as BG accelerates already",s="(lifted by "+j+")",console.error(b));var H=1;if($<1)return(H=Math.min(bg_isf,j))<a.autoisf_min&&(b="final ISF factor "+o(H,2)+" limited by autoisf_min "+a.autoisf_min,console.error(b),s="(limited to "+a.autoisf_min+")",H=a.autoisf_min),earlysens=Math.min(720,o(a.sens/Math.min(M,H),1)),console.error("early Return autoISF: "+n(earlysens,a)),l=", bg-ISF Ratio"+s+": "+o($,2)+", ISF: "+n(earlysens,a),i+=l,s=l,earlysens;$>1&&(R=!0,i+=", bg-ISF Ratio"+s+": "+o($,2));var J=t.delta;P>0?console.error("delta_ISF adaptation by-passed as average glucose < "+n(r+10,a)):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>=(_-v.lastCarbTime)/1e3/3600?(D=1+Math.max(0,J*a.postmeal_ISF_weight),console.error("pp_ISF adaptation is "+o(D,2)),d=", pp-ISF Ratio: "+o(D,2),1!=D&&(R=!0)):(U=p(J,a),P>-20&&(U*=.5),U=1+U,console.error("delta_ISF adaptation is "+o(U,2)),u=", Δ-ISF Ratio: "+o(U,2),1!=U&&(R=!0));var K=1,Q=a.autoisf_hourlychange;return v.mealCOB>0&&!a.enableautoisf_with_COB?console.error("dura_ISF by-passed; preferences disabled mealCOB of "+o(v.mealCOB,1)):O<10?console.error("dura_ISF by-passed; BG is only "+O+"m at level "+A):A<=r?console.error("dura_ISF by-passed; avg. glucose "+A+" below target "+n(r,a)):(R=!0,m=", dura-ISF Ratio: "+o(K+=O/60*(Q/r)*(A-r),2),console.error("dura_ISF adaptation is "+o(K,2)+" because ISF "+e+" did not do it for "+o(O,1)+"m")),H=1,R?(H=Math.max(K,$,U,j,D),console.error("autoISF adaption ratios:"),console.error(" dura "+o(K,2)),console.error(" bg "+o($,2)),console.error(" delta "+o(U,2)),console.error(" pp "+o(D,2)),console.error(" accel "+o(j,2)),j<1&&(console.error("strongest ISF factor "+o(H,2)+" weakened to "+o(H*j,2)+" as bg decelerates already"),H*=j),H<a.autoisf_min?(console.error("final ISF factor "+o(H,2)+" limited by autoisf_min "+a.autoisf_min),H=a.autoisf_min):H>T&&(console.error("final ISF factor "+o(H,2)+" limited by autoisf_max "+T),H=T),H>=1&&(e=o(a.sens/Math.max(H,M),1)),H<1&&(e=o(a.sens/Math.min(H,M),1))):H=M,i+=", Duration: "+O+", Avg: "+n(A,a)+s+d+u+m+", Ratio: "+o(H,2)+", ISF: "+n(e,a),console.error("Inside autoISF: Ratio "+o(H,2)+" resulting in "+n(e,a)),e}console.error("BG dependant autoISF by-passed; preferences disabled mealCOB of "+o(v.mealCOB,1))}(Y,q,v,e,B,y,0,sensitivityRatio),void 0===a)return C.error="Error: iob_data undefined. ",C;var ee,re=a;if(a.length,a.length>1&&(a=re[0]),void 0===a.activity||void 0===a.iob)return C.error="Error: iob_data missing some property. ",C;var ae=((ee=void 0!==a.lastTemp?o((new Date(w).getTime()-a.lastTemp.date)/6e4):0)+r.duration)%30;if(console.error("currenttemp:"+r.rate+" lastTempAge:"+ee+"m, tempModulus:"+ae+"m"),C.temp="absolute",C.deliverAt=I,x&&r&&a.lastTemp&&r.rate!==a.lastTemp.rate&&ee>10&&r.duration)return C.reason="Warning: currenttemp rate "+r.rate+" != lastTemp rate "+a.lastTemp.rate+" from pumphistory; canceling temp",M.setTempBasal(0,0,v,C,r);if(r&&a.lastTemp&&r.duration>0){var te=ee-a.lastTemp.duration;if(te>5&&ee>10)return C.reason="Warning: currenttemp running but lastTemp from pumphistory ended "+te+"m ago; canceling temp",M.setTempBasal(0,0,v,C,r)}var oe=o(-a.activity*Y*5,2),ne=o(6*(U-oe));ne<0&&(ne=o(6*(j-oe)))<0&&(ne=o(6*(e.long_avgdelta-oe)));var ie=R,se=(ie=a.iob>0?o(R-a.iob*Y):o(R-a.iob*Math.min(Y,v.sens)))+ne;if(void 0===se||isNaN(se))return C.error="Error: could not calculate eventualBG. Sensitivity: "+Y+" Deviation: "+ne,C;var le=function(e,r,a){return o(a+(e-r)/24,1)}(q,se,oe);C={temp:"absolute",bg:R,tick:O,eventualBG:se,insulinReq:0,reservoir:S,deliverAt:I,sensitivityRatio};var me=[],de=[],ue=[],ce=[];me.push(R),de.push(R),ce.push(R),ue.push(R);var ge=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)}(v,x,B,q),be=v.enableUAM,fe=0,pe=0;fe=o(U-oe,1);var he=o(U-oe,1);csf=Y/v.carb_ratio,console.error("profile.sens:"+n(v.sens,v)+", sens:"+n(Y,v)+", CSF:"+o(csf,1));var ve=o(30*csf*5/60,1);fe>ve&&(console.error("Limiting carb impact from "+fe+" to "+ve+"mg/dL/5m (30g/h)"),fe=ve);var _e=3;sensitivityRatio&&(_e/=sensitivityRatio);var Be=_e;if(B.carbs){_e=Math.max(_e,B.mealCOB/20);var Me=o((new Date(w).getTime()-B.lastCarbTime)/6e4),xe=(B.carbs-B.mealCOB)/B.carbs;Be=o(Be=_e+1.5*Me/60,1),console.error("Last carbs "+Me+" minutes ago; remainingCATime:"+Be+"hours; "+o(100*xe)+"% carbs absorbed")}var Se=Math.max(0,fe/5*60*Be/2)/csf,ye=90,Ce=1;v.remainingCarbsCap&&(ye=Math.min(90,v.remainingCarbsCap)),v.remainingCarbsFraction&&(Ce=Math.min(1,v.remainingCarbsFraction));var Ie=1-Ce,Ge=Math.max(0,B.mealCOB-Se-B.carbs*Ie),Fe=(Ge=Math.min(ye,Ge))*csf*5/60/(Be/2),we=o(B.slopeFromMaxDeviation,2),Oe=o(B.slopeFromMinDeviation,2),Ae=Math.min(we,-Oe/3),Te=0;0===fe?pe=0:!0===v.floating_carbs?(pe=Math.min(60*Be/5/2,Math.max(0,B.carbs*csf/fe)),Te=Math.min(60*Be/5/2,Math.max(0,B.mealCOB*csf/fe)),B.carbs>0&&(i+=", Floating Carbs:, CID: "+o(pe,1)+", MealCarbs: "+o(B.carbs,1)+", Not Floating:, CID: "+o(Te,1)+", MealCOB: "+o(B.mealCOB,1),console.error("Floating Carbs CID: "+o(pe,1)+" / MealCarbs: "+o(B.carbs,1)+" vs. Not Floating:"+o(Te,1)+" / MealCOB:"+o(B.mealCOB,1)))):pe=Math.min(60*Be/5/2,Math.max(0,B.mealCOB*csf/fe)),console.error("Carb Impact:"+fe+"mg/dL per 5m; CI Duration:"+o(5*pe/60*2,1)+"hours; remaining CI ("+Be/2+"h peak):",o(Fe,1)+"mg/dL per 5m");var Re,De,Ue,je,Pe,qe=999,Ee=999,We=999,ke=R,ze=999,Le=999,Ne=999,Ze=999,$e=se,He=R,Je=R,Ke=0,Qe=[],Ve=[];try{re.forEach((function(e){var r=o(-e.activity*Y*5,2),a=o(-e.iobWithZeroTemp.activity*Y*5,2),t=fe*(1-Math.min(1,de.length/12));$e=de[de.length-1]+r+t;var n=ce[ce.length-1]+a,i=Math.max(0,Math.max(0,fe)*(1-me.length/Math.max(2*pe,1))),s=Math.min(me.length,12*Be-me.length),l=Math.max(0,s/(Be/2*12)*Fe);i+l,Qe.push(o(l,0)),Ve.push(o(i,0)),COBpredBG=me[me.length-1]+r+Math.min(0,t)+i+l;var m=Math.max(0,he+ue.length*Ae),d=Math.max(0,he*(1-ue.length/Math.max(36,1))),u=Math.min(m,d);u>0&&(Ke=o(5*(ue.length+1)/60,1)),UAMpredBG=ue[ue.length-1]+r+Math.min(0,t)+u,de.length<48&&de.push($e),me.length<48&&me.push(COBpredBG),ue.length<48&&ue.push(UAMpredBG),ce.length<48&&ce.push(n),COBpredBG<ze&&(ze=o(COBpredBG)),UAMpredBG<Le&&(Le=o(UAMpredBG)),$e<Ne&&(Ne=o($e)),n<Ze&&(Ze=o(n));de.length>18&&$e<qe&&(qe=o($e)),$e>He&&(He=$e),(pe||Fe>0)&&me.length>18&&COBpredBG<Ee&&(Ee=o(COBpredBG)),(pe||Fe>0)&&COBpredBG>He&&(Je=COBpredBG),be&&ue.length>12&&UAMpredBG<We&&(We=o(UAMpredBG)),be&&UAMpredBG>He&&UAMpredBG}))}catch(e){console.error("Problem with iobArray. Optional feature Advanced Meal Assist disabled")}B.mealCOB&&(console.error("predCIs (mg/dL/5m):"+Ve.join(" ")),console.error("remainingCIs: "+Qe.join(" "))),C.predBGs={},de.forEach((function(e,r,a){a[r]=o(Math.min(401,Math.max(39,e)))}));for(var Xe=de.length-1;Xe>12&&de[Xe-1]===de[Xe];Xe--)de.pop();for(C.predBGs.IOB=de,Ue=o(de[de.length-1]),ce.forEach((function(e,r,a){a[r]=o(Math.min(401,Math.max(39,e)))})),Xe=ce.length-1;Xe>6&&!(ce[Xe-1]>=ce[Xe]||ce[Xe]<=q);Xe--)ce.pop();if(C.predBGs.ZT=ce,o(ce[ce.length-1]),B.mealCOB>0&&(fe>0||Fe>0)){for(me.forEach((function(e,r,a){a[r]=o(Math.min(401,Math.max(39,e)))})),Xe=me.length-1;Xe>12&&me[Xe-1]===me[Xe];Xe--)me.pop();C.predBGs.COB=me,je=o(me[me.length-1]),se=Math.max(se,o(me[me.length-1]))}if(fe>0||Fe>0){if(be){for(ue.forEach((function(e,r,a){a[r]=o(Math.min(401,Math.max(39,e)))})),Xe=ue.length-1;Xe>12&&ue[Xe-1]===ue[Xe];Xe--)ue.pop();C.predBGs.UAM=ue,Pe=o(ue[ue.length-1]),ue[ue.length-1]&&(se=Math.max(se,o(ue[ue.length-1])))}C.eventualBG=se}console.error("UAM Impact:"+he+"mg/dL per 5m; UAM Duration:"+Ke+"hours"),qe=Math.max(39,qe),Ee=Math.max(39,Ee),We=Math.max(39,We),Re=o(qe);var Ye=B.mealCOB/B.carbs;De=o(We<999&&Ee<999?(1-Ye)*UAMpredBG+Ye*COBpredBG:Ee<999?($e+COBpredBG)/2:We<999?($e+UAMpredBG)/2:$e),Ze>De&&(De=Ze),ke=o(ke=pe||Fe>0?be?Ye*ze+(1-Ye)*Le:ze:be?Le:Ne);var er=We;if(Ze<V)er=(We+Ze)/2;else if(Ze<q){var rr=(Ze-V)/(q-V);er=(We+(We*rr+Ze*(1-rr)))/2}else Ze>We&&(er=(We+Ze)/2);if(er=o(er),B.carbs)if(!be&&Ee<999)Re=o(Math.max(qe,Ee));else if(Ee<999){var ar=Ye*Ee+(1-Ye)*er;Re=o(Math.max(qe,Ee,ar))}else Re=be?er:ke;else be&&(Re=o(Math.max(qe,er)));Re=Math.min(Re,De),process.stderr.write("minPredBG: "+Re+" minIOBPredBG: "+qe+" minZTGuardBG: "+Ze),Ee<999&&process.stderr.write(" minCOBPredBG: "+Ee),We<999&&process.stderr.write(" minUAMPredBG: "+We),console.error(" avgPredBG:"+De+" COB/Carbs:"+B.mealCOB+"/"+B.carbs),Je>R&&(Re=Math.min(Re,Je)),C.COB=B.mealCOB,C.IOB=a.iob,C.BGI=n(oe,v),C.deviation=n(ne,v),C.ISF=n(Y,v),C.CR=o(v.carb_ratio,2),C.target_bg=n(q,v),C.reason=i+", Standard, COB: "+C.COB+", Dev: "+C.deviation+", BGI: "+C.BGI+", CR: "+C.CR+", Target: "+C.target_bg+", minPredBG "+n(Re,v)+", minGuardBG "+n(ke,v)+", IOBpredBG "+n(Ue,v),je>0&&(C.reason+=", COBpredBG "+n(je,v)),Pe>0&&(C.reason+=", UAMpredBG "+n(Pe,v)),C.reason+="; ";var tr=ie;tr<40&&(tr=Math.min(ke,tr));var or,nr=V-tr,ir=240,sr=240;if(B.mealCOB>0&&(fe>0||Fe>0)){for(Xe=0;Xe<me.length;Xe++)if(me[Xe]<E){ir=5*Xe;break}for(Xe=0;Xe<me.length;Xe++)if(me[Xe]<V){sr=5*Xe;break}}else{for(Xe=0;Xe<de.length;Xe++)if(de[Xe]<E){ir=5*Xe;break}for(Xe=0;Xe<de.length;Xe++)if(de[Xe]<V){sr=5*Xe;break}}ge&&ke<V&&(console.error("minGuardBG "+n(ke,v)+" projected below "+n(V,v)+" - disabling SMB"),ge=!1),void 0===v.maxDelta_bg_threshold&&(or=.2),void 0!==v.maxDelta_bg_threshold&&(or=Math.min(v.maxDelta_bg_threshold,.4)),P>or*R&&(console.error("maxDelta "+n(P,v)+" > "+100*or+"% of BG "+n(R,v)+" - disabling SMB"),C.reason+="maxDelta "+n(P,v)+" > "+100*or+"% of BG "+n(R,v)+" - SMB disabled!, ",ge=!1),console.error("BG projected to remain above "+n(E,v)+" for "+ir+"minutes"),(sr<240||ir<60)&&console.error("BG projected to remain above "+n(V,v)+" for "+sr+"minutes");var lr=sr,mr=v.current_basal*Y*lr/60,dr=Math.max(0,B.mealCOB-.25*B.carbs),ur=(nr-mr)/csf-dr;mr=o(mr),ur=o(ur),console.error("naive_eventualBG:"+ie+" bgUndershoot:"+nr+" zeroTempDuration:"+lr+" zeroTempEffect:"+mr+" carbsReq:"+ur),ur>=v.carbsReqThreshold&&sr<=45&&(C.carbsReq=ur,C.reason+=ur+" add'l carbs req w/in "+sr+"m; ");var cr=0;if(R<V&&a.iob<20*-v.current_basal/60&&U>0&&U>le)C.reason+="IOB "+a.iob+" < "+o(20*-v.current_basal/60,2),C.reason+=" and minDelta "+n(U,v)+" > expectedDelta "+n(le,v)+"; ";else if(R<V||ke<V)return C.reason+="minGuardBG "+n(ke,v)+"<"+n(V,v),cr=o(60*((nr=q-ke)/Y)/v.current_basal),cr=30*o(cr/30),cr=Math.min(120,Math.max(30,cr)),M.setTempBasal(0,cr,v,C,r);if(v.skip_neutral_temps&&C.deliverAt.getMinutes()>=55)return C.reason+="; Canceling temp at "+C.deliverAt.getMinutes()+"m past the hour. ",M.setTempBasal(0,0,v,C,r);var gr=0,br=F;if(se<E){if(C.reason+="Eventual BG "+n(se,v)+" < "+n(E,v),U>le&&U>0&&!ur)return ie<40?(C.reason+=", naive_eventualBG < 40. ",M.setTempBasal(0,30,v,C,r)):(e.delta>U?C.reason+=", but Delta "+n(O,v)+" > expectedDelta "+n(le,v):C.reason+=", but Min. Delta "+U.toFixed(2)+" > Exp. Delta "+n(le,v),r.duration>15&&t(F,v)===t(r.rate,v)?(C.reason+=", temp "+r.rate+" ~ req "+F+"U/hr. ",C):(C.reason+="; setting current basal of "+F+" as temp. ",M.setTempBasal(F,30,v,C,r)));gr=o(gr=2*Math.min(0,(se-q)/Y),2);var fr=Math.min(0,(ie-q)/Y);if(fr=o(fr,2),U<0&&U>le)gr=o(gr*(U/le),2);if(br=t(br=F+2*gr,v),r.duration*(r.rate-F)/60<Math.min(gr,fr)-.3*F)return C.reason+=", "+r.duration+"m@"+r.rate.toFixed(2)+" is a lot less than needed. ",M.setTempBasal(br,30,v,C,r);if(void 0!==r.rate&&r.duration>5&&br>=.8*r.rate)return C.reason+=", temp "+r.rate+" ~< req "+br+"U/hr. ",C;if(br<=0){if((cr=o(60*((nr=q-ie)/Y)/v.current_basal))<0?cr=0:(cr=30*o(cr/30),cr=Math.min(120,Math.max(0,cr))),cr>0)return C.reason+=", setting "+cr+"m zero temp. ",M.setTempBasal(br,cr,v,C,r)}else C.reason+=", setting "+br+"U/hr. ";return M.setTempBasal(br,30,v,C,r)}if(U<le&&(!x||!ge))return e.delta<U?C.reason+="Eventual BG "+n(se,v)+" > "+n(E,v)+" but Delta "+n(O,v)+" < Exp. Delta "+n(le,v):C.reason+="Eventual BG "+n(se,v)+" > "+n(E,v)+" but Min. Delta "+U.toFixed(2)+" < Exp. Delta "+n(le,v),r.duration>15&&t(F,v)===t(r.rate,v)?(C.reason+=", temp "+r.rate+" ~ req "+F+"U/hr. ",C):(C.reason+="; setting current basal of "+F+" as temp. ",M.setTempBasal(F,30,v,C,r));if(Math.min(se,Re)<W&&(!x||!ge))return C.reason+=n(se,v)+"-"+n(Re,v)+" in range: no temp required",r.duration>15&&t(F,v)===t(r.rate,v)?(C.reason+=", temp "+r.rate+" ~ req "+F+"U/hr. ",C):(C.reason+="; setting current basal of "+F+" as temp. ",M.setTempBasal(F,30,v,C,r));if(se>=W&&(C.reason+="Eventual BG "+n(se,v)+" >= "+n(W,v)+", "),a.iob>k)return C.reason+="IOB "+o(a.iob,2)+" > max_iob "+k,r.duration>15&&t(F,v)===t(r.rate,v)?(C.reason+=", temp "+r.rate+" ~ req "+F+"U/hr. ",C):(C.reason+="; setting current basal of "+F+" as temp. ",M.setTempBasal(F,30,v,C,r));(gr=o((Math.min(Re,se)-q)/Y,2))>k-a.iob&&(C.reason+="max_iob "+k+", ",gr=k-a.iob),br=t(br=F+2*gr,v),gr=o(gr,3),C.insulinReq=gr;var pr=o((new Date(w).getTime()-a.lastBolusTime)/6e4,1);if(x&&ge&&R>V){var hr=o(B.mealCOB/v.carb_ratio,3);if(v.use_autoisf)vr=v.smb_max_range_extension;else{console.error("autoISF disabled, SMB range extension disabled");var vr=1}vr>1&&console.error("SMB max range extended from default by factor "+vr);var _r=0;void 0===v.maxSMBBasalMinutes?(_r=o(vr*v.current_basal*30/60,1),console.error("profile.maxSMBBasalMinutes undefined: defaulting to 30m")):a.iob>hr&&a.iob>0?(console.error("IOB",a.iob,"> COB",B.mealCOB+"; mealInsulinReq =",hr),v.maxUAMSMBBasalMinutes?(console.error("profile.maxUAMSMBBasalMinutes:",v.maxUAMSMBBasalMinutes,"profile.current_basal:",v.current_basal),_r=o(vr*v.current_basal*v.maxUAMSMBBasalMinutes/60,1)):(console.error("profile.maxUAMSMBBasalMinutes undefined: defaulting to 30m"),_r=o(30*v.current_basal/60,1))):(console.error("profile.maxSMBBasalMinutes:",v.maxSMBBasalMinutes,"profile.current_basal:",v.current_basal),_r=o(vr*v.current_basal*v.maxSMBBasalMinutes/60,1));var Br=v.bolus_increment,Mr=1/Br;if(v.use_autoisf)var xr=h(v,R,q);else console.error("autoISF disabled, don't adjust SMB Delivery Ratio"),xr=.5;xr>.5&&console.error("SMB Delivery Ratio increased from default 0.5 to "+o(xr,2));var Sr=Math.min(gr*xr,_r);Sr=Math.floor(Sr*Mr)/Mr,cr=o(60*((q-(ie+qe)/2)/Y)/v.current_basal),gr>0&&Sr<Br&&(cr=0);var yr=0;cr<=0?cr=0:cr>=30?(cr=30*o(cr/30),cr=Math.min(60,Math.max(0,cr))):(yr=o(F*cr/30,2),cr=30),C.reason+=" insulinReq "+gr,Sr>=_r&&(C.reason+="; maxBolus "+_r),cr>0&&(C.reason+="; setting "+cr+"m low temp of "+yr+"U/h"),C.reason+=". ";var Cr=3;v.SMBInterval&&(Cr=Math.min(10,Math.max(1,v.SMBInterval)));var Ir=o(Cr-pr,0),Gr=o(60*(Cr-pr),0)%60;if(console.error("naive_eventualBG",ie+",",cr+"m "+yr+"U/h temp needed; last bolus",pr+"m ago; maxBolus: "+_r),pr>Cr?Sr>0&&(C.units=Sr,C.reason+="Microbolusing "+Sr+"U. "):C.reason+="Waiting "+Ir+"m "+Gr+"s to microbolus again. ",cr>0)return C.rate=yr,C.duration=cr,C}var Fr=M.getMaxSafeBasal(v);return br>Fr&&(C.reason+="adj. req. rate: "+br+" to maxSafeBasal: "+Fr+", ",br=t(Fr,v)),r.duration*(r.rate-F)/60>=2*gr?(C.reason+=r.duration+"m@"+r.rate.toFixed(2)+" > 2 * insulinReq. Setting temp basal of "+br+"U/hr. ",M.setTempBasal(br,30,v,C,r)):void 0===r.duration||0===r.duration?(C.reason+="no temp, setting "+br+"U/hr. ",M.setTempBasal(br,30,v,C,r)):r.duration>5&&t(br,v)<=t(r.rate,v)?(C.reason+="temp "+r.rate+" >~ req "+br+"U/hr. ",C):(C.reason+="temp "+r.rate+"<"+br+"U/hr. ",M.setTempBasal(br,30,v,C,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})();