import*as Common from'../common/common.js';import*as SDK from'../sdk/sdk.js';import{Events,throttlingManager}from'./ThrottlingManager.js';import{advancedMobilePresets,CustomConditions,mobilePresets,NoThrottlingConditions,}from'./ThrottlingPresets.js';export class MobileThrottlingSelector{constructor(populateCallback,selectCallback){this._populateCallback=populateCallback;this._selectCallback=selectCallback;throttlingManager().addEventListener(Events.RateChanged,this._conditionsChanged,this);self.SDK.multitargetNetworkManager.addEventListener(SDK.NetworkManager.MultitargetNetworkManager.Events.ConditionsChanged,this._conditionsChanged,this);this._options=this._populateOptions();this._conditionsChanged();}
optionSelected(conditions){self.SDK.multitargetNetworkManager.setNetworkConditions(conditions.network);throttlingManager().setCPUThrottlingRate(conditions.cpuThrottlingRate);}
_populateOptions(){const disabledGroup={title:Common.UIString.UIString('Disabled'),items:[NoThrottlingConditions]};const presetsGroup={title:Common.UIString.UIString('Presets'),items:mobilePresets};const advancedGroup={title:Common.UIString.UIString('Advanced'),items:advancedMobilePresets};return this._populateCallback([disabledGroup,presetsGroup,advancedGroup]);}
_conditionsChanged(){const networkConditions=self.SDK.multitargetNetworkManager.networkConditions();const cpuThrottlingRate=throttlingManager().cpuThrottlingRate();for(let index=0;index<this._options.length;++index){const option=this._options[index];if(option&&option.network===networkConditions&&option.cpuThrottlingRate===cpuThrottlingRate){this._selectCallback(index);return;}}
this._selectCallback(this._options.indexOf(CustomConditions));}}