import*as Common from"../common/common.js";import*as i18n from"../i18n/i18n.js";const UIStrings={sLatencySDownloadSTotal:"{PH1} latency, {PH2} download ({PH3} total)",sLatency:"{PH1} latency",sDownload:"{PH1} download",sFromServiceworker:"{PH1} (from `ServiceWorker`)",sFromCache:"{PH1} (from cache)"},str_=i18n.i18n.registerUIStrings("network/NetworkTimeCalculator.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);export class NetworkTimeBoundary{constructor(t,r){this.minimum=t,this.maximum=r}equals(t){return this.minimum===t.minimum&&this.maximum===t.maximum}}export class NetworkTimeCalculator extends Common.ObjectWrapper.ObjectWrapper{constructor(t){super(),this.startAtZero=t,this._minimumBoundary=-1,this._maximumBoundary=-1,this._boundryChangedEventThrottler=new Common.Throttler.Throttler(0),this._window=null}setWindow(t){this._window=t,this._boundaryChanged()}setInitialUserFriendlyBoundaries(){this._minimumBoundary=0,this._maximumBoundary=1}computePosition(t){return(t-this.minimumBoundary())/this.boundarySpan()*(this._workingArea||0)}formatValue(t,r){return Number.secondsToString(t,Boolean(r))}minimumBoundary(){return this._window?this._window.minimum:this._minimumBoundary}zeroTime(){return this._minimumBoundary}maximumBoundary(){return this._window?this._window.maximum:this._maximumBoundary}boundary(){return new NetworkTimeBoundary(this.minimumBoundary(),this.maximumBoundary())}boundarySpan(){return this.maximumBoundary()-this.minimumBoundary()}reset(){this._minimumBoundary=-1,this._maximumBoundary=-1,this._boundaryChanged()}_value(){return 0}setDisplayWidth(t){this._workingArea=t}computeBarGraphPercentages(t){let r,i,n;return r=-1!==t.startTime?(t.startTime-this.minimumBoundary())/this.boundarySpan()*100:0,i=-1!==t.responseReceivedTime?(t.responseReceivedTime-this.minimumBoundary())/this.boundarySpan()*100:this.startAtZero?r:100,n=-1!==t.endTime?(t.endTime-this.minimumBoundary())/this.boundarySpan()*100:this.startAtZero?i:100,this.startAtZero&&(n-=r,i-=r,r=0),{start:r,middle:i,end:n}}computePercentageFromEventTime(t){return-1===t||this.startAtZero?0:(t-this.minimumBoundary())/this.boundarySpan()*100}percentageToTime(t){return t*this.boundarySpan()/100+this.minimumBoundary()}_boundaryChanged(){this._boundryChangedEventThrottler.schedule((async()=>{this.dispatchEventToListeners(Events.BoundariesChanged)}))}updateBoundariesForEventTime(t){-1===t||this.startAtZero||(void 0===this._maximumBoundary||t>this._maximumBoundary)&&(this._maximumBoundary=t,this._boundaryChanged())}computeBarGraphLabels(t){let r="";-1!==t.responseReceivedTime&&-1!==t.endTime&&(r=Number.secondsToString(t.endTime-t.responseReceivedTime));const i=t.latency>0,n=i?Number.secondsToString(t.latency):r;if(t.timing)return{left:n,right:r,tooltip:void 0};let e;if(i&&r){const i=Number.secondsToString(t.duration);e=i18nString(UIStrings.sLatencySDownloadSTotal,{PH1:n,PH2:r,PH3:i})}else i?e=i18nString(UIStrings.sLatency,{PH1:n}):r&&(e=i18nString(UIStrings.sDownload,{PH1:r}));return t.fetchedViaServiceWorker?e=i18nString(UIStrings.sFromServiceworker,{PH1:e}):t.cached()&&(e=i18nString(UIStrings.sFromCache,{PH1:e})),{left:n,right:r,tooltip:e}}updateBoundaries(t){const r=this._lowerBound(t),i=this._upperBound(t);let n=!1;(-1!==r||this.startAtZero)&&(n=this._extendBoundariesToIncludeTimestamp(this.startAtZero?0:r)),-1!==i&&(n=this._extendBoundariesToIncludeTimestamp(i)||n),n&&this._boundaryChanged()}_extendBoundariesToIncludeTimestamp(t){const r=this._minimumBoundary,i=this._maximumBoundary,n=_minimumSpread;return-1===this._minimumBoundary||-1===this._maximumBoundary?(this._minimumBoundary=t,this._maximumBoundary=t+n):(this._minimumBoundary=Math.min(t,this._minimumBoundary),this._maximumBoundary=Math.max(t,this._minimumBoundary+n,this._maximumBoundary)),r!==this._minimumBoundary||i!==this._maximumBoundary}_lowerBound(t){return 0}_upperBound(t){return 0}}export const _minimumSpread=.1;export var Events;!function(t){t.BoundariesChanged="BoundariesChanged"}(Events||(Events={}));export class NetworkTransferTimeCalculator extends NetworkTimeCalculator{constructor(){super(!1)}formatValue(t,r){return Number.secondsToString(t-this.zeroTime(),Boolean(r))}_lowerBound(t){return t.issueTime()}_upperBound(t){return t.endTime}}export class NetworkTransferDurationCalculator extends NetworkTimeCalculator{constructor(){super(!0)}formatValue(t,r){return Number.secondsToString(t,Boolean(r))}_upperBound(t){return t.duration}}