import*as i18n from"../i18n/i18n.js";import*as SDK from"../sdk/sdk.js";import{Events,throttlingManager}from"./ThrottlingManager.js";import{ThrottlingPresets}from"./ThrottlingPresets.js";const UIStrings={disabled:"Disabled",presets:"Presets",advanced:"Advanced"},str_=i18n.i18n.registerUIStrings("mobile_throttling/MobileThrottlingSelector.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);export class MobileThrottlingSelector{constructor(t,e){this._populateCallback=t,this._selectCallback=e,throttlingManager().addEventListener(Events.RateChanged,this._conditionsChanged,this),SDK.NetworkManager.MultitargetNetworkManager.instance().addEventListener(SDK.NetworkManager.MultitargetNetworkManager.Events.ConditionsChanged,this._conditionsChanged,this),this._options=this._populateOptions(),this._conditionsChanged()}optionSelected(t){SDK.NetworkManager.MultitargetNetworkManager.instance().setNetworkConditions(t.network),throttlingManager().setCPUThrottlingRate(t.cpuThrottlingRate)}_populateOptions(){const t={title:i18nString(UIStrings.disabled),items:[ThrottlingPresets.getNoThrottlingConditions()]},e={title:i18nString(UIStrings.presets),items:ThrottlingPresets.getMobilePresets()},i={title:i18nString(UIStrings.advanced),items:ThrottlingPresets.getAdvancedMobilePresets()};return this._populateCallback([t,e,i])}_conditionsChanged(){const t=SDK.NetworkManager.MultitargetNetworkManager.instance().networkConditions(),e=throttlingManager().cpuThrottlingRate();for(let n=0;n<this._options.length;++n){const i=this._options[n];if(i&&"network"in i&&i.network===t&&i.cpuThrottlingRate===e)return void this._selectCallback(n)}const i=ThrottlingPresets.getCustomConditions();for(let n=0;n<this._options.length;++n){const t=this._options[n];if(t&&t.title===i.title&&t.description===i.description)return void this._selectCallback(n)}}}