import * as Common from "../../core/common/common.js";
import * as Host from "../../core/host/host.js";
import * as i18n from "../../core/i18n/i18n.js";
import * as SDK from "../../core/sdk/sdk.js";
import * as UI from "../../ui/legacy/legacy.js";
import { MobileThrottlingSelector } from "./MobileThrottlingSelector.js";
import { NetworkThrottlingSelector } from "./NetworkThrottlingSelector.js";
import { ThrottlingPresets } from "./ThrottlingPresets.js";
const UIStrings = {
  sS: "{PH1}: {PH2}",
  add: "Add\u2026",
  addS: "Add {PH1}",
  offline: "Offline",
  forceDisconnectedFromNetwork: "Force disconnected from network",
  throttling: "Throttling",
  cpuThrottlingIsEnabled: "CPU throttling is enabled",
  cpuThrottling: "CPU throttling",
  noThrottling: "No throttling",
  dSlowdown: "{PH1}\xD7 slowdown"
};
const str_ = i18n.i18n.registerUIStrings("panels/mobile_throttling/ThrottlingManager.ts", UIStrings);
const i18nString = i18n.i18n.getLocalizedString.bind(void 0, str_);
let throttlingManagerInstance;
export class ThrottlingManager {
  cpuThrottlingControls;
  cpuThrottlingRates;
  customNetworkConditionsSetting;
  currentNetworkThrottlingConditionsSetting;
  lastNetworkThrottlingConditions;
  cpuThrottlingManager;
  constructor() {
    this.cpuThrottlingManager = SDK.CPUThrottlingManager.CPUThrottlingManager.instance();
    this.cpuThrottlingControls = /* @__PURE__ */ new Set();
    this.cpuThrottlingRates = ThrottlingPresets.cpuThrottlingPresets;
    this.customNetworkConditionsSetting = Common.Settings.Settings.instance().moduleSetting("customNetworkConditions");
    this.currentNetworkThrottlingConditionsSetting = Common.Settings.Settings.instance().createSetting("preferredNetworkCondition", SDK.NetworkManager.NoThrottlingConditions);
    this.currentNetworkThrottlingConditionsSetting.setSerializer(new SDK.NetworkManager.ConditionsSerializer());
    SDK.NetworkManager.MultitargetNetworkManager.instance().addEventListener(SDK.NetworkManager.MultitargetNetworkManager.Events.ConditionsChanged, () => {
      this.lastNetworkThrottlingConditions = this.currentNetworkThrottlingConditionsSetting.get();
      this.currentNetworkThrottlingConditionsSetting.set(SDK.NetworkManager.MultitargetNetworkManager.instance().networkConditions());
    });
    if (this.isDirty()) {
      SDK.NetworkManager.MultitargetNetworkManager.instance().setNetworkConditions(this.currentNetworkThrottlingConditionsSetting.get());
    }
  }
  static instance(opts = { forceNew: null }) {
    const { forceNew } = opts;
    if (!throttlingManagerInstance || forceNew) {
      throttlingManagerInstance = new ThrottlingManager();
    }
    return throttlingManagerInstance;
  }
  decorateSelectWithNetworkThrottling(selectElement) {
    let options = [];
    const selector = new NetworkThrottlingSelector(populate, select, this.customNetworkConditionsSetting);
    selectElement.addEventListener("change", optionSelected, false);
    return selector;
    function populate(groups) {
      selectElement.removeChildren();
      options = [];
      for (let i = 0; i < groups.length; ++i) {
        const group = groups[i];
        const groupElement = selectElement.createChild("optgroup");
        groupElement.label = group.title;
        for (const conditions of group.items) {
          const title = typeof conditions.title === "function" ? conditions.title() : conditions.title;
          const option = new Option(title, title);
          UI.ARIAUtils.setAccessibleName(option, i18nString(UIStrings.sS, { PH1: group.title, PH2: title }));
          groupElement.appendChild(option);
          options.push(conditions);
        }
        if (i === groups.length - 1) {
          const option = new Option(i18nString(UIStrings.add), i18nString(UIStrings.add));
          UI.ARIAUtils.setAccessibleName(option, i18nString(UIStrings.addS, { PH1: group.title }));
          groupElement.appendChild(option);
          options.push(null);
        }
      }
      return options;
    }
    function optionSelected() {
      if (selectElement.selectedIndex === selectElement.options.length - 1) {
        selector.revealAndUpdate();
      } else {
        const option = options[selectElement.selectedIndex];
        if (option) {
          selector.optionSelected(option);
        }
      }
    }
    function select(index) {
      if (selectElement.selectedIndex !== index) {
        selectElement.selectedIndex = index;
      }
    }
  }
  createOfflineToolbarCheckbox() {
    const checkbox = new UI.Toolbar.ToolbarCheckbox(i18nString(UIStrings.offline), i18nString(UIStrings.forceDisconnectedFromNetwork), forceOffline.bind(this));
    SDK.NetworkManager.MultitargetNetworkManager.instance().addEventListener(SDK.NetworkManager.MultitargetNetworkManager.Events.ConditionsChanged, networkConditionsChanged);
    checkbox.setChecked(SDK.NetworkManager.MultitargetNetworkManager.instance().networkConditions() === SDK.NetworkManager.OfflineConditions);
    function forceOffline() {
      if (checkbox.checked()) {
        SDK.NetworkManager.MultitargetNetworkManager.instance().setNetworkConditions(SDK.NetworkManager.OfflineConditions);
      } else {
        SDK.NetworkManager.MultitargetNetworkManager.instance().setNetworkConditions(this.lastNetworkThrottlingConditions);
      }
    }
    function networkConditionsChanged() {
      checkbox.setChecked(SDK.NetworkManager.MultitargetNetworkManager.instance().networkConditions() === SDK.NetworkManager.OfflineConditions);
    }
    return checkbox;
  }
  createMobileThrottlingButton() {
    const button = new UI.Toolbar.ToolbarMenuButton(appendItems);
    button.setTitle(i18nString(UIStrings.throttling));
    button.setGlyph("");
    button.turnIntoSelect();
    button.setDarkText();
    let options = [];
    let selectedIndex = -1;
    const selector = new MobileThrottlingSelector(populate, select);
    return button;
    function appendItems(contextMenu) {
      for (let index = 0; index < options.length; ++index) {
        const conditions = options[index];
        if (!conditions) {
          continue;
        }
        if (conditions.title === ThrottlingPresets.getCustomConditions().title && conditions.description === ThrottlingPresets.getCustomConditions().description) {
          continue;
        }
        contextMenu.defaultSection().appendCheckboxItem(conditions.title, selector.optionSelected.bind(selector, conditions), selectedIndex === index);
      }
    }
    function populate(groups) {
      options = [];
      for (const group of groups) {
        for (const conditions of group.items) {
          options.push(conditions);
        }
        options.push(null);
      }
      return options;
    }
    function select(index) {
      selectedIndex = index;
      const option = options[index];
      if (option) {
        button.setText(option.title);
        button.setTitle(option.description);
      }
    }
  }
  setCPUThrottlingRate(rate) {
    this.cpuThrottlingManager.setCPUThrottlingRate(rate);
    let icon = null;
    if (rate !== SDK.CPUThrottlingManager.CPUThrottlingRates.NoThrottling) {
      Host.userMetrics.actionTaken(Host.UserMetrics.Action.CpuThrottlingEnabled);
      icon = UI.Icon.Icon.create("smallicon-warning");
      UI.Tooltip.Tooltip.install(icon, i18nString(UIStrings.cpuThrottlingIsEnabled));
    }
    const index = this.cpuThrottlingRates.indexOf(rate);
    for (const control of this.cpuThrottlingControls) {
      control.setSelectedIndex(index);
    }
    UI.InspectorView.InspectorView.instance().setPanelIcon("timeline", icon);
  }
  createCPUThrottlingSelector() {
    const control = new UI.Toolbar.ToolbarComboBox((event) => this.setCPUThrottlingRate(this.cpuThrottlingRates[event.target.selectedIndex]), i18nString(UIStrings.cpuThrottling));
    this.cpuThrottlingControls.add(control);
    const currentRate = this.cpuThrottlingManager.cpuThrottlingRate();
    for (let i = 0; i < this.cpuThrottlingRates.length; ++i) {
      const rate = this.cpuThrottlingRates[i];
      const title = rate === 1 ? i18nString(UIStrings.noThrottling) : i18nString(UIStrings.dSlowdown, { PH1: rate });
      const option = control.createOption(title);
      control.addOption(option);
      if (currentRate === rate) {
        control.setSelectedIndex(i);
      }
    }
    return control;
  }
  isDirty() {
    const networkConditions = SDK.NetworkManager.MultitargetNetworkManager.instance().networkConditions();
    const knownCurrentConditions = this.currentNetworkThrottlingConditionsSetting.get();
    return !SDK.NetworkManager.networkConditionsEqual(networkConditions, knownCurrentConditions);
  }
}
let actionDelegateInstance;
export class ActionDelegate {
  static instance(opts = { forceNew: null }) {
    const { forceNew } = opts;
    if (!actionDelegateInstance || forceNew) {
      actionDelegateInstance = new ActionDelegate();
    }
    return actionDelegateInstance;
  }
  handleAction(context, actionId) {
    if (actionId === "network-conditions.network-online") {
      SDK.NetworkManager.MultitargetNetworkManager.instance().setNetworkConditions(SDK.NetworkManager.NoThrottlingConditions);
      return true;
    }
    if (actionId === "network-conditions.network-low-end-mobile") {
      SDK.NetworkManager.MultitargetNetworkManager.instance().setNetworkConditions(SDK.NetworkManager.Slow3GConditions);
      return true;
    }
    if (actionId === "network-conditions.network-mid-tier-mobile") {
      SDK.NetworkManager.MultitargetNetworkManager.instance().setNetworkConditions(SDK.NetworkManager.Fast3GConditions);
      return true;
    }
    if (actionId === "network-conditions.network-offline") {
      SDK.NetworkManager.MultitargetNetworkManager.instance().setNetworkConditions(SDK.NetworkManager.OfflineConditions);
      return true;
    }
    return false;
  }
}
export function throttlingManager() {
  return ThrottlingManager.instance();
}
//# sourceMappingURL=ThrottlingManager.js.map
