import*as i18n from"../i18n/i18n.js";import*as UI from"../ui/ui.js";import{LinearMemoryInspector}from"./LinearMemoryInspector.js";import{LinearMemoryInspectorController}from"./LinearMemoryInspectorController.js";const UIStrings={noOpenInspections:"No open inspections"},str_=i18n.i18n.registerUIStrings("linear_memory_inspector/LinearMemoryInspectorPane.js",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);let inspectorInstance,wrapperInstance;export class Wrapper extends UI.Widget.VBox{constructor(){super(),this.view=LinearMemoryInspectorPaneImpl.instance()}static instance(e={forceNew:null}){const{forceNew:t}=e;return wrapperInstance&&!t||(wrapperInstance=new Wrapper),wrapperInstance}wasShown(){this.view.show(this.contentElement)}}export class LinearMemoryInspectorPaneImpl extends UI.Widget.VBox{constructor(){super(!1);const e=document.createElement("div");e.textContent=i18nString(UIStrings.noOpenInspections),e.style.display="flex",this._tabbedPane=new UI.TabbedPane.TabbedPane,this._tabbedPane.setPlaceholderElement(e),this._tabbedPane.setCloseableTabs(!0),this._tabbedPane.setAllowTabReorder(!0,!0),this._tabbedPane.addEventListener(UI.TabbedPane.Events.TabClosed,this._tabClosed,this),this._tabbedPane.show(this.contentElement),this._tabIdToInspectorView=new Map}static instance(){return inspectorInstance||(inspectorInstance=new LinearMemoryInspectorPaneImpl),inspectorInstance}create(e,t,s,r){const n=new LinearMemoryInspectorView(s,r);this._tabIdToInspectorView.set(e,n),this._tabbedPane.appendTab(e,t,n,void 0,!1,!0),this._tabbedPane.selectTab(e)}close(e){this._tabbedPane.closeTab(e,!1)}reveal(e,t){const s=this._tabIdToInspectorView.get(e);if(!s)throw new Error(`No linear memory inspector view for given tab id: ${e}`);void 0!==t&&s.updateAddress(t),this.refreshView(e),this._tabbedPane.selectTab(e)}refreshView(e){const t=this._tabIdToInspectorView.get(e);if(!t)throw new Error(`View for specified tab id does not exist: ${e}`);t.refreshData()}_tabClosed(e){const t=e.data.tabId;this._tabIdToInspectorView.delete(t),this.dispatchEventToListeners("view-closed",t)}}class LinearMemoryInspectorView extends UI.Widget.VBox{constructor(e,t=0){if(super(!1),t<0||t>=e.length())throw new Error("Requested address is out of bounds.");this._memoryWrapper=e,this._address=t,this._inspector=new LinearMemoryInspector,this._inspector.addEventListener("memory-request",(e=>{this._memoryRequested(e)})),this._inspector.addEventListener("address-changed",(e=>{this.updateAddress(e.data)})),this.contentElement.appendChild(this._inspector)}wasShown(){this.refreshData()}updateAddress(e){if(e<0||e>=this._memoryWrapper.length())throw new Error("Requested address is out of bounds.");this._address=e}refreshData(){LinearMemoryInspectorController.getMemoryForAddress(this._memoryWrapper,this._address).then((({memory:e,offset:t})=>{this._inspector.data={memory:e,address:this._address,memoryOffset:t,outerMemoryLength:this._memoryWrapper.length()}}))}_memoryRequested(e){const{start:t,end:s,address:r}=e.data;if(r<t||r>=s)throw new Error("Requested address is out of bounds.");LinearMemoryInspectorController.getMemoryRange(this._memoryWrapper,t,s).then((e=>{this._inspector.data={memory:e,address:r,memoryOffset:t,outerMemoryLength:this._memoryWrapper.length()}}))}}