import*as Common from"../common/common.js";import*as Root from"../root/root.js";import*as SDK from"../sdk/sdk.js";import*as UI from"../ui/ui.js";import{ElementsPanel}from"./ElementsPanel.js";let inspectElementModeController,toggleSearchActionDelegateInstance;export class InspectElementModeController{constructor(){this._toggleSearchAction=UI.ActionRegistry.ActionRegistry.instance().action("elements.toggle-element-search"),this._mode=Protocol.Overlay.InspectMode.None,SDK.SDKModel.TargetManager.instance().addEventListener(SDK.SDKModel.Events.SuspendStateChanged,this._suspendStateChanged,this),SDK.SDKModel.TargetManager.instance().addModelListener(SDK.OverlayModel.OverlayModel,SDK.OverlayModel.Events.ExitedInspectMode,(()=>this._setMode(Protocol.Overlay.InspectMode.None))),SDK.OverlayModel.OverlayModel.setInspectNodeHandler(this._inspectNode.bind(this)),SDK.SDKModel.TargetManager.instance().observeModels(SDK.OverlayModel.OverlayModel,this),this._showDetailedInspectTooltipSetting=Common.Settings.Settings.instance().moduleSetting("showDetailedInspectTooltip"),this._showDetailedInspectTooltipSetting.addChangeListener(this._showDetailedInspectTooltipChanged.bind(this)),document.addEventListener("keydown",(e=>{e.keyCode===UI.KeyboardShortcut.Keys.Esc.code&&this._isInInspectElementMode()&&(this._setMode(Protocol.Overlay.InspectMode.None),e.consume(!0))}),!0)}static instance({forceNew:e}={forceNew:!1}){return inspectElementModeController&&!e||(inspectElementModeController=new InspectElementModeController),inspectElementModeController}modelAdded(e){this._mode!==Protocol.Overlay.InspectMode.None&&e.setInspectMode(this._mode,this._showDetailedInspectTooltipSetting.get())}modelRemoved(e){}_isInInspectElementMode(){return this._mode!==Protocol.Overlay.InspectMode.None}_toggleInspectMode(){let e;e=this._isInInspectElementMode()?Protocol.Overlay.InspectMode.None:Common.Settings.Settings.instance().moduleSetting("showUAShadowDOM").get()?Protocol.Overlay.InspectMode.SearchForUAShadowDOM:Protocol.Overlay.InspectMode.SearchForNode,this._setMode(e)}_captureScreenshotMode(){this._setMode(Protocol.Overlay.InspectMode.CaptureAreaScreenshot)}_setMode(e){if(!SDK.SDKModel.TargetManager.instance().allTargetsSuspended()){this._mode=e;for(const t of SDK.SDKModel.TargetManager.instance().models(SDK.OverlayModel.OverlayModel))t.setInspectMode(e,this._showDetailedInspectTooltipSetting.get());this._toggleSearchAction&&this._toggleSearchAction.setToggled(this._isInInspectElementMode())}}_suspendStateChanged(){SDK.SDKModel.TargetManager.instance().allTargetsSuspended()&&(this._mode=Protocol.Overlay.InspectMode.None,this._toggleSearchAction&&this._toggleSearchAction.setToggled(!1))}_inspectNode(e){ElementsPanel.instance().revealAndSelectNode(e,!0,!0)}_showDetailedInspectTooltipChanged(){this._setMode(this._mode)}}export class ToggleSearchActionDelegate{handleAction(e,t){return!Root.Runtime.Runtime.queryParam("isSharedWorker")&&(inspectElementModeController=InspectElementModeController.instance(),!!inspectElementModeController&&("elements.toggle-element-search"===t?inspectElementModeController._toggleInspectMode():"elements.capture-area-screenshot"===t&&inspectElementModeController._captureScreenshotMode(),!0))}static instance(e={forceNew:null}){const{forceNew:t}=e;return toggleSearchActionDelegateInstance&&!t||(toggleSearchActionDelegateInstance=new ToggleSearchActionDelegate),toggleSearchActionDelegateInstance}}