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