import*as Common from"../common/common.js";import*as i18n from"../i18n/i18n.js";import*as SDK from"../sdk/sdk.js";import{ThrottlingPresets}from"./ThrottlingPresets.js";const UIStrings={disabled:"Disabled",presets:"Presets",custom:"Custom"},str_=i18n.i18n.registerUIStrings("mobile_throttling/NetworkThrottlingSelector.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);export class NetworkThrottlingSelector{constructor(t,e,i){this._populateCallback=t,this._selectCallback=e,this._customNetworkConditionsSetting=i,this._customNetworkConditionsSetting.addChangeListener(this._populateOptions,this),SDK.NetworkManager.MultitargetNetworkManager.instance().addEventListener(SDK.NetworkManager.MultitargetNetworkManager.Events.ConditionsChanged,(()=>{this._networkConditionsChanged()}),this),this._populateOptions()}revealAndUpdate(){Common.Revealer.reveal(this._customNetworkConditionsSetting),this._networkConditionsChanged()}optionSelected(t){SDK.NetworkManager.MultitargetNetworkManager.instance().setNetworkConditions(t)}_populateOptions(){const t={title:i18nString(UIStrings.disabled),items:[SDK.NetworkManager.NoThrottlingConditions]},e={title:i18nString(UIStrings.presets),items:ThrottlingPresets.networkPresets},i={title:i18nString(UIStrings.custom),items:this._customNetworkConditionsSetting.get()};if(this._options=this._populateCallback([t,e,i]),!this._networkConditionsChanged())for(let o=this._options.length-1;o>=0;o--)if(this._options[o]){this.optionSelected(this._options[o]);break}}_networkConditionsChanged(){const t=SDK.NetworkManager.MultitargetNetworkManager.instance().networkConditions();for(let e=0;e<this._options.length;++e){const i=this._options[e];if(i&&i.download===t.download&&i.upload===t.upload&&i.latency===t.latency&&i.title===t.title)return this._selectCallback(e),!0}return!1}}