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,d,u,c,g,h,p,v){var f=i.min_bg,b=v.overrideTarget;const B=v.smbIsOff,M=v.advancedSettings,_=v.isfAndCr,y=v.isf,x=v.cr,S=v.smbIsAlwaysOff,w=v.start,D=v.end,T=v.smbMinutes,C=v.uamMinutes;var U=0,G="",O="",R="",A="",I="",j=0,F=0,P=0,E=0,q=0,W=0;const k=v.weightedAverage;var L=1,z=i.sens,N=i.carb_ratio;v.useOverride&&(L=v.overridePercentage/100,_?(z/=L,N/=L):(x&&(N/=L),y&&(z/=L)));const H=i.weightPercentage,Z=v.average_total_data;function $(e,t){var a=e.getTime();return new Date(a+36e5*t)}function J(e){var t=i.bolus_increment;.025!=t&&(t=.05);var a=e/t;return a>=1?o(Math.floor(a)*t,5):0}function K(e){function t(e){return e<10&&(e="0"+e),e}return t(e.getHours())+":"+t(e.getMinutes())+":00"}function Q(e,t){var a=new Date("1/1/1999 "+e),r=new Date("1/1/1999 "+t);return(a.getTime()-r.getTime())/36e5}function V(e,t){var a=0,r=t,o=(e-t)/36e5,n=0,i=o,s=0;do{if(o>0){var l=K(r),m=p[0].rate;for(let e=0;e=(s=Q(p[e+1].start,p[e].start))?n=s:o=s?n=s:od)if(e+1=(s=Q(u,l))?n=s:o=(s=Q("23:59:59",l))?n=s:o0&&o21)q=V(X,(ee=24-U,te=X.getTime(),new Date(te-36e5*ee))),A="24 hours of data is required for an accurate tdd calculation. Currently only "+U.toPrecision(3)+" hours of pump history data are available. Using your pump scheduled basals to fill in the missing hours. Scheduled basals added: "+q.toPrecision(5)+" U. ";else U<21?(ge=!1,enableDynamicCR=!1):A=""}else console.log("Pumphistory is empty!"),ge=!1,enableDynamicCR=!1;var ee,te;for(let e=0;e0){j=e,W=g[e].rate;var ae=g[e-1]["duration (min)"]/60,re=ae,oe=new Date(g[e-1].timestamp),ne=oe,ie=0;do{if(e--,0==e){ne=new Date;break}if("TempBasal"==g[e]._type||"PumpSuspend"==g[e]._type){ne=new Date(g[e].timestamp);break}var se=e-2;if(se>=0&&"Rewind"==g[se]._type){let e=g[se].timestamp;for(;se-1>=0&&"Prime"==g[se-=1]._type;)ie=(g[se].timestamp-e)/36e5;ie>=ae&&(ne=e,ie=0)}}while(e>0);var le=(ne-oe)/36e5;le0&&(--r,"TempBasal"==g[r]._type)){a=new Date(g[r].timestamp);break}}while(r>0);(a-t)/36e5>0&&(q+=V(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 me=a;let r=e;do{if(--r,r>=0&&("TempBasal"==g[r]._type||"PumpSuspend"==g[r]._type)){me=new Date(g[r].timestamp);break}}while(r>0);if(0==e&&"TempBasalDuration"==g[0]._type&&(me=new Date,t=g[e]["duration (min)"]/60),(me-a)/36e5-t>0){q+=V(me,$(a,t))}}var de,ue={TDD:o(F=E+P+q,5),bolus:o(E,5),temp_basal:o(P,5),scheduled_basal:o(q,5)};U>21?(O=". Bolus insulin: "+E.toPrecision(5)+" U",R=". Temporary basal insulin: "+P.toPrecision(5)+" U",G=". Insulin with scheduled basal rate: "+q.toPrecision(5)+" U",I=A+(" TDD past 24h is: "+F.toPrecision(5)+" U")+O+R+G,tddReason=", Total insulin: "+o(F,2)+" U, "+o(E/F*100,0)+"% Bolus "+o((P+q)/F*100,0)+"% Basal"):tddReason=", TDD: Not enough pumpData (< 21h)";const ce=e.glucose;var ge=h.useNewFormula;const he=h.enableDynamicCR,pe=Math.min(i.autosens_min,i.autosens_max),ve=Math.max(i.autosens_min,i.autosens_max);(ve==pe||ve<1||pe>1)&&(ge=!1,console.log("Dynamic ISF disabled due to current autosens settings"));const fe=h.adjustmentFactor,be=i.min_bg;var Be=!1,Me="",_e=1,ye="";Z>0&&(_e=k/Z),ye=_e>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: "+(_e=o(_e=Math.min(_e,i.autosens_max),2))+". Upper limit = Autosens max ("+i.autosens_max+")":_e<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: "+(_e=o(_e=Math.max(_e,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: "+_e,ye=", Basal ratio: "+_e,(i.high_temptarget_raises_sensitivity||i.exercise_mode||v.isEnabled)&&(Be=!0),be>=118&&Be&&(ge=!1,Me="Dynamic ISF temporarily off due to a high temp target/exercising. Current min target: "+be);var xe=", Dynamic ratios log: ",Se=", AF: "+fe,we="BG: "+ce+" mg/dl ("+(.0555*ce).toPrecision(2)+" mmol/l)",De="",Te="";const Ce=h.curve,Ue=h.insulinPeakTime,Ge=h.useCustomPeakTime;var Oe=55,Re=65;switch(Ce){case"rapid-acting":Re=65;break;case"ultra-rapid":Re=50}Ge?(Oe=120-Ue,console.log("Custom insulinpeakTime set to :"+Ue+", insulinFactor: "+Oe)):(Oe=120-Re,console.log("insulinFactor set to : "+Oe)),de=F,H<1&&k>0&&(F=k,console.log("Using weighted TDD average: "+o(F,2)+" U, instead of past 24 h ("+o(de,2)+" U), weight: "+H),Te=", Weighted TDD: "+o(F,2)+" U");const Ae=h.sigmoid;var Ie="";if(ge){var je=z*fe*F*Math.log(ce/Oe+1)/1800;De=", Logarithmic formula"}if(ge&&Ae){const e=pe,t=ve-e,a=.0555*(ce-i.min_bg);var Fe=_e,Pe=ve-1;1==ve&&(Pe=ve+.01-1);const r=Math.log10(1/Pe-e/Pe)/Math.log10(Math.E),o=a*fe*Fe+r;je=t/(1+Math.exp(-o))+e,De=", Sigmoid function"}var Ee=N;const qe=o(N,1);var We="",ke="";if(ge&&F>0){if(We=", Dynamic ISF/CR: On/",je>ve?(Me=", Dynamic ISF limited by autosens_max setting: "+ve+" ("+o(je,2)+"), ",ke=", Autosens/Dynamic Limit: "+ve+" ("+o(je,2)+")",je=ve):je1&&(Le=(je-1)/2+1);var ze=" CR: "+(Ee=o(Ee/Le,2))+" g/U";N=Ee}else ze=" CR: "+Ee+" g/U",We+="Off";const e=z/je;s.ratio=je,Ie=". Using Sigmoid function, the autosens ratio has been adjusted with sigmoid factor to: "+o(s.ratio,2)+". New ISF = "+o(e,2)+" mg/dl ("+o(.0555*e,2)+" (mmol/l). CR adjusted from "+o(qe,2)+" to "+o(Ee,2),Me+=Ae?Ie:", Dynamic autosens.ratio set to "+o(je,2)+" with ISF: "+e.toPrecision(3)+" mg/dl/U ("+(.0555*e).toPrecision(3)+" mmol/l/U)",I+=xe+we+Se+De+Me+We+ze+Te}else I+=xe+"Dynamic Settings disabled";console.log(I),ge||he?ge&&i.tddAdjBasal?tddReason+=We+De+ke+Se+ye:ge&&!i.tddAdjBasal&&(tddReason+=We+De+ke+Se):tddReason+="";var Ne={},He=new Date;if(c&&(He=c),void 0===i||void 0===i.current_basal)return Ne.error="Error: could not get current basal rate",Ne;var Ze=r(i.current_basal,i)*L,$e=Ze;v.useOverride&&(0==v.duration?console.log("Profile Override is active. Override "+o(100*L,0)+"%. Override Duration: Enabled indefinitely"):console.log("Profile Override is active. Override "+o(100*L,0)+"%. Override Expires in: "+v.duration+" min."));var Je=new Date;c&&(Je=c);var Ke,Qe=new Date(e.date),Ve=o((Je-Qe)/60/1e3,1),Xe=e.glucose,Ye=e.noise;Ke=e.delta>-.5?"+"+o(e.delta,0):o(e.delta,0);var et=Math.min(e.delta,e.short_avgdelta),tt=Math.min(e.short_avgdelta,e.long_avgdelta),at=Math.max(e.delta,e.short_avgdelta,e.long_avgdelta);(Xe<=10||38===Xe||Ye>=3)&&(Ne.reason="CGM is calibrating, in ??? state, or noise is high");if(Xe>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(Xe,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),Ve>12||Ve<-5?Ne.reason="If current system time "+Je+" is correct, then BG data is too old. The last BG data was read "+Ve+"m ago at "+Qe:0===e.short_avgdelta&&0===e.long_avgdelta&&(e.last_cal&&e.last_cal<3?Ne.reason="CGM was just calibrated":Ne.reason="CGM data is unchanged ("+n(Xe,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"),Xe<=10||38===Xe||Ye>=3||Ve>12||Ve<-5||0===e.short_avgdelta&&0===e.long_avgdelta)return t.rate>=$e?(Ne.reason+=". Canceling high temp basal of "+t.rate,Ne.deliverAt=He,Ne.temp="absolute",Ne.duration=0,Ne.rate=0,Ne):0===t.rate&&t.duration>30?(Ne.reason+=". Shortening "+t.duration+"m long zero temp to 30m. ",Ne.deliverAt=He,Ne.temp="absolute",Ne.duration=30,Ne.rate=0,Ne):(Ne.reason+=". Temp "+t.rate+" <= current basal "+$e+"U/hr; doing nothing. ",Ne);var rt,ot,nt,it,st=i.max_iob;if(void 0!==i.min_bg&&(ot=i.min_bg),void 0!==i.max_bg&&(nt=i.max_bg),void 0!==i.enableSMB_high_bg_target&&(it=i.enableSMB_high_bg_target),void 0===i.min_bg||void 0===i.max_bg)return Ne.error="Error: could not determine target_bg. ",Ne;rt=(i.min_bg+i.max_bg)/2;var lt=i.exercise_mode||i.high_temptarget_raises_sensitivity||v.isEnabled,mt=100,dt=160;if(dt=i.half_basal_exercise_target,v.isEnabled){const e=v.hbt;console.log("Half Basal Target used: "+n(e,i)+" "+i.out_units),dt=e}else console.log("Default Half Basal Target used: "+n(dt,i)+" "+i.out_units);if(lt&&i.temptargetSet&&rt>mt||i.low_temptarget_lowers_sensitivity&&i.temptargetSet&&rt=rt&&sensitivityRatio0&&(process.stderr.write("TDD-adjustment of basals activated, using tdd24h_14d_Ratio "+o(_e,2)+", TDD 24h = "+o(de,2)+"U, Weighted average TDD = "+o(k,2)+"U, (Weight percentage = "+H+"), Total data of TDDs (up to 14 days) average = "+o(Z,2)+"U. "),$e!==Ze*L?process.stderr.write("Adjusting basal from "+Ze*L+" U/h to "+$e+" U/h; "):process.stderr.write("Basal unchanged: "+$e+" U/h; "))),i.temptargetSet);else if(void 0!==s&&s&&(i.sensitivity_raises_target&&s.ratio<1||i.resistance_lowers_target&&s.ratio>1)){ot=o((ot-60)/s.ratio)+60,nt=o((nt-60)/s.ratio)+60;var ct=o((rt-60)/s.ratio)+60;rt===(ct=Math.max(80,ct))?process.stderr.write("target_bg unchanged: "+n(ct,i)+"; "):process.stderr.write("target_bg from "+n(ct,i)+" to "+n(ct,i)+"; "),rt=ct}var gt=n(rt,i);rt!=f&&(gt=0!==b&&b!==rt?n(f,i)+"→"+n(b,i)+"→"+n(rt,i):n(f,i)+"→"+n(rt,i));var ht=200,pt=200,vt=200;if(e.noise>=2){var ft=Math.max(1.1,i.noisyCGMTargetMultiplier);Math.min(250,i.maxRaw);ht=o(Math.min(200,ot*ft)),pt=o(Math.min(200,rt*ft)),vt=o(Math.min(200,nt*ft)),process.stderr.write("Raising target_bg for noisy / raw CGM data, from "+n(ct,i)+" to "+n(pt,i)+"; "),ot=ht,rt=pt,nt=vt}var bt=ot-.5*(ot-40),Bt=i.threshold_setting;Bt>bt&&Bt<=120&&Bt>=65?(console.error("Threshold changed in settings from "+n(bt,i)+" to "+n(Bt,i)+". "),bt=Bt):console.error("Current threshold: "+n(bt,i));var Mt="",_t=(o(z,1),z);if(void 0!==s&&s&&((_t=o(_t=z/sensitivityRatio,1))!==z?process.stderr.write("ISF from "+n(z,i)+" to "+n(_t,i)):process.stderr.write("ISF unchanged: "+n(_t,i)),Mt+="Autosens ratio: "+o(sensitivityRatio,2)+", ISF: "+n(z,i)+"→"+n(_t,i)),console.error("CR:"+N),void 0===a)return Ne.error="Error: iob_data undefined. ",Ne;var yt,xt=a;if(a.length,a.length>1&&(a=xt[0]),void 0===a.activity||void 0===a.iob)return Ne.error="Error: iob_data missing some property. ",Ne;var St=((yt=void 0!==a.lastTemp?o((new Date(Je).getTime()-a.lastTemp.date)/6e4):0)+t.duration)%30;if(console.error("currenttemp:"+t.rate+" lastTempAge:"+yt+"m, tempModulus:"+St+"m"),Ne.temp="absolute",Ne.deliverAt=He,d&&t&&a.lastTemp&&t.rate!==a.lastTemp.rate&&yt>10&&t.duration)return Ne.reason="Warning: currenttemp rate "+t.rate+" != lastTemp rate "+a.lastTemp.rate+" from pumphistory; canceling temp",m.setTempBasal(0,0,i,Ne,t);if(t&&a.lastTemp&&t.duration>0){var wt=yt-a.lastTemp.duration;if(wt>5&&yt>10)return Ne.reason="Warning: currenttemp running but lastTemp from pumphistory ended "+wt+"m ago; canceling temp",m.setTempBasal(0,0,i,Ne,t)}var Dt=o(-a.activity*_t*5,2),Tt=o(6*(et-Dt));Tt<0&&(Tt=o(6*(tt-Dt)))<0&&(Tt=o(6*(e.long_avgdelta-Dt)));var Ct=Xe,Ut=(Ct=a.iob>0?o(Xe-a.iob*_t):o(Xe-a.iob*Math.min(_t,z)))+Tt;if(void 0===Ut||isNaN(Ut))return Ne.error="Error: could not calculate eventualBG. Sensitivity: "+_t+" Deviation: "+Tt,Ne;var Gt=function(e,t,a){return o(a+(e-t)/24,1)}(rt,Ut,Dt);Ne={temp:"absolute",bg:Xe,tick:Ke,eventualBG:Ut,insulinReq:0,reservoir:u,deliverAt:He,sensitivityRatio,TDD:de,insulin:ue,current_target:rt};var Ot=[],Rt=[],At=[],It=[];Ot.push(Xe),Rt.push(Xe),It.push(Xe),At.push(Xe);var jt=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,d,l,Xe,rt,it);if(B)if(S){let e=c.getHours();e>=w&&e<=D&&(console.error("SMB disabled by schedule (a Profile is active with SMBs disabled)"),jt=!1)}else console.error("SMBs are disabled (a Profile is active with SMBs disabled)"),jt=!1;var Ft=i.enableUAM,Pt=0,Et=0;Pt=o(et-Dt,1);var qt=o(et-Dt,1);csf=_t/N,console.error("profile.sens:"+n(z,i)+", sens:"+n(_t,i)+", CSF:"+o(csf,1));var Wt=o(30*csf*5/60,1);Pt>Wt&&(console.error("Limiting carb impact from "+Pt+" to "+Wt+"mg/dL/5m (30g/h)"),Pt=Wt);var kt=3;sensitivityRatio&&(kt/=sensitivityRatio);var Lt=kt;if(l.carbs){kt=Math.max(kt,l.mealCOB/20);var zt=o((new Date(Je).getTime()-l.lastCarbTime)/6e4),Nt=(l.carbs-l.mealCOB)/l.carbs;Lt=o(Lt=kt+1.5*zt/60,1),console.error("Last carbs "+zt+" minutes ago; remainingCATime:"+Lt+"hours; "+o(100*Nt,1)+"% carbs absorbed")}var Ht=Math.max(0,Pt/5*60*Lt/2)/csf,Zt=90,$t=1;i.remainingCarbsCap&&(Zt=Math.min(90,i.remainingCarbsCap)),i.remainingCarbsFraction&&($t=Math.min(1,i.remainingCarbsFraction));var Jt=1-$t,Kt=Math.max(0,l.mealCOB-Ht-l.carbs*Jt),Qt=(Kt=Math.min(Zt,Kt))*csf*5/60/(Lt/2),Vt=o(l.slopeFromMaxDeviation,2),Xt=o(l.slopeFromMinDeviation,2),Yt=Math.min(Vt,-Xt/3);Et=0===Pt?0:Math.min(60*Lt/5/2,Math.max(0,l.mealCOB*csf/Pt)),console.error("Carb Impact:"+Pt+"mg/dL per 5m; CI Duration:"+o(5*Et/60*2,1)+"hours; remaining CI ("+Lt/2+"h peak):"+o(Qt,1)+"mg/dL per 5m");var ea,ta,aa,ra,oa,na=999,ia=999,sa=999,la=Xe,ma=999,da=999,ua=999,ca=999,ga=Ut,ha=Xe,pa=Xe,va=0,fa=[],ba=[];try{xt.forEach((function(e){var t=o(-e.activity*_t*5,2),a=o(-e.iobWithZeroTemp.activity*_t*5,2),r=Ct,n=Pt*(1-Math.min(1,Rt.length/12));if(!0===(ge&&!Ae))ga=Rt[Rt.length-1]+o(-e.activity*(1800/(F*fe*Math.log(Math.max(Rt[Rt.length-1],39)/Oe+1)))*5,2)+n,r=It[It.length-1]+o(-e.iobWithZeroTemp.activity*(1800/(F*fe*Math.log(Math.max(It[It.length-1],39)/Oe+1)))*5,2),console.log("Dynamic ISF (Logarithmic Formula) )adjusted predictions for IOB and ZT: IOBpredBG: "+o(ga,2)+" , ZTpredBG: "+o(r,2));else ga=Rt[Rt.length-1]+t+n,r=It[It.length-1]+a;var i=Math.max(0,Math.max(0,Pt)*(1-Ot.length/Math.max(2*Et,1))),s=Math.min(Ot.length,12*Lt-Ot.length),l=Math.max(0,s/(Lt/2*12)*Qt);i+l,fa.push(o(l,0)),ba.push(o(i,0)),COBpredBG=Ot[Ot.length-1]+t+Math.min(0,n)+i+l;var m=Math.max(0,qt+At.length*Yt),d=Math.max(0,qt*(1-At.length/Math.max(36,1))),u=Math.min(m,d);if(u>0&&(va=o(5*(At.length+1)/60,1)),!0===(ge&&!Ae))UAMpredBG=At[At.length-1]+o(-e.activity*(1800/(F*fe*Math.log(Math.max(At[At.length-1],39)/Oe+1)))*5,2)+Math.min(0,n)+u,console.log("Dynamic ISF (Logarithmic Formula) adjusted prediction for UAM: UAMpredBG: "+o(UAMpredBG,2));else UAMpredBG=At[At.length-1]+t+Math.min(0,n)+u;Rt.length<48&&Rt.push(ga),Ot.length<48&&Ot.push(COBpredBG),At.length<48&&At.push(UAMpredBG),It.length<48&&It.push(r),COBpredBG18&&gaha&&(ha=ga),(Et||Qt>0)&&Ot.length>18&&COBpredBG0)&&COBpredBG>ha&&(pa=COBpredBG),Ft&&At.length>12&&UAMpredBGha&&UAMpredBG}))}catch(e){console.error("Problem with iobArray. Optional feature Advanced Meal Assist disabled")}l.mealCOB&&(console.error("predCIs (mg/dL/5m):"+ba.join(" ")),console.error("remainingCIs: "+fa.join(" "))),Ne.predBGs={},Rt.forEach((function(e,t,a){a[t]=o(Math.min(401,Math.max(39,e)))}));for(var Ba=Rt.length-1;Ba>12&&Rt[Ba-1]===Rt[Ba];Ba--)Rt.pop();for(Ne.predBGs.IOB=Rt,aa=o(Rt[Rt.length-1]),It.forEach((function(e,t,a){a[t]=o(Math.min(401,Math.max(39,e)))})),Ba=It.length-1;Ba>6&&!(It[Ba-1]>=It[Ba]||It[Ba]<=rt);Ba--)It.pop();if(Ne.predBGs.ZT=It,o(It[It.length-1]),l.mealCOB>0&&(Pt>0||Qt>0)){for(Ot.forEach((function(e,t,a){a[t]=o(Math.min(401,Math.max(39,e)))})),Ba=Ot.length-1;Ba>12&&Ot[Ba-1]===Ot[Ba];Ba--)Ot.pop();Ne.predBGs.COB=Ot,ra=o(Ot[Ot.length-1]),Ut=Math.max(Ut,o(Ot[Ot.length-1]))}if(Pt>0||Qt>0){if(Ft){for(At.forEach((function(e,t,a){a[t]=o(Math.min(401,Math.max(39,e)))})),Ba=At.length-1;Ba>12&&At[Ba-1]===At[Ba];Ba--)At.pop();Ne.predBGs.UAM=At,oa=o(At[At.length-1]),At[At.length-1]&&(Ut=Math.max(Ut,o(At[At.length-1])))}Ne.eventualBG=Ut}console.error("UAM Impact:"+qt+"mg/dL per 5m; UAM Duration:"+va+"hours"),na=Math.max(39,na),ia=Math.max(39,ia),sa=Math.max(39,sa),ea=o(na);var Ma=l.mealCOB/l.carbs;ta=o(sa<999&&ia<999?(1-Ma)*UAMpredBG+Ma*COBpredBG:ia<999?(ga+COBpredBG)/2:sa<999?(ga+UAMpredBG)/2:ga),ca>ta&&(ta=ca),la=o(la=Et||Qt>0?Ft?Ma*ma+(1-Ma)*da:ma:Ft?da:ua);var _a=sa;if(casa&&(_a=(sa+ca)/2);if(_a=o(_a),l.carbs)if(!Ft&&ia<999)ea=o(Math.max(na,ia));else if(ia<999){var xa=Ma*ia+(1-Ma)*_a;ea=o(Math.max(na,ia,xa))}else ea=Ft?_a:la;else Ft&&(ea=o(Math.max(na,_a)));ea=Math.min(ea,ta),process.stderr.write("minPredBG: "+ea+" minIOBPredBG: "+na+" minZTGuardBG: "+ca),ia<999&&process.stderr.write(" minCOBPredBG: "+ia),sa<999&&process.stderr.write(" minUAMPredBG: "+sa),console.error(" avgPredBG:"+ta+" COB/Carbs:"+l.mealCOB+"/"+l.carbs),pa>Xe&&(ea=Math.min(ea,pa)),Ne.COB=l.mealCOB,Ne.IOB=a.iob,Ne.BGI=n(Dt,i),Ne.deviation=n(Tt,i),Ne.ISF=n(_t,i),Ne.CR=o(N,1),Ne.target_bg=n(rt,i),Ne.TDD=o(de,2),Ne.current_target=o(rt,0);var Sa=Ne.CR;qe!=Ne.CR&&(Sa=qe+"→"+Ne.CR),Ne.reason=Mt+", COB: "+Ne.COB+", Dev: "+Ne.deviation+", BGI: "+Ne.BGI+", CR: "+Sa+", Target: "+gt+", minPredBG "+n(ea,i)+", minGuardBG "+n(la,i)+", IOBpredBG "+n(aa,i),ra>0&&(Ne.reason+=", COBpredBG "+n(ra,i)),oa>0&&(Ne.reason+=", UAMpredBG "+n(oa,i)),Ne.reason+=tddReason,Ne.reason+="; ";var wa=Ct;wa<40&&(wa=Math.min(la,wa));var Da,Ta=bt-wa,Ca=240,Ua=240;if(l.mealCOB>0&&(Pt>0||Qt>0)){for(Ba=0;BaDa*Xe&&(console.error("maxDelta "+n(at,i)+" > "+100*Da+"% of BG "+n(Xe,i)+" - disabling SMB"),Ne.reason+="maxDelta "+n(at,i)+" > "+100*Da+"% of BG "+n(Xe,i)+" - SMB disabled!, ",jt=!1),console.error("BG projected to remain above "+n(ot,i)+" for "+Ca+"minutes"),(Ua<240||Ca<60)&&console.error("BG projected to remain above "+n(bt,i)+" for "+Ua+"minutes");var Ga=Ua,Oa=i.current_basal*L*_t*Ga/60,Ra=Math.max(0,l.mealCOB-.25*l.carbs),Aa=(Ta-Oa)/csf-Ra;Oa=o(Oa),Aa=o(Aa),console.error("naive_eventualBG:",Ct,"bgUndershoot:",Ta,"zeroTempDuration:",Ga,"zeroTempEffect:",Oa,"carbsReq:",Aa),"Could not parse clock data"==l.reason?console.error("carbsReq unknown: Could not parse clock data"):Aa>=i.carbsReqThreshold&&Ua<=45&&(Ne.carbsReq=Aa,Ne.reason+=Aa+" add'l carbs req w/in "+Ua+"m; ");var Ia=0;if(Xe0&&et>Gt)Ne.reason+="IOB "+a.iob+" < "+o(-i.current_basal*L*20/60,2),Ne.reason+=" and minDelta "+n(et,i)+" > expectedDelta "+n(Gt,i)+"; ";else if(Xe=55)return Ne.reason+="; Canceling temp at "+Ne.deliverAt.getMinutes()+"m past the hour. ",m.setTempBasal(0,0,i,Ne,t);var ja=0,Fa=$e;if(UtGt&&et>0&&!Aa)return Ct<40?(Ne.reason+=", naive_eventualBG < 40. ",m.setTempBasal(0,30,i,Ne,t)):(e.delta>et?Ne.reason+=", but Delta "+n(Ke,i)+" > expectedDelta "+n(Gt,i):Ne.reason+=", but Min. Delta "+et.toFixed(2)+" > Exp. Delta "+n(Gt,i),t.duration>15&&r($e,i)===r(t.rate,i)?(Ne.reason+=", temp "+t.rate+" ~ req "+$e+"U/hr. ",Ne):(Ne.reason+="; setting current basal of "+$e+" as temp. ",m.setTempBasal($e,30,i,Ne,t)));ja=o(ja=2*Math.min(0,(Ut-rt)/_t),2);var Pa=Math.min(0,(Ct-rt)/_t);if(Pa=o(Pa,2),et<0&&et>Gt)ja=o(ja*(et/Gt),2);if(Fa=r(Fa=$e+2*ja,i),t.duration*(t.rate-$e)/605&&Fa>=.8*t.rate)return Ne.reason+=", temp "+t.rate+" ~< req "+Fa+"U/hr. ",Ne;if(Fa<=0){if((Ia=o(60*((Ta=rt-Ct)/_t)/i.current_basal*L))<0?Ia=0:(Ia=30*o(Ia/30),Ia=Math.min(120,Math.max(0,Ia))),Ia>0)return Ne.reason+=", setting "+Ia+"m zero temp. ",m.setTempBasal(Fa,Ia,i,Ne,t)}else Ne.reason+=", setting "+Fa+"U/hr. ";return m.setTempBasal(Fa,30,i,Ne,t)}if(et "+n(ot,i)+" but Delta "+n(Ke,i)+" < Exp. Delta "+n(Gt,i):Ne.reason+="Eventual BG "+n(Ut,i)+" > "+n(ot,i)+" but Min. Delta "+et.toFixed(2)+" < Exp. Delta "+n(Gt,i),t.duration>15&&r($e,i)===r(t.rate,i)?(Ne.reason+=", temp "+t.rate+" ~ req "+$e+"U/hr. ",Ne):(Ne.reason+="; setting current basal of "+$e+" as temp. ",m.setTempBasal($e,30,i,Ne,t));if(Math.min(Ut,ea)15&&r($e,i)===r(t.rate,i)?(Ne.reason+=", temp "+t.rate+" ~ req "+$e+"U/hr. ",Ne):(Ne.reason+="; setting current basal of "+$e+" as temp. ",m.setTempBasal($e,30,i,Ne,t));if(Ut>=nt&&(Ne.reason+="Eventual BG "+n(Ut,i)+" >= "+n(nt,i)+", "),a.iob>st)return Ne.reason+="IOB "+o(a.iob,2)+" > max_iob "+st,t.duration>15&&r($e,i)===r(t.rate,i)?(Ne.reason+=", temp "+t.rate+" ~ req "+$e+"U/hr. ",Ne):(Ne.reason+="; setting current basal of "+$e+" as temp. ",m.setTempBasal($e,30,i,Ne,t));(ja=o((Math.min(ea,Ut)-rt)/_t,2))>st-a.iob?(console.error("SMB limited by maxIOB: "+st-a.iob+" (. insulinReq: "+ja+" U)"),Ne.reason+="max_iob "+st+", ",ja=st-a.iob):console.error("SMB not limited by maxIOB ( insulinReq: "+ja+" U)."),Fa=r(Fa=$e+2*ja,i),ja=o(ja,3),Ne.insulinReq=ja;var Ea=o((new Date(Je).getTime()-a.lastBolusTime)/6e4,1);if(d&&jt&&Xe>bt){var qa=30;void 0!==i.maxSMBBasalMinutes&&(qa=i.maxSMBBasalMinutes);var Wa=30;void 0!==i.maxUAMSMBBasalMinutes&&(Wa=i.maxUAMSMBBasalMinutes),v.useOverride&&M&&T!==qa&&(console.error("SMB Max Minutes - setting overriden from "+qa+" to "+T),qa=T),v.useOverride&&M&&C!==Wa&&(console.error("UAM Max Minutes - setting overriden from "+Wa+" to "+C),Wa=C);var ka=o(l.mealCOB/N,3),La=0;void 0===qa?(La=o(i.current_basal*L*30/60,1),console.error("smbMinutesSetting undefined: defaulting to 30m"),ja>La&&console.error("SMB limited by maxBolus: "+La+" ( "+ja+" U)")):a.iob>ka&&a.iob>0?(console.error("IOB"+a.iob+"> COB"+l.mealCOB+"; mealInsulinReq ="+ka),Wa?(console.error("maxUAMSMBBasalMinutes: "+Wa+", profile.current_basal: "+i.current_basal*L),La=o(i.current_basal*L*Wa/60,1)):(console.error("maxUAMSMBBasalMinutes undefined: defaulting to 30m"),La=o(i.current_basal*L*30/60,1)),ja>La?console.error("SMB limited by maxUAMSMBBasalMinutes [ "+Wa+"m ]: "+La+"U ( "+ja+"U )"):console.error("SMB is not limited by maxUAMSMBBasalMinutes. ( insulinReq: "+ja+"U )")):(console.error(".maxSMBBasalMinutes: "+qa+", profile.current_basal: "+i.current_basal*L),ja>(La=o(i.current_basal*qa/60,1))?console.error("SMB limited by maxSMBBasalMinutes: "+qa+"m ]: "+La+"U ( insulinReq: "+ja+"U )"):console.error("SMB is not limited by maxSMBBasalMinutes. ( insulinReq: "+ja+"U )"));var za=i.bolus_increment,Na=1/za,Ha=i.smb_delivery_ratio;Ha>.5&&console.error("SMB Delivery Ratio increased from default 0.5 to "+o(Ha,2));var Za=Math.min(ja*Ha,La);Za=Math.floor(Za*Na)/Na,Ia=o(60*((rt-(Ct+na)/2)/_t)/i.current_basal*L),ja>0&&Za=30?(Ia=30*o(Ia/30),Ia=Math.min(60,Math.max(0,Ia))):($a=o($e*Ia/30,2),Ia=30),Ne.reason+=" insulinReq "+ja,Za>=La&&(Ne.reason+="; maxBolus "+La),Ia>0&&(Ne.reason+="; setting "+Ia+"m low temp of "+$a+"U/h"),Ne.reason+=". ";var Ja=3;i.SMBInterval&&(Ja=Math.min(10,Math.max(1,i.SMBInterval)));var Ka=o(Ja-Ea,0),Qa=o(60*(Ja-Ea),0)%60;if(console.error("naive_eventualBG "+Ct+","+Ia+"m "+$a+"U/h temp needed; last bolus "+Ea+"m ago; maxBolus: "+La),Ea>Ja?Za>0&&(Ne.units=Za,Ne.reason+="Microbolusing "+Za+"U. "):Ne.reason+="Waiting "+Ka+"m "+Qa+"s to microbolus again. ",Ia>0)return Ne.rate=$a,Ne.duration=Ia,Ne}var Va=m.getMaxSafeBasal(i);return Fa>Va&&(Ne.reason+="adj. req. rate: "+Fa+" to maxSafeBasal: "+o(Va,2)+", ",Fa=r(Va,i)),t.duration*(t.rate-$e)/60>=2*ja?(Ne.reason+=t.duration+"m@"+t.rate.toFixed(2)+" > 2 * insulinReq. Setting temp basal of "+Fa+"U/hr. ",m.setTempBasal(Fa,30,i,Ne,t)):void 0===t.duration||0===t.duration?(Ne.reason+="no temp, setting "+Fa+"U/hr. ",m.setTempBasal(Fa,30,i,Ne,t)):t.duration>5&&r(Fa,i)<=r(t.rate,i)?(Ne.reason+="temp "+t.rate+" >~ req "+Fa+"U/hr. ",Ne):(Ne.reason+="temp "+t.rate+"<"+Fa+"U/hr. ",m.setTempBasal(Fa,30,i,Ne,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})();