import*as LitHtml from"../../lit-html/lit-html.js";import*as ComponentHelpers from"../helpers/helpers.js";import{toHexString}from"./LinearMemoryInspectorUtils.js";import linearMemoryViewerStyles from"./linearMemoryViewer.css.js";const{render:render,html:html}=LitHtml;export class ByteSelectedEvent extends Event{static eventName="byteselected";data;constructor(e){super(ByteSelectedEvent.eventName),this.data=e}}export class ResizeEvent extends Event{static eventName="resize";data;constructor(e){super(ResizeEvent.eventName),this.data=e}}const BYTE_GROUP_MARGIN=8,BYTE_GROUP_SIZE=4;export class LinearMemoryViewer extends HTMLElement{static litTagName=LitHtml.literal`devtools-linear-memory-inspector-viewer`;#e=this.attachShadow({mode:"open"});#t=new ResizeObserver((()=>this.#s()));#i=!1;#r=new Uint8Array;#o=0;#n=0;#h;#d;#a=1;#l=4;#m=!0;#c=void 0;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.#r=e.memory,this.#o=e.address,this.#h=e.highlightInfo,this.#d=e.focusedMemoryHighlight,this.#n=e.memoryOffset,this.#m=e.focus,this.#u()}connectedCallback(){ComponentHelpers.SetCSSProperty.set(this,"--byte-group-margin","8px"),this.#e.adoptedStyleSheets=[linearMemoryViewerStyles]}disconnectedCallback(){this.#i=!1,this.#t.disconnect()}#u(){this.#y(),this.#f(),this.#g(),this.#w()}#g(){if(this.#m){const e=this.#e.querySelector(".view");e&&e.focus()}}#s(){this.#u(),this.dispatchEvent(new ResizeEvent(this.#l*this.#a))}#y(){if(0===this.clientWidth||0===this.clientHeight||!this.shadowRoot)return this.#l=4,void(this.#a=1);const e=this.shadowRoot.querySelector(".byte-cell"),t=this.shadowRoot.querySelector(".text-cell"),s=this.shadowRoot.querySelector(".divider"),i=this.shadowRoot.querySelector(".row"),r=this.shadowRoot.querySelector(".address");if(!(e&&t&&s&&i&&r))return this.#l=4,void(this.#a=1);const o=4*(e.getBoundingClientRect().width+t.getBoundingClientRect().width)+8,n=s.getBoundingClientRect().width,h=e.getBoundingClientRect().left-r.getBoundingClientRect().left,d=this.clientWidth-1-h-n;if(d<o)return this.#l=4,void(this.#a=1);this.#l=4*Math.floor(d/o),this.#a=Math.floor(this.clientHeight/i.clientHeight)}#w(){this.#t&&!this.#i&&(this.#t.observe(this),this.#i=!0)}#f(){render(html`
      <div class="view" tabindex="0" @keydown=${this.#p}>
          ${this.#v()}
      </div>
      `,this.#e,{host:this})}#p(e){const t=e;let s;"ArrowUp"===t.code?s=this.#o-this.#l:"ArrowDown"===t.code?s=this.#o+this.#l:"ArrowLeft"===t.code?s=this.#o-1:"ArrowRight"===t.code?s=this.#o+1:"PageUp"===t.code?s=this.#o-this.#l*this.#a:"PageDown"===t.code&&(s=this.#o+this.#l*this.#a),void 0!==s&&s!==this.#c&&(this.#c=s,this.dispatchEvent(new ByteSelectedEvent(s)))}#v(){const e=[];for(let t=0;t<this.#a;++t)e.push(this.#R(t));return html`${e}`}#R(e){const{startIndex:t,endIndex:s}={startIndex:e*this.#l,endIndex:(e+1)*this.#l},i={address:!0,selected:Math.floor((this.#o-this.#n)/this.#l)===e};return html`
    <div class="row">
      <span class=${LitHtml.Directives.classMap(i)}>${toHexString({number:t+this.#n,pad:8,prefix:!1})}</span>
      <span class="divider"></span>
      ${this.#B(t,s)}
      <span class="divider"></span>
      ${this.#O(t,s)}
    </div>
    `}#B(e,t){const s=[];for(let i=e;i<t;++i){const t=i+this.#n,r={cell:!0,"byte-cell":!0,"byte-group-margin":i!==e&&(i-e)%4==0,selected:i===this.#o-this.#n,"highlight-area":this.#S(t),"focused-area":this.#I(t)},o=i<this.#r.length,n=o?html`${toHexString({number:this.#r[i],pad:2,prefix:!1})}`:"",h=o?this.#b.bind(this,t):"";s.push(html`<span class=${LitHtml.Directives.classMap(r)} @click=${h}>${n}</span>`)}return html`${s}`}#O(e,t){const s=[];for(let i=e;i<t;++i){const e=i+this.#n,t=this.#S(e),r=this.#I(e),o={cell:!0,"text-cell":!0,selected:this.#o-this.#n===i,"highlight-area":t,"focused-area":r},n=i<this.#r.length,h=n?html`${this.#H(this.#r[i])}`:"",d=n?this.#b.bind(this,i+this.#n):"";s.push(html`<span class=${LitHtml.Directives.classMap(o)} @click=${d}>${h}</span>`)}return html`${s}`}#H(e){return e>=20&&e<=127?String.fromCharCode(e):"."}#b(e){this.dispatchEvent(new ByteSelectedEvent(e))}#S(e){return void 0!==this.#h&&(this.#h.startAddress<=e&&e<this.#h.startAddress+this.#h.size)}#I(e){return!!this.#d&&(this.#d.startAddress<=e&&e<this.#d.startAddress+this.#d.size)}}ComponentHelpers.CustomElements.defineComponent("devtools-linear-memory-inspector-viewer",LinearMemoryViewer);