import*as Root from"../root/root.js";import*as SDK from"../sdk/sdk.js";import*as UI from"../ui/ui.js";import{DeviceModeModel}from"./DeviceModeModel.js";import{DeviceModeView}from"./DeviceModeView.js";let deviceModeWrapperInstance,actionDelegateInstance;export class DeviceModeWrapper extends UI.Widget.VBox{constructor(e){super(),this._inspectedPagePlaceholder=e,this._deviceModeView=null,this._toggleDeviceModeAction=UI.ActionRegistry.ActionRegistry.instance().action("emulation.toggle-device-mode");const t=DeviceModeModel.instance();this._showDeviceModeSetting=t.enabledSetting(),this._showDeviceModeSetting.setRequiresUserAction(Boolean(Root.Runtime.Runtime.queryParam("hasOtherClients"))),this._showDeviceModeSetting.addChangeListener(this._update.bind(this,!1)),SDK.SDKModel.TargetManager.instance().addModelListener(SDK.OverlayModel.OverlayModel,SDK.OverlayModel.Events.ScreenshotRequested,this._screenshotRequestedFromOverlay,this),this._update(!0)}static instance(e={forceNew:null,inspectedPagePlaceholder:null}){const{forceNew:t,inspectedPagePlaceholder:i}=e;if(!deviceModeWrapperInstance||t){if(!i)throw new Error(`Unable to create DeviceModeWrapper: inspectedPagePlaceholder must be provided: ${(new Error).stack}`);deviceModeWrapperInstance=new DeviceModeWrapper(i)}return deviceModeWrapperInstance}_toggleDeviceMode(){this._showDeviceModeSetting.set(!this._showDeviceModeSetting.get())}_captureScreenshot(e,t){return this._deviceModeView||(this._deviceModeView=new DeviceModeView),this._deviceModeView.setNonEmulatedAvailableSize(this._inspectedPagePlaceholder.element),e?this._deviceModeView.captureFullSizeScreenshot():t?this._deviceModeView.captureAreaScreenshot(t):this._deviceModeView.captureScreenshot(),!0}_screenshotRequestedFromOverlay(e){const t=e.data;this._captureScreenshot(!1,t)}_update(e){if(this._toggleDeviceModeAction&&this._toggleDeviceModeAction.setToggled(this._showDeviceModeSetting.get()),!e){const e=this._deviceModeView&&this._deviceModeView.isShowing();if(this._showDeviceModeSetting.get()===e)return}this._showDeviceModeSetting.get()?(this._deviceModeView||(this._deviceModeView=new DeviceModeView),this._deviceModeView.show(this.element),this._inspectedPagePlaceholder.clearMinimumSize(),this._inspectedPagePlaceholder.show(this._deviceModeView.element)):(this._deviceModeView&&(this._deviceModeView.exitHingeMode(),this._deviceModeView.detach()),this._inspectedPagePlaceholder.restoreMinimumSize(),this._inspectedPagePlaceholder.show(this.element))}}export class ActionDelegate{handleAction(e,t){if(DeviceModeWrapper.instance())switch(t){case"emulation.capture-screenshot":return DeviceModeWrapper.instance()._captureScreenshot();case"emulation.capture-node-screenshot":{const e=UI.Context.Context.instance().flavor(SDK.DOMModel.DOMNode);if(!e)return!0;async function t(){if(!e)return;const t=await e.resolveToObject();if(!t)return;const i=await t.callFunction((function(){const e=this.getBoundingClientRect(),t=this.ownerDocument.documentElement.getBoundingClientRect();return JSON.stringify({x:e.left-t.left,y:e.top-t.top,width:e.width,height:e.height,scale:1})}));if(!i.object)throw new Error("Clipping error: could not get object data.");const o=JSON.parse(i.object.value),n=await e.domModel().target().pageAgent().invoke_getLayoutMetrics(),c=!n.getError()&&n.visualViewport.zoom||1;o.x*=c,o.y*=c,o.width*=c,o.height*=c,DeviceModeWrapper.instance()._captureScreenshot(!1,o)}return t(),!0}case"emulation.capture-full-height-screenshot":return DeviceModeWrapper.instance()._captureScreenshot(!0);case"emulation.toggle-device-mode":return DeviceModeWrapper.instance()._toggleDeviceMode(),!0}return!1}static instance(e={forceNew:null}){const{forceNew:t}=e;return actionDelegateInstance&&!t||(actionDelegateInstance=new ActionDelegate),actionDelegateInstance}}