import*as Common from"../common/common.js";import*as EventListeners from"../event_listeners/event_listeners.js";import*as i18n from"../i18n/i18n.js";import*as SDK from"../sdk/sdk.js";import*as UI from"../ui/ui.js";const UIStrings={frameworkListeners:"`Framework` listeners",refresh:"Refresh",showListenersOnTheAncestors:"Show listeners on the ancestors",ancestors:"Ancestors",eventListenersCategory:"Event listeners category",all:"All",passive:"Passive",blocking:"Blocking",resolveEventListenersBoundWith:"Resolve event listeners bound with framework"},str_=i18n.i18n.registerUIStrings("elements/EventListenersWidget.js",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);let eventListenersWidgetInstance;export class EventListenersWidget extends UI.ThrottledWidget.ThrottledWidget{constructor(){super(),this._toolbarItems=[],this._showForAncestorsSetting=Common.Settings.Settings.instance().moduleSetting("showEventListenersForAncestors"),this._showForAncestorsSetting.addChangeListener(this.update.bind(this)),this._dispatchFilterBySetting=Common.Settings.Settings.instance().createSetting("eventListenerDispatchFilterType",DispatchFilterBy.All),this._dispatchFilterBySetting.addChangeListener(this.update.bind(this)),this._showFrameworkListenersSetting=Common.Settings.Settings.instance().createSetting("showFrameowkrListeners",!0),this._showFrameworkListenersSetting.setTitle(i18nString(UIStrings.frameworkListeners)),this._showFrameworkListenersSetting.addChangeListener(this._showFrameworkListenersChanged.bind(this)),this._eventListenersView=new EventListeners.EventListenersView.EventListenersView(this.update.bind(this)),this._eventListenersView.show(this.element);const e=new UI.Toolbar.ToolbarButton(i18nString(UIStrings.refresh),"largeicon-refresh");e.addEventListener(UI.Toolbar.ToolbarButton.Events.Click,this.update.bind(this)),this._toolbarItems.push(e),this._toolbarItems.push(new UI.Toolbar.ToolbarSettingCheckbox(this._showForAncestorsSetting,i18nString(UIStrings.showListenersOnTheAncestors),i18nString(UIStrings.ancestors)));const t=new UI.Toolbar.ToolbarComboBox(this._onDispatchFilterTypeChanged.bind(this),i18nString(UIStrings.eventListenersCategory));function s(e,s){const i=t.createOption(e,s);s===this._dispatchFilterBySetting.get()&&t.select(i)}s.call(this,i18nString(UIStrings.all),DispatchFilterBy.All),s.call(this,i18nString(UIStrings.passive),DispatchFilterBy.Passive),s.call(this,i18nString(UIStrings.blocking),DispatchFilterBy.Blocking),t.setMaxWidth(200),this._toolbarItems.push(t),this._toolbarItems.push(new UI.Toolbar.ToolbarSettingCheckbox(this._showFrameworkListenersSetting,i18nString(UIStrings.resolveEventListenersBoundWith))),UI.Context.Context.instance().addFlavorChangeListener(SDK.DOMModel.DOMNode,this.update,this),this.update()}static instance(e={forceNew:null}){const{forceNew:t}=e;return eventListenersWidgetInstance&&!t||(eventListenersWidgetInstance=new EventListenersWidget),eventListenersWidgetInstance}doUpdate(){this._lastRequestedNode&&(this._lastRequestedNode.domModel().runtimeModel().releaseObjectGroup(_objectGroupName),delete this._lastRequestedNode);const e=UI.Context.Context.instance().flavor(SDK.DOMModel.DOMNode);if(!e)return this._eventListenersView.reset(),this._eventListenersView.addEmptyHolderIfNeeded(),Promise.resolve();this._lastRequestedNode=e;const t=!this._showForAncestorsSetting.get(),s=[];if(s.push(e.resolveToObject(_objectGroupName)),!t){let t=e.parentNode;for(;t;)s.push(t.resolveToObject(_objectGroupName)),t=t.parentNode;s.push(this._windowObjectInNodeContext(e))}return Promise.all(s).then(this._eventListenersView.addObjects.bind(this._eventListenersView)).then(this._showFrameworkListenersChanged.bind(this))}toolbarItems(){return this._toolbarItems}_onDispatchFilterTypeChanged(e){const t=e.target;this._dispatchFilterBySetting.set(t.value)}_showFrameworkListenersChanged(){const e=this._dispatchFilterBySetting.get(),t=e===DispatchFilterBy.All||e===DispatchFilterBy.Passive,s=e===DispatchFilterBy.All||e===DispatchFilterBy.Blocking;this._eventListenersView.showFrameworkListeners(this._showFrameworkListenersSetting.get(),t,s)}_windowObjectInNodeContext(e){const t=e.domModel().runtimeModel().executionContexts();let s=t[0];if(e.frameId())for(let i=0;i<t.length;++i){const n=t[i];n.frameId===e.frameId()&&n.isDefault&&(s=n)}return s.evaluate({expression:"self",objectGroup:_objectGroupName,includeCommandLineAPI:!1,silent:!0,returnByValue:!1,generatePreview:!1,throwOnSideEffect:void 0,timeout:void 0,disableBreaks:void 0,replMode:void 0,allowUnsafeEvalBlockedByCSP:void 0},!1,!1).then((e=>"object"in e?e.object:null))}_eventListenersArrivedForTest(){}}export const DispatchFilterBy={All:"All",Blocking:"Blocking",Passive:"Passive"};export const _objectGroupName="event-listeners-panel";