import*as e from"../third_party/lit-html/lit-html.js";import{Directives as t}from"../third_party/lit-html/lit-html.js";import*as s from"../common/common.js";import{ls as r,SimpleHistoryManager as n}from"../common/common.js";import{SDKModel as o,RuntimeModel as i,DebuggerModel as a,RemoteObject as d}from"../sdk/sdk.js";import{Widget as l,TabbedPane as c,Context as h,ViewManager as p}from"../ui/ui.js";import{Workspace as u}from"../workspace/workspace.js";function m(e,t){return e.toString(16).padStart(t,"0").toUpperCase()}var g=Object.freeze({__proto__:null,toHexString:m});const{render:y,html:v}=e;class f extends Event{constructor(e){super("page-navigation",{}),this.data=e}}class w extends Event{constructor(e){super("history-navigation",{}),this.data=e}}class b extends Event{constructor(){super("refresh",{})}}class x extends HTMLElement{constructor(){super(...arguments),this.shadow=this.attachShadow({mode:"open"}),this.address=0}set data(e){if(e.address<0)throw new Error("Address should be greater or equal to zero.");this.address=e.address,this.render()}render(){y(v`
      <style>
        .navigator {
          min-height: 24px;
          display: flex;
          flex-wrap: nowrap;
          justify-content: space-between;
          overflow: hidden;
          align-items: center;
        }

        .navigator-item {
          white-space: nowrap;
          overflow: hidden;
        }

        input {
          text-align: center;
        }

        .navigator-button {
          background: transparent;
          overflow: hidden;
          vertical-align: middle;
          border: none;
          padding: 0px;
        }
      </style>
      <div class="navigator">
        <div class="navigator-item">
          ${this.createButton("ic_undo_16x16_icon",new w("Backward"))}
          ${this.createButton("ic_redo_16x16_icon",new w("Forward"))}
        </div>
        <div class="navigator-item">
          ${this.createButton("ic_page_prev_16x16_icon",new f("Backward"))}
          <input data-input="true" contenteditable="true" value="0x${m(this.address,8)}"/>
          ${this.createButton("ic_page_next_16x16_icon",new f("Forward"))}
        </div>
        ${this.createButton("refresh_12x12_icon",new b)}
      </div>
      `,this.shadow,{eventContext:this})}createButton(e,t){return v`
      <button class="navigator-button" data-button=${t.type} @click=${this.dispatchEvent.bind(this,t)}>
        <devtools-icon .data=${{iconName:e,color:"rgb(110 110 110)",width:"16px"}}>
        </devtools-icon>
      </button>`}}customElements.define("devtools-linear-memory-inspector-navigator",x);var _=Object.freeze({__proto__:null,PageNavigationEvent:f,HistoryNavigationEvent:w,RefreshEvent:b,LinearMemoryNavigator:x});function T(e,t){switch(e){case"Integer 8-bit":case"Integer 16-bit":case"Integer 32-bit":case"Integer 64-bit":return"dec"===t||"hex"===t||"oct"===t;case"Float 32-bit":case"Float 64-bit":return"sci"===t||"dec"===t;case"String":return"none"===t}}function E(e){switch(e){case"Integer 8-bit":case"Integer 16-bit":case"Integer 32-bit":case"Integer 64-bit":return!0;default:return!1}}function I(e){switch(e){case"Integer 8-bit":case"Integer 16-bit":case"Integer 32-bit":case"Integer 64-bit":case"Float 32-bit":case"Float 64-bit":return!0;default:return!1}}function R(e){const t=new DataView(e.buffer),s="Little Endian"===e.endianness;let r;switch(e.type){case"Integer 8-bit":return r=e.signed?t.getInt8(0):t.getUint8(0),M(r,e.mode);case"Integer 16-bit":return r=e.signed?t.getInt16(0,s):t.getUint16(0,s),M(r,e.mode);case"Integer 32-bit":return r=e.signed?t.getInt32(0,s):t.getUint32(0,s),M(r,e.mode);case"Integer 64-bit":return r=e.signed?t.getBigInt64(0,s):t.getBigUint64(0,s),M(r,e.mode);case"Float 32-bit":return r=t.getFloat32(0,s),O(r,e.mode);case"Float 64-bit":return r=t.getFloat64(0,s),O(r,e.mode);case"String":throw new Error(`Type ${e.type} is not yet implemented`)}}function O(e,t){switch(t){case"dec":return e.toFixed(2).toString();case"sci":return e.toExponential(2).toString();default:throw new Error(`Unknown mode for floats: ${t}.`)}}function M(e,t){switch(t){case"dec":return e.toString();case"hex":return e.toString(16);case"oct":return e.toString(8);default:throw new Error(`Unknown mode for integers: ${t}.`)}}var $=Object.freeze({__proto__:null,VALUE_INTEPRETER_MAX_NUM_BYTES:8,isValidMode:T,typeHasSignedNotation:E,isNumber:I,format:R,formatFloat:O,formatInteger:M});const B=r,{render:S,html:P}=e,C=new Map([["Integer 8-bit","dec"],["Integer 16-bit","dec"],["Integer 32-bit","dec"],["Integer 64-bit","dec"],["Float 32-bit","dec"],["Float 64-bit","dec"],["String","none"]]),j=Array.from(C.keys());class A extends HTMLElement{constructor(){super(...arguments),this.shadow=this.attachShadow({mode:"open"}),this.endianness="Little Endian",this.buffer=new ArrayBuffer(0),this.valueTypes=new Set,this.valueTypeModeConfig=C}set data(e){this.buffer=e.buffer,this.valueTypes=e.valueTypes,this.valueTypeModeConfig=C,e.valueTypeModes&&e.valueTypeModes.forEach(((e,t)=>{T(t,e)&&this.valueTypeModeConfig.set(t,e)})),this.render()}render(){S(P`
      <style>
        :host {
          flex: auto;
          display: flex;
        }

        .mode-type {
          color: var(--text-highlight-color);
        }

        .value-types {
          display: grid;
          grid-template-columns: auto auto 1fr 1fr;
          grid-column-gap: 24px;
          grid-row-gap: 4px;
          overflow: hidden;
          padding-left: 12px;
          padding-right: 12px;
        }

        .value-type-cell {
          height: 21px;
          text-overflow: ellipsis;
          white-space: nowrap;
          overflow: hidden;
          display: flex;
        }

        .value-type-cell-no-sign {
          grid-column: 3 / 5;
        }

        .value-type-cell-no-mode {
          grid-column: grid-column: 1 / 3;
        }

      </style>
        <div class="value-types">
          ${j.map((e=>this.valueTypes.has(e)?this.showValue(e):""))}
        </div>
      </div>
    `,this.shadow,{eventContext:this})}showValue(e){return P`
      ${I(e)?P`
          <span class="value-type-cell">${B`${e}`}</span>
          <span class="mode-type value-type-cell">${B`${this.valueTypeModeConfig.get(e)}`}</span>`:P`
          <span class="value-type-cell-no-mode value-type-cell">${B`${e}`}</span>`}

        ${E(e)?P`
          <span class="value-type-cell" data-value="true">+ ${this.parse({type:e,signed:!1})}</span>
          <span class="value-type-cell" data-value="true">± ${this.parse({type:e,signed:!0})}</span>`:P`
          <span class="value-type-cell-no-sign value-type-cell" data-value="true">${this.parse({type:e})}</span>`}
    `}parse(e){const t=this.valueTypeModeConfig.get(e.type);if(!t)throw new Error("No known way of showing value for "+e.type);return R({buffer:this.buffer,type:e.type,endianness:this.endianness,signed:e.signed||!1,mode:t})}}customElements.define("devtools-linear-memory-inspector-interpreter-display",A);var k=Object.freeze({__proto__:null,ValueInterpreterDisplay:A});const z=r,{render:V,html:L}=e,U=new Map([["Integer",["Integer 8-bit","Integer 16-bit","Integer 32-bit","Integer 64-bit"]],["Floating point",["Float 32-bit","Float 64-bit"]],["Other",["String"]]]);class F extends Event{constructor(e,t){super("type-toggle"),this.data={type:e,checked:t}}}class N extends HTMLElement{constructor(){super(...arguments),this.shadow=this.attachShadow({mode:"open"}),this.valueTypes=new Set}set data(e){this.valueTypes=e.valueTypes,this.render()}render(){V(L`
      <style>
        :host {
          flex: auto;
          display: flex;
          min-height: 20px;
        }

        .settings {
          display: flex;
          flex-wrap: wrap;
          margin: 0px 12px 12px 12px;
          column-gap: 45px;
          row-gap: 15px;
        }

        .value-types-selection {
          display: flex;
          flex-direction: column;
        }

        .value-types-selection + value-types-selection {
          margin-left: 45px;
        }

        .group {
          font-weight: bold;
          margin-bottom: 11px;
        }

        .type-label {
          white-space: nowrap;
        }

        .group + .type-label {
          margin-top: 5px;
        }

        .type-label input {
          margin: 0 6px 0 0;
          padding: 0;
        }

        .type-label + .type-label {
          margin-top: 6px;
        }
      </style>
      <div class="settings">
       ${[...U.keys()].map((e=>L`
          <div class="value-types-selection">
            <span class="group">${e}</span>
            ${this.plotTypeSelections(e)}
          </div>
        `))}
      </div>
      `,this.shadow,{eventContext:this})}plotTypeSelections(e){const t=U.get(e);if(!t)throw new Error("Unknown group "+e);return L`
      ${t.map((e=>L`
          <label class="type-label" title=${e}>
            <input data-input="true" type="checkbox" .checked=${this.valueTypes.has(e)} @change=${t=>this.onTypeToggle(e,t)}>
            <span data-title="true">${z`${e}`}</span>
          </label>
     `))}`}onTypeToggle(e,t){const s=t.target;this.dispatchEvent(new F(e,s.checked))}}customElements.define("devtools-linear-memory-inspector-interpreter-settings",N);var G=Object.freeze({__proto__:null,TypeToggleEvent:F,ValueInterpreterSettings:N});const q=r,{render:H,html:Y}=e;class W extends HTMLElement{constructor(){super(...arguments),this.shadow=this.attachShadow({mode:"open"}),this.endianness="Little Endian",this.buffer=new ArrayBuffer(0),this.valueTypes=new Set,this.valueTypeModeConfig=new Map,this.showSettings=!1}set data(e){this.endianness=e.endianness,this.buffer=e.value,this.valueTypes=e.valueTypes,this.valueTypeModeConfig=e.valueTypeModes||new Map,this.render()}render(){H(Y`
      <style>
        :host {
          flex: auto;
          display: flex;
        }

        .value-interpreter {
          border: var(--divider-border, 1px solid #d0d0d0);
          background-color: var(--toolbar-bg-color, #f3f3f3);
          overflow: hidden;
          --text-highlight-color: #80868b;
          width: 400px;
        }

        .settings-toolbar {
          min-height: 26px;
          display: flex;
          flex-wrap: nowrap;
          justify-content: space-between;
          padding-left: 12px;
          padding-right: 12px;
          align-items: center;
        }

        .settings-toolbar-button {
          display: flex;
          justify-content: center;
          width: 30px;
          height: 20px;
        }

        .divider {
          display: block;
          height: 1px;
          margin-bottom: 12px;
          background-color: var(--divider-color,  #d0d0d0);
        }
      </style>
      <div class="value-interpreter">
        <div class="settings-toolbar">
          <div>
            <span>${q`${this.endianness}`}</span>
            <devtools-icon
              .data=${{iconName:"dropdown_7x6_icon",color:"rgb(110 110 110)",width:"7px"}}>
            </devtools-icon>
          </div>
          <button data-settings="true" class="settings-toolbar-button" title="Toggle value type settings" @click=${this.onSettingsToggled}>
            <devtools-icon
              .data=${{iconName:"settings_14x14_icon",color:"rgb(110 110 110)",width:"14px"}}>
            </devtools-icon>
          </button>
        </div>
        <span class="divider"></span>
        <div>
          ${this.showSettings?Y`
              <devtools-linear-memory-inspector-interpreter-settings
                .data=${{valueTypes:this.valueTypes}}
                @type-toggle=${this.onTypeToggle}>
              </devtools-linear-memory-inspector-interpreter-settings>`:Y`
              <devtools-linear-memory-inspector-interpreter-display
                .data=${{buffer:this.buffer,valueTypes:this.valueTypes,endianness:this.endianness,valueTypeModes:this.valueTypeModeConfig}}>
              </devtools-linear-memory-inspector-interpreter-display>`}
        </div>
      </div>
    `,this.shadow,{eventContext:this})}onSettingsToggled(){this.showSettings=!this.showSettings,this.render()}onTypeToggle(e){this.dispatchEvent(new D(e.data.type,e.data.checked))}}class D extends Event{constructor(e,t){super("value-type-toggle"),this.data={type:e,checked:t}}}customElements.define("devtools-linear-memory-inspector-interpreter",W);var Z=Object.freeze({__proto__:null,LinearMemoryValueInterpreter:W,ValueTypeToggleEvent:D});const{render:K,html:X}=e;class J extends Event{constructor(e){super("byte-selected"),this.data=e}}class Q extends Event{constructor(e){super("resize"),this.data=e}}class ee extends HTMLElement{constructor(){super(...arguments),this.shadow=this.attachShadow({mode:"open"}),this.resizeObserver=new ResizeObserver((()=>this.resize())),this.isObservingResize=!1,this.memory=new Uint8Array,this.address=0,this.memoryOffset=0,this.numRows=1,this.numBytesInRow=ee.BYTE_GROUP_SIZE}set data(e){if(e.address<e.memoryOffset||e.address>e.memoryOffset+e.memory.length||e.address<0)throw new Error("Address is out of bounds.");if(e.memoryOffset<0)throw new Error("Memory offset has to be greater or equal to zero.");this.memory=e.memory,this.address=e.address,this.memoryOffset=e.memoryOffset,this.update()}disconnectedCallback(){this.isObservingResize=!1,this.resizeObserver.disconnect()}update(){this.updateDimensions(),this.render(),this.engageResizeObserver()}resize(){this.update(),this.dispatchEvent(new Q(this.numBytesInRow*this.numRows))}updateDimensions(){if(0===this.clientWidth||0===this.clientHeight||!this.shadowRoot)return this.numBytesInRow=ee.BYTE_GROUP_SIZE,void(this.numRows=1);const e=this.shadowRoot.querySelector(".byte-cell"),t=this.shadowRoot.querySelector(".text-cell"),s=this.shadowRoot.querySelector(".divider"),r=this.shadowRoot.querySelector(".row");if(!(e&&t&&s&&r))return this.numBytesInRow=ee.BYTE_GROUP_SIZE,void(this.numRows=1);const n=e.getBoundingClientRect().width,o=t.getBoundingClientRect().width,i=ee.BYTE_GROUP_SIZE*(n+o)+ee.BYTE_GROUP_MARGIN,a=s.getBoundingClientRect().width,d=this.clientWidth-(e.getBoundingClientRect().left-this.getBoundingClientRect().left)-a;if(d<i)return this.numBytesInRow=ee.BYTE_GROUP_SIZE,void(this.numRows=1);this.numBytesInRow=Math.floor(d/i)*ee.BYTE_GROUP_SIZE,this.numRows=Math.floor(this.clientHeight/r.clientHeight)}engageResizeObserver(){this.resizeObserver&&!this.isObservingResize&&(this.resizeObserver.observe(this),this.isObservingResize=!0)}render(){K(X`
      <style>
        :host {
          flex: auto;
          display: flex;
          min-height: 20px;
        }

        .view {
          overflow: hidden;
          text-overflow: ellipsis;
          box-sizing: border-box;
          background: var(--color-background);
        }

        .row {
          display: flex;
          height: 20px;
          align-items: center;
        }

        .cell {
          text-align: center;
          border: 1px solid transparent;
          border-radius: 2px;
        }

        .cell.selected {
          border-color: var(--color-syntax-3);
          color: var(--color-syntax-3);
          background-color: var(--item-selection-bg-color);
        }

        .byte-cell {
          min-width: 21px;
          color: var(--color-text-primary)
        }

        .byte-group-margin {
          margin-left: ${ee.BYTE_GROUP_MARGIN}px;
        }

        .text-cell {
          min-width: 14px;
          color: var(--color-syntax-3);
        }

        .address {
          color: var(--color-text-disabled);
        }

        .address.selected {
          font-weight: bold;
          color: var(--color-text-primary);
        }

        .divider {
          width: 1px;
          height: inherit;
          background-color: var(--divider-color);
          margin: 0px 4px 0px 4px;
        }
      </style>
      <div class="view">
          ${this.renderView()}
      </div>
      `,this.shadow,{eventContext:this})}renderView(){const e=[];for(let t=0;t<this.numRows;++t)e.push(this.renderRow(t));return X`${e}`}renderRow(e){const{startIndex:s,endIndex:r}={startIndex:e*this.numBytesInRow,endIndex:(e+1)*this.numBytesInRow},n={address:!0,selected:Math.floor((this.address-this.memoryOffset)/this.numBytesInRow)===e};return X`
    <div class="row">
      <span class="${t.classMap(n)}">${m(s+this.memoryOffset,8)}</span>
      <span class="divider"></span>
      ${this.renderByteValues(s,r)}
      <span class="divider"></span>
      ${this.renderCharacterValues(s,r)}
    </div>
    `}renderByteValues(e,s){const r=[];for(let n=e;n<s;++n){const s={cell:!0,"byte-cell":!0,"byte-group-margin":n!==e&&(n-e)%ee.BYTE_GROUP_SIZE==0,selected:n===this.address-this.memoryOffset},o=n<this.memory.length?X`${m(this.memory[n],2)}`:"";r.push(X`
        <span class="${t.classMap(s)}" @click=${this.onSelectedByte(n+this.memoryOffset)}>
          ${o}
        </span>`)}return X`${r}`}renderCharacterValues(e,s){const r=[];for(let n=e;n<s;++n){const e={cell:!0,"text-cell":!0,selected:this.address-this.memoryOffset===n},s=n<this.memory.length?X`${this.toAscii(this.memory[n])}`:"";r.push(X`<span class="${t.classMap(e)}" @click=${this.onSelectedByte(n+this.memoryOffset)}>${s}</span>`)}return X`${r}`}toAscii(e){return e>=20&&e<=127?String.fromCharCode(e):"."}onSelectedByte(e){return()=>{this.dispatchEvent(new J(e))}}}ee.BYTE_GROUP_MARGIN=8,ee.BYTE_GROUP_SIZE=4,customElements.define("devtools-linear-memory-inspector-viewer",ee);var te=Object.freeze({__proto__:null,ByteSelectedEvent:J,ResizeEvent:Q,LinearMemoryViewer:ee});const{render:se,html:re}=e;class ne{constructor(e,t){if(this.address=0,e<0)throw new Error("Address should be a greater or equal to zero");this.address=e,this.callback=t}valid(){return!0}reveal(){this.callback(this.address)}}class oe extends Event{constructor(e,t,s){super("memory-request"),this.data={start:e,end:t,address:s}}}class ie extends HTMLElement{constructor(){super(...arguments),this.shadow=this.attachShadow({mode:"open"}),this.history=new n.SimpleHistoryManager(10),this.memory=new Uint8Array,this.memoryOffset=0,this.address=0,this.numBytesPerPage=4,this.valueTypes=new Set(["Integer 8-bit","Float 32-bit"])}set data(e){if(e.address<e.memoryOffset||e.address>e.memoryOffset+e.memory.length||e.address<0)throw new Error("Address is out of bounds.");if(e.memoryOffset<0)throw new Error("Memory offset has to be greater or equal to zero.");this.memory=e.memory,this.address=e.address,this.memoryOffset=e.memoryOffset,this.render()}render(){const{start:e,end:t}=this.getPageRangeForAddress(this.address,this.numBytesPerPage);se(re`
      <style>
        :host {
          flex: auto;
          display: flex;
        }

        .view {
          width: 100%;
          display: flex;
          flex: 1;
          flex-direction: column;
          font-family: monospace;
          padding: 9px 12px 9px 7px;
        }

        devtools-linear-memory-inspector-navigator + devtools-linear-memory-inspector-viewer {
          margin-top: 12px;
        }

        .value-interpreter {
          display: flex;
        }
      </style>
      <div class="view">
        <devtools-linear-memory-inspector-navigator
          .data=${{address:this.address}}
          @page-navigation=${this.navigatePage}
          @history-navigation=${this.navigateHistory}></devtools-linear-memory-inspector-navigator>
        <devtools-linear-memory-inspector-viewer
          .data=${{memory:this.memory.slice(e-this.memoryOffset,t-this.memoryOffset),address:this.address,memoryOffset:e}}
          @byte-selected=${e=>this.jumpToAddress(e.data)}
          @resize=${this.resize}>
        </devtools-linear-memory-inspector-viewer>
      </div>
      <div class="value-interpreter">
        <devtools-linear-memory-inspector-interpreter
          .data=${{value:this.memory.slice(this.address-this.memoryOffset,this.address+8).buffer,valueTypes:this.valueTypes,endianness:"Little Endian"}}
          @value-type-toggle=${this.onValueTypeToggled}>
        </devtools-linear-memory-inspector-interpreter/>
      </div>
      `,this.shadow,{eventContext:this})}onValueTypeToggled(e){const{type:t,checked:s}=e.data;s?this.valueTypes.add(t):this.valueTypes.delete(t),this.render()}navigateHistory(e){return"Forward"===e.data?this.history.rollover():this.history.rollback()}navigatePage(e){const t="Forward"===e.data?this.address+this.numBytesPerPage:Math.max(this.address-this.numBytesPerPage,0);this.jumpToAddress(t)}jumpToAddress(e){const t=new ne(e,(e=>this.jumpToAddress(e)));this.history.push(t),this.address=e,this.update()}getPageRangeForAddress(e,t){const s=Math.floor(e/t)*t;return{start:s,end:s+t}}resize(e){this.numBytesPerPage=e.data,this.update()}update(){const{start:e,end:t}=this.getPageRangeForAddress(this.address,this.numBytesPerPage);e<this.memoryOffset||t>this.memoryOffset+this.memory.length?this.dispatchEvent(new oe(e,t,this.address)):this.render()}}customElements.define("devtools-linear-memory-inspector-inspector",ie);var ae=Object.freeze({__proto__:null,MemoryRequestEvent:oe,LinearMemoryInspector:ie});const{ls:de}=s;let le;class ce extends Event{constructor(e){super("view-closed"),this.data=e}}class he extends l.VBox{constructor(){super(),this.view=pe.instance()}wasShown(){this.view.show(this.contentElement)}}class pe extends l.VBox{constructor(){super(!1);const e=document.createElement("div");e.textContent=de`No open inspections`,e.style.display="flex",this._tabbedPane=new c.TabbedPane,this._tabbedPane.setPlaceholderElement(e),this._tabbedPane.setCloseableTabs(!0),this._tabbedPane.setAllowTabReorder(!0,!0),this._tabbedPane.addEventListener(c.Events.TabClosed,this._tabClosed,this),this._tabbedPane.show(this.contentElement),this._tabIdToInspectorView=new Map}static instance(){return le||(le=new pe),le}create(e,t,s,r){const n=new ue(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){if(!this._tabIdToInspectorView.has(e))throw new Error("View for specified tab id does not exist: "+e);this._tabbedPane.selectTab(e)}_tabClosed(e){const t=e.data.tabId;this._tabIdToInspectorView.delete(t),this.dispatchEventToListeners("view-closed",t)}}class ue extends l.VBox{constructor(e,t){if(super(!1),t<0||t>e.length())throw new Error("Invalid address to show");this.memoryWrapper=e,this._inspector=new ie,this._inspector.addEventListener("memory-request",(e=>{this._memoryRequested(e)})),this.contentElement.appendChild(this._inspector);const s=Math.max(0,t-500),r=s+1e3;this.memoryWrapper.getRange(s,r).then((e=>{this._inspector.data={memory:e,address:t,memoryOffset:s}}))}_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.");if(t<0||t>s||t>=this.memoryWrapper.length())throw new Error("Requested range is out of bounds.");const n=Math.max(s,t+1e3);this.memoryWrapper.getRange(t,n).then((e=>{this._inspector.data={memory:e,address:r,memoryOffset:t}}))}}var me=Object.freeze({__proto__:null,ViewClosedEvent:ce,Wrapper:he,LinearMemoryInspectorPaneImpl:pe,LazyUint8Array:class{getRange(e,t){return Promise.resolve(new Uint8Array([]))}length(){return 0}}});let ge;class ye{constructor(e){this.remoteArray=e}length(){return this.remoteArray.length()}async getRange(e,t){const s=Math.min(t,this.remoteArray.length());if(e<0||s>=this.remoteArray.length()||e>s)return Promise.resolve(new Uint8Array(0));const r=await this.extractByteArray(e,s);return new Uint8Array(r)}async extractByteArray(e,t){const s=[];for(let r=e;r<t&&r<this.remoteArray.length();++r)s.push(this.remoteArray.at(r).then((e=>e.value)));return await Promise.all(s)}}class ve extends o.SDKModelObserver{constructor(){super(),this.paneInstance=pe.instance(),this.scriptIdToRemoteObject=new Map,o.TargetManager.instance().observeModels(i.RuntimeModel,this),o.TargetManager.instance().addModelListener(a.DebuggerModel,a.Events.GlobalObjectCleared,this.globalObjectCleared,this),this.paneInstance.addEventListener("view-closed",this.viewClosed.bind(this))}static instance(){return ge||(ge=new ve,ge)}async openInspectorView(e,t){const s=h.Context.instance().flavor(a.CallFrame);if(!s)throw new Error(`Cannot find call frame for ${e.description}.`);const r=s.script.scriptId,n=u.WorkspaceImpl.instance().uiSourceCodeForURL(s.script.sourceURL);if(!n)throw new Error("Cannot find source code object for source url: "+s.script.sourceURL);const o=n.displayName();if(this.scriptIdToRemoteObject.has(r))return void this.paneInstance.reveal(r);const i=await this.getObjectWithExtendedLifetime(e);this.scriptIdToRemoteObject.set(r,i);const l=new d.RemoteArray(i),c=new ye(l);this.paneInstance.create(r,o,c,t),p.ViewManager.instance().showView("linear-memory-inspector")}modelRemoved(e){for(const[t,s]of this.scriptIdToRemoteObject)e===s.runtimeModel()&&(this.scriptIdToRemoteObject.delete(t),this.paneInstance.close(t))}globalObjectCleared(e){const t=e.data;this.modelRemoved(t._runtimeModel)}async getObjectWithExtendedLifetime(e){const t=await e.runtimeModel()._agent.invoke_callFunctionOn({objectId:e.objectId,functionDeclaration:"function() { return this; }",silent:!0,objectGroup:"linear-memory-inspector"}),s=t.getError();if(s)throw new Error("Remote object representing Uint8Array could not be retrieved: "+s);return e.runtimeModel().createRemoteObject(t.result)}viewClosed(e){const t=e.data,s=this.scriptIdToRemoteObject.get(t);s&&s.release(),this.scriptIdToRemoteObject.delete(e.data)}}var fe=Object.freeze({__proto__:null,RemoteArrayWrapper:ye,LinearMemoryInspectorController:ve});export{ae as LinearMemoryInspector,fe as LinearMemoryInspectorController,me as LinearMemoryInspectorPane,g as LinearMemoryInspectorUtils,_ as LinearMemoryNavigator,Z as LinearMemoryValueInterpreter,te as LinearMemoryViewer,k as ValueInterpreterDisplay,$ as ValueInterpreterDisplayUtils,G as ValueInterpreterSettings};
